// createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy // based on the passed block height to the provided address. When the address // is nil, the coinbase transaction will instead be redeemable by anyone. // // See the comment for NewBlockTemplate for more information about why the nil // address handling is useful. func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int64, addr btcutil.Address) (*btcutil.Tx, error) { // Create the script to pay to the provided payment address if one was // specified. Otherwise create a script that allows the coinbase to be // redeemable by anyone. var pkScript []byte if addr != nil { var err error pkScript, err = btcscript.PayToAddrScript(addr) if err != nil { return nil, err } } else { scriptBuilder := btcscript.NewScriptBuilder() pkScript = scriptBuilder.AddOp(btcscript.OP_TRUE).Script() } tx := btcwire.NewMsgTx() tx.AddTxIn(&btcwire.TxIn{ // Coinbase transactions have no inputs, so previous outpoint is // zero hash and max index. PreviousOutPoint: *btcwire.NewOutPoint(&btcwire.ShaHash{}, btcwire.MaxPrevOutIndex), SignatureScript: coinbaseScript, Sequence: btcwire.MaxTxInSequenceNum, }) tx.AddTxOut(&btcwire.TxOut{ Value: btcchain.CalcBlockSubsidy(nextBlockHeight, activeNetParams.Params), PkScript: pkScript, }) return btcutil.NewTx(tx), nil }
// TestScriptBuilderAddInt64 tests that pushing signed integers to a script via // the ScriptBuilder API works as expected. func TestScriptBuilderAddInt64(t *testing.T) { tests := []struct { name string val int64 expected []byte }{ {name: "push -1", val: -1, expected: []byte{btcscript.OP_1NEGATE}}, {name: "push small int 0", val: 0, expected: []byte{btcscript.OP_0}}, {name: "push small int 1", val: 1, expected: []byte{btcscript.OP_1}}, {name: "push small int 2", val: 2, expected: []byte{btcscript.OP_2}}, {name: "push small int 3", val: 3, expected: []byte{btcscript.OP_3}}, {name: "push small int 4", val: 4, expected: []byte{btcscript.OP_4}}, {name: "push small int 5", val: 5, expected: []byte{btcscript.OP_5}}, {name: "push small int 6", val: 6, expected: []byte{btcscript.OP_6}}, {name: "push small int 7", val: 7, expected: []byte{btcscript.OP_7}}, {name: "push small int 8", val: 8, expected: []byte{btcscript.OP_8}}, {name: "push small int 9", val: 9, expected: []byte{btcscript.OP_9}}, {name: "push small int 10", val: 10, expected: []byte{btcscript.OP_10}}, {name: "push small int 11", val: 11, expected: []byte{btcscript.OP_11}}, {name: "push small int 12", val: 12, expected: []byte{btcscript.OP_12}}, {name: "push small int 13", val: 13, expected: []byte{btcscript.OP_13}}, {name: "push small int 14", val: 14, expected: []byte{btcscript.OP_14}}, {name: "push small int 15", val: 15, expected: []byte{btcscript.OP_15}}, {name: "push small int 16", val: 16, expected: []byte{btcscript.OP_16}}, {name: "push 17", val: 17, expected: []byte{btcscript.OP_DATA_1, 0x11}}, {name: "push 65", val: 65, expected: []byte{btcscript.OP_DATA_1, 0x41}}, {name: "push 127", val: 127, expected: []byte{btcscript.OP_DATA_1, 0x7f}}, {name: "push 128", val: 128, expected: []byte{btcscript.OP_DATA_2, 0x80, 0}}, {name: "push 255", val: 255, expected: []byte{btcscript.OP_DATA_2, 0xff, 0}}, {name: "push 256", val: 256, expected: []byte{btcscript.OP_DATA_2, 0, 0x01}}, {name: "push 32767", val: 32767, expected: []byte{btcscript.OP_DATA_2, 0xff, 0x7f}}, {name: "push 32768", val: 32768, expected: []byte{btcscript.OP_DATA_3, 0, 0x80, 0}}, {name: "push -2", val: -2, expected: []byte{btcscript.OP_DATA_1, 0x82}}, {name: "push -3", val: -3, expected: []byte{btcscript.OP_DATA_1, 0x83}}, {name: "push -4", val: -4, expected: []byte{btcscript.OP_DATA_1, 0x84}}, {name: "push -5", val: -5, expected: []byte{btcscript.OP_DATA_1, 0x85}}, {name: "push -17", val: -17, expected: []byte{btcscript.OP_DATA_1, 0x91}}, {name: "push -65", val: -65, expected: []byte{btcscript.OP_DATA_1, 0xc1}}, {name: "push -127", val: -127, expected: []byte{btcscript.OP_DATA_1, 0xff}}, {name: "push -128", val: -128, expected: []byte{btcscript.OP_DATA_2, 0x80, 0x80}}, {name: "push -255", val: -255, expected: []byte{btcscript.OP_DATA_2, 0xff, 0x80}}, {name: "push -256", val: -256, expected: []byte{btcscript.OP_DATA_2, 0x00, 0x81}}, {name: "push -32767", val: -32767, expected: []byte{btcscript.OP_DATA_2, 0xff, 0xff}}, {name: "push -32768", val: -32768, expected: []byte{btcscript.OP_DATA_3, 0x00, 0x80, 0x80}}, } builder := btcscript.NewScriptBuilder() t.Logf("Running %d tests", len(tests)) for i, test := range tests { builder.Reset().AddInt64(test.val) result := builder.Script() if !bytes.Equal(result, test.expected) { t.Errorf("ScriptBuilder.AddInt64 #%d (%s) wrong result\n"+ "got: %x\nwant: %x", i, test.name, result, test.expected) continue } } }
func makeScriptPubKey(toAddr string) ([]byte, error) { addr, err := btcutil.DecodeAddress(toAddr, &btcnet.MainNetParams) if err != nil { return nil, err } log.Println("script addr:", hex.EncodeToString(addr.ScriptAddress())) builder := btcscript.NewScriptBuilder() builder.AddOp(btcscript.OP_DUP).AddOp(btcscript.OP_HASH160) builder.AddData(addr.ScriptAddress()) builder.AddOp(btcscript.OP_EQUALVERIFY).AddOp(btcscript.OP_CHECKSIG) //script := "76" + "a9" + "14" + hex.EncodeToString(addr.ScriptAddress()) + "88" + "ac" return builder.Script(), nil }
func (ndB *NullDataBuilder) Build() (*btcwire.MsgTx, error) { utxo, err := specificUnspent(ndB.SatNeeded(), ndB.Params) if err != nil { return nil, err } msgtx := btcwire.NewMsgTx() if len(ndB.Data) > 40 { return nil, errors.New("Data is too long to make this a standard tx.") } // OP Return output retbuilder := btcscript.NewScriptBuilder().AddOp(btcscript.OP_RETURN).AddData(ndB.Data) op_return := btcwire.NewTxOut(0, retbuilder.Script()) msgtx.AddTxOut(op_return) if ndB.Change { // change ouput addr, _ := newAddr(ndB.Params.Client) change, ok := changeOutput(ndB.SatNeeded()-ndB.Params.Fee, ndB.Params.DustAmnt, addr) if !ok { return nil, errors.New("Not enough for change") } msgtx.AddTxOut(change) } // funding input txin := btcwire.NewTxIn(utxo.OutPoint, []byte{}) msgtx.AddTxIn(txin) // sign msgtx privkey := utxo.Wif.PrivKey scriptSig, err := btcscript.SignatureScript(msgtx, 0, utxo.TxOut.PkScript, btcscript.SigHashAll, privkey, true) if err != nil { return nil, err } txin.SignatureScript = scriptSig return msgtx, nil }
// TestScriptBuilderAddOp tests that pushing opcodes to a script via the // ScriptBuilder API works as expected. func TestScriptBuilderAddOp(t *testing.T) { tests := []struct { name string opcodes []byte expected []byte }{ { name: "push OP_0", opcodes: []byte{btcscript.OP_0}, expected: []byte{btcscript.OP_0}, }, { name: "push OP_1 OP_2", opcodes: []byte{btcscript.OP_1, btcscript.OP_2}, expected: []byte{btcscript.OP_1, btcscript.OP_2}, }, { name: "push OP_HASH160 OP_EQUAL", opcodes: []byte{btcscript.OP_HASH160, btcscript.OP_EQUAL}, expected: []byte{btcscript.OP_HASH160, btcscript.OP_EQUAL}, }, } builder := btcscript.NewScriptBuilder() t.Logf("Running %d tests", len(tests)) for i, test := range tests { builder.Reset() for _, opcode := range test.opcodes { builder.AddOp(opcode) } result := builder.Script() if !bytes.Equal(result, test.expected) { t.Errorf("ScriptBuilder.AddOp #%d (%s) wrong result\n"+ "got: %x\nwant: %x", i, test.name, result, test.expected) continue } } }
// TODO This will add multisig Txouts to the unspent set be AWARE func (msB *MultiSigBuilder) Build() (*btcwire.MsgTx, error) { utxo, err := specificUnspent(msB.SatNeeded(), msB.Params) if err != nil { return nil, err } msgtx := btcwire.NewMsgTx() txin := btcwire.NewTxIn(utxo.OutPoint, []byte{}) msgtx.AddTxIn(txin) for _, pubkeys := range msB.PubKeyList { // M pubkey pubkey pubkey N OP_CHECKMULTISIG scriptBuilder := btcscript.NewScriptBuilder().AddInt64(msB.M) for _, pk := range pubkeys { scriptBuilder = scriptBuilder.AddData(pk) } scriptBuilder = scriptBuilder.AddInt64(msB.N).AddOp(btcscript.OP_CHECKMULTISIG) PkScript := scriptBuilder.Script() txout := btcwire.NewTxOut(msB.eachOutVal(), PkScript) msgtx.AddTxOut(txout) } // Sign this puppy privkey := utxo.Wif.PrivKey subscript := utxo.TxOut.PkScript sigflag := btcscript.SigHashAll scriptSig, err := btcscript.SignatureScript(msgtx, 0, subscript, sigflag, privkey, true) if err != nil { return nil, err } msgtx.TxIn[0].SignatureScript = scriptSig return msgtx, nil }
// standardCoinbaseScript returns a standard script suitable for use as the // signature script of the coinbase transaction of a new block. In particular, // it starts with the block height that is required by version 2 blocks and adds // the extra nonce as well as additional coinbase flags. func standardCoinbaseScript(nextBlockHeight int64, extraNonce uint64) []byte { return btcscript.NewScriptBuilder().AddInt64(nextBlockHeight). AddUint64(extraNonce).AddData([]byte(coinbaseFlags)).Script() }
// TestScriptBuilderAddData tests that pushing data to a script via the // ScriptBuilder API works as expected. func TestScriptBuilderAddData(t *testing.T) { tests := []struct { name string data []byte expected []byte }{ // Start off with the small ints to ensure canonical encoding. {name: "push small int 0", data: []byte{0}, expected: []byte{btcscript.OP_0}}, {name: "push small int 1", data: []byte{1}, expected: []byte{btcscript.OP_1}}, {name: "push small int 2", data: []byte{2}, expected: []byte{btcscript.OP_2}}, {name: "push small int 3", data: []byte{3}, expected: []byte{btcscript.OP_3}}, {name: "push small int 4", data: []byte{4}, expected: []byte{btcscript.OP_4}}, {name: "push small int 5", data: []byte{5}, expected: []byte{btcscript.OP_5}}, {name: "push small int 6", data: []byte{6}, expected: []byte{btcscript.OP_6}}, {name: "push small int 7", data: []byte{7}, expected: []byte{btcscript.OP_7}}, {name: "push small int 8", data: []byte{8}, expected: []byte{btcscript.OP_8}}, {name: "push small int 9", data: []byte{9}, expected: []byte{btcscript.OP_9}}, {name: "push small int 10", data: []byte{10}, expected: []byte{btcscript.OP_10}}, {name: "push small int 11", data: []byte{11}, expected: []byte{btcscript.OP_11}}, {name: "push small int 12", data: []byte{12}, expected: []byte{btcscript.OP_12}}, {name: "push small int 13", data: []byte{13}, expected: []byte{btcscript.OP_13}}, {name: "push small int 14", data: []byte{14}, expected: []byte{btcscript.OP_14}}, {name: "push small int 15", data: []byte{15}, expected: []byte{btcscript.OP_15}}, {name: "push small int 16", data: []byte{16}, expected: []byte{btcscript.OP_16}}, // 1-byte data push opcodes. { name: "push data len 17", data: bytes.Repeat([]byte{0x49}, 17), expected: append([]byte{btcscript.OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...), }, { name: "push data len 75", data: bytes.Repeat([]byte{0x49}, 75), expected: append([]byte{btcscript.OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...), }, // 2-byte data push via OP_PUSHDATA_1. { name: "push data len 76", data: bytes.Repeat([]byte{0x49}, 76), expected: append([]byte{btcscript.OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...), }, { name: "push data len 255", data: bytes.Repeat([]byte{0x49}, 255), expected: append([]byte{btcscript.OP_PUSHDATA1, 255}, bytes.Repeat([]byte{0x49}, 255)...), }, // 3-byte data push via OP_PUSHDATA_2. { name: "push data len 256", data: bytes.Repeat([]byte{0x49}, 256), expected: append([]byte{btcscript.OP_PUSHDATA2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...), }, { name: "push data len 32767", data: bytes.Repeat([]byte{0x49}, 32767), expected: append([]byte{btcscript.OP_PUSHDATA2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...), }, // 5-byte data push via OP_PUSHDATA_4. { name: "push data len 65536", data: bytes.Repeat([]byte{0x49}, 65536), expected: append([]byte{btcscript.OP_PUSHDATA4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...), }, } builder := btcscript.NewScriptBuilder() t.Logf("Running %d tests", len(tests)) for i, test := range tests { builder.Reset().AddData(test.data) result := builder.Script() if !bytes.Equal(result, test.expected) { t.Errorf("ScriptBuilder.AddData #%d (%s) wrong result\n"+ "got: %x\nwant: %x", i, test.name, result, test.expected) continue } } }