Skip to content

Commit

Permalink
Python: add multi address (#1658)
Browse files Browse the repository at this point in the history
* add weighted address and multi address

* fmt
  • Loading branch information
Alex6323 authored Nov 21, 2023
1 parent 4270b7c commit b23d0e2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions bindings/python/iota_sdk/types/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -24,6 +25,7 @@ class AddressType(IntEnum):
NFT = 16
ANCHOR = 24
IMPLICIT_ACCOUNT_CREATION = 32
MULTI = 40
RESTRICTED = 48


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -149,6 +179,7 @@ class AddressWithUnspentOutputs():
NFTAddress,
AnchorAddress,
ImplicitAccountCreationAddress,
MultiAddress,
RestrictedAddress]


Expand All @@ -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}')
Expand Down

0 comments on commit b23d0e2

Please sign in to comment.