// 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