Skip to content

Commit

Permalink
Test for coverage: previous scriptSig must be valid script
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin authored and afk11 committed Oct 27, 2020
1 parent 56fc2f0 commit d3fc723
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions txscript/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,40 @@ func TestSignTxWitnessErrors(t *testing.T) {
t.Fatalf("Unexpected error %v", err)
}
})
t.Run("test scriptSigs are valid scripts", func(t *testing.T) {
key, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("failed to make privKey: %v", err)
}

pk1 := (*btcec.PublicKey)(&key.PublicKey).
SerializeCompressed()

p2pkhAddr, err := btcutil.NewAddressPubKeyHash(
btcutil.Hash160(pk1), &chaincfg.TestNet3Params)
if err != nil {
t.Fatalf("failed to make p2wpkh address %v", err)
}

pkScript, err := PayToAddrScript(p2pkhAddr)
if err != nil {
t.Fatalf("failed to make p2wpkh script %v", err)
}

txCpy := *tx
prevScript := []byte{0x20}
_, _, err = SignTxWitness(&chaincfg.TestNet3Params,
&txCpy, sigHashes, 0, pkScript, inputAmount, SigHashAll,
mkGetKey(map[string]addressToKey{
p2pkhAddr.EncodeAddress(): {key, true},
}), mkGetScript(map[string][]byte{}), prevScript, nil)
if err == nil {
t.Fatal("Error expected")
}
if err.Error() != "opcode OP_DATA_32 requires 33 bytes, but script only has 1 remaining" {
t.Fatalf("Unexpected error %v", err)
}
})
}

type tstInput struct {
Expand Down

0 comments on commit d3fc723

Please sign in to comment.