Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: glacial_boy on August 14, 2018, 09:17:50 AM



Title: transaction failed to validate input
Post by: glacial_boy on August 14, 2018, 09:17:50 AM
Hello,

Im owner of this btc testnet address mxsdtqnL3trUZcuqniHJ3G5DXKeNKBQ3Td
If I want to spend this output https://testnet.blockchain.info/tx/984d6478625df41d675a39d908f5147348b07fe2f0828c5866af3aca438d29aa (https://testnet.blockchain.info/tx/984d6478625df41d675a39d908f5147348b07fe2f0828c5866af3aca438d29aa), how can I sign it to be valid?
This unspent output is of type pay-to-pubkey

Thank you


Title: Re: transaction failed to validate input
Post by: bob123 on August 14, 2018, 09:57:07 AM
Which wallet/client/library are you using to create the transaction ?
Are you trying to manually sign it ?

For manually signing transactions (and a small python code snippet), look here: https://bitcoin.stackexchange.com/questions/36440/signing-a-raw-transaction-with-python-ecdsa-or-openssl (https://bitcoin.stackexchange.com/questions/36440/signing-a-raw-transaction-with-python-ecdsa-or-openssl)


Title: Re: transaction failed to validate input
Post by: glacial_boy on August 14, 2018, 10:02:05 AM
Im doing a manual sign, code written in Golang with library https://github.com/btcsuite/btcd (https://github.com/btcsuite/btcd)

As for unspent output with type of P2PKH, I managed to create transaction and broadcast successfully. I tried to apply same technique for P2PK but it didnt work


Title: Re: transaction failed to validate input
Post by: bob123 on August 14, 2018, 10:07:12 AM
It would be way more easier for us to find your mistake if you would share the relevant code.

Can't tell much without looking at it. But generally speaking, you seem to have built the transaction wrong.


Title: Re: transaction failed to validate input
Post by: glacial_boy on August 14, 2018, 10:11:30 AM
Code:
func CreateTransaction(secret, sender, destination string, amount int64, txHash string) (*wire.MsgTx, error) {

// convert private key (string) -> ECDSA format
priv, _ := stringToPrivateECDSA(secret)

// init new transaction
// TxVersion = 1
redeemTx := wire.NewMsgTx(TxVersion)
// convert sender address (string) -> BTC Address format
sourceAddress, _ := btcutil.DecodeAddress(sender, ChainParams)
// create script base on address converted
sourcePkScript, _ := txscript.PayToAddrScript(sourceAddress)

// prepare output of sending #btc to destination address
// convert destination address (string) -> BTC Address format
// trying pay-to-pubkey method
destinationAddress, _ := btcutil.DecodeAddress(destination, ChainParams)
// create script for output address
destinationPkScript, _ := txscript.PayToAddrScript(destinationAddress)
// set amount to send to destination
redeemTxOut := wire.NewTxOut(amount, destinationPkScript)
redeemTx.AddTxOut(redeemTxOut)

// prepare output for Tx
// return #btc changes back to our wallet
// Total = #btc of that unspent output
// amount = #btc send to destination
redeemTxOut = wire.NewTxOut(Total-amount-TxFee, sourcePkScript)
redeemTx.AddTxOut(redeemTxOut)

// txHash = previous unspent output of sourceAddress
sourceUtxoHash, _ := chainhash.NewHashFromStr(txHash)
prevOut := wire.NewOutPoint(sourceUtxoHash, 0)
redeemTxIn := wire.NewTxIn(prevOut, nil, nil)
redeemTx.AddTxIn(redeemTxIn)

sig, err := txscript.SignatureScript(redeemTx, 0, sourcePkScript, txscript.SigHashAll, priv, false)
if err != nil {
fmt.Println(err)
return nil, err
}
redeemTx.TxIn[0].SignatureScript = sig

return redeemTx, nil
}

This is my function to create a transaction written in Go.
Thank you and really appreciate for your help  :D