Skip to content

Commit

Permalink
Python: Add restricted and implicit address types (#1381)
Browse files Browse the repository at this point in the history
* Add new address types to python

* import-ant

* rework

* docstring

* custom serialization for implicit account address

* unused import

* fix to_dict overrides

* doccccc

* docs

* remove implicit from restricted

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
DaughterOfMars and thibault-martinez authored Oct 23, 2023
1 parent 2682368 commit 3dc6d56
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/types/block/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class NftAddress extends Address {
}

/**
* An implicit account creation address.
* An implicit account creation address that can be used to convert a Basic Output to an Account Output.
*/
class ImplicitAccountCreationAddress extends Address {
private pubKeyHash: HexEncodedString;
Expand Down
61 changes: 60 additions & 1 deletion bindings/python/iota_sdk/types/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ class AddressType(IntEnum):
ED25519 (0): Ed25519 address.
ACCOUNT (8): Account address.
NFT (16): Nft address.
IMPLICIT_ACCOUNT_CREATION (24): Implicit Account Creation address.
RESTRICTED (40): Address with restricted capabilities.
"""
ED25519 = 0
ACCOUNT = 8
NFT = 16
IMPLICIT_ACCOUNT_CREATION = 24
RESTRICTED = 40


@json
Expand Down Expand Up @@ -59,6 +63,56 @@ class NFTAddress:
type: int = field(default_factory=lambda: int(AddressType.NFT), init=False)


@json
@dataclass
class ImplicitAccountCreationAddress:
"""An implicit account creation address that can be used to convert a Basic Output to an Account Output.
Attributes:
address: The hex encoded Ed25519 Address.
"""
address: Ed25519Address
type: int = field(default_factory=lambda: int(
AddressType.IMPLICIT_ACCOUNT_CREATION), init=False)

@staticmethod
def _to_dict_custom(addr_dict: dict) -> dict:
"""
Converts an implicit account creation address to the dictionary representation.
"""
if 'address' in addr_dict:
address = addr_dict.pop('address')
addr_dict['pubKeyHash'] = address.pop('pubKeyHash')
return addr_dict

@staticmethod
def from_dict(addr_dict: dict):
"""
Creates an implicit account creation address from a dictionary representation.
"""
return ImplicitAccountCreationAddress(Ed25519Address(addr_dict['pubKeyHash']))


@json
@dataclass
class RestrictedAddress:
"""Represents an address with restricted capabilities.
Attributes:
address: The inner restricted Address.
allowed_capabilities: The allowed capabilities bitflags.
"""
address: Union[Ed25519Address, AccountAddress, NFTAddress]
allowed_capabilities: HexStr = field(default='0x00', init=False)
type: int = field(default_factory=lambda: int(
AddressType.RESTRICTED), init=False)

def with_allowed_capabilities(self, allowed_capabilities: bytes):
"""Sets the allowed capabilities from a byte array.
Attributes:
allowed_capabilities: The allowed capabilities bitflags.
"""
self.allowed_capabilities = '0x00' + allowed_capabilities.hex()


@json
@dataclass
class AddressWithUnspentOutputs():
Expand All @@ -70,7 +124,8 @@ class AddressWithUnspentOutputs():
output_ids: bool


Address: TypeAlias = Union[Ed25519Address, AccountAddress, NFTAddress]
Address: TypeAlias = Union[Ed25519Address, AccountAddress,
NFTAddress, ImplicitAccountCreationAddress, RestrictedAddress]


def deserialize_address(d: Dict[str, Any]) -> Address:
Expand All @@ -87,6 +142,10 @@ def deserialize_address(d: Dict[str, Any]) -> Address:
return AccountAddress.from_dict(d)
if address_type == AddressType.NFT:
return NFTAddress.from_dict(d)
if address_type == AddressType.IMPLICIT_ACCOUNT_CREATION:
return ImplicitAccountCreationAddress.from_dict(d)
if address_type == AddressType.RESTRICTED:
return RestrictedAddress.from_dict(d)
raise Exception(f'invalid address type: {address_type}')


Expand Down
2 changes: 1 addition & 1 deletion bindings/python/iota_sdk/types/token_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SimpleTokenScheme:
type: int = field(default_factory=lambda: 0, init=False)

@staticmethod
def to_dict_custom(config):
def _to_dict_custom(config):
"""
The function converts integer values in the config to hexadecimal strings.
"""
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/iota_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def parse_bech32_address(address: str) -> Address:
'address': address
})

deserialize_address(response)
return deserialize_address(response)

@staticmethod
def is_address_valid(address: str) -> bool:
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/types/block/address/implicit_account_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use packable::Packable;

use crate::types::block::address::Ed25519Address;

/// An implicit account creation address that can be used to transition an account.
/// An implicit account creation address that can be used to convert a
/// [`BasicOutput`](crate::types::block::output::BasicOutput) to an
/// [`AccountOutput`](crate::types::block::output::AccountOutput).
#[derive(Copy, Clone, Debug, Display, Eq, PartialEq, Ord, PartialOrd, Hash, FromStr, AsRef, Deref, From, Packable)]
#[as_ref(forward)]
pub struct ImplicitAccountCreationAddress(Ed25519Address);
Expand Down

0 comments on commit 3dc6d56

Please sign in to comment.