txscript: Remove unneeded param from NewScript.

This commit removes the unnecessary sigScript parameter from the
txscript.NewScript function.  This has bothered me for a while because it
can and really should be obtained from the provided transaction and input
index.  The way it was, the passed script could technically be different
than what is in the transaction.  Obviously that would be an improper use
of the API, but it's safer and more convenient to simply pull it from the
provided transaction and index.

Also, since the function signature is changing anyways, make the input
index parameter come after the transaction which it references.
This commit is contained in:
Dave Collins 2015-04-20 00:32:43 -05:00
parent de12e101e1
commit bec90e253c
6 changed files with 37 additions and 41 deletions

View file

@ -83,8 +83,8 @@ out:
// Create a new script engine for the script pair. // Create a new script engine for the script pair.
sigScript := txIn.SignatureScript sigScript := txIn.SignatureScript
pkScript := originMsgTx.TxOut[originTxIndex].PkScript pkScript := originMsgTx.TxOut[originTxIndex].PkScript
engine, err := txscript.NewScript(sigScript, pkScript, engine, err := txscript.NewScript(pkScript,
txVI.txInIndex, txVI.tx.MsgTx(), v.flags) txVI.tx.MsgTx(), txVI.txInIndex, v.flags)
if err != nil { if err != nil {
str := fmt.Sprintf("failed to parse input "+ str := fmt.Sprintf("failed to parse input "+
"%s:%d which references output %s:%d - "+ "%s:%d which references output %s:%d - "+

View file

@ -164,8 +164,8 @@ func ExampleSignTxOutput() {
flags := txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures | flags := txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures |
txscript.ScriptStrictMultiSig | txscript.ScriptStrictMultiSig |
txscript.ScriptDiscourageUpgradableNops txscript.ScriptDiscourageUpgradableNops
s, err := txscript.NewScript(redeemTx.TxIn[0].SignatureScript, s, err := txscript.NewScript(originTx.TxOut[0].PkScript, redeemTx, 0,
originTx.TxOut[0].PkScript, 0, redeemTx, flags) flags)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View file

@ -4202,7 +4202,7 @@ func TestBitcoindInvalidTests(t *testing.T) {
continue continue
} }
tx := createSpendingTx(scriptSig, scriptPubKey) tx := createSpendingTx(scriptSig, scriptPubKey)
s, err := NewScript(scriptSig, scriptPubKey, 0, tx, flags) s, err := NewScript(scriptPubKey, tx, 0, flags)
if err == nil { if err == nil {
if err := s.Execute(); err == nil { if err := s.Execute(); err == nil {
t.Errorf("%s test succeeded when it "+ t.Errorf("%s test succeeded when it "+
@ -4254,7 +4254,7 @@ func TestBitcoindValidTests(t *testing.T) {
continue continue
} }
tx := createSpendingTx(scriptSig, scriptPubKey) tx := createSpendingTx(scriptSig, scriptPubKey)
s, err := NewScript(scriptSig, scriptPubKey, 0, tx, flags) s, err := NewScript(scriptPubKey, tx, 0, flags)
if err != nil { if err != nil {
t.Errorf("%s failed to create script: %v", name, err) t.Errorf("%s failed to create script: %v", name, err)
continue continue
@ -4391,8 +4391,7 @@ testloop:
k, i, test) k, i, test)
continue testloop continue testloop
} }
s, err := NewScript(txin.SignatureScript, pkScript, k, s, err := NewScript(pkScript, tx.MsgTx(), k, flags)
tx.MsgTx(), flags)
if err != nil { if err != nil {
t.Errorf("test (%d:%v:%d) failed to create "+ t.Errorf("test (%d:%v:%d) failed to create "+
"script: %v", i, test, k, err) "script: %v", i, test, k, err)
@ -4537,8 +4536,7 @@ testloop:
// These are meant to fail, so as soon as the first // These are meant to fail, so as soon as the first
// input fails the transaction has failed. (some of the // input fails the transaction has failed. (some of the
// test txns have good inputs, too.. // test txns have good inputs, too..
s, err := NewScript(txin.SignatureScript, pkScript, k, s, err := NewScript(pkScript, tx.MsgTx(), k, flags)
tx.MsgTx(), flags)
if err != nil { if err != nil {
continue testloop continue testloop
} }

View file

@ -507,9 +507,7 @@ func TestScripts(t *testing.T) {
flags = txscript.ScriptVerifyDERSignatures flags = txscript.ScriptVerifyDERSignatures
} }
mockTx.TxOut[0].PkScript = test.script mockTx.TxOut[0].PkScript = test.script
sigScript := mockTx.TxIn[0].SignatureScript engine, err := txscript.NewScript(test.script, mockTx, 0, flags)
engine, err := txscript.NewScript(sigScript, test.script, 0,
mockTx, flags)
if err == nil { if err == nil {
err = engine.Execute() err = engine.Execute()
} }
@ -4285,8 +4283,7 @@ func testOpcode(t *testing.T, test *detailedTest) {
tx.TxOut[0].PkScript = test.script tx.TxOut[0].PkScript = test.script
engine, err := txscript.NewScript(tx.TxIn[0].SignatureScript, engine, err := txscript.NewScript(tx.TxOut[0].PkScript, tx, 0, 0)
tx.TxOut[0].PkScript, 0, tx, 0)
if err != nil { if err != nil {
if err != test.expectedReturn { if err != test.expectedReturn {
t.Errorf("Error return not expected %s: %v %v", t.Errorf("Error return not expected %s: %v %v",

View file

@ -146,6 +146,10 @@ var (
// ErrInvalidFlags is returned when the passed flags to NewScript contain // ErrInvalidFlags is returned when the passed flags to NewScript contain
// an invalid combination. // an invalid combination.
ErrInvalidFlags = errors.New("invalid flags combination") ErrInvalidFlags = errors.New("invalid flags combination")
// ErrInvalidIndex is returned when the passed input index for the
// provided transaction is out of range.
ErrInvalidIndex = errors.New("invalid input index")
) )
const ( const (
@ -699,10 +703,15 @@ const (
) )
// NewScript returns a new script engine for the provided tx and input idx with // NewScript returns a new script engine for the provided tx and input idx with
// a signature script scriptSig and a pubkeyscript scriptPubKey. If bip16 is // with a pubkeyscript scriptPubKey. If bip16 is true then it will be treated as
// true then it will be treated as if the bip16 threshhold has passed and thus // if the bip16 threshhold has passed and thus pay-to-script hash transactions
// pay-to-script hash transactions will be fully validated. // will be fully validated.
func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *wire.MsgTx, flags ScriptFlags) (*Script, error) { func NewScript(scriptPubKey []byte, tx *wire.MsgTx, txidx int, flags ScriptFlags) (*Script, error) {
if txidx < 0 || txidx >= len(tx.TxIn) {
return nil, ErrInvalidIndex
}
scriptSig := tx.TxIn[txidx].SignatureScript
var m Script var m Script
if flags&ScriptVerifySigPushOnly == ScriptVerifySigPushOnly && !IsPushOnlyScript(scriptSig) { if flags&ScriptVerifySigPushOnly == ScriptVerifySigPushOnly && !IsPushOnlyScript(scriptSig) {
return nil, ErrStackNonPushOnly return nil, ErrStackNonPushOnly

View file

@ -1636,9 +1636,8 @@ func testTx(t *testing.T, test txTest) {
if test.strictSigs { if test.strictSigs {
flags |= txscript.ScriptVerifyDERSignatures flags |= txscript.ScriptVerifyDERSignatures
} }
engine, err := txscript.NewScript( engine, err := txscript.NewScript(test.pkScript, test.tx, test.idx,
test.tx.TxIn[test.idx].SignatureScript, test.pkScript, flags)
test.idx, test.tx, flags)
if err != nil { if err != nil {
if err != test.parseErr { if err != test.parseErr {
t.Errorf("Failed to parse %s: got \"%v\" expected "+ t.Errorf("Failed to parse %s: got \"%v\" expected "+
@ -2470,8 +2469,7 @@ func TestBadPC(t *testing.T) {
pkScript := []byte{txscript.OP_NOP} pkScript := []byte{txscript.OP_NOP}
for _, test := range pcTests { for _, test := range pcTests {
engine, err := txscript.NewScript(tx.TxIn[0].SignatureScript, engine, err := txscript.NewScript(pkScript, tx, 0, 0)
pkScript, 0, tx, 0)
if err != nil { if err != nil {
t.Errorf("Failed to create script: %v", err) t.Errorf("Failed to create script: %v", err)
} }
@ -2542,8 +2540,7 @@ func TestCheckErrorCondition(t *testing.T) {
txscript.OP_TRUE, txscript.OP_TRUE,
} }
engine, err := txscript.NewScript(tx.TxIn[0].SignatureScript, pkScript, engine, err := txscript.NewScript(pkScript, tx, 0, 0)
0, tx, 0)
if err != nil { if err != nil {
t.Errorf("failed to create script: %v", err) t.Errorf("failed to create script: %v", err)
} }
@ -2892,10 +2889,9 @@ nexttest:
// Validate tx input scripts // Validate tx input scripts
scriptFlags := txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures scriptFlags := txscript.ScriptBip16 | txscript.ScriptVerifyDERSignatures
for j, txin := range tx.TxIn { for j := range tx.TxIn {
engine, err := txscript.NewScript(txin.SignatureScript, engine, err := txscript.NewScript(SigScriptTests[i].
SigScriptTests[i].inputs[j].txout.PkScript, inputs[j].txout.PkScript, tx, j, scriptFlags)
j, tx, scriptFlags)
if err != nil { if err != nil {
t.Errorf("cannot create script vm for test %v: %v", t.Errorf("cannot create script vm for test %v: %v",
SigScriptTests[i].name, err) SigScriptTests[i].name, err)
@ -3307,9 +3303,8 @@ func signAndCheck(msg string, tx *wire.MsgTx, idx int, pkScript []byte,
hashType txscript.SigHashType, kdb txscript.KeyDB, sdb txscript.ScriptDB, hashType txscript.SigHashType, kdb txscript.KeyDB, sdb txscript.ScriptDB,
previousScript []byte) error { previousScript []byte) error {
sigScript, err := txscript.SignTxOutput( sigScript, err := txscript.SignTxOutput(&chaincfg.TestNet3Params, tx,
&chaincfg.TestNet3Params, tx, idx, pkScript, hashType, idx, pkScript, hashType, kdb, sdb, []byte{})
kdb, sdb, []byte{})
if err != nil { if err != nil {
return fmt.Errorf("failed to sign output %s: %v", msg, err) return fmt.Errorf("failed to sign output %s: %v", msg, err)
} }
@ -3317,11 +3312,10 @@ func signAndCheck(msg string, tx *wire.MsgTx, idx int, pkScript []byte,
return checkScripts(msg, tx, idx, sigScript, pkScript) return checkScripts(msg, tx, idx, sigScript, pkScript)
} }
func checkScripts(msg string, tx *wire.MsgTx, idx int, func checkScripts(msg string, tx *wire.MsgTx, idx int, sigScript, pkScript []byte) error {
sigScript, pkScript []byte) error { tx.TxIn[idx].SignatureScript = sigScript
engine, err := txscript.NewScript(sigScript, pkScript, idx, tx, engine, err := txscript.NewScript(pkScript, tx, idx,
txscript.ScriptBip16| txscript.ScriptBip16|txscript.ScriptVerifyDERSignatures)
txscript.ScriptVerifyDERSignatures)
if err != nil { if err != nil {
return fmt.Errorf("failed to make script engine for %s: %v", return fmt.Errorf("failed to make script engine for %s: %v",
msg, err) msg, err)
@ -4857,9 +4851,7 @@ func TestInvalidFlagCombinations(t *testing.T) {
pkScript := []byte{txscript.OP_NOP} pkScript := []byte{txscript.OP_NOP}
for i, test := range tests { for i, test := range tests {
_, err := txscript.NewScript(tx.TxIn[0].SignatureScript, _, err := txscript.NewScript(pkScript, tx, 0, test)
pkScript, 0, tx, test)
if err != txscript.ErrInvalidFlags { if err != txscript.ErrInvalidFlags {
t.Fatalf("TestInvalidFlagCombinations #%d unexpected "+ t.Fatalf("TestInvalidFlagCombinations #%d unexpected "+
"error: %v", i, err) "error: %v", i, err)