diff --git a/bindings/python/iota_sdk/__init__.py b/bindings/python/iota_sdk/__init__.py index f5ee09fd6c..11d3869198 100644 --- a/bindings/python/iota_sdk/__init__.py +++ b/bindings/python/iota_sdk/__init__.py @@ -20,6 +20,7 @@ from .types.block.body.type import * from .types.block.body.validation import * from .types.block.metadata import * +from .types.block.id import * from .types.block_builder_options import * from .types.block_issuer_key import * from .types.burn import * @@ -43,10 +44,12 @@ from .types.output_params import * from .types.payload import * from .types.send_params import * +from .types.slot import * from .types.token_scheme import * -from .types.transaction_with_metadata import * from .types.transaction_data import * +from .types.transaction_id import * from .types.transaction_options import * +from .types.transaction_with_metadata import * from .types.unlock import * from .types.unlock_condition import * from .types.utxo_changes import * diff --git a/bindings/python/iota_sdk/client/_high_level_api.py b/bindings/python/iota_sdk/client/_high_level_api.py index 928d165804..3a0068899b 100644 --- a/bindings/python/iota_sdk/client/_high_level_api.py +++ b/bindings/python/iota_sdk/client/_high_level_api.py @@ -5,7 +5,8 @@ from dataclasses import dataclass from abc import ABCMeta, abstractmethod from iota_sdk.types.block.block import Block -from iota_sdk.types.common import CoinType, HexStr, json +from iota_sdk.types.block.id import BlockId +from iota_sdk.types.common import CoinType, json from iota_sdk.types.output_metadata import OutputWithMetadata from iota_sdk.types.output_id import OutputId @@ -111,7 +112,7 @@ def get_outputs_ignore_errors( }) return [OutputWithMetadata.from_dict(o) for o in outputs] - def find_blocks(self, block_ids: List[HexStr]) -> List[Block]: + def find_blocks(self, block_ids: List[BlockId]) -> List[Block]: """Find all blocks by provided block IDs. Args: diff --git a/bindings/python/iota_sdk/client/_node_core_api.py b/bindings/python/iota_sdk/client/_node_core_api.py index f149e8c961..7181c491a3 100644 --- a/bindings/python/iota_sdk/client/_node_core_api.py +++ b/bindings/python/iota_sdk/client/_node_core_api.py @@ -6,11 +6,13 @@ from dacite import from_dict from iota_sdk.types.block.block import Block +from iota_sdk.types.block.id import BlockId from iota_sdk.types.block.metadata import BlockMetadata, BlockWithMetadata from iota_sdk.types.common import HexStr from iota_sdk.types.node_info import NodeInfo, NodeInfoWrapper from iota_sdk.types.output_metadata import OutputWithMetadata, OutputMetadata from iota_sdk.types.output_id import OutputId +from iota_sdk.types.transaction_id import TransactionId class NodeCoreAPI(metaclass=ABCMeta): @@ -65,12 +67,12 @@ def get_info(self) -> NodeInfoWrapper: """ return from_dict(NodeInfoWrapper, self._call_method('getInfo')) - def get_tips(self) -> List[HexStr]: + def get_tips(self) -> List[BlockId]: """Request tips from the node. """ return self._call_method('getTips') - def post_block(self, block: Block) -> HexStr: + def post_block(self, block: Block) -> BlockId: """Post a block. Args: @@ -79,47 +81,47 @@ def post_block(self, block: Block) -> HexStr: Returns: The block id of the posted block. """ - return self._call_method('postBlock', { + return BlockId(self._call_method('postBlock', { 'block': block - }) + })) - def get_block(self, block_id: HexStr) -> Block: + def get_block(self, block_id: BlockId) -> Block: """Get the block corresponding to the given block id. """ return Block.from_dict(self._call_method('getBlock', { 'blockId': block_id })) - def get_block_metadata(self, block_id: HexStr) -> BlockMetadata: + def get_block_metadata(self, block_id: BlockId) -> BlockMetadata: """Get the block metadata corresponding to the given block id. """ return BlockMetadata.from_dict(self._call_method('getBlockMetadata', { 'blockId': block_id })) - def get_block_with_metadata(self, block_id: HexStr) -> BlockWithMetadata: + def get_block_with_metadata(self, block_id: BlockId) -> BlockWithMetadata: """Get a block with its metadata corresponding to the given block id. """ return BlockWithMetadata.from_dict(self._call_method('getBlockWithMetadata', { 'blockId': block_id })) - def get_block_raw(self, block_id: HexStr) -> List[int]: + def get_block_raw(self, block_id: BlockId) -> List[int]: """Get the raw bytes of the block corresponding to the given block id. """ return self._call_method('getBlockRaw', { 'blockId': block_id }) - def post_block_raw(self, block_bytes: List[int]) -> HexStr: + def post_block_raw(self, block_bytes: List[int]) -> BlockId: """Post a block as raw bytes. Returns: The corresponding block id of the block. """ - return self._call_method('postBlockRaw', { + return BlockId(self._call_method('postBlockRaw', { 'blockBytes': block_bytes - }) + })) def get_output( self, output_id: Union[OutputId, HexStr]) -> OutputWithMetadata: @@ -147,7 +149,7 @@ def get_output_metadata( 'outputId': output_id_str })) - def get_included_block(self, transaction_id: HexStr) -> Block: + def get_included_block(self, transaction_id: TransactionId) -> Block: """Returns the included block of the given transaction. Returns: @@ -158,7 +160,7 @@ def get_included_block(self, transaction_id: HexStr) -> Block: })) def get_included_block_metadata( - self, transaction_id: HexStr) -> BlockMetadata: + self, transaction_id: TransactionId) -> BlockMetadata: """Returns the metadata of the included block of the given transaction. Returns: diff --git a/bindings/python/iota_sdk/client/_utils.py b/bindings/python/iota_sdk/client/_utils.py index e0be939390..b9a956d2a1 100644 --- a/bindings/python/iota_sdk/client/_utils.py +++ b/bindings/python/iota_sdk/client/_utils.py @@ -5,6 +5,7 @@ from abc import ABCMeta, abstractmethod from iota_sdk.types.block.block import Block +from iota_sdk.types.block.id import BlockId from iota_sdk.types.common import HexStr from iota_sdk.types.output import Output @@ -88,9 +89,9 @@ def request_funds_from_faucet(self, url: str, address: str) -> str: } ) - def block_id(self, block: Block) -> HexStr: + def block_id(self, block: Block) -> BlockId: """ Return a block ID (Blake2b256 hash of block bytes) from a block. """ - return self._call_method('blockId', { + return BlockId(self._call_method('blockId', { 'block': block, - }) + })) diff --git a/bindings/python/iota_sdk/types/balance.py b/bindings/python/iota_sdk/types/balance.py index 8745faa35c..1b9a3d4a35 100644 --- a/bindings/python/iota_sdk/types/balance.py +++ b/bindings/python/iota_sdk/types/balance.py @@ -7,6 +7,7 @@ from dataclasses_json import config from iota_sdk.types.decayed_mana import DecayedMana from iota_sdk.types.common import hex_str_decoder, HexStr, json +from iota_sdk.types.output_id import OutputId @json @@ -118,4 +119,4 @@ class Balance: foundries: List[HexStr] nfts: List[HexStr] delegations: List[HexStr] - potentially_locked_outputs: dict[HexStr, bool] + potentially_locked_outputs: dict[OutputId, bool] diff --git a/bindings/python/iota_sdk/types/block/block.py b/bindings/python/iota_sdk/types/block/block.py index 766fcba107..3938565ed0 100644 --- a/bindings/python/iota_sdk/types/block/block.py +++ b/bindings/python/iota_sdk/types/block/block.py @@ -9,9 +9,11 @@ from iota_sdk.types.common import HexStr, json, SlotIndex from iota_sdk.types.node_info import ProtocolParameters from iota_sdk.types.signature import Signature +from iota_sdk.types.slot import SlotCommitmentId from iota_sdk.types.block.body.basic import BasicBlockBody from iota_sdk.types.block.body.validation import ValidationBlockBody from iota_sdk.types.block.body.type import BlockBodyType +from iota_sdk.types.block.id import BlockId @json @@ -34,7 +36,7 @@ class BlockHeader: issuing_time: int = field(metadata=config( encoder=str )) - slot_commitment_id: HexStr + slot_commitment_id: SlotCommitmentId latest_finalized_slot: SlotIndex issuer_id: HexStr @@ -85,7 +87,7 @@ class Block: )) signature: Signature - def id(self, params: ProtocolParameters) -> HexStr: + def id(self, params: ProtocolParameters) -> BlockId: """Returns the block ID as a hexadecimal string. """ return Utils.block_id(self, params) diff --git a/bindings/python/iota_sdk/types/block/body/basic.py b/bindings/python/iota_sdk/types/block/body/basic.py index efea797300..41e19a35c6 100644 --- a/bindings/python/iota_sdk/types/block/body/basic.py +++ b/bindings/python/iota_sdk/types/block/body/basic.py @@ -5,8 +5,9 @@ from dataclasses import dataclass, field from typing import List, Optional from dataclasses_json import config +from iota_sdk.types.block.id import BlockId from iota_sdk.types.block.body.type import BlockBodyType -from iota_sdk.types.common import HexStr, json +from iota_sdk.types.common import json from iota_sdk.types.payload import Payload, deserialize_payload @@ -26,12 +27,12 @@ class BasicBlockBody: type: int = field( default_factory=lambda: int(BlockBodyType.Basic), init=False) - strong_parents: List[HexStr] + strong_parents: List[BlockId] max_burned_mana: int = field(metadata=config( encoder=str )) - weak_parents: Optional[List[HexStr]] = None - shallow_like_parents: Optional[List[HexStr]] = None + weak_parents: Optional[List[BlockId]] = None + shallow_like_parents: Optional[List[BlockId]] = None payload: Optional[Payload] = field(default=None, metadata=config( decoder=deserialize_payload )) diff --git a/bindings/python/iota_sdk/types/block/body/validation.py b/bindings/python/iota_sdk/types/block/body/validation.py index 130eda5ff3..ad1a98c86f 100644 --- a/bindings/python/iota_sdk/types/block/body/validation.py +++ b/bindings/python/iota_sdk/types/block/body/validation.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass, field from typing import List, Optional +from iota_sdk.types.block.id import BlockId from iota_sdk.types.block.body.type import BlockBodyType from iota_sdk.types.common import HexStr, json @@ -25,8 +26,8 @@ class ValidationBlockBody: type: int = field( default_factory=lambda: int(BlockBodyType.Validation), init=False) - strong_parents: List[HexStr] + strong_parents: List[BlockId] highest_supported_version: int protocol_parameters_hash: HexStr - weak_parents: Optional[List[HexStr]] = None - shallow_like_parents: Optional[List[HexStr]] = None + weak_parents: Optional[List[BlockId]] = None + shallow_like_parents: Optional[List[BlockId]] = None diff --git a/bindings/python/iota_sdk/types/block/id.py b/bindings/python/iota_sdk/types/block/id.py new file mode 100644 index 0000000000..0f55d34aae --- /dev/null +++ b/bindings/python/iota_sdk/types/block/id.py @@ -0,0 +1,9 @@ +# Copyright 2024 IOTA Stiftung +# SPDX-License-Identifier: Apache-2.0 + +from iota_sdk.types.common import IdWithSlotIndex + + +class BlockId(IdWithSlotIndex): + """Represents a block ID. + """ diff --git a/bindings/python/iota_sdk/types/block/metadata.py b/bindings/python/iota_sdk/types/block/metadata.py index b511392e62..521f3d23b3 100644 --- a/bindings/python/iota_sdk/types/block/metadata.py +++ b/bindings/python/iota_sdk/types/block/metadata.py @@ -5,8 +5,9 @@ from enum import Enum, IntEnum from dataclasses import dataclass from typing import Optional -from iota_sdk.types.common import HexStr, json +from iota_sdk.types.common import json from iota_sdk.types.block.block import Block +from iota_sdk.types.block.id import BlockId @json @@ -21,7 +22,7 @@ class BlockMetadata: block_failure_reason: The block failure reason. transaction_failure_reason: The transaction failure reason. """ - block_id: HexStr + block_id: BlockId block_state: BlockState transaction_state: Optional[TransactionState] = None block_failure_reason: Optional[BlockFailureReason] = None diff --git a/bindings/python/iota_sdk/types/block_builder_options.py b/bindings/python/iota_sdk/types/block_builder_options.py index fbcb88703c..7899686ab2 100644 --- a/bindings/python/iota_sdk/types/block_builder_options.py +++ b/bindings/python/iota_sdk/types/block_builder_options.py @@ -7,6 +7,7 @@ from dataclasses_json import config from iota_sdk.types.common import HexStr, AddressAndAmount, json from iota_sdk.client._high_level_api import Range +from iota_sdk.types.block.id import BlockId from iota_sdk.types.burn import Burn from iota_sdk.types.output import Output from iota_sdk.types.input import UtxoInput @@ -34,7 +35,7 @@ class BlockBuilderOptions: custom_remainder_address: Optional[str] = None tag: Optional[HexStr] = None data: Optional[HexStr] = None - strong_parents: Optional[List[HexStr]] = None - weak_parents: Optional[List[HexStr]] = None - shallow_like_parents: Optional[List[HexStr]] = None + strong_parents: Optional[List[BlockId]] = None + weak_parents: Optional[List[BlockId]] = None + shallow_like_parents: Optional[List[BlockId]] = None burn: Optional[Burn] = None diff --git a/bindings/python/iota_sdk/types/common.py b/bindings/python/iota_sdk/types/common.py index 2f85d96704..6591e84be4 100644 --- a/bindings/python/iota_sdk/types/common.py +++ b/bindings/python/iota_sdk/types/common.py @@ -139,3 +139,18 @@ class AddressAndAmount(): encoder=str )) address: str + + +class IdWithSlotIndex(str): + """Represents an hex encoded ID that contains a slot index at the end. + + Attributes: + id: The hex encoded ID with a slot index. + + """ + + def slot_index(self): + """Returns the slot index of the ID. + """ + return int.from_bytes( + bytes.fromhex(self[-8:]), 'little') diff --git a/bindings/python/iota_sdk/types/context_input.py b/bindings/python/iota_sdk/types/context_input.py index 19015f3a28..d67ee093a2 100644 --- a/bindings/python/iota_sdk/types/context_input.py +++ b/bindings/python/iota_sdk/types/context_input.py @@ -6,6 +6,7 @@ from enum import IntEnum from typing import Any, Dict, List, TypeAlias, Union from iota_sdk.types.common import HexStr, json +from iota_sdk.types.slot import SlotCommitmentId class ContextInputType(IntEnum): @@ -29,7 +30,7 @@ class CommitmentContextInput: default_factory=lambda: int( ContextInputType.Commitment), init=False) - commitment_id: HexStr + commitment_id: SlotCommitmentId @json diff --git a/bindings/python/iota_sdk/types/input.py b/bindings/python/iota_sdk/types/input.py index 53da298111..e599908b9d 100644 --- a/bindings/python/iota_sdk/types/input.py +++ b/bindings/python/iota_sdk/types/input.py @@ -5,7 +5,8 @@ from enum import IntEnum from typing import Dict, List, TypeAlias, Union, Any from dataclasses import dataclass, field -from iota_sdk.types.common import HexStr, json +from iota_sdk.types.common import json +from iota_sdk.types.transaction_id import TransactionId class InputType(IntEnum): @@ -31,7 +32,7 @@ class UtxoInput: default_factory=lambda: int( InputType.Utxo), init=False) - transaction_id: HexStr + transaction_id: TransactionId transaction_output_index: int diff --git a/bindings/python/iota_sdk/types/node_info.py b/bindings/python/iota_sdk/types/node_info.py index 37dd91f024..63f7803f4d 100644 --- a/bindings/python/iota_sdk/types/node_info.py +++ b/bindings/python/iota_sdk/types/node_info.py @@ -5,7 +5,8 @@ from dataclasses import dataclass, field from typing import List, Optional from dataclasses_json import config -from iota_sdk.types.common import EpochIndex, HexStr, json, SlotIndex +from iota_sdk.types.common import EpochIndex, json, SlotIndex +from iota_sdk.types.slot import SlotCommitmentId @json @@ -38,7 +39,7 @@ class NodeInfoStatus: relative_confirmed_tangle_time: int = field(metadata=config( encoder=str )) - latest_commitment_id: HexStr + latest_commitment_id: SlotCommitmentId latest_finalized_slot: SlotIndex latest_accepted_block_slot: SlotIndex latest_confirmed_block_slot: SlotIndex diff --git a/bindings/python/iota_sdk/types/output_data.py b/bindings/python/iota_sdk/types/output_data.py index e18c7938e3..189449764f 100644 --- a/bindings/python/iota_sdk/types/output_data.py +++ b/bindings/python/iota_sdk/types/output_data.py @@ -5,8 +5,9 @@ from dataclasses import dataclass from typing import Optional from iota_sdk.types.address import Address -from iota_sdk.types.common import HexStr, json +from iota_sdk.types.common import json from iota_sdk.types.output import Output +from iota_sdk.types.output_id import OutputId from iota_sdk.types.output_metadata import OutputMetadata from iota_sdk.types.signature import Bip44 @@ -25,7 +26,7 @@ class OutputData(): remainder: Whether the output represents a remainder amount. chain: A list of chain state indexes. """ - output_id: HexStr + output_id: OutputId metadata: OutputMetadata output: Output address: Address diff --git a/bindings/python/iota_sdk/types/output_id.py b/bindings/python/iota_sdk/types/output_id.py index 0a824cfd47..b666779fbd 100644 --- a/bindings/python/iota_sdk/types/output_id.py +++ b/bindings/python/iota_sdk/types/output_id.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 from iota_sdk.types.common import HexStr +from iota_sdk.types.transaction_id import TransactionId class OutputId(dict): @@ -14,17 +15,17 @@ class OutputId(dict): """ - def __init__(self, transaction_id: HexStr, output_index: int): + def __init__(self, transaction_id: TransactionId, output_index: int): """Initialize OutputId """ - if len(transaction_id) != 66: + if len(transaction_id) != 74: raise ValueError( - 'transaction_id length must be 66 characters with 0x prefix') + 'transaction_id length must be 74 characters with 0x prefix') if not transaction_id.startswith('0x'): raise ValueError('transaction_id must start with 0x') # Validate that it has only valid hex characters int(transaction_id[2:], 16) - output_index_hex = (output_index).to_bytes(4, "little").hex() + output_index_hex = (output_index).to_bytes(2, "little").hex() self.output_id = transaction_id + output_index_hex self.transaction_id = transaction_id self.output_index = output_index @@ -49,9 +50,9 @@ def from_string(cls, output_id: HexStr): # Validate that it has only valid hex characters int(output_id[2:], 16) obj.output_id = output_id - obj.transaction_id = HexStr(output_id[:66]) + obj.transaction_id = TransactionId(output_id[:74]) obj.output_index = int.from_bytes( - bytes.fromhex(output_id[66:]), 'little') + bytes.fromhex(output_id[74:]), 'little') return obj def __repr__(self): diff --git a/bindings/python/iota_sdk/types/output_metadata.py b/bindings/python/iota_sdk/types/output_metadata.py index 5be99566d5..947d844281 100644 --- a/bindings/python/iota_sdk/types/output_metadata.py +++ b/bindings/python/iota_sdk/types/output_metadata.py @@ -5,9 +5,12 @@ from typing import Dict, Optional, Union from dataclasses import dataclass, field from dataclasses_json import config -from iota_sdk.types.common import HexStr, SlotIndex, json +from iota_sdk.types.block.id import BlockId +from iota_sdk.types.common import SlotIndex, json from iota_sdk.types.output import AccountOutput, BasicOutput, DelegationOutput, FoundryOutput, NftOutput, deserialize_output from iota_sdk.types.output_id import OutputId +from iota_sdk.types.slot import SlotCommitmentId +from iota_sdk.types.transaction_id import TransactionId @json @@ -23,9 +26,9 @@ class OutputMetadata: spent: Metadata of the output if it is marked as spent in the ledger. """ output_id: OutputId - block_id: HexStr + block_id: BlockId included: OutputInclusionMetadata - latest_commitment_id: HexStr + latest_commitment_id: SlotCommitmentId spent: Optional[OutputConsumptionMetadata] = None @@ -77,8 +80,8 @@ class OutputInclusionMetadata: commitment_id: Commitment ID that includes the creation of the output. """ slot: SlotIndex - transaction_id: HexStr - commitment_id: Optional[HexStr] = None + transaction_id: TransactionId + commitment_id: Optional[SlotCommitmentId] = None @json @@ -92,5 +95,5 @@ class OutputConsumptionMetadata: commitment_id: Commitment ID that includes the spending of the output. """ slot: SlotIndex - transaction_id: HexStr - commitment_id: Optional[HexStr] = None + transaction_id: TransactionId + commitment_id: Optional[SlotCommitmentId] = None diff --git a/bindings/python/iota_sdk/types/slot.py b/bindings/python/iota_sdk/types/slot.py new file mode 100644 index 0000000000..fa7389e691 --- /dev/null +++ b/bindings/python/iota_sdk/types/slot.py @@ -0,0 +1,9 @@ +# Copyright 2024 IOTA Stiftung +# SPDX-License-Identifier: Apache-2.0 + +from iota_sdk.types.common import IdWithSlotIndex + + +class SlotCommitmentId(IdWithSlotIndex): + """Represents a slot commitment ID. + """ diff --git a/bindings/python/iota_sdk/types/transaction_id.py b/bindings/python/iota_sdk/types/transaction_id.py new file mode 100644 index 0000000000..af4effc6da --- /dev/null +++ b/bindings/python/iota_sdk/types/transaction_id.py @@ -0,0 +1,9 @@ +# Copyright 2024 IOTA Stiftung +# SPDX-License-Identifier: Apache-2.0 + +from iota_sdk.types.common import IdWithSlotIndex + + +class TransactionId(IdWithSlotIndex): + """Represents a transaction ID. + """ diff --git a/bindings/python/iota_sdk/types/transaction_with_metadata.py b/bindings/python/iota_sdk/types/transaction_with_metadata.py index 75de849163..a10fc62751 100644 --- a/bindings/python/iota_sdk/types/transaction_with_metadata.py +++ b/bindings/python/iota_sdk/types/transaction_with_metadata.py @@ -5,9 +5,11 @@ from dataclasses import dataclass from typing import List, Optional from enum import Enum +from iota_sdk.types.block.id import BlockId from iota_sdk.types.common import HexStr, json from iota_sdk.types.output_metadata import OutputWithMetadata from iota_sdk.types.payload import SignedTransactionPayload +from iota_sdk.types.transaction_id import TransactionId class InclusionState(str, Enum): @@ -44,12 +46,12 @@ class TransactionWithMetadata: payload: SignedTransactionPayload inclusion_state: InclusionState timestamp: int - transaction_id: HexStr + transaction_id: TransactionId network_id: int incoming: bool inputs = List[OutputWithMetadata] note: Optional[str] = None - block_id: Optional[HexStr] = None + block_id: Optional[BlockId] = None @json diff --git a/bindings/python/iota_sdk/types/utxo_changes.py b/bindings/python/iota_sdk/types/utxo_changes.py index 628f479449..71fea39610 100644 --- a/bindings/python/iota_sdk/types/utxo_changes.py +++ b/bindings/python/iota_sdk/types/utxo_changes.py @@ -2,9 +2,9 @@ # SPDX-License-Identifier: Apache-2.0 from typing import List - from dataclasses import dataclass -from iota_sdk.types.common import HexStr, json +from iota_sdk.types.common import json +from iota_sdk.types.output_id import OutputId @json @@ -14,5 +14,5 @@ class UtxoChanges(): Returns all UTXO changes that happened at a specific milestone. """ index: int - created_outputs: List[HexStr] - consumed_outputs: List[HexStr] + created_outputs: List[OutputId] + consumed_outputs: List[OutputId] diff --git a/bindings/python/iota_sdk/utils.py b/bindings/python/iota_sdk/utils.py index b61f6186fd..ff22fcc128 100644 --- a/bindings/python/iota_sdk/utils.py +++ b/bindings/python/iota_sdk/utils.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, List, Optional from iota_sdk.common import custom_encoder +from iota_sdk.types.block.id import BlockId from iota_sdk.types.signature import Ed25519Signature from iota_sdk.types.address import Address, deserialize_address from iota_sdk.types.common import HexStr @@ -16,6 +17,7 @@ from iota_sdk.types.output_id import OutputId from iota_sdk.types.unlock import Unlock from iota_sdk.types.transaction_data import InputSigningData +from iota_sdk.types.transaction_id import TransactionId from iota_sdk.external import call_utils_method # Required to prevent circular import @@ -145,7 +147,7 @@ def compute_nft_id(output_id: OutputId) -> HexStr: }) @staticmethod - def compute_output_id(transaction_id: HexStr, index: int) -> OutputId: + def compute_output_id(transaction_id: TransactionId, index: int) -> OutputId: """Compute the output id from transaction id and output index. """ return OutputId.from_string(_call_method('computeOutputId', { @@ -165,21 +167,21 @@ def compute_token_id(account_id: HexStr, serial_number: int, }) @staticmethod - def block_id(block: Block, params: ProtocolParameters) -> HexStr: + def block_id(block: Block, params: ProtocolParameters) -> BlockId: """ Return a block ID (Blake2b256 hash of block bytes) from a block. """ - return _call_method('blockId', { + return BlockId(_call_method('blockId', { 'block': block, 'protocolParameters': params, - }) + })) @staticmethod - def transaction_id(payload: SignedTransactionPayload) -> HexStr: + def transaction_id(payload: SignedTransactionPayload) -> TransactionId: """ Compute the transaction ID (Blake2b256 hash of the provided transaction payload) of a transaction payload. """ - return _call_method('transactionId', { + return TransactionId(_call_method('transactionId', { 'payload': payload - }) + })) @staticmethod def protocol_parameters_hash(params: ProtocolParameters) -> HexStr: diff --git a/bindings/python/iota_sdk/wallet/wallet.py b/bindings/python/iota_sdk/wallet/wallet.py index 4644570568..33788554ee 100644 --- a/bindings/python/iota_sdk/wallet/wallet.py +++ b/bindings/python/iota_sdk/wallet/wallet.py @@ -11,6 +11,7 @@ from iota_sdk.wallet.prepared_transaction import PreparedCreateDelegationTransaction, PreparedCreateDelegationTransactionData, PreparedCreateTokenTransactionData, PreparedTransaction, PreparedCreateTokenTransaction from iota_sdk.wallet.sync_options import SyncOptions from iota_sdk.types.balance import Balance +from iota_sdk.types.block.id import BlockId from iota_sdk.types.burn import Burn from iota_sdk.types.common import HexStr, json from iota_sdk.types.client_options import ClientOptions @@ -21,6 +22,7 @@ from iota_sdk.types.output import BasicOutput, NftOutput, Output, deserialize_output from iota_sdk.types.output_params import OutputParams from iota_sdk.types.transaction_data import PreparedTransactionData, SignedTransactionData +from iota_sdk.types.transaction_id import TransactionId from iota_sdk.types.send_params import BeginStakingParams, CreateAccountOutputParams, CreateDelegationParams, CreateNativeTokenParams, MintNftParams, SendNativeTokenParams, SendNftParams, SendParams from iota_sdk.types.signature import Bip44 from iota_sdk.types.transaction_with_metadata import CreateDelegationTransaction, CreateNativeTokenTransaction, TransactionWithMetadata @@ -424,7 +426,7 @@ def claimable_outputs(self, outputs_to_claim: List[OutputId]): ) def get_transaction( - self, transaction_id: HexStr) -> TransactionWithMetadata: + self, transaction_id: TransactionId) -> TransactionWithMetadata: """Get transaction. """ return TransactionWithMetadata.from_dict(self._call_method( @@ -688,14 +690,14 @@ def prepare_end_staking(self, account_id: HexStr) -> PreparedTransaction: )) return PreparedTransaction(self, prepared) - def announce_candidacy(self, account_id: HexStr) -> HexStr: + def announce_candidacy(self, account_id: HexStr) -> BlockId: """Announce a staking account's candidacy for the staking period. """ - self._call_method( + return BlockId(self._call_method( 'announceCandidacy', { 'accountId': account_id, } - ) + )) def send_transaction( self, outputs: List[Output], options: Optional[TransactionOptions] = None) -> TransactionWithMetadata: @@ -716,17 +718,17 @@ def prepare_transaction( return PreparedTransaction(self, prepared) def reissue_transaction_until_included( - self, transaction_id: HexStr, interval=None, max_attempts=None) -> HexStr: + self, transaction_id: TransactionId, interval=None, max_attempts=None) -> BlockId: """Reissues a transaction sent from the wallet for a provided transaction id until it's included (referenced by a milestone). Returns the included block id. """ - return self._call_method( + return BlockId(self._call_method( 'reissueTransactionUntilIncluded', { 'transactionId': transaction_id, 'interval': interval, 'maxAttempts': max_attempts } - ) + )) def send(self, amount: int, address: str, options: Optional[TransactionOptions] = None) -> TransactionWithMetadata: diff --git a/bindings/python/tests/test_offline.py b/bindings/python/tests/test_offline.py index 8a4a892a8c..536ee757da 100644 --- a/bindings/python/tests/test_offline.py +++ b/bindings/python/tests/test_offline.py @@ -3,7 +3,7 @@ import json import unittest -from iota_sdk import Client, MnemonicSecretManager, Utils, SecretManager, OutputId, hex_to_utf8, utf8_to_hex, Bip44, CoinType, Irc27Metadata, Irc30Metadata +from iota_sdk import Client, MnemonicSecretManager, Utils, SecretManager, OutputId, hex_to_utf8, utf8_to_hex, Bip44, CoinType, Irc27Metadata, Irc30Metadata, TransactionId # Read the test vector @@ -57,35 +57,36 @@ def test_sign_verify_ed25519(): class TestTypes(unittest.TestCase): def test_output_id(self): - transaction_id = '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649' + transaction_id = TransactionId( + '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64900000000') output_index = 42 output_id = OutputId(transaction_id, output_index) assert repr( - output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a000000' + output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00' new_output_id = OutputId.from_string( - '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a0000000000') + '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00') assert repr( - new_output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a0000000000' + new_output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649000000002a00' assert new_output_id.transaction_id == transaction_id assert new_output_id.output_index == output_index - transaction_id_missing_0x_prefix = '52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649' + transaction_id_missing_0x_prefix = '52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64900000000' with self.assertRaises(ValueError): OutputId(transaction_id_missing_0x_prefix, output_index) - transaction_id__invalid_hex_prefix = '0052fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649' + transaction_id__invalid_hex_prefix = '0052fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64900000000' with self.assertRaises(ValueError): OutputId(transaction_id__invalid_hex_prefix, output_index) - transaction_id_invalid_hex_char = '0xz2fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649' + transaction_id_invalid_hex_char = '0xz2fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64900000000' with self.assertRaises(ValueError): OutputId(transaction_id_invalid_hex_char, output_index) - output_id_missing_0x_prefix = '52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00000000' + output_id_missing_0x_prefix = '52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a000000000000' with self.assertRaises(ValueError): OutputId.from_string(output_id_missing_0x_prefix) - output_id_invalid_hex_char = '0xz2fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00000000' + output_id_invalid_hex_char = '0xz2fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a000000000000' with self.assertRaises(ValueError): OutputId.from_string(output_id_invalid_hex_char) - output_id_invalid_hex_prefix = '0052fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00000000' + output_id_invalid_hex_prefix = '0052fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a000000000000' with self.assertRaises(ValueError): OutputId.from_string(output_id_invalid_hex_prefix)