Skip to content

Commit

Permalink
Python: add Utils::transaction_id() (iotaledger#1407)
Browse files Browse the repository at this point in the history
Co-authored-by: DaughterOfMars <[email protected]>
  • Loading branch information
Thoralf-M and DaughterOfMars authored Oct 9, 2023
1 parent b3740c2 commit 904fded
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.1.1 - 2023-MM-DD

### Added

- `Utils:transaction_id()`;

## 1.1.0 - 2023-09-29

Stable release.
Expand Down
3 changes: 2 additions & 1 deletion bindings/python/examples/exchange/1_create_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# should not be done in production.
load_dotenv()

for env_var in ['WALLET_DB_PATH', 'NODE_URL', 'STRONGHOLD_SNAPSHOT_PATH', 'STRONGHOLD_PASSWORD', 'MNEMONIC']:
for env_var in ['WALLET_DB_PATH', 'NODE_URL',
'STRONGHOLD_SNAPSHOT_PATH', 'STRONGHOLD_PASSWORD', 'MNEMONIC']:
if env_var not in os.environ:
raise Exception(f'.env {env_var} is undefined, see .env.example')

Expand Down
9 changes: 9 additions & 0 deletions bindings/python/iota_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from iota_sdk.types.common import HexStr
from iota_sdk.types.output_id import OutputId
from iota_sdk.types.output import Output
from iota_sdk.types.payload import TransactionPayload
from json import dumps, loads
from typing import TYPE_CHECKING, List
from dacite import from_dict
Expand Down Expand Up @@ -176,6 +177,14 @@ def block_id(block: Block) -> HexStr:
'block': block.as_dict()
})

@staticmethod
def transaction_id(transaction_payload: TransactionPayload) -> HexStr:
""" Compute the transaction ID (Blake2b256 hash of the provided transaction payload) of a transaction payload.
"""
return _call_method('transactionId', {
'payload': transaction_payload.as_dict()
})

@staticmethod
def hash_transaction_essence(essence) -> HexStr:
""" Compute the hash of a transaction essence.
Expand Down
6 changes: 4 additions & 2 deletions bindings/python/tests/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def test_output_id(self):
transaction_id = '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649'
output_index = 42
output_id = OutputId(transaction_id, output_index)
assert repr(output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00'
assert repr(
output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00'

new_output_id = OutputId.from_string(
'0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00')
assert repr(new_output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00'
assert repr(
new_output_id) == '0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c6492a00'
assert new_output_id.transaction_id == transaction_id
assert new_output_id.output_index == output_index

Expand Down

0 comments on commit 904fded

Please sign in to comment.