Skip to content

Commit

Permalink
Move python tests from counterparty-rs to counterparty-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 13, 2025
1 parent 7fe6b46 commit 17439b5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

import bitcoin as bitcoinlib
import pytest # noqa: F401
from bitcoin import bech32 as bech32lib
from bitcoin.core.script import CScript
from counterparty_rs import utils
from counterparty_rs import b58, utils

from counterpartycore.lib import config
from counterpartycore.lib.opcodes import * # noqa: F403
Expand Down Expand Up @@ -186,3 +187,105 @@ def decode_p2w(script_pubkey):
print(f"{iterations} decode_p2w with rust: {rust_duration}s")

assert python_duration > rust_duration


def test_b58():
assert b58.b58_encode(b"hello world") == "3vQB7B6MrGQZaxCuFg4oh"
assert bytes(b58.b58_decode("3vQB7B6MrGQZaxCuFg4oh")) == b"hello world"

with pytest.raises(ValueError) as excinfo:
b58.b58_decode("hello world")
assert str(excinfo.value) == "Bad input"


def decode_p2w(script_pubkey):
try:
bech32 = bech32lib.CBech32Data.from_bytes(0, script_pubkey[2:22])
return str(bech32), None
except TypeError as e: # noqa: F841
raise Exception("bech32 decoding error") # noqa: B904


def inverse_hash_py(hashstring):
return "".join([hashstring[i : i + 2][::-1] for i in range(0, len(hashstring), 2)])[::-1]


def test_inverse_hash():
h = "b5276739a3e0f32147bd4a921f936c6013dee4a5ca426ee2de868810b068ec0d"
assert (
utils.inverse_hash(h) == "0dec68b0108886dee26e42caa5e4de13606c931f924abd4721f3e0a3396727b5"
)

# iterations = 1000000
iterations = 10

start_time = time.time()
for i in range(iterations): # noqa: B007
inverse_hash_py(h)
python_duration = time.time() - start_time
print(f"{iterations} inverse hash with python: {python_duration}s")

start_time = time.time()
for i in range(iterations): # noqa: B007
utils.inverse_hash(h)
rust_duration = time.time() - start_time
print(f"{iterations} inverse hash with rust: {rust_duration}s")

assert python_duration > rust_duration


def test_script_to_asm():
asm = utils.script_to_asm(b"v\xa9\x14H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607\x88\xac")
assert asm == [
b"v",
b"\xa9",
b"H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607",
b"\x88",
b"\xac",
]
""" [
'OP_DUP',
'OP_HASH160',
b'H8\xd8\xb3X\x8cL{\xa7\xc1\xd0o\x86n\x9b79\xc607',
'OP_EQUALVERIFY',
'OP_CHECKSIG'
] """

asm = utils.script_to_asm(
b"Q!\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5!\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17!\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9wS\xae"
)
assert asm == [
b"\x01",
b"\x03\\\xa5\x1e\xa1u\xf1\x08\xa1\xc65\x88h=\xc4\xc4:qF\xc4g\x99\xf8d\xa3\x00&<\x08\x13\xf5\xfe5",
b"\x020\x9a\x14\xa1\xa3\x02\x02\xf2\xe7oF\xac\xdb)\x17u#q\xcaB\xb9t`\xf7\x92\x8a\xde\x8e\xcb\x02\xea\x17",
b"\x03\x19\xf6\xe0{\x0b\x8duaV9K\x9d\xcf;\x01\x1f\xe9\xac\x19\xf2p\x0b\xd6\xb6\x9aj\x17\x83\xdb\xb8\xb9w",
b"\x03",
b"\xae", # OP_CHECKMULTISIG
]


def test_decode_p2w():
script_pubkey = b"\x00\x14u\x1ev\xe8\x19\x91\x96\xd4T\x94\x1cE\xd1\xb3\xa3#\xf1C;\xd6"
bech32 = utils.script_to_address(script_pubkey, "testnet")
assert bech32 == "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx"

bech32 = utils.script_to_address(script_pubkey, "mainnet")
assert bech32 == "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"

bitcoinlib.SelectParams("mainnet")

assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet")

script_pubkey = binascii.unhexlify(
"0020dcbc2340bd1f6cc3ab0a3887020647ec471a279e3c889fb4414df30e3dd59f96"
)
assert decode_p2w(script_pubkey) == ("bc1qmj7zxs9arakv82c28zrsypj8a3r35fu7pure55", None)
assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet")

script_pubkey = binascii.unhexlify(
"0020dfe1739dc0711f64ced999a2306691ff98fff038b2f40aec2b5ae917610ea0ac"
)
assert decode_p2w(script_pubkey) == ("bc1qmlsh88wqwy0kfnkenx3rqe53l7v0lupc6q5xx6", None)
assert decode_p2w(script_pubkey)[0] == utils.script_to_address(script_pubkey, "mainnet")

bitcoinlib.SelectParams("testnet")
12 changes: 0 additions & 12 deletions counterparty-rs/tests/test_b58.py

This file was deleted.

95 changes: 0 additions & 95 deletions counterparty-rs/tests/test_utils.py

This file was deleted.

0 comments on commit 17439b5

Please sign in to comment.