Skip to content

Commit

Permalink
more tests; more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 31, 2024
1 parent 15ff5ac commit 405a506
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 22 deletions.
1 change: 0 additions & 1 deletion counterparty-core/counterpartycore/lib/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def rpc(method, params):

# no retry for requests from the API
def safe_rpc(method, params):
print("safe_rpc")
start_time = time.time()
try:
payload = {
Expand Down
23 changes: 13 additions & 10 deletions counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,18 @@ def create_tx_output(value, address_or_script, unspent_list, construct_params):
# if hex string we assume it is a script
if all(c in string.hexdigits for c in address_or_script):
has_segwit = script.is_segwit_output(address_or_script)
script = Script.from_raw(address_or_script, has_segwit=has_segwit)
# check if it is a valid script
output_script = Script.from_raw(address_or_script, has_segwit=has_segwit)
else:
script = address_to_script_pub_key(address_or_script, unspent_list, construct_params)
output_script = address_to_script_pub_key(
address_or_script, unspent_list, construct_params
)
except Exception as e:
raise exceptions.ComposeError(
f"Invalid script or address for output: {address_or_script}"
f"Invalid script or address for output: {address_or_script} (error: {e})"
) from e

return TxOutput(value, script)
return TxOutput(value, output_script)


def regular_dust_size(construct_params):
Expand Down Expand Up @@ -112,7 +115,7 @@ def determine_encoding(data, construct_params):
else:
encoding = desired_encoding
if encoding not in ("multisig", "opreturn"):
raise exceptions.TransactionError(f"Not supported encoding: {encoding}")
raise exceptions.ComposeError(f"Not supported encoding: {encoding}")
return encoding


Expand All @@ -123,7 +126,7 @@ def encrypt_data(data, arc4_key):

def prepare_opreturn_output(data, arc4_key):
if len(data) + len(config.PREFIX) > config.OP_RETURN_MAX_SIZE:
raise exceptions.TransactionError("One `OP_RETURN` output per transaction")
raise exceptions.ComposeError("One `OP_RETURN` output per transaction")
opreturn_data = config.PREFIX + data
opreturn_data = encrypt_data(opreturn_data, arc4_key)
return [TxOutput(0, Script(["OP_RETURN", b_to_h(opreturn_data)]))]
Expand Down Expand Up @@ -233,21 +236,21 @@ def prepare_data_outputs(source, data, unspent_list, construct_params):
return prepare_multisig_output(source, data, arc4_key, unspent_list, construct_params)
if encoding == "opreturn":
return prepare_opreturn_output(data, arc4_key)
raise exceptions.TransactionError(f"Not supported encoding: {encoding}")
raise exceptions.ComposeError(f"Not supported encoding: {encoding}")


def prepare_more_outputs(more_outputs, unspent_list, construct_params):
output_list = [output.split(":") for output in more_outputs.split(",")]
outputs = []
for output in output_list:
if len(output) != 2:
raise exceptions.ComposeError(f"Invalid output format: {output}")
address_or_script, value = output
raise exceptions.ComposeError(f"Invalid output format: {':'.join(output)}")
value, address_or_script = output
# check value
try:
value = int(value)
except ValueError as e:
raise exceptions.ComposeError(f"Invalid value for output: {output}") from e
raise exceptions.ComposeError(f"Invalid value for output: {':'.join(output)}") from e
# create output
tx_output = create_tx_output(value, address_or_script, unspent_list, construct_params)
outputs.append(tx_output)
Expand Down
9 changes: 3 additions & 6 deletions counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,7 @@ def date_passed(date):
def init_api_access_log(app):
pass

def pubkeyhash_to_pubkey(address, unspent_list, construct_params):
multisig_pubkey = construct_params.get("multisig_pubkey")
if multisig_pubkey:
return multisig_pubkey
def search_pubkey(address, _tx_hashes):
if "_" in address:
return multisig_pubkeyhashes_to_pubkeys(address)
return DEFAULT_PARAMS["pubkey"][address]
Expand Down Expand Up @@ -670,8 +667,8 @@ def determine_encoding(data, construct_params):
)

monkeypatch.setattr(
"counterpartycore.lib.composer.search_pubkey",
pubkeyhash_to_pubkey,
"counterpartycore.lib.backend.search_pubkey",
search_pubkey,
)

monkeypatch.setattr("counterpartycore.lib.database.check_wal_file", check_wal_file)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import binascii

from bitcoinutils.keys import P2pkhAddress, P2wpkhAddress
from bitcoinutils.script import Script, b_to_h
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
Expand Down Expand Up @@ -61,6 +63,85 @@
),
},
],
"create_tx_output": [
{
"comment": "from address",
"in": (666, ADDR[0], [], {}),
"out": TxOutput(666, P2pkhAddress(ADDR[0]).to_script_pub_key()),
},
{
"comment": "from script",
"in": (666, P2pkhAddress(ADDR[0]).to_script_pub_key().to_hex(), [], {}),
"out": TxOutput(666, P2pkhAddress(ADDR[0]).to_script_pub_key()),
},
{
"comment": "from script",
"in": (666, "00aaff", [], {}),
"out": TxOutput(666, Script.from_raw("00aaff")),
},
{
"comment": "from invalid script",
"in": (666, "00aafff", [], {}),
"error": (
exceptions.ComposeError,
"Invalid script or address for output: 00aafff (error: invalid script)",
),
},
{
"comment": "from invalid address",
"in": (666, "toto", [], {}),
"error": (
exceptions.ComposeError,
"Invalid script or address for output: toto (error: Invalid address: toto)",
),
},
],
"regular_dust_size": [
{
"in": ({},),
"out": 546,
},
{
"in": ({"regular_dust_size": 666},),
"out": 666,
},
{
"in": ({"regular_dust_size": None},),
"out": 546,
},
],
"multisig_dust_size": [
{
"in": ({},),
"out": 1000,
},
{
"in": ({"multisig_dust_size": 666},),
"out": 666,
},
{
"in": ({"multisig_dust_size": None},),
"out": 1000,
},
],
"dust_size": [
{
"in": (ADDR[0], {}),
"out": 546,
},
{
"in": (ADDR[0], {"regular_dust_size": 666}),
"out": 666,
},
{
"in": (MULTISIGADDR[0], {}),
"out": 1000,
},
{
"in": (MULTISIGADDR[0], {"multisig_dust_size": 666}),
"out": 666,
},
],
"perpare_non_data_outputs": [
{
"comment": "P2PKH address",
Expand Down Expand Up @@ -101,11 +182,11 @@
},
{
"in": (b"Hello, World!", {"encoding": "p2sh"}),
"error": (exceptions.TransactionError, "Not supported encoding: p2sh"),
"error": (exceptions.ComposeError, "Not supported encoding: p2sh"),
},
{
"in": (b"Hello, World!", {"encoding": "toto"}),
"error": (exceptions.TransactionError, "Not supported encoding: toto"),
"error": (exceptions.ComposeError, "Not supported encoding: toto"),
},
],
"encrypt_data": [
Expand All @@ -130,6 +211,36 @@
],
},
],
"is_valid_pubkey": [
{
"in": (DEFAULT_PARAMS["pubkey"][ADDR[0]],),
"out": True,
},
{
"in": (DEFAULT_PARAMS["pubkey"][ADDR[0]][::-1],),
"out": False,
},
],
"search_pubkey": [
{
"in": (ADDR[0], [], {}),
"out": DEFAULT_PARAMS["pubkey"][ADDR[0]],
},
{
"in": (ADDR[0], [], {"pubkeys": PROVIDED_PUBKEYS}),
"out": DEFAULT_PARAMS["pubkey"][ADDR[0]],
},
],
"make_valid_pubkey": [
{
"in": (binascii.unhexlify("aa" * 31),),
"out": b"\x02\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa|",
},
{
"in": (binascii.unhexlify("bb" * 31),),
"out": b"\x03\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xc4",
},
],
"data_to_pubkey_pairs": [
{
"in": (b"Hello, World!" * 10, ARC4_KEY),
Expand Down Expand Up @@ -195,7 +306,7 @@
[{"txid": ARC4_KEY}],
{"pubkeys": PROVIDED_PUBKEYS, "encoding": "opreturn"},
),
"error": (exceptions.TransactionError, "One `OP_RETURN` output per transaction"),
"error": (exceptions.ComposeError, "One `OP_RETURN` output per transaction"),
},
{
"in": (
Expand Down Expand Up @@ -253,7 +364,63 @@
[],
{"pubkeys": PROVIDED_PUBKEYS, "encoding": "p2sh"},
),
"error": (exceptions.TransactionError, "Not supported encoding: p2sh"),
"error": (exceptions.ComposeError, "Not supported encoding: p2sh"),
},
],
"prepare_more_outputs": [
{
"in": (f"546:{ADDR[0]}", [], {}),
"out": [TxOutput(546, P2pkhAddress(ADDR[0]).to_script_pub_key())],
},
{
"comment": "Multisig address",
"in": (f"546:{MULTISIGADDR[0]}", [], {"pubkeys": PROVIDED_PUBKEYS}),
"out": [
TxOutput(
546,
Script(
[
1,
DEFAULT_PARAMS["pubkey"][ADDR[0]],
DEFAULT_PARAMS["pubkey"][ADDR[1]],
2,
"OP_CHECKMULTISIG",
]
),
)
],
},
{
"in": (f"2024:{ADDR[0]}", [], {}),
"out": [TxOutput(2024, P2pkhAddress(ADDR[0]).to_script_pub_key())],
},
{
"in": ("666:00aaff", [], {}),
"out": [TxOutput(666, Script.from_raw("00aaff"))],
},
{
"in": (
f"546:{ADDR[0]},546:{MULTISIGADDR[0]},2024:{ADDR[0]},666:00aaff",
[],
{"pubkeys": PROVIDED_PUBKEYS},
),
"out": [
TxOutput(546, P2pkhAddress(ADDR[0]).to_script_pub_key()),
TxOutput(
546,
Script(
[
1,
DEFAULT_PARAMS["pubkey"][ADDR[0]],
DEFAULT_PARAMS["pubkey"][ADDR[1]],
2,
"OP_CHECKMULTISIG",
]
),
),
TxOutput(2024, P2pkhAddress(ADDR[0]).to_script_pub_key()),
TxOutput(666, Script.from_raw("00aaff")),
],
},
],
"prepare_outputs": [
Expand Down Expand Up @@ -336,7 +503,11 @@
[(ADDR[0], 9999)],
b"Hello, World!" * 10,
[{"txid": ARC4_KEY}],
{"pubkeys": PROVIDED_PUBKEYS, "encoding": "multisig"},
{
"pubkeys": PROVIDED_PUBKEYS,
"encoding": "multisig",
"more_outputs": f"546:{ADDR[0]}",
},
),
"out": [
TxOutput(9999, P2pkhAddress(ADDR[0]).to_script_pub_key()),
Expand Down Expand Up @@ -379,6 +550,7 @@
]
),
),
TxOutput(546, P2pkhAddress(ADDR[0]).to_script_pub_key()),
],
},
],
Expand Down

0 comments on commit 405a506

Please sign in to comment.