At present I am attempting to create, signal & broadcast bitcoin transaction utilizing btcsuite. For begin, I’ve already had testnet3 handle & its related privatekey for testing. Nonetheless, searching by the publish and articles like under:-
The answer proposed above its not full, for first one, it solely covers till signing (i knew the writer claimed its not broadcastable until you present the utxo which i did if i am proper) however when tried to braodcast it failed with message
“Error validating transaction: Transaction be9b294695bfb201a5cff32af074a4bf72b073e3c9dad1969111165bf118a622 orphaned, lacking reference f0c0d3b3eecf911ede996a74ceadc9366068791450c9e6bacee9ae202f3690d1.”
I don’t know what is going on on and I believe its script is incompatible.
So, the underside line is I simply desire a workable instance in bitcoin testnet3 that exhibits
“from 1 handle switch some bitcoin to different handle” by exhibiting the method of making uncooked transaction, signal it with non-public key, flip it to uncooked transaction in hex format & broadcast it utilizing one thing like https://dwell.blockcypher.com/btc/pushtx/ (BTC testnet)
at present my code is as observe:-
package deal predominant
import (
"fmt"
"encoding/hex"
"bytes"
"github.com/btcsuite/btcutil"
btcchain "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
func txToHex(tx *wire.MsgTx) string {
buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
tx.Serialize(buf)
return hex.EncodeToString(buf.Bytes())
}
func stkbtc(){
pvkey := "cNjXNxcfawzyfGUxaG94rKqayAL2n7QWioKhCkHbQsBRT7SbDyGu"
txHash := "e028b5bf030a24986a03b03b89dec037e8462ae32bc93679cb49d7c779685987"
vacation spot := "n2kRiAkW1xr5DVy7QKVGaYiZbwpS7j23jJ"
var quantity int64 = 100000000
txFee := int64(500000)
//strive ship btc
decodedwif,err := btcutil.DecodeWIF(pvkey)
if err != nil {
fmt.Printf("decodedwif error: %vn",err)
}
fmt.Printf("decodedwif : %vn",decodedwif)
addresspubkey, _ := btcutil.NewAddressPubKey(decodedwif.PrivKey.PubKey().SerializeUncompressed(), &btcchain.TestNet3Params)
sourceTx := wire.NewMsgTx(wire.TxVersion)
sourceUtxoHash, _ := chainhash.NewHashFromStr(txHash)
sourceUtxo := wire.NewOutPoint(sourceUtxoHash, 0)
sourceTxIn := wire.NewTxIn(sourceUtxo, nil, nil)
destinationAddress, _ := btcutil.DecodeAddress(vacation spot, &btcchain.TestNet3Params)
sourceAddress, err := btcutil.DecodeAddress(addresspubkey.EncodeAddress(), &btcchain.TestNet3Params)
if err != nil {
fmt.Printf("sourceAddress err: %vn",err)
}
destinationPkScript, _ := txscript.PayToAddrScript(destinationAddress)
sourcePkScript, _ := txscript.PayToAddrScript(sourceAddress)
sourceTxOut := wire.NewTxOut(quantity, sourcePkScript)
sourceTx.AddTxIn(sourceTxIn)
sourceTx.AddTxOut(sourceTxOut)
sourceTxHash := sourceTx.TxHash()
redeemTx := wire.NewMsgTx(wire.TxVersion)
prevOut := wire.NewOutPoint(&sourceTxHash, 0)
redeemTxIn := wire.NewTxIn(prevOut, nil, nil)
redeemTx.AddTxIn(redeemTxIn)
redeemTxOut := wire.NewTxOut((quantity - txFee), destinationPkScript)
redeemTx.AddTxOut(redeemTxOut)
sigScript, err := txscript.SignatureScript(redeemTx, 0, sourceTx.TxOut[0].PkScript, txscript.SigHashAll, decodedwif.PrivKey, false)
if err != nil {
fmt.Printf("sigScript err: %vn",err)
}
redeemTx.TxIn[0].SignatureScript = sigScript
fmt.Printf("sigScript: %vn",hex.EncodeToString(sigScript))
//Validate signature
flags := txscript.StandardVerifyFlags
vm, err := txscript.NewEngine(sourceTx.TxOut[0].PkScript, redeemTx, 0, flags, nil, nil, quantity)
if err != nil {
fmt.Printf("err != nil: %vn",err)
}
if err := vm.Execute(); err != nil {
fmt.Printf("vm.Execute > err != nil: %vn",err)
}
fmt.Printf("redeemTx: %vn",txToHex(redeemTx))
}
func predominant(){
stkbtc()
}
the txhash was from earlier transaction the place i acquired the testnet bitcoin from faucet & nothing else..
Please recommendation what’s mistaken with the code above, actually admire it if somebody can level it out.