Skip to content

Commit

Permalink
wally_scriptpubkey_get_type: only check 1st byte for OP_RETURN scripts
Browse files Browse the repository at this point in the history
Even though a script might not be created with
wally_scriptpubkey_op_return_from_bytes (e.g. <OP_RETURN>) or it
is not standard (e.g. <OP_RETURN OP_RETURN>), if it's starting with
OP_RETURN, it should still be considered a OP_RETURN script.
  • Loading branch information
LeoComandini authored and jgriffiths committed Mar 27, 2024
1 parent ec348c8 commit 1c6888e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## Unreleased

### Fixed
- wally_scriptpubkey_get_type: mark all scripts starting with OP_RETURN as
WALLY_SCRIPT_TYPE_OP_RETURN.

## Version 1.2.0

### Added
Expand Down
7 changes: 1 addition & 6 deletions src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ size_t confidential_value_to_bytes(const unsigned char *bytes, size_t bytes_len,

static bool scriptpubkey_is_op_return(const unsigned char *bytes, size_t bytes_len)
{
size_t n_op, n_push;

return bytes_len && bytes[0] == OP_RETURN &&
get_push_size(bytes + 1, bytes_len - 1, true, &n_op) == WALLY_OK &&
get_push_size(bytes + 1, bytes_len - 1, false, &n_push) == WALLY_OK &&
bytes_len == 1 + n_op + n_push;
return bytes_len && bytes[0] == OP_RETURN;
}

static bool scriptpubkey_is_p2pkh(const unsigned char *bytes, size_t bytes_len)
Expand Down
10 changes: 10 additions & 0 deletions src/test/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def test_scriptpubkey_op_return_from_bytes(self):
ret = wally_scriptpubkey_get_type(out, ret[1])
self.assertEqual(ret, (WALLY_OK, SCRIPT_TYPE_OP_RETURN))

# Some scripts are considered op_return but they cannot be created with
# wally_scriptpubkey_op_return_from_bytes and they might not be standard
for script_hex in [
'6a',
'6a6a',
]:
script, script_len = make_cbuffer(script_hex)
ret, typ = wally_scriptpubkey_get_type(script, script_len)
self.assertEqual((ret, typ), (WALLY_OK, SCRIPT_TYPE_OP_RETURN))

def test_scriptpubkey_p2pkh_from_bytes(self):
"""Tests for creating p2pkh scriptPubKeys"""
# Invalid args
Expand Down

0 comments on commit 1c6888e

Please sign in to comment.