From 21217b9cb28be6786fc291cbdf01a6f087da29c4 Mon Sep 17 00:00:00 2001 From: /alex/ Date: Tue, 21 Nov 2023 16:27:38 +0100 Subject: [PATCH] Python: add multi address (#1658) * add weighted address and multi address * fmt --- bindings/python/iota_sdk/types/address.py | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bindings/python/iota_sdk/types/address.py b/bindings/python/iota_sdk/types/address.py index 36697010b5..aa55d0fe5f 100644 --- a/bindings/python/iota_sdk/types/address.py +++ b/bindings/python/iota_sdk/types/address.py @@ -16,6 +16,7 @@ class AddressType(IntEnum): NFT (16): Nft address. ANCHOR (24): Anchor address. IMPLICIT_ACCOUNT_CREATION (32): Implicit Account Creation address. + MULTI (40): Multi address. RESTRICTED (48): Address with restricted capabilities. """ @@ -24,6 +25,7 @@ class AddressType(IntEnum): NFT = 16 ANCHOR = 24 IMPLICIT_ACCOUNT_CREATION = 32 + MULTI = 40 RESTRICTED = 48 @@ -109,6 +111,34 @@ def from_dict(addr_dict: dict): Ed25519Address(addr_dict['pubKeyHash'])) +@json +@dataclass +class WeightedAddress: + """An address with an assigned weight. + Attributes: + address: The unlocked address. + weight: The weight of the unlocked address. + """ + address: Union[Ed25519Address, AccountAddress, NFTAddress, AnchorAddress] + weight: int + + +@json +@dataclass +class MultiAddress: + """An address that consists of addresses with weights and a threshold value. + The Multi Address can be unlocked if the cumulative weight of all unlocked addresses is equal to or exceeds the + threshold. + Attributes: + addresses: The weighted unlocked addresses. + threshold: The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address. + """ + addresses: List[WeightedAddress] + threshold: int + type: int = field(default_factory=lambda: int( + AddressType.MULTI), init=False) + + @json @dataclass class RestrictedAddress: @@ -149,6 +179,7 @@ class AddressWithUnspentOutputs(): NFTAddress, AnchorAddress, ImplicitAccountCreationAddress, + MultiAddress, RestrictedAddress] @@ -170,6 +201,8 @@ def deserialize_address(d: Dict[str, Any]) -> Address: return AnchorAddress.from_dict(d) if address_type == AddressType.IMPLICIT_ACCOUNT_CREATION: return ImplicitAccountCreationAddress.from_dict(d) + if address_type == AddressType.MULTI: + return MultiAddress.from_dict(d) if address_type == AddressType.RESTRICTED: return RestrictedAddress.from_dict(d) raise Exception(f'invalid address type: {address_type}')