Skip to content

Commit

Permalink
[bcs] Add deserialization of RawTransactionWithData
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Nov 7, 2024
1 parent 39e9bf8 commit b70e15f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
48 changes: 48 additions & 0 deletions aptos_sdk/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit b70e15f

Please sign in to comment.