|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +from hypothesis import strategies as st |
| 4 | +from hypothesis.strategies import SearchStrategy |
| 5 | + |
| 6 | +from hiero_sdk_python import AccountId, HbarUnit, PrivateKey, TransactionId, TransferTransaction |
| 7 | + |
| 8 | +from tests.fuzz.support.classes import HbarConstructorCase, HbarStringCase |
| 9 | + |
| 10 | + |
| 11 | +def sized_hex(byte_length: int) -> SearchStrategy[str]: |
| 12 | + """Return a fixed-length hex string strategy.""" |
| 13 | + return st.binary(min_size=byte_length, max_size=byte_length).map(bytes.hex) |
| 14 | + |
| 15 | + |
| 16 | +def with_optional_0x(hex_strategy: SearchStrategy[str]) -> SearchStrategy[str]: |
| 17 | + """Allow a hex string with or without the `0x` prefix.""" |
| 18 | + return st.one_of(hex_strategy, hex_strategy.map(lambda value: f"0x{value}")) |
| 19 | + |
| 20 | + |
| 21 | +def decimal_string(value: Decimal) -> str: |
| 22 | + """Format a Decimal without trailing zeros.""" |
| 23 | + text = format(value, "f") |
| 24 | + if "." in text: |
| 25 | + text = text.rstrip("0").rstrip(".") |
| 26 | + return text or "0" |
| 27 | + |
| 28 | + |
| 29 | +def hbar_string_case(unit: HbarUnit, tinybars: int) -> HbarStringCase: |
| 30 | + """Build a valid Hbar string case from an exact tinybar amount.""" |
| 31 | + amount = Decimal(tinybars) / Decimal(unit.tinybar) |
| 32 | + if unit == HbarUnit.HBAR: |
| 33 | + return HbarStringCase(text=decimal_string(amount), tinybars=tinybars) |
| 34 | + return HbarStringCase(text=f"{decimal_string(amount)} {unit.symbol}", tinybars=tinybars) |
| 35 | + |
| 36 | + |
| 37 | +def hbar_constructor_case(unit: HbarUnit, tinybars: int) -> HbarConstructorCase: |
| 38 | + """Build a valid Hbar constructor case from an exact tinybar amount.""" |
| 39 | + if unit == HbarUnit.TINYBAR: |
| 40 | + amount: int | float | Decimal = tinybars |
| 41 | + else: |
| 42 | + amount = Decimal(tinybars) / Decimal(unit.tinybar) |
| 43 | + return HbarConstructorCase(amount=amount, unit=unit, tinybars=tinybars) |
| 44 | + |
| 45 | + |
| 46 | +def build_valid_transaction_bytes() -> tuple[bytes, bytes]: |
| 47 | + """Build one valid unsigned and one valid signed transaction payload.""" |
| 48 | + operator_id = AccountId.from_string("0.0.1234") |
| 49 | + node_id = AccountId.from_string("0.0.3") |
| 50 | + receiver_id = AccountId.from_string("0.0.5678") |
| 51 | + |
| 52 | + tx = ( |
| 53 | + TransferTransaction() |
| 54 | + .add_hbar_transfer(operator_id, -100_000_000) |
| 55 | + .add_hbar_transfer(receiver_id, 100_000_000) |
| 56 | + ) |
| 57 | + tx.transaction_id = TransactionId.generate(operator_id) |
| 58 | + tx.node_account_id = node_id |
| 59 | + tx.freeze() |
| 60 | + unsigned_bytes = tx.to_bytes() |
| 61 | + |
| 62 | + signed_tx = TransferTransaction.from_bytes(unsigned_bytes) |
| 63 | + signed_tx.sign(PrivateKey.from_string_ed25519("02" * 32)) |
| 64 | + signed_bytes = signed_tx.to_bytes() |
| 65 | + return unsigned_bytes, signed_bytes |
0 commit comments