Beispiel #1
1
// commitScriptToSelf constructs the public key script for the output on the
// commitment transaction paying to the "owner" of said commitment transaction.
// If the other party learns of the pre-image to the revocation hash, then they
// can claim all the settled funds in the channel, plus the unsettled funds.
func commitScriptToSelf(csvTimeout uint32, selfKey, theirKey *btcec.PublicKey, revokeHash []byte) ([]byte, error) {
	// This script is spendable under two conditions: either the 'csvTimeout'
	// has passed and we can redeem our funds, or they have the pre-image
	// to 'revokeHash'.
	builder := txscript.NewScriptBuilder()

	// If the pre-image for the revocation hash is presented, then allow a
	// spend provided the proper signature.
	builder.AddOp(txscript.OP_HASH160)
	builder.AddData(revokeHash)
	builder.AddOp(txscript.OP_EQUAL)
	builder.AddOp(txscript.OP_IF)
	builder.AddData(theirKey.SerializeCompressed())
	builder.AddOp(txscript.OP_ELSE)

	// Otherwise, we can re-claim our funds after a CSV delay of
	// 'csvTimeout' timeout blocks, and a valid signature.
	builder.AddInt64(int64(csvTimeout))
	builder.AddOp(OP_CHECKSEQUENCEVERIFY)
	builder.AddOp(txscript.OP_DROP)
	builder.AddData(selfKey.SerializeCompressed())
	builder.AddOp(txscript.OP_ENDIF)
	builder.AddOp(txscript.OP_CHECKSIG)

	return builder.Script()
}
// TestHasCanonicalPush ensures the canonicalPush function works as expected.
func TestHasCanonicalPush(t *testing.T) {
	t.Parallel()

	for i := 0; i < 65535; i++ {
		builder := txscript.NewScriptBuilder()
		builder.AddInt64(int64(i))
		script, err := builder.Script()
		if err != nil {
			t.Errorf("Script: test #%d unexpected error: %v\n", i,
				err)
			continue
		}
		if result := txscript.IsPushOnlyScript(script); !result {
			t.Errorf("IsPushOnlyScript: test #%d failed: %x\n", i,
				script)
			continue
		}
		pops, err := txscript.TstParseScript(script)
		if err != nil {
			t.Errorf("TstParseScript: #%d failed: %v", i, err)
			continue
		}
		for _, pop := range pops {
			if result := txscript.TstHasCanonicalPushes(pop); !result {
				t.Errorf("TstHasCanonicalPushes: test #%d "+
					"failed: %x\n", i, script)
				break
			}
		}
	}
	for i := 0; i <= txscript.MaxScriptElementSize; i++ {
		builder := txscript.NewScriptBuilder()
		builder.AddData(bytes.Repeat([]byte{0x49}, i))
		script, err := builder.Script()
		if err != nil {
			t.Errorf("StandardPushesTests test #%d unexpected error: %v\n", i, err)
			continue
		}
		if result := txscript.IsPushOnlyScript(script); !result {
			t.Errorf("StandardPushesTests IsPushOnlyScript test #%d failed: %x\n", i, script)
			continue
		}
		pops, err := txscript.TstParseScript(script)
		if err != nil {
			t.Errorf("StandardPushesTests #%d failed to TstParseScript: %v", i, err)
			continue
		}
		for _, pop := range pops {
			if result := txscript.TstHasCanonicalPushes(pop); !result {
				t.Errorf("StandardPushesTests TstHasCanonicalPushes test #%d failed: %x\n", i, script)
				break
			}
		}
	}
}
Beispiel #3
0
// 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(params *chaincfg.Params, coinbaseScript []byte, nextBlockHeight int32, 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 = txscript.PayToAddrScript(addr)
		if err != nil {
			return nil, err
		}
	} else {
		var err error
		scriptBuilder := txscript.NewScriptBuilder()
		pkScript, err = scriptBuilder.AddOp(txscript.OP_TRUE).Script()
		if err != nil {
			return nil, err
		}
	}

	tx := wire.NewMsgTx(wire.TxVersion)
	tx.AddTxIn(&wire.TxIn{
		// Coinbase transactions have no inputs, so previous outpoint is
		// zero hash and max index.
		PreviousOutPoint: *wire.NewOutPoint(&chainhash.Hash{},
			wire.MaxPrevOutIndex),
		SignatureScript: coinbaseScript,
		Sequence:        wire.MaxTxInSequenceNum,
	})
	tx.AddTxOut(&wire.TxOut{
		Value:    blockchain.CalcBlockSubsidy(nextBlockHeight, params),
		PkScript: pkScript,
	})
	return btcutil.NewTx(tx), nil
}
Beispiel #4
0
// scriptHashPkScript generates a pay-to-script-hash public key script paying
// to the hash160 of the passed redeem script.
func scriptHashPkScript(redeemScript []byte) ([]byte, error) {
	bldr := txscript.NewScriptBuilder()
	bldr.AddOp(txscript.OP_HASH160)
	bldr.AddData(btcutil.Hash160(redeemScript))
	bldr.AddOp(txscript.OP_EQUAL)
	return bldr.Script()
}
Beispiel #5
0
// commitScriptUnencumbered constructs the public key script on the commitment
// transaction paying to the "other" party. This output is spendable
// immediately, requiring no contestation period.
func commitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) {
	// This script goes to the "other" party, and it spendable immediately.
	builder := txscript.NewScriptBuilder()
	builder.AddOp(txscript.OP_DUP)
	builder.AddOp(txscript.OP_HASH160)
	builder.AddData(btcutil.Hash160(key.SerializeCompressed()))
	builder.AddOp(txscript.OP_EQUALVERIFY)
	builder.AddOp(txscript.OP_CHECKSIG)

	return builder.Script()
}
Beispiel #6
0
// TestScriptBuilderAddUint64 tests that pushing unsigned integers to a script
// via the ScriptBuilder API works as expected.
func TestScriptBuilderAddUint64(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name     string
		val      uint64
		expected []byte
	}{
		{name: "push small int 0", val: 0, expected: []byte{txscript.OP_0}},
		{name: "push small int 1", val: 1, expected: []byte{txscript.OP_1}},
		{name: "push small int 2", val: 2, expected: []byte{txscript.OP_2}},
		{name: "push small int 3", val: 3, expected: []byte{txscript.OP_3}},
		{name: "push small int 4", val: 4, expected: []byte{txscript.OP_4}},
		{name: "push small int 5", val: 5, expected: []byte{txscript.OP_5}},
		{name: "push small int 6", val: 6, expected: []byte{txscript.OP_6}},
		{name: "push small int 7", val: 7, expected: []byte{txscript.OP_7}},
		{name: "push small int 8", val: 8, expected: []byte{txscript.OP_8}},
		{name: "push small int 9", val: 9, expected: []byte{txscript.OP_9}},
		{name: "push small int 10", val: 10, expected: []byte{txscript.OP_10}},
		{name: "push small int 11", val: 11, expected: []byte{txscript.OP_11}},
		{name: "push small int 12", val: 12, expected: []byte{txscript.OP_12}},
		{name: "push small int 13", val: 13, expected: []byte{txscript.OP_13}},
		{name: "push small int 14", val: 14, expected: []byte{txscript.OP_14}},
		{name: "push small int 15", val: 15, expected: []byte{txscript.OP_15}},
		{name: "push small int 16", val: 16, expected: []byte{txscript.OP_16}},
		{name: "push 17", val: 17, expected: []byte{txscript.OP_DATA_1, 0x11}},
		{name: "push 65", val: 65, expected: []byte{txscript.OP_DATA_1, 0x41}},
		{name: "push 127", val: 127, expected: []byte{txscript.OP_DATA_1, 0x7f}},
		{name: "push 128", val: 128, expected: []byte{txscript.OP_DATA_2, 0x80, 0}},
		{name: "push 255", val: 255, expected: []byte{txscript.OP_DATA_2, 0xff, 0}},
		{name: "push 256", val: 256, expected: []byte{txscript.OP_DATA_2, 0, 0x01}},
		{name: "push 32767", val: 32767, expected: []byte{txscript.OP_DATA_2, 0xff, 0x7f}},
		{name: "push 32768", val: 32768, expected: []byte{txscript.OP_DATA_3, 0, 0x80, 0}},
	}

	builder := txscript.NewScriptBuilder()
	t.Logf("Running %d tests", len(tests))
	for i, test := range tests {
		builder.Reset().AddUint64(test.val)
		result, err := builder.Script()
		if err != nil {
			t.Errorf("ScriptBuilder.AddUint64 #%d (%s) unexpected "+
				"error: %v", i, test.name, err)
			continue
		}
		if !bytes.Equal(result, test.expected) {
			t.Errorf("ScriptBuilder.AddUint64 #%d (%s) wrong result\n"+
				"got: %x\nwant: %x", i, test.name, result,
				test.expected)
			continue
		}
	}
}
Beispiel #7
0
// TestExceedMaxScriptSize ensures that all of the functions that can be used
// to add data to a script don't allow the script to exceed the max allowed
// size.
func TestExceedMaxScriptSize(t *testing.T) {
	t.Parallel()

	// Start off by constructing a max size script.
	maxScriptSize := txscript.TstMaxScriptSize
	builder := txscript.NewScriptBuilder()
	builder.Reset().AddFullData(make([]byte, maxScriptSize-3))
	origScript, err := builder.Script()
	if err != nil {
		t.Fatalf("Unexpected error for max size script: %v", err)
	}

	// Ensure adding data that would exceed the maximum size of the script
	// does not add the data.
	script, err := builder.AddData([]byte{0x00}).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+
			"size: %v", len(script))
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}

	// Ensure adding an opcode that would exceed the maximum size of the
	// script does not add the data.
	builder.Reset().AddFullData(make([]byte, maxScriptSize-3))
	script, err = builder.AddOp(txscript.OP_0).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}

	// Ensure adding an integer that would exceed the maximum size of the
	// script does not add the data.
	builder.Reset().AddFullData(make([]byte, maxScriptSize-3))
	script, err = builder.AddInt64(0).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}
}
Beispiel #8
0
// TestScriptBuilderAddOp tests that pushing opcodes to a script via the
// ScriptBuilder API works as expected.
func TestScriptBuilderAddOp(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name     string
		opcodes  []byte
		expected []byte
	}{
		{
			name:     "push OP_0",
			opcodes:  []byte{txscript.OP_0},
			expected: []byte{txscript.OP_0},
		},
		{
			name:     "push OP_1 OP_2",
			opcodes:  []byte{txscript.OP_1, txscript.OP_2},
			expected: []byte{txscript.OP_1, txscript.OP_2},
		},
		{
			name:     "push OP_HASH160 OP_EQUAL",
			opcodes:  []byte{txscript.OP_HASH160, txscript.OP_EQUAL},
			expected: []byte{txscript.OP_HASH160, txscript.OP_EQUAL},
		},
	}

	builder := txscript.NewScriptBuilder()
	t.Logf("Running %d tests", len(tests))
	for i, test := range tests {
		builder.Reset()
		for _, opcode := range test.opcodes {
			builder.AddOp(opcode)
		}
		result, err := builder.Script()
		if err != nil {
			t.Errorf("ScriptBuilder.AddOp #%d (%s) unexpected "+
				"error: %v", i, test.name, err)
			continue
		}
		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
		}
	}
}
Beispiel #9
0
// signMultiSigUTXO signs the P2SH UTXO with the given index by constructing a
// script containing all given signatures plus the redeem (multi-sig) script. The
// redeem script is obtained by looking up the address of the given P2SH pkScript
// on the address manager.
// The order of the signatures must match that of the public keys in the multi-sig
// script as OP_CHECKMULTISIG expects that.
// This function must be called with the manager unlocked.
func signMultiSigUTXO(mgr *waddrmgr.Manager, tx *wire.MsgTx, idx int, pkScript []byte, sigs []RawSig) error {
	class, addresses, _, err := txscript.ExtractPkScriptAddrs(pkScript, mgr.ChainParams())
	if err != nil {
		return newError(ErrTxSigning, "unparseable pkScript", err)
	}
	if class != txscript.ScriptHashTy {
		return newError(ErrTxSigning, fmt.Sprintf("pkScript is not P2SH: %s", class), nil)
	}
	redeemScript, err := getRedeemScript(mgr, addresses[0].(*btcutil.AddressScriptHash))
	if err != nil {
		return newError(ErrTxSigning, "unable to retrieve redeem script", err)
	}

	class, _, nRequired, err := txscript.ExtractPkScriptAddrs(redeemScript, mgr.ChainParams())
	if err != nil {
		return newError(ErrTxSigning, "unparseable redeem script", err)
	}
	if class != txscript.MultiSigTy {
		return newError(ErrTxSigning, fmt.Sprintf("redeem script is not multi-sig: %v", class), nil)
	}
	if len(sigs) < nRequired {
		errStr := fmt.Sprintf("not enough signatures; need %d but got only %d", nRequired,
			len(sigs))
		return newError(ErrTxSigning, errStr, nil)
	}

	// Construct the unlocking script.
	// Start with an OP_0 because of the bug in bitcoind, then add nRequired signatures.
	unlockingScript := txscript.NewScriptBuilder().AddOp(txscript.OP_FALSE)
	for _, sig := range sigs[:nRequired] {
		unlockingScript.AddData(sig)
	}

	// Combine the redeem script and the unlocking script to get the actual signature script.
	sigScript := unlockingScript.AddData(redeemScript)
	script, err := sigScript.Script()
	if err != nil {
		return newError(ErrTxSigning, "error building sigscript", err)
	}
	tx.TxIn[idx].SignatureScript = script

	if err := validateSigScript(tx, idx, pkScript); err != nil {
		return err
	}
	return nil
}
Beispiel #10
0
// spendMultiSig generates the scriptSig required to redeem the 2-of-2 p2sh
// multi-sig output.
func spendMultiSig(redeemScript, sigA, sigB []byte) ([]byte, error) {
	bldr := txscript.NewScriptBuilder()

	// add a 0 for some multisig fun
	bldr.AddOp(txscript.OP_0)

	// add sigA
	bldr.AddData(sigA)
	// add sigB
	bldr.AddData(sigB)

	// preimage goes on AT THE ENDDDD
	bldr.AddData(redeemScript)

	// that's all, get bytes
	return bldr.Script()
}
Beispiel #11
0
// senderHTLCScript constructs the public key script for an incoming HTLC
// output payment for the receiver's commitment transaction.
func receiverHTLCScript(absoluteTimeout, relativeTimeout uint32, senderKey,
	receiverKey *btcec.PublicKey, revokeHash, paymentHash []byte) ([]byte, error) {

	builder := txscript.NewScriptBuilder()

	// Was the pre-image to the payment hash presented?
	builder.AddOp(txscript.OP_HASH160)
	builder.AddOp(txscript.OP_DUP)
	builder.AddData(paymentHash)
	builder.AddOp(txscript.OP_EQUAL)
	builder.AddOp(txscript.OP_IF)

	// If so, let the receiver redeem after a relative timeout. This added
	// delay gives the sender (at this time) an opportunity to re-claim the
	// pending HTLC in the event that the receiver (at this time) broadcasts
	// this old commitment transaction after it has been revoked.
	builder.AddInt64(int64(relativeTimeout))
	builder.AddOp(OP_CHECKSEQUENCEVERIFY)
	builder.AddOp(txscript.OP_2DROP)
	builder.AddData(receiverKey.SerializeCompressed())

	// Otherwise, if the sender has the revocation pre-image to the
	// receiver's commitment transactions, then let them claim the
	// funds immediately.
	builder.AddOp(txscript.OP_ELSE)
	builder.AddData(revokeHash)
	builder.AddOp(txscript.OP_EQUAL)

	// If not, then the sender needs to wait for the HTLC timeout. This
	// clause may be executed if the receiver fails to present the r-value
	// in time. This prevents the pending funds from being locked up
	// indefinately.
	builder.AddOp(txscript.OP_NOTIF)
	builder.AddInt64(int64(absoluteTimeout))
	builder.AddOp(OP_CHECKSEQUENCEVERIFY)
	builder.AddOp(txscript.OP_DROP)
	builder.AddOp(txscript.OP_ENDIF)

	builder.AddData(senderKey.SerializeCompressed())

	builder.AddOp(txscript.OP_ENDIF)
	builder.AddOp(txscript.OP_CHECKSIG)

	return builder.Script()
}
Beispiel #12
0
// getFundingPkScript generates the non-p2sh'd multisig script for 2 of 2
// pubkeys.
func genFundingPkScript(aPub, bPub []byte) ([]byte, error) {
	if len(aPub) != 33 || len(bPub) != 33 {
		return nil, fmt.Errorf("Pubkey size error. Compressed pubkeys only")
	}

	// Swap to sort pubkeys if needed. Keys are sorted in lexicographical
	// order. The signatures within the scriptSig must also adhere to the
	// order, ensuring that the signatures for each public key appears
	// in the proper order on the stack.
	if bytes.Compare(aPub, bPub) == -1 {
		aPub, bPub = bPub, aPub
	}

	bldr := txscript.NewScriptBuilder()
	bldr.AddOp(txscript.OP_2)
	bldr.AddData(aPub) // Add both pubkeys (sorted).
	bldr.AddData(bPub)
	bldr.AddOp(txscript.OP_2)
	bldr.AddOp(txscript.OP_CHECKMULTISIG)
	return bldr.Script()
}
Beispiel #13
0
// senderHTLCScript constructs the public key script for an outgoing HTLC
// output payment for the sender's commitment transaction.
func senderHTLCScript(absoluteTimeout, relativeTimeout uint32, senderKey,
	receiverKey *btcec.PublicKey, revokeHash, paymentHash []byte) ([]byte, error) {

	builder := txscript.NewScriptBuilder()

	// Was the pre-image to the payment hash presented?
	builder.AddOp(txscript.OP_HASH160)
	builder.AddOp(txscript.OP_DUP)
	builder.AddData(paymentHash)
	builder.AddOp(txscript.OP_EQUAL)

	// How about the pre-image for our commitment revocation hash?
	builder.AddOp(txscript.OP_SWAP)
	builder.AddData(revokeHash)
	builder.AddOp(txscript.OP_EQUAL)
	builder.AddOp(txscript.OP_SWAP)
	builder.AddOp(txscript.OP_ADD)

	// If either is present, then the receiver can claim immediately.
	builder.AddOp(txscript.OP_IF)
	builder.AddData(receiverKey.SerializeCompressed())

	// Otherwise, we (the sender) need to wait for an absolute HTLC
	// timeout, then afterwards a relative timeout before we claim re-claim
	// the unsettled funds. This delay gives the other party a chance to
	// present the pre-image to the revocation hash in the event that the
	// sender (at this time) broadcasts this commitment transaction after
	// it has been revoked.
	builder.AddOp(txscript.OP_ELSE)
	builder.AddInt64(int64(absoluteTimeout))
	builder.AddOp(txscript.OP_CHECKLOCKTIMEVERIFY)
	builder.AddInt64(int64(relativeTimeout))
	builder.AddOp(OP_CHECKSEQUENCEVERIFY)
	builder.AddOp(txscript.OP_2DROP)
	builder.AddData(senderKey.SerializeCompressed())
	builder.AddOp(txscript.OP_ENDIF)
	builder.AddOp(txscript.OP_CHECKSIG)

	return builder.Script()
}
Beispiel #14
0
// spendMultiSig generates the scriptSig required to redeem the 2-of-2 p2sh
// multi-sig output.
func spendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) ([]byte, error) {
	bldr := txscript.NewScriptBuilder()

	// add a 0 for some multisig fun
	bldr.AddOp(txscript.OP_0)

	// When initially generating the redeemScript, we sorted the serialized
	// public keys in descending order. So we do a quick comparison in order
	// ensure the signatures appear on the Script Virual Machine stack in
	// the correct order.
	if bytes.Compare(pubA, pubB) == -1 {
		bldr.AddData(sigB)
		bldr.AddData(sigA)
	} else {
		bldr.AddData(sigA)
		bldr.AddData(sigB)
	}

	// preimage goes on AT THE ENDDDD
	bldr.AddData(redeemScript)

	// that's all, get bytes
	return bldr.Script()
}
Beispiel #15
0
// CreateCoinbaseTx returns a coinbase transaction with the requested number of
// outputs paying an appropriate subsidy based on the passed block height to the
// address associated with the harness.  It automatically uses a standard
// signature script that starts with the block height that is required by
// version 2 blocks.
func (p *poolHarness) CreateCoinbaseTx(blockHeight int32, numOutputs uint32) (*btcutil.Tx, error) {
	// Create standard coinbase script.
	extraNonce := int64(0)
	coinbaseScript, err := txscript.NewScriptBuilder().
		AddInt64(int64(blockHeight)).AddInt64(extraNonce).Script()
	if err != nil {
		return nil, err
	}

	tx := wire.NewMsgTx(wire.TxVersion)
	tx.AddTxIn(&wire.TxIn{
		// Coinbase transactions have no inputs, so previous outpoint is
		// zero hash and max index.
		PreviousOutPoint: *wire.NewOutPoint(&chainhash.Hash{},
			wire.MaxPrevOutIndex),
		SignatureScript: coinbaseScript,
		Sequence:        wire.MaxTxInSequenceNum,
	})
	totalInput := blockchain.CalcBlockSubsidy(blockHeight, p.chainParams)
	amountPerOutput := totalInput / int64(numOutputs)
	remainder := totalInput - amountPerOutput*int64(numOutputs)
	for i := uint32(0); i < numOutputs; i++ {
		// Ensure the final output accounts for any remainder that might
		// be left from splitting the input amount.
		amount := amountPerOutput
		if i == numOutputs-1 {
			amount = amountPerOutput + remainder
		}
		tx.AddTxOut(&wire.TxOut{
			PkScript: p.payScript,
			Value:    amount,
		})
	}

	return btcutil.NewTx(tx), nil
}
Beispiel #16
0
// 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 int32, extraNonce uint64) ([]byte, error) {
	return txscript.NewScriptBuilder().AddInt64(int64(nextBlockHeight)).
		AddInt64(int64(extraNonce)).AddData([]byte(coinbaseFlags)).
		Script()
}
Beispiel #17
0
// TestErroredScript ensures that all of the functions that can be used to add
// data to a script don't modify the script once an error has happened.
func TestErroredScript(t *testing.T) {
	t.Parallel()

	// Start off by constructing a near max size script that has enough
	// space left to add each data type without an error and force an
	// initial error condition.
	maxScriptSize := txscript.TstMaxScriptSize
	builder := txscript.NewScriptBuilder()
	builder.Reset().AddFullData(make([]byte, maxScriptSize-8))
	origScript, err := builder.Script()
	if err != nil {
		t.Fatalf("ScriptBuilder.AddFullData unexpected error: %v", err)
	}
	script, err := builder.AddData([]byte{0x00, 0x00, 0x00, 0x00, 0x00}).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+
			"size: %v", len(script))
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}

	// Ensure adding data, even using the non-canonical path, to a script
	// that has errored doesn't succeed.
	script, err = builder.AddFullData([]byte{0x00}).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatal("ScriptBuilder.AddFullData succeeded on errored script")
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddFullData unexpected modified "+
			"script - got len %d, want len %d", len(script),
			len(origScript))
	}

	// Ensure adding data to a script that has errored doesn't succeed.
	script, err = builder.AddData([]byte{0x00}).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatal("ScriptBuilder.AddData succeeded on errored script")
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddData unexpected modified "+
			"script - got len %d, want len %d", len(script),
			len(origScript))
	}

	// Ensure adding an opcode to a script that has errored doesn't succeed.
	script, err = builder.AddOp(txscript.OP_0).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatal("ScriptBuilder.AddOp succeeded on errored script")
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}

	// Ensure adding an integer to a script that has errored doesn't
	// succeed.
	script, err = builder.AddInt64(0).Script()
	if _, ok := err.(txscript.ErrScriptNotCanonical); !ok || err == nil {
		t.Fatal("ScriptBuilder.AddInt64 succeeded on errored script")
	}
	if !bytes.Equal(script, origScript) {
		t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+
			"got len %d, want len %d", len(script), len(origScript))
	}

	// Ensure the error has a message set.
	if err.Error() == "" {
		t.Fatal("ErrScriptNotCanonical.Error does not have any text")
	}
}
Beispiel #18
0
// TestScriptBuilderAddData tests that pushing data to a script via the
// ScriptBuilder API works as expected and conforms to BIP0062.
func TestScriptBuilderAddData(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name     string
		data     []byte
		expected []byte
		useFull  bool // use AddFullData instead of AddData.
	}{
		// BIP0062: Pushing an empty byte sequence must use OP_0.
		{name: "push empty byte sequence", data: nil, expected: []byte{txscript.OP_0}},
		{name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{txscript.OP_0}},

		// BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n.
		{name: "push 1 byte 0x01", data: []byte{0x01}, expected: []byte{txscript.OP_1}},
		{name: "push 1 byte 0x02", data: []byte{0x02}, expected: []byte{txscript.OP_2}},
		{name: "push 1 byte 0x03", data: []byte{0x03}, expected: []byte{txscript.OP_3}},
		{name: "push 1 byte 0x04", data: []byte{0x04}, expected: []byte{txscript.OP_4}},
		{name: "push 1 byte 0x05", data: []byte{0x05}, expected: []byte{txscript.OP_5}},
		{name: "push 1 byte 0x06", data: []byte{0x06}, expected: []byte{txscript.OP_6}},
		{name: "push 1 byte 0x07", data: []byte{0x07}, expected: []byte{txscript.OP_7}},
		{name: "push 1 byte 0x08", data: []byte{0x08}, expected: []byte{txscript.OP_8}},
		{name: "push 1 byte 0x09", data: []byte{0x09}, expected: []byte{txscript.OP_9}},
		{name: "push 1 byte 0x0a", data: []byte{0x0a}, expected: []byte{txscript.OP_10}},
		{name: "push 1 byte 0x0b", data: []byte{0x0b}, expected: []byte{txscript.OP_11}},
		{name: "push 1 byte 0x0c", data: []byte{0x0c}, expected: []byte{txscript.OP_12}},
		{name: "push 1 byte 0x0d", data: []byte{0x0d}, expected: []byte{txscript.OP_13}},
		{name: "push 1 byte 0x0e", data: []byte{0x0e}, expected: []byte{txscript.OP_14}},
		{name: "push 1 byte 0x0f", data: []byte{0x0f}, expected: []byte{txscript.OP_15}},
		{name: "push 1 byte 0x10", data: []byte{0x10}, expected: []byte{txscript.OP_16}},

		// BIP0062: Pushing the byte 0x81 must use OP_1NEGATE.
		{name: "push 1 byte 0x81", data: []byte{0x81}, expected: []byte{txscript.OP_1NEGATE}},

		// BIP0062: Pushing any other byte sequence up to 75 bytes must
		// use the normal data push (opcode byte n, with n the number of
		// bytes, followed n bytes of data being pushed).
		{name: "push 1 byte 0x11", data: []byte{0x11}, expected: []byte{txscript.OP_DATA_1, 0x11}},
		{name: "push 1 byte 0x80", data: []byte{0x80}, expected: []byte{txscript.OP_DATA_1, 0x80}},
		{name: "push 1 byte 0x82", data: []byte{0x82}, expected: []byte{txscript.OP_DATA_1, 0x82}},
		{name: "push 1 byte 0xff", data: []byte{0xff}, expected: []byte{txscript.OP_DATA_1, 0xff}},
		{
			name:     "push data len 17",
			data:     bytes.Repeat([]byte{0x49}, 17),
			expected: append([]byte{txscript.OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...),
		},
		{
			name:     "push data len 75",
			data:     bytes.Repeat([]byte{0x49}, 75),
			expected: append([]byte{txscript.OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...),
		},

		// BIP0062: Pushing 76 to 255 bytes must use OP_PUSHDATA1.
		{
			name:     "push data len 76",
			data:     bytes.Repeat([]byte{0x49}, 76),
			expected: append([]byte{txscript.OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...),
		},
		{
			name:     "push data len 255",
			data:     bytes.Repeat([]byte{0x49}, 255),
			expected: append([]byte{txscript.OP_PUSHDATA1, 255}, bytes.Repeat([]byte{0x49}, 255)...),
		},

		// BIP0062: Pushing 256 to 520 bytes must use OP_PUSHDATA2.
		{
			name:     "push data len 256",
			data:     bytes.Repeat([]byte{0x49}, 256),
			expected: append([]byte{txscript.OP_PUSHDATA2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...),
		},
		{
			name:     "push data len 520",
			data:     bytes.Repeat([]byte{0x49}, 520),
			expected: append([]byte{txscript.OP_PUSHDATA2, 0x08, 0x02}, bytes.Repeat([]byte{0x49}, 520)...),
		},

		// BIP0062: OP_PUSHDATA4 can never be used, as pushes over 520
		// bytes are not allowed, and those below can be done using
		// other operators.
		{
			name:     "push data len 521",
			data:     bytes.Repeat([]byte{0x49}, 521),
			expected: nil,
		},
		{
			name:     "push data len 32767 (canonical)",
			data:     bytes.Repeat([]byte{0x49}, 32767),
			expected: nil,
		},
		{
			name:     "push data len 65536 (canonical)",
			data:     bytes.Repeat([]byte{0x49}, 65536),
			expected: nil,
		},

		// Additional tests for the PushFullData function that
		// intentionally allows data pushes to exceed the limit for
		// regression testing purposes.

		// 3-byte data push via OP_PUSHDATA_2.
		{
			name:     "push data len 32767 (non-canonical)",
			data:     bytes.Repeat([]byte{0x49}, 32767),
			expected: append([]byte{txscript.OP_PUSHDATA2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...),
			useFull:  true,
		},

		// 5-byte data push via OP_PUSHDATA_4.
		{
			name:     "push data len 65536 (non-canonical)",
			data:     bytes.Repeat([]byte{0x49}, 65536),
			expected: append([]byte{txscript.OP_PUSHDATA4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...),
			useFull:  true,
		},
	}

	builder := txscript.NewScriptBuilder()
	t.Logf("Running %d tests", len(tests))
	for i, test := range tests {
		if !test.useFull {
			builder.Reset().AddData(test.data)
		} else {
			builder.Reset().AddFullData(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
		}
	}
}
Beispiel #19
0
// TestCheckPkScriptStandard tests the checkPkScriptStandard API.
func TestCheckPkScriptStandard(t *testing.T) {
	var pubKeys [][]byte
	for i := 0; i < 4; i++ {
		pk, err := btcec.NewPrivateKey(btcec.S256())
		if err != nil {
			t.Fatalf("TestCheckPkScriptStandard NewPrivateKey failed: %v",
				err)
			return
		}
		pubKeys = append(pubKeys, pk.PubKey().SerializeCompressed())
	}

	tests := []struct {
		name       string // test description.
		script     *txscript.ScriptBuilder
		isStandard bool
	}{
		{
			"key1 and key2",
			txscript.NewScriptBuilder().AddOp(txscript.OP_2).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
			true,
		},
		{
			"key1 or key2",
			txscript.NewScriptBuilder().AddOp(txscript.OP_1).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
			true,
		},
		{
			"escrow",
			txscript.NewScriptBuilder().AddOp(txscript.OP_2).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddData(pubKeys[2]).
				AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG),
			true,
		},
		{
			"one of four",
			txscript.NewScriptBuilder().AddOp(txscript.OP_1).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddData(pubKeys[2]).AddData(pubKeys[3]).
				AddOp(txscript.OP_4).AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed1",
			txscript.NewScriptBuilder().AddOp(txscript.OP_3).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed2",
			txscript.NewScriptBuilder().AddOp(txscript.OP_2).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_3).AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed3",
			txscript.NewScriptBuilder().AddOp(txscript.OP_0).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_2).AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed4",
			txscript.NewScriptBuilder().AddOp(txscript.OP_1).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_0).AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed5",
			txscript.NewScriptBuilder().AddOp(txscript.OP_1).
				AddData(pubKeys[0]).AddData(pubKeys[1]).
				AddOp(txscript.OP_CHECKMULTISIG),
			false,
		},
		{
			"malformed6",
			txscript.NewScriptBuilder().AddOp(txscript.OP_1).
				AddData(pubKeys[0]).AddData(pubKeys[1]),
			false,
		},
	}

	for _, test := range tests {
		script, err := test.script.Script()
		if err != nil {
			t.Fatalf("TestCheckPkScriptStandard test '%s' "+
				"failed: %v", test.name, err)
			continue
		}
		scriptClass := txscript.GetScriptClass(script)
		got := checkPkScriptStandard(script, scriptClass)
		if (test.isStandard && got != nil) ||
			(!test.isStandard && got == nil) {

			t.Fatalf("TestCheckPkScriptStandard test '%s' failed",
				test.name)
			return
		}
	}
}