From b70e15f90d738d24ebdd1bdd7f5bd5dbf65a5fa9 Mon Sep 17 00:00:00 2001 From: Greg Nazario Date: Thu, 7 Nov 2024 15:28:59 -0500 Subject: [PATCH] [bcs] Add deserialization of RawTransactionWithData --- CHANGELOG.md | 1 + aptos_sdk/transactions.py | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37341ea..41e78ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to the Aptos Python SDK will be captured in this file. This ## Unreleased +- Added support for deserialize RawTransactionWithData - Added support for AIP-80 compliance for Ed25519 and Secp256k1 private keys. - Added helper functions for AIP-80 including `PrivateKey.format_private_key` and `PrivateKey.parse_hex_input` diff --git a/aptos_sdk/transactions.py b/aptos_sdk/transactions.py index 0d56689..952546e 100644 --- a/aptos_sdk/transactions.py +++ b/aptos_sdk/transactions.py @@ -77,6 +77,16 @@ def prehash(self) -> bytes: hasher.update(b"APTOS::RawTransactionWithData") return hasher.digest() + @staticmethod + def deserialize(deserializer: Deserializer) -> RawTransactionWithData: + enum_type = deserializer.u8() + if enum_type == 0: + return MultiAgentRawTransaction.deserialize_inner(deserializer) + elif enum_type == 1: + return FeePayerRawTransaction.deserialize_inner(deserializer) + else: + raise Exception("Unhandled RawTransaction enum type") + class RawTransaction(Deserializable, RawTransactionInternal, Serializable): # Sender's address @@ -179,6 +189,22 @@ def serialize(self, serializer: Serializer) -> None: serializer.struct(self.raw_transaction) serializer.sequence(self.secondary_signers, Serializer.struct) + @staticmethod + def deserialize(deserializer: Deserializer) -> MultiAgentRawTransaction: + raw_txn_type = deserializer.u8() + if raw_txn_type != 0: + raise Exception(f"Enum type mismatch, expected 0 got {raw_txn_type}") + + return MultiAgentRawTransaction.deserialize_inner(deserializer) + + @staticmethod + def deserialize_inner(deserializer: Deserializer) -> MultiAgentRawTransaction: + raw_txn = RawTransaction.deserialize(deserializer) + secondary_signers = deserializer.sequence(AccountAddress.deserialize) + + return MultiAgentRawTransaction( + raw_txn, secondary_signers + ) class FeePayerRawTransaction(RawTransactionWithData): secondary_signers: List[AccountAddress] @@ -203,6 +229,28 @@ def serialize(self, serializer: Serializer) -> None: ) serializer.struct(fee_payer) + @staticmethod + def deserialize(deserializer: Deserializer) -> FeePayerRawTransaction: + raw_txn_type = deserializer.u8() + if raw_txn_type != 1: + raise Exception(f"Enum type mismatch, expected 1 got {raw_txn_type}") + + return FeePayerRawTransaction.deserialize_inner(deserializer) + + @staticmethod + def deserialize_inner(deserializer: Deserializer) -> FeePayerRawTransaction: + raw_txn = RawTransaction.deserialize(deserializer) + secondary_signers = deserializer.sequence(AccountAddress.deserialize) + fee_payer = deserializer.sequence(AccountAddress.deserialize) + if len(fee_payer) == 0: + fee_payer = None + else: + fee_payer = fee_payer[0] + + return FeePayerRawTransaction( + raw_txn, secondary_signers, fee_payer + ) + class TransactionPayload: SCRIPT: int = 0