Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error : only uncompressed keys are accepted post-segwit #1511

Open
koderholic opened this issue Jan 2, 2020 · 5 comments
Open

Error : only uncompressed keys are accepted post-segwit #1511

koderholic opened this issue Jan 2, 2020 · 5 comments

Comments

@koderholic
Copy link

`
func SignTransaction(redeemTx *wire.MsgTx, secret string, destination string, amount float64, utxos []dto.Utxo) error {

privByte, _ := hex.DecodeString(privKey)
privateKey, pubKey := btcec.PrivKeyFromBytes( btcec.S256(), privByte)
fromAddress, _ := btcutil.NewAddressWitnessPubKeyHash(btcutil.Hash160(pubKey.SerializeUncompressed()), &chaincfg.MainNetParams)

sourceAddress, err := btcutil.DecodeAddress(fromAddress, &chaincfg.MainNetParams)
destinationAddress, err := btcutil.DecodeAddress(destination, &chaincfg.MainNetParams)
if err != nil {
	return err
}

// TODO : Calculate fee estimation
estimatedFee, err := btcutil.NewAmount(0.0001) 
if err != nil {
	return err
}
satoshiAmount, err := btcutil.NewAmount(amount) 
if err != nil {
	return err
}
totalSatoshiAmount := satoshiAmount + estimatedFee 

// TODO : optimize for selecting which UTXO to be used
totalSum := int64(0)
for i := 0; i < len(utxos); i++ {
	txValue, err := strconv.Atoi(utxos[i].Value)
	if err != nil {
		return err
	}
	totalSum += int64(txValue)

	sourceUtxoHash, _ := chainhash.NewHashFromStr(utxos[i].Txid)
	prevOut := wire.NewOutPoint(sourceUtxoHash, uint32(i))
	redeemTxIn := wire.NewTxIn(prevOut, nil, nil)
	redeemTx.AddTxIn(redeemTxIn)
	if totalSum > int64(totalSatoshiAmount) {
		break
	}
}

// Prepare pay to witness public script and add transaction outputs 
sourcePkScriptForOutput, _ := txscript.PayToAddrScript(sourceAddress)
destinationPkScriptForOutput, _ := txscript.PayToAddrScript(destinationAddress)
change := totalSum - int64(totalSatoshiAmount)
changeTxOut := wire.NewTxOut(int64(change), sourcePkScriptForOutput)
redeemTx.AddTxOut(changeTxOut)
redeemTxOut := wire.NewTxOut(int64(satoshiAmount), destinationPkScriptForOutput)
redeemTx.AddTxOut(redeemTxOut)

// Signing transaction
sigHash := txscript.NewTxSigHashes(redeemTx)
witnessSig, err := txscript.WitnessSignature(redeemTx, sigHash, 0, int64(satoshiAmount), sourcePkScriptForOutput, txscript.SigHashAll, privateKey, false)
if err != nil {
	return err
}

redeemTx.TxIn[0].Witness = witnessSig
flags := txscript.StandardVerifyFlags
vm, err := txscript.NewEngine(sourcePkScriptForOutput, redeemTx, 0, flags, nil, sigHash, int64(satoshiAmount))
if err != nil {
	fmt.Println("flag transactionerr > ", err)
	return err
}
if err := vm.Execute(); err != nil {
	fmt.Println("vm transactionerr > ", err)
	return err
}

return nil

}`

The above is to spend UTXO from a segwit address to a receiving segwit address, I am not sure if the issue I am having is with the implement, but I keep getting : Only uncompressed keys are accepted post-segwit when the code execution gets to vm.Execute() failing to validate the signature.

@JeremyRubin
Copy link

It looks like the error message you're getting is inaccurate. According to https://github.com/btcsuite/btcd/blob/master/txscript/engine.go#L587 and the segwit spec, keys must be compressed inside segwit.

Make sure your pubkey is compressed!

@koderholic
Copy link
Author

@JeremyRubin Thank you for the response, I have tried making it compressed, but couldn't get it to work still. Had to move to implement it in Java.

@JeremyRubin
Copy link

I don't know anything about how competent the Java Bitcoin libraries are these days so it might make sense to wait until a maintainer/contributor of btcd can chime in on this issue; I believe btcd enjoys much more ecosystem support than java implementations I'm aware of.

@koderholic
Copy link
Author

Yeah! But the Java implementation seems a bit straightforward. I think to continue with it, pending when a maintainer/contributor can help out with this issue.

@jakesylvestre
Copy link
Collaborator

keys must be compressed - can you link to the implementation you're referencing? Is this Bitcoinj?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants