Skip to content

Commit

Permalink
Added check for nonstandard short script when parsing PublicKeyHash (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Jul 22, 2022
1 parent eaaecfe commit 34e82d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bscript/script.go
Expand Up @@ -336,7 +336,7 @@ func (s *Script) PublicKeyHash() ([]byte, error) {
return nil, ErrEmptyScript
}

if (*s)[0] != OpDUP || (*s)[1] != OpHASH160 {
if (*s)[0] != OpDUP || len(*s) <= 2 || (*s)[1] != OpHASH160 {
return nil, ErrNotP2PKH
}

Expand Down
11 changes: 11 additions & 0 deletions bscript/script_test.go
Expand Up @@ -236,6 +236,17 @@ func TestScript_PublicKeyHash(t *testing.T) {
assert.Error(t, err)
assert.EqualError(t, err, "script is empty")
})

t.Run("nonstandard script", func(t *testing.T) {
// example tx 37d4cc9f8a3b62e7f2e7c97c07a3282bfa924739c0e174733ff1b764ef8e3ebc
s, err := bscript.NewFromHexString("76")
assert.NoError(t, err)
assert.NotNil(t, s)

_, err = s.PublicKeyHash()
assert.Error(t, err)
assert.EqualError(t, err, "not a P2PKH")
})
}

func TestErrorIsAppended(t *testing.T) {
Expand Down

0 comments on commit 34e82d8

Please sign in to comment.