|
| 1 | +import sys |
| 2 | +import binascii |
| 3 | + |
| 4 | +def parse_multisend_transactions(tx_bytes: bytes): |
| 5 | + i = 0 |
| 6 | + txs = [] |
| 7 | + |
| 8 | + while i < len(tx_bytes): |
| 9 | + if i + 1 + 20 + 32 + 32 > len(tx_bytes): |
| 10 | + raise ValueError(f"Not enough data for header at offset {i}") |
| 11 | + |
| 12 | + operation = tx_bytes[i] |
| 13 | + if operation != 0: |
| 14 | + raise ValueError(f"Invalid operation at offset {i}: expected 0, got {operation}") |
| 15 | + i += 1 |
| 16 | + |
| 17 | + to = tx_bytes[i:i+20] |
| 18 | + i += 20 |
| 19 | + |
| 20 | + value = int.from_bytes(tx_bytes[i:i+32], byteorder='big') |
| 21 | + i += 32 |
| 22 | + |
| 23 | + data_length = int.from_bytes(tx_bytes[i:i+32], byteorder='big') |
| 24 | + i += 32 |
| 25 | + |
| 26 | + if i + data_length > len(tx_bytes): |
| 27 | + raise ValueError(f"Not enough data for transaction data at offset {i}") |
| 28 | + |
| 29 | + data = tx_bytes[i:i+data_length] |
| 30 | + i += data_length |
| 31 | + |
| 32 | + txs.append({ |
| 33 | + 'operation': operation, |
| 34 | + 'to': '0x' + to.hex(), |
| 35 | + 'value': value, |
| 36 | + 'data_length': data_length, |
| 37 | + 'data': '0x' + data.hex() |
| 38 | + }) |
| 39 | + |
| 40 | + return txs |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + if len(sys.argv) != 2: |
| 45 | + print(f"Usage: {sys.argv[0]} <0x-prefixed hex string>") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + hex_input = sys.argv[1] |
| 49 | + if hex_input.startswith("0x"): |
| 50 | + hex_input = hex_input[2:] |
| 51 | + |
| 52 | + try: |
| 53 | + tx_bytes = binascii.unhexlify(hex_input) |
| 54 | + except binascii.Error: |
| 55 | + print("Invalid hex string.") |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + try: |
| 59 | + transactions = parse_multisend_transactions(tx_bytes) |
| 60 | + except Exception as e: |
| 61 | + print(f"Error parsing transactions: {e}") |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + for idx, tx in enumerate(transactions): |
| 65 | + print(f"Transaction {idx + 1}:") |
| 66 | + print(f" operation: {tx['operation']}") |
| 67 | + print(f" to: {tx['to']}") |
| 68 | + print(f" value: {tx['value']}") |
| 69 | + print(f" data_length: {tx['data_length']}") |
| 70 | + print(f" data: {tx['data']}") |
0 commit comments