Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for nonstandard short script when parsing PublicKeyHash #125

Merged
merged 1 commit into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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