From af13b1466314402d142a3f8c20568e272f393e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Wed, 6 Mar 2024 18:16:07 +0100 Subject: [PATCH 1/2] Remove Bech32ToHex, HexToBech32, AccountIdToBech32, AnchorIdToBech32, NftIdToBech32 --- bindings/core/src/method/client.rs | 32 ------- bindings/core/src/method/utils.rs | 30 +----- bindings/core/src/method_handler/client.rs | 12 --- bindings/core/src/method_handler/utils.rs | 12 +-- bindings/core/src/response.rs | 12 --- bindings/core/tests/utils.rs | 10 +- .../nodejs/examples/client/11-build-output.ts | 14 +-- .../client/13-build-account-output.ts | 11 +-- .../examples/client/15-build-nft-output.ts | 11 +-- .../how_tos/account_output/request-funds.ts | 6 +- .../how_tos/account_output/send-amount.ts | 6 +- .../advanced_transaction.ts | 7 +- .../nft_collection/01_mint_collection_nft.ts | 14 ++- .../nodejs/examples/how_tos/nfts/mint_nft.ts | 11 +-- .../examples/how_tos/outputs/features.ts | 11 +-- .../how_tos/outputs/unlock-conditions.ts | 20 ++-- .../how_tos/wallet/consolidate-outputs.ts | 4 +- .../wallet/17-check-unlock-conditions.ts | 14 ++- bindings/nodejs/lib/client/client.ts | 88 ------------------ .../nodejs/lib/types/client/bridge/client.ts | 32 ------- .../nodejs/lib/types/client/bridge/index.ts | 8 -- .../nodejs/lib/types/utils/bridge/index.ts | 12 +-- .../nodejs/lib/types/utils/bridge/utils.ts | 42 +-------- bindings/nodejs/lib/utils/utils.ts | 93 ------------------- .../tests/client/outputBuilders.spec.ts | 12 +-- .../tests/client/utilityMethods.spec.ts | 20 ++-- .../python/examples/client/build_account.py | 10 +- .../python/examples/client/build_basic.py | 14 ++- bindings/python/examples/client/build_nft.py | 9 +- .../how_tos/account_output/request_funds.py | 6 +- .../how_tos/account_output/send_amount.py | 6 +- .../advanced_transaction.py | 5 +- .../nft_collection/01_mint_collection_nft.py | 8 +- .../examples/how_tos/outputs/features.py | 11 +-- .../how_tos/outputs/unlock_conditions.py | 16 ++-- .../how_tos/wallet/consolidate_outputs.py | 4 +- .../wallet/13-check-unlock-conditions.py | 4 +- bindings/python/iota_sdk/client/_utils.py | 38 -------- bindings/python/iota_sdk/utils.py | 46 --------- bindings/wasm/tests/utilityMethods.spec.ts | 14 +-- sdk/src/client/utils.rs | 74 --------------- 41 files changed, 134 insertions(+), 675 deletions(-) diff --git a/bindings/core/src/method/client.rs b/bindings/core/src/method/client.rs index 1959bb429c..dff0781fb1 100644 --- a/bindings/core/src/method/client.rs +++ b/bindings/core/src/method/client.rs @@ -432,41 +432,9 @@ pub enum ClientMethod { ////////////////////////////////////////////////////////////////////// // Utils ////////////////////////////////////////////////////////////////////// - /// Transforms a hex encoded address to a bech32 encoded address - #[serde(rename_all = "camelCase")] - HexToBech32 { - /// Hex encoded bech32 address - hex: String, - /// Human readable part - bech32_hrp: Option, - }, /// Converts an address to its bech32 representation #[serde(rename_all = "camelCase")] AddressToBech32 { address: Address, bech32_hrp: Option }, - /// Transforms an account id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - AccountIdToBech32 { - /// Account ID - account_id: AccountId, - /// Human readable part - bech32_hrp: Option, - }, - /// Transforms an anchor id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - AnchorIdToBech32 { - /// Anchor ID - anchor_id: AnchorId, - /// Human readable part - bech32_hrp: Option, - }, - /// Transforms an nft id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - NftIdToBech32 { - /// Nft ID - nft_id: NftId, - /// Human readable part - bech32_hrp: Option, - }, /// Calculate the minimum required amount for an output. /// Expected response: /// [`Amount`](crate::Response::Amount) diff --git a/bindings/core/src/method/utils.rs b/bindings/core/src/method/utils.rs index a6fd6ef84b..9eacde8567 100644 --- a/bindings/core/src/method/utils.rs +++ b/bindings/core/src/method/utils.rs @@ -8,7 +8,7 @@ use iota_sdk::{ client::secret::types::InputSigningData, types::block::{ address::{Address, Bech32Address, Hrp}, - output::{AccountId, AnchorId, NftId, Output, OutputId, StorageScoreParameters}, + output::{AccountId, Output, OutputId, StorageScoreParameters}, payload::signed_transaction::{ dto::{SignedTransactionPayloadDto, TransactionDto}, TransactionId, @@ -31,40 +31,12 @@ use crate::OmittedDebug; #[serde(tag = "name", content = "data", rename_all = "camelCase")] #[non_exhaustive] pub enum UtilsMethod { - /// Transforms bech32 to hex - Bech32ToHex { - bech32: Bech32Address, - }, - /// Transforms a hex encoded address to a bech32 encoded address - #[serde(rename_all = "camelCase")] - HexToBech32 { - hex: String, - bech32_hrp: Hrp, - }, /// Converts an address to its bech32 representation #[serde(rename_all = "camelCase")] AddressToBech32 { address: Address, bech32_hrp: Hrp, }, - /// Transforms an account id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - AccountIdToBech32 { - account_id: AccountId, - bech32_hrp: Hrp, - }, - /// Transforms an anchor id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - AnchorIdToBech32 { - anchor_id: AnchorId, - bech32_hrp: Hrp, - }, - /// Transforms an nft id to a bech32 encoded address - #[serde(rename_all = "camelCase")] - NftIdToBech32 { - nft_id: NftId, - bech32_hrp: Hrp, - }, /// Returns a valid Address parsed from a String. ParseBech32Address { address: Bech32Address, diff --git a/bindings/core/src/method_handler/client.rs b/bindings/core/src/method_handler/client.rs index 1f220cba00..b63fe2cec0 100644 --- a/bindings/core/src/method_handler/client.rs +++ b/bindings/core/src/method_handler/client.rs @@ -306,21 +306,9 @@ pub(crate) async fn call_client_method_internal( ClientMethod::FindInputs { addresses, amount } => { Response::Inputs(client.find_inputs(addresses, amount).await?) } - ClientMethod::HexToBech32 { hex, bech32_hrp } => { - Response::Bech32Address(client.hex_to_bech32(&hex, bech32_hrp).await?) - } ClientMethod::AddressToBech32 { address, bech32_hrp } => { Response::Bech32Address(client.address_to_bech32(address, bech32_hrp).await?) } - ClientMethod::AccountIdToBech32 { account_id, bech32_hrp } => { - Response::Bech32Address(client.account_id_to_bech32(account_id, bech32_hrp).await?) - } - ClientMethod::AnchorIdToBech32 { anchor_id, bech32_hrp } => { - Response::Bech32Address(client.anchor_id_to_bech32(anchor_id, bech32_hrp).await?) - } - ClientMethod::NftIdToBech32 { nft_id, bech32_hrp } => { - Response::Bech32Address(client.nft_id_to_bech32(nft_id, bech32_hrp).await?) - } ClientMethod::ComputeMinimumOutputAmount { output } => { let storage_score_params = client.get_storage_score_parameters().await?; diff --git a/bindings/core/src/method_handler/utils.rs b/bindings/core/src/method_handler/utils.rs index c966f83cdd..8933f10600 100644 --- a/bindings/core/src/method_handler/utils.rs +++ b/bindings/core/src/method_handler/utils.rs @@ -6,7 +6,7 @@ use crypto::{ keys::bip39::Mnemonic, }; use iota_sdk::{ - client::{hex_to_bech32, verify_mnemonic, Client}, + client::{verify_mnemonic, Client}, types::{ block::{ address::{AccountAddress, Address, ToBech32Ext}, @@ -27,17 +27,7 @@ use crate::{method::UtilsMethod, response::Response}; /// Call a utils method. pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result { let response = match method { - UtilsMethod::Bech32ToHex { bech32 } => Response::Bech32ToHex(Client::bech32_to_hex(bech32)?), - UtilsMethod::HexToBech32 { hex, bech32_hrp } => Response::Bech32Address(hex_to_bech32(&hex, bech32_hrp)?), UtilsMethod::AddressToBech32 { address, bech32_hrp } => Response::Bech32Address(address.to_bech32(bech32_hrp)), - UtilsMethod::AccountIdToBech32 { account_id, bech32_hrp } => { - Response::Bech32Address(account_id.to_bech32(bech32_hrp)) - } - UtilsMethod::AnchorIdToBech32 { anchor_id, bech32_hrp } => { - Response::Bech32Address(anchor_id.to_bech32(bech32_hrp)) - } - UtilsMethod::NftIdToBech32 { nft_id, bech32_hrp } => Response::Bech32Address(nft_id.to_bech32(bech32_hrp)), - UtilsMethod::ParseBech32Address { address } => Response::ParsedBech32Address(address.into_inner()), UtilsMethod::IsAddressValid { address } => Response::Bool(Address::is_valid_bech32(&address)), UtilsMethod::GenerateMnemonic => Response::GeneratedMnemonic(Client::generate_mnemonic()?.to_string()), diff --git a/bindings/core/src/response.rs b/bindings/core/src/response.rs index 2b7a8f889c..b53baa6045 100644 --- a/bindings/core/src/response.rs +++ b/bindings/core/src/response.rs @@ -198,9 +198,6 @@ pub enum Response { /// [`OutputIdToUtxoInput`](crate::method::UtilsMethod::OutputIdToUtxoInput) Input(UtxoInput), /// Response for: - /// - [`Bech32ToHex`](crate::method::UtilsMethod::Bech32ToHex) - Bech32ToHex(String), - /// Response for: /// - [`ParseBech32Address`](crate::method::UtilsMethod::ParseBech32Address) ParsedBech32Address(Address), /// Response for: @@ -219,8 +216,6 @@ pub enum Response { /// - [`Blake2b256Hash`](crate::method::UtilsMethod::Blake2b256Hash) /// - [`TransactionSigningHash`](crate::method::UtilsMethod::TransactionSigningHash) Hash(String), - /// Response for [`Bech32ToHex`](crate::method::UtilsMethod::Bech32ToHex) - HexAddress(String), /// Response for [`OutputHexBytes`](crate::method::UtilsMethod::OutputHexBytes) HexBytes(String), /// Response for [`CallPluginRoute`](crate::method::ClientMethod::CallPluginRoute) @@ -240,13 +235,6 @@ pub enum Response { /// Response for: /// - [`AddressToBech32`](crate::method::ClientMethod::AddressToBech32) /// - [`AddressToBech32`](crate::method::UtilsMethod::AddressToBech32) - /// - [`AccountIdToBech32`](crate::method::ClientMethod::AccountIdToBech32) - /// - [`AccountIdToBech32`](crate::method::UtilsMethod::AccountIdToBech32) - /// - [`AnchorIdToBech32`](crate::method::ClientMethod::AnchorIdToBech32) - /// - [`AnchorIdToBech32`](crate::method::UtilsMethod::AnchorIdToBech32) - /// - [`NftIdToBech32`](crate::method::ClientMethod::NftIdToBech32) - /// - [`NftIdToBech32`](crate::method::UtilsMethod::NftIdToBech32) - /// - [`HexToBech32`](crate::method::ClientMethod::HexToBech32) /// - [`ImplicitAccountCreationAddress`](crate::method::WalletMethod::ImplicitAccountCreationAddress) Bech32Address(Bech32Address), /// - [`Faucet`](crate::method::ClientMethod::RequestFundsFromFaucet) diff --git a/bindings/core/tests/utils.rs b/bindings/core/tests/utils.rs index 4c0cf2d96c..358041f6c6 100644 --- a/bindings/core/tests/utils.rs +++ b/bindings/core/tests/utils.rs @@ -15,15 +15,15 @@ async fn utils() -> Result<(), Box> { let bech32_address = Bech32Address::try_from_str("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?; - let method = UtilsMethod::Bech32ToHex { - bech32: bech32_address.clone(), + let method = UtilsMethod::ParseBech32Address { + address: bech32_address.clone(), }; let response = call_utils_method(method); match response { - Response::Bech32ToHex(hex) => { - match call_utils_method(UtilsMethod::HexToBech32 { - hex, + Response::ParsedBech32Address(address) => { + match call_utils_method(UtilsMethod::AddressToBech32 { + address, bech32_hrp: Hrp::from_str_unchecked("rms"), }) { Response::Bech32Address(address_bech32) => { diff --git a/bindings/nodejs/examples/client/11-build-output.ts b/bindings/nodejs/examples/client/11-build-output.ts index 187d4dc64b..4af2342088 100644 --- a/bindings/nodejs/examples/client/11-build-output.ts +++ b/bindings/nodejs/examples/client/11-build-output.ts @@ -11,7 +11,6 @@ import { SenderFeature, TagFeature, StorageDepositReturnUnlockCondition, - Ed25519Address, ExpirationUnlockCondition, TimelockUnlockCondition, utf8ToHex, @@ -36,12 +35,12 @@ async function run() { }); try { - const hexAddress = Utils.bech32ToHex( + const ed25519Address = Utils.parseBech32Address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ); const addressUnlockCondition: UnlockCondition = - new AddressUnlockCondition(new Ed25519Address(hexAddress)); + new AddressUnlockCondition(ed25519Address); // Build most basic output with amount and a single address unlock condition const basicOutput = await client.buildBasicOutput({ @@ -68,7 +67,7 @@ async function run() { unlockConditions: [ addressUnlockCondition, new StorageDepositReturnUnlockCondition( - new Ed25519Address(hexAddress), + ed25519Address, '1000000', ), ], @@ -81,10 +80,7 @@ async function run() { amount: BigInt(1000000), unlockConditions: [ addressUnlockCondition, - new ExpirationUnlockCondition( - new Ed25519Address(hexAddress), - 1, - ), + new ExpirationUnlockCondition(ed25519Address, 1), ], }); @@ -114,7 +110,7 @@ async function run() { const basicOutputWithSender = await client.buildBasicOutput({ amount: BigInt(1000000), unlockConditions: [addressUnlockCondition], - features: [new SenderFeature(new Ed25519Address(hexAddress))], + features: [new SenderFeature(ed25519Address)], }); console.log(JSON.stringify(basicOutputWithSender, null, 2)); diff --git a/bindings/nodejs/examples/client/13-build-account-output.ts b/bindings/nodejs/examples/client/13-build-account-output.ts index 0b37a76302..9d3772329a 100644 --- a/bindings/nodejs/examples/client/13-build-account-output.ts +++ b/bindings/nodejs/examples/client/13-build-account-output.ts @@ -7,7 +7,6 @@ import { Utils, MetadataFeature, SenderFeature, - Ed25519Address, IssuerFeature, AddressUnlockCondition, utf8ToHex, @@ -32,22 +31,20 @@ async function run() { }); try { - const hexAddress = Utils.bech32ToHex( + const ed25519Address = Utils.parseBech32Address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ); const accountOutput = await client.buildAccountOutput({ accountId: '0x0000000000000000000000000000000000000000000000000000000000000000', - unlockConditions: [ - new AddressUnlockCondition(new Ed25519Address(hexAddress)), - ], + unlockConditions: [new AddressUnlockCondition(ed25519Address)], features: [ - new SenderFeature(new Ed25519Address(hexAddress)), + new SenderFeature(ed25519Address), new MetadataFeature({ data: utf8ToHex('hello') }), ], immutableFeatures: [ - new IssuerFeature(new Ed25519Address(hexAddress)), + new IssuerFeature(ed25519Address), new MetadataFeature({ data: utf8ToHex('hello') }), ], }); diff --git a/bindings/nodejs/examples/client/15-build-nft-output.ts b/bindings/nodejs/examples/client/15-build-nft-output.ts index 494695a5d7..c91fba145e 100644 --- a/bindings/nodejs/examples/client/15-build-nft-output.ts +++ b/bindings/nodejs/examples/client/15-build-nft-output.ts @@ -10,7 +10,6 @@ import { TagFeature, MetadataFeature, SenderFeature, - Ed25519Address, IssuerFeature, Irc27Metadata, } from '@iota/sdk'; @@ -34,7 +33,7 @@ async function run() { }); try { - const hexAddress = Utils.bech32ToHex( + const ed25519Address = Utils.parseBech32Address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ); @@ -47,15 +46,13 @@ async function run() { const nftOutput = await client.buildNftOutput({ // NftId needs to be null the first time nftId: '0x0000000000000000000000000000000000000000000000000000000000000000', - unlockConditions: [ - new AddressUnlockCondition(new Ed25519Address(hexAddress)), - ], + unlockConditions: [new AddressUnlockCondition(ed25519Address)], immutableFeatures: [ - new IssuerFeature(new Ed25519Address(hexAddress)), + new IssuerFeature(ed25519Address), tip27ImmutableMetadata.asFeature(), ], features: [ - new SenderFeature(new Ed25519Address(hexAddress)), + new SenderFeature(ed25519Address), new MetadataFeature({ data: utf8ToHex('mutable metadata'), }), diff --git a/bindings/nodejs/examples/how_tos/account_output/request-funds.ts b/bindings/nodejs/examples/how_tos/account_output/request-funds.ts index 5810703e76..ee8fd0aa22 100644 --- a/bindings/nodejs/examples/how_tos/account_output/request-funds.ts +++ b/bindings/nodejs/examples/how_tos/account_output/request-funds.ts @@ -1,7 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Utils, Wallet, initLogger } from '@iota/sdk'; +import { Utils, Wallet, initLogger, AccountAddress } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. // @@ -41,8 +41,8 @@ async function run() { const client = await wallet.getClient(); // Get Account address - const accountAddress = Utils.accountIdToBech32( - accountId, + const accountAddress = Utils.addressToBech32( + new AccountAddress(accountId), await client.getBech32Hrp(), ); diff --git a/bindings/nodejs/examples/how_tos/account_output/send-amount.ts b/bindings/nodejs/examples/how_tos/account_output/send-amount.ts index bf046dc034..0c18455776 100644 --- a/bindings/nodejs/examples/how_tos/account_output/send-amount.ts +++ b/bindings/nodejs/examples/how_tos/account_output/send-amount.ts @@ -1,7 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Wallet, initLogger, Utils } from '@iota/sdk'; +import { Wallet, initLogger, Utils, AccountAddress } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. // @@ -52,8 +52,8 @@ async function run() { const client = await wallet.getClient(); // Get Account address - const accountAddress = Utils.accountIdToBech32( - accountId, + const accountAddress = Utils.addressToBech32( + new AccountAddress(accountId), await client.getBech32Hrp(), ); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts index 982043f1a7..8d6f216af4 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts @@ -3,7 +3,6 @@ import { AddressUnlockCondition, - Ed25519Address, TimelockUnlockCondition, Utils, Wallet, @@ -49,10 +48,8 @@ async function run() { const basicOutput = await client.buildBasicOutput({ unlockConditions: [ new AddressUnlockCondition( - new Ed25519Address( - Utils.bech32ToHex( - 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', - ), + Utils.parseBech32Address( + 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ), ), new TimelockUnlockCondition(slotIndex), diff --git a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts index b79dfefa58..ce79c8447a 100644 --- a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts +++ b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts @@ -1,7 +1,14 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { MintNftParams, NftId, Utils, Wallet, Irc27Metadata } from '@iota/sdk'; +import { + MintNftParams, + NftId, + Utils, + Wallet, + Irc27Metadata, + NftAddress, +} from '@iota/sdk'; require('dotenv').config({ path: '.env' }); // The NFT collection size @@ -49,7 +56,10 @@ async function run() { const client = await wallet.getClient(); const bech32Hrp = await client.getBech32Hrp(); - const issuer = Utils.nftIdToBech32(issuerNftId, bech32Hrp); + const issuer = Utils.addressToBech32( + new NftAddress(issuerNftId), + bech32Hrp, + ); const nftMintParams = []; // Create the metadata with another index for each diff --git a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts index 83c23c4d56..aa2ca76030 100644 --- a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts @@ -3,7 +3,6 @@ import { AddressUnlockCondition, - Ed25519Address, IssuerFeature, MintNftParams, SenderFeature, @@ -83,19 +82,17 @@ async function run() { // Build an NFT manually by using the `NftOutputBuilder` const client = await wallet.getClient(); - const hexAddress = Utils.bech32ToHex(senderAddress); + const ed25519Address = Utils.parseBech32Address(senderAddress); const output = await client.buildNftOutput({ amount: NFT2_AMOUNT, nftId: '0x0000000000000000000000000000000000000000000000000000000000000000', unlockConditions: [ new AddressUnlockCondition( - new Ed25519Address(Utils.bech32ToHex(NFT1_OWNER_ADDRESS)), + Utils.parseBech32Address(NFT1_OWNER_ADDRESS), ), ], - immutableFeatures: [ - new IssuerFeature(new Ed25519Address(hexAddress)), - ], - features: [new SenderFeature(new Ed25519Address(hexAddress))], + immutableFeatures: [new IssuerFeature(ed25519Address)], + features: [new SenderFeature(ed25519Address)], }); transaction = await wallet.sendOutputs([output]); diff --git a/bindings/nodejs/examples/how_tos/outputs/features.ts b/bindings/nodejs/examples/how_tos/outputs/features.ts index 996261c13b..6dcbc60e74 100644 --- a/bindings/nodejs/examples/how_tos/outputs/features.ts +++ b/bindings/nodejs/examples/how_tos/outputs/features.ts @@ -10,7 +10,6 @@ import { MetadataFeature, SenderFeature, TagFeature, - Ed25519Address, IssuerFeature, utf8ToHex, } from '@iota/sdk'; @@ -26,27 +25,25 @@ async function run() { const client = await Client.create({}); try { - const hexAddress = Utils.bech32ToHex( + const ed25519Address = Utils.parseBech32Address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ); const addressUnlockCondition: UnlockCondition = - new AddressUnlockCondition(new Ed25519Address(hexAddress)); + new AddressUnlockCondition(ed25519Address); // Output with sender feature const nftOutputWithSender = await client.buildNftOutput({ nftId: '0x0000000000000000000000000000000000000000000000000000000000000000', unlockConditions: [addressUnlockCondition], - features: [new SenderFeature(new Ed25519Address(hexAddress))], + features: [new SenderFeature(ed25519Address)], }); // Output with issuer feature const nftOutputWithIssuer = await client.buildNftOutput({ nftId: '0x0000000000000000000000000000000000000000000000000000000000000000', unlockConditions: [addressUnlockCondition], - immutableFeatures: [ - new IssuerFeature(new Ed25519Address(hexAddress)), - ], + immutableFeatures: [new IssuerFeature(ed25519Address)], }); // Output with metadata feature diff --git a/bindings/nodejs/examples/how_tos/outputs/unlock-conditions.ts b/bindings/nodejs/examples/how_tos/outputs/unlock-conditions.ts index 4be6b6fd61..83e38a4c28 100644 --- a/bindings/nodejs/examples/how_tos/outputs/unlock-conditions.ts +++ b/bindings/nodejs/examples/how_tos/outputs/unlock-conditions.ts @@ -8,7 +8,6 @@ import { UnlockCondition, AddressUnlockCondition, StorageDepositReturnUnlockCondition, - Ed25519Address, ExpirationUnlockCondition, TimelockUnlockCondition, SimpleTokenScheme, @@ -27,13 +26,13 @@ async function run() { const client = await Client.create({}); try { - const hexAddress = Utils.bech32ToHex( + const ed25519Address = Utils.parseBech32Address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', ); - const accountHexAddress = Utils.bech32ToHex( + const accountAddress = Utils.parseBech32Address( 'rms1pr59qm43mjtvhcajfmupqf23x29llam88yecn6pyul80rx099krmv2fnnux', - ); + ) as AccountAddress; const tokenSchema = new SimpleTokenScheme( BigInt(50), @@ -42,7 +41,7 @@ async function run() { ); const addressUnlockCondition: UnlockCondition = - new AddressUnlockCondition(new Ed25519Address(hexAddress)); + new AddressUnlockCondition(ed25519Address); // Most simple output const basicOutput = await client.buildBasicOutput({ @@ -54,7 +53,7 @@ async function run() { unlockConditions: [ addressUnlockCondition, new StorageDepositReturnUnlockCondition( - new Ed25519Address(hexAddress), + ed25519Address, '1000000', ), ], @@ -72,10 +71,7 @@ async function run() { const basicOutputWithExpiration = await client.buildBasicOutput({ unlockConditions: [ addressUnlockCondition, - new ExpirationUnlockCondition( - new Ed25519Address(hexAddress), - 1, - ), + new ExpirationUnlockCondition(ed25519Address, 1), ], }); @@ -84,9 +80,7 @@ async function run() { serialNumber: 1, tokenScheme: tokenSchema, unlockConditions: [ - new ImmutableAccountAddressUnlockCondition( - new AccountAddress(accountHexAddress), - ), + new ImmutableAccountAddressUnlockCondition(accountAddress), ], }); diff --git a/bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts b/bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts index a3e0bd95e8..9e28f71cb4 100644 --- a/bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts +++ b/bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts @@ -50,7 +50,7 @@ async function run() { console.log(`OUTPUT #${i}`); console.log( '- address: %s\n- amount: %d\n- native token: %s', - Utils.hexToBech32(address.toString(), 'rms'), + Utils.addressToBech32(address, 'rms'), output.getAmount(), output instanceof CommonOutput ? (output as CommonOutput).getNativeToken() ?? [] @@ -84,7 +84,7 @@ async function run() { console.log(`OUTPUT #${i}`); console.log( '- address: %s\n- amount: %d\n- native tokens: %s', - Utils.hexToBech32(address.toString(), 'rms'), + Utils.addressToBech32(address, 'rms'), output.getAmount(), output instanceof CommonOutput ? (output as CommonOutput).getNativeToken() diff --git a/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts b/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts index 480dbf7fa9..abd9c5562a 100644 --- a/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts +++ b/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts @@ -1,7 +1,13 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { AddressUnlockCondition, BasicOutput, Output, Utils } from '@iota/sdk'; +import { + AddressUnlockCondition, + BasicOutput, + Ed25519Address, + Output, + Utils, +} from '@iota/sdk'; import { getUnlockedWallet } from './common'; @@ -27,7 +33,9 @@ async function run() { amount: AMOUNT, }); - const hexEncodedwalletAddress = Utils.bech32ToHex(walletAddress); + const hexEncodedwalletAddress = Utils.parseBech32Address( + walletAddress, + ) as Ed25519Address; if (output instanceof BasicOutput) { const basicOutput = output as BasicOutput; @@ -36,7 +44,7 @@ async function run() { basicOutput.unlockConditions.length === 1 && basicOutput.unlockConditions[0] instanceof AddressUnlockCondition && - hexEncodedwalletAddress.includes( + hexEncodedwalletAddress.pubKeyHash.includes( ( basicOutput .unlockConditions[0] as AddressUnlockCondition diff --git a/bindings/nodejs/lib/client/client.ts b/bindings/nodejs/lib/client/client.ts index e93a7a1b92..ed3c1e43c6 100644 --- a/bindings/nodejs/lib/client/client.ts +++ b/bindings/nodejs/lib/client/client.ts @@ -836,28 +836,6 @@ export class Client { return JSON.parse(response).payload; } - /** - * Convert a hex encoded address to a Bech32 encoded address. - * - * @param hex The hexadecimal string representation of an address. - * @param bech32Hrp The Bech32 HRP (human readable part) to be used. - * @returns The corresponding Bech32 address. - */ - async hexToBech32( - hex: HexEncodedString, - bech32Hrp?: string, - ): Promise { - const response = await this.methodHandler.callMethod({ - name: 'hexToBech32', - data: { - hex, - bech32Hrp, - }, - }); - - return JSON.parse(response).payload; - } - /** * Converts an address to its bech32 representation * @@ -880,72 +858,6 @@ export class Client { return JSON.parse(response).payload; } - /** - * Transforms an account id to a bech32 encoded address. - * - * @param accountId An account ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to be used. - * @returns The corresponding Bech32 address. - */ - async accountIdToBech32( - accountId: AccountId, - bech32Hrp?: string, - ): Promise { - const response = await this.methodHandler.callMethod({ - name: 'accountIdToBech32', - data: { - accountId, - bech32Hrp, - }, - }); - - return JSON.parse(response).payload; - } - - /** - * Transforms an anchor id to a bech32 encoded address. - * - * @param anchorId An anchor ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to be used. - * @returns The corresponding Bech32 address. - */ - async anchorIdToBech32( - anchorId: AnchorId, - bech32Hrp?: string, - ): Promise { - const response = await this.methodHandler.callMethod({ - name: 'anchorIdToBech32', - data: { - anchorId, - bech32Hrp, - }, - }); - - return JSON.parse(response).payload; - } - - /** - * Convert an NFT ID to a Bech32 encoded address. - * - * @param nftId An NFT ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to be used. - * @returns The corresponding Bech32 address. - */ - async nftIdToBech32( - nftId: NftId, - bech32Hrp?: string, - ): Promise { - const response = await this.methodHandler.callMethod({ - name: 'nftIdToBech32', - data: { - nftId, - bech32Hrp, - }, - }); - - return JSON.parse(response).payload; - } - /** * Return the unhealthy nodes. */ diff --git a/bindings/nodejs/lib/types/client/bridge/client.ts b/bindings/nodejs/lib/types/client/bridge/client.ts index 4a01110729..6d1cc5f7c1 100644 --- a/bindings/nodejs/lib/types/client/bridge/client.ts +++ b/bindings/nodejs/lib/types/client/bridge/client.ts @@ -317,14 +317,6 @@ export interface __GetProtocolParametersMethod__ { name: 'getProtocolParameters'; } -export interface __HexToBech32Method__ { - name: 'hexToBech32'; - data: { - hex: HexEncodedString; - bech32Hrp?: string; - }; -} - export interface __AddressToBech32Method__ { name: 'addressToBech32'; data: { @@ -333,30 +325,6 @@ export interface __AddressToBech32Method__ { }; } -export interface __AccountIdToBech32Method__ { - name: 'accountIdToBech32'; - data: { - accountId: AccountId; - bech32Hrp?: string; - }; -} - -export interface __AnchorIdToBech32Method__ { - name: 'anchorIdToBech32'; - data: { - anchorId: AnchorId; - bech32Hrp?: string; - }; -} - -export interface __NftIdToBech32Method__ { - name: 'nftIdToBech32'; - data: { - nftId: NftId; - bech32Hrp?: string; - }; -} - export interface __FindBlocksMethod__ { name: 'findBlocks'; data: { diff --git a/bindings/nodejs/lib/types/client/bridge/index.ts b/bindings/nodejs/lib/types/client/bridge/index.ts index 72d414c2d3..2bfb84fc57 100644 --- a/bindings/nodejs/lib/types/client/bridge/index.ts +++ b/bindings/nodejs/lib/types/client/bridge/index.ts @@ -44,11 +44,7 @@ import type { __GetCommitmentBySlotRawMethod__, __GetUtxoChangesBySlotMethod__, __GetUtxoChangesFullBySlotMethod__, - __HexToBech32Method__, __AddressToBech32Method__, - __AccountIdToBech32Method__, - __AnchorIdToBech32Method__, - __NftIdToBech32Method__, __AccountOutputIdsMethod__, __AccountOutputIdMethod__, __AnchorOutputIdsMethod__, @@ -115,11 +111,7 @@ export type __ClientMethods__ = | __GetCommitmentBySlotRawMethod__ | __GetUtxoChangesBySlotMethod__ | __GetUtxoChangesFullBySlotMethod__ - | __HexToBech32Method__ | __AddressToBech32Method__ - | __AccountIdToBech32Method__ - | __AnchorIdToBech32Method__ - | __NftIdToBech32Method__ | __AccountOutputIdsMethod__ | __AccountOutputIdMethod__ | __AnchorOutputIdsMethod__ diff --git a/bindings/nodejs/lib/types/utils/bridge/index.ts b/bindings/nodejs/lib/types/utils/bridge/index.ts index 405f4bf2b9..22de248ba6 100644 --- a/bindings/nodejs/lib/types/utils/bridge/index.ts +++ b/bindings/nodejs/lib/types/utils/bridge/index.ts @@ -5,16 +5,11 @@ import type { __ComputeTokenIdMethod__, __ComputeFoundryIdMethod__, __ComputeMinimumOutputAmountMethod__, - __ParseBech32AddressMethod__, __Blake2b256HashMethod__, __BlockIdMethod__, __TransactionIdMethod__, - __Bech32ToHexMethod__, - __HexToBech32Method__, __AddressToBech32Method__, - __AccountIdToBech32Method__, - __AnchorIdToBech32Method__, - __NftIdToBech32Method__, + __ParseBech32AddressMethod__, __IsAddressValidMethod__, __ProtocolParametersHashMethod__, __TransactionSigningHashMethod__, @@ -46,12 +41,7 @@ export type __UtilsMethods__ = | __Blake2b256HashMethod__ | __BlockIdMethod__ | __TransactionIdMethod__ - | __Bech32ToHexMethod__ - | __HexToBech32Method__ | __AddressToBech32Method__ - | __AccountIdToBech32Method__ - | __AnchorIdToBech32Method__ - | __NftIdToBech32Method__ | __IsAddressValidMethod__ | __ProtocolParametersHashMethod__ | __TransactionSigningHashMethod__ diff --git a/bindings/nodejs/lib/types/utils/bridge/utils.ts b/bindings/nodejs/lib/types/utils/bridge/utils.ts index 8b6952439c..b27576946e 100644 --- a/bindings/nodejs/lib/types/utils/bridge/utils.ts +++ b/bindings/nodejs/lib/types/utils/bridge/utils.ts @@ -10,12 +10,11 @@ import { Block, ProtocolParameters, OutputId, - NftId, Bech32Address, Unlock, Address, } from '../../'; -import { AccountId, AnchorId } from '../../block/id'; +import { AccountId } from '../../block/id'; import { SlotCommitment } from '../../block/slot'; import { InputSigningData } from '../../client'; import { NumericString } from '../numeric'; @@ -94,21 +93,6 @@ export interface __TransactionIdMethod__ { }; } -export interface __Bech32ToHexMethod__ { - name: 'bech32ToHex'; - data: { - bech32: Bech32Address; - }; -} - -export interface __HexToBech32Method__ { - name: 'hexToBech32'; - data: { - hex: HexEncodedString; - bech32Hrp: string; - }; -} - export interface __AddressToBech32Method__ { name: 'addressToBech32'; data: { @@ -117,30 +101,6 @@ export interface __AddressToBech32Method__ { }; } -export interface __AccountIdToBech32Method__ { - name: 'accountIdToBech32'; - data: { - accountId: AccountId; - bech32Hrp: string; - }; -} - -export interface __AnchorIdToBech32Method__ { - name: 'anchorIdToBech32'; - data: { - anchorId: AnchorId; - bech32Hrp: string; - }; -} - -export interface __NftIdToBech32Method__ { - name: 'nftIdToBech32'; - data: { - nftId: NftId; - bech32Hrp: string; - }; -} - export interface __IsAddressValidMethod__ { name: 'isAddressValid'; data: { diff --git a/bindings/nodejs/lib/utils/utils.ts b/bindings/nodejs/lib/utils/utils.ts index d0f81b51c1..073be7a5bb 100644 --- a/bindings/nodejs/lib/utils/utils.ts +++ b/bindings/nodejs/lib/utils/utils.ts @@ -25,7 +25,6 @@ import { } from '../types'; import { AccountId, - AnchorId, BlockId, FoundryId, NftId, @@ -260,41 +259,6 @@ export class Utils { }); } - /** - * Convert a Bech32 address to a hex-encoded string. - * - * @param bech32 A Bech32 address. - * @returns The hex-encoded string. - */ - static bech32ToHex(bech32: Bech32Address): HexEncodedString { - return callUtilsMethod({ - name: 'bech32ToHex', - data: { - bech32, - }, - }); - } - - /** - * Convert a hex-encoded address string to a Bech32-encoded address string. - * - * @param hex A hex-encoded address string. - * @param bech32Hrp The Bech32 HRP (human readable part) to use. - * @returns The Bech32-encoded address string. - */ - static hexToBech32( - hex: HexEncodedString, - bech32Hrp: string, - ): Bech32Address { - return callUtilsMethod({ - name: 'hexToBech32', - data: { - hex, - bech32Hrp, - }, - }); - } - /** * Converts an address to its bech32 representation. * @@ -312,63 +276,6 @@ export class Utils { }); } - /** - * Transforms an account id to a bech32 encoded address. - * - * @param accountId An account ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to use. - * @returns The Bech32-encoded address string. - */ - static accountIdToBech32( - accountId: AccountId, - bech32Hrp: string, - ): Bech32Address { - return callUtilsMethod({ - name: 'accountIdToBech32', - data: { - accountId, - bech32Hrp, - }, - }); - } - - /** - * Transforms an anchor id to a bech32 encoded address. - * - * @param anchorId An anchor ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to use. - * @returns The Bech32-encoded address string. - */ - static anchorIdToBech32( - anchorId: AnchorId, - bech32Hrp: string, - ): Bech32Address { - return callUtilsMethod({ - name: 'anchorIdToBech32', - data: { - anchorId, - bech32Hrp, - }, - }); - } - - /** - * Convert an NFT ID to a Bech32-encoded address string. - * - * @param nftId An NFT ID. - * @param bech32Hrp The Bech32 HRP (human readable part) to use. - * @returns The Bech32-encoded address string. - */ - static nftIdToBech32(nftId: NftId, bech32Hrp: string): Bech32Address { - return callUtilsMethod({ - name: 'nftIdToBech32', - data: { - nftId, - bech32Hrp, - }, - }); - } - /** * Hashes a hex encoded public key with Blake2b256. * diff --git a/bindings/nodejs/tests/client/outputBuilders.spec.ts b/bindings/nodejs/tests/client/outputBuilders.spec.ts index 074b69700d..b5b1452933 100644 --- a/bindings/nodejs/tests/client/outputBuilders.spec.ts +++ b/bindings/nodejs/tests/client/outputBuilders.spec.ts @@ -33,7 +33,7 @@ describe.skip('Output builder methods', () => { }, }); - const hexAddress = Utils.bech32ToHex(addresses[0]); + const address = Utils.parseBech32Address(addresses[0]); const client = await makeClient(); // most simple basic output @@ -41,7 +41,7 @@ describe.skip('Output builder methods', () => { amount: BigInt(1000000), unlockConditions: [ new AddressUnlockCondition( - new Ed25519Address(hexAddress), + address, ), ], }); @@ -57,7 +57,7 @@ describe.skip('Output builder methods', () => { }, }); - const hexAddress = Utils.bech32ToHex(addresses[0]); + const address = Utils.parseBech32Address(addresses[0]); const client = await makeClient(); const accountId = @@ -67,7 +67,7 @@ describe.skip('Output builder methods', () => { accountId, unlockConditions: [ new AddressUnlockCondition( - new Ed25519Address(hexAddress), + address, ), ], }); @@ -110,14 +110,14 @@ describe.skip('Output builder methods', () => { }, }); - const hexAddress = Utils.bech32ToHex(addresses[0]); + const address = Utils.parseBech32Address(addresses[0]); // most simple nft output const nftOutput = await client.buildNftOutput({ nftId: '0x7ffec9e1233204d9c6dce6812b1539ee96af691ca2e4d9065daa85907d33e5d3', unlockConditions: [ new AddressUnlockCondition( - new Ed25519Address(hexAddress), + address ), ], }); diff --git a/bindings/nodejs/tests/client/utilityMethods.spec.ts b/bindings/nodejs/tests/client/utilityMethods.spec.ts index c1bc80b825..f238292604 100644 --- a/bindings/nodejs/tests/client/utilityMethods.spec.ts +++ b/bindings/nodejs/tests/client/utilityMethods.spec.ts @@ -7,7 +7,7 @@ import 'dotenv/config'; import { Client, SecretManager, Utils } from '../../'; import '../customMatchers'; -import { SlotCommitment } from '../../out/types/block/slot'; +import { AccountAddress, Ed25519Address } from '../../lib'; async function makeOfflineClient(): Promise { return await Client.create({}); @@ -30,19 +30,19 @@ describe('Client utility methods', () => { }); it('converts address to hex and bech32', async () => { - const address = + const bech32address = 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy'; - const hexAddress = Utils.bech32ToHex(address); + const address = Utils.parseBech32Address(bech32address) as Ed25519Address; - expect(hexAddress.slice(0, 2)).toBe('0x'); + expect(address.pubKeyHash.slice(0, 2)).toBe('0x'); let offlineClient = await makeOfflineClient(); - const bech32Address = await offlineClient.hexToBech32( - hexAddress, + const convertedBech32Address = await offlineClient.addressToBech32( + address, 'rms', ); - expect(bech32Address).toBe(address); + expect(convertedBech32Address).toBe(bech32address); }); it('account id to address', async () => { @@ -50,12 +50,12 @@ describe('Client utility methods', () => { '0xcf077d276686ba64c0404b9eb2d15556782113c5a1985f262b70f9964d3bbd7f'; const offlineClient = await makeOfflineClient(); - const accountAddress = await offlineClient.accountIdToBech32( - accountId, + const bech32AccountAddress = await offlineClient.addressToBech32( + new AccountAddress(accountId), 'rms', ); - expect(accountAddress).toBe( + expect(bech32AccountAddress).toBe( 'rms1pr8swlf8v6rt5exqgp9eavk324t8sggnckseshex9dc0n9jd8w7h7wcnhn7', ); }); diff --git a/bindings/python/examples/client/build_account.py b/bindings/python/examples/client/build_account.py index adaf36a53b..bc7369c233 100644 --- a/bindings/python/examples/client/build_account.py +++ b/bindings/python/examples/client/build_account.py @@ -2,7 +2,7 @@ import os from dotenv import load_dotenv -from iota_sdk import (Client, Ed25519Address, AddressUnlockCondition, +from iota_sdk import (Client, AddressUnlockCondition, IssuerFeature, MetadataFeature, SenderFeature, Utils, utf8_to_hex) @@ -14,19 +14,19 @@ # Create a Client instance client = Client(nodes=[node_url]) -hexAddress = Utils.bech32_to_hex( +address = Utils.parse_bech32_address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') account_id = '0x0000000000000000000000000000000000000000000000000000000000000000' unlock_conditions = [ - AddressUnlockCondition(Ed25519Address(hexAddress)), + AddressUnlockCondition(address), ] features = [ - SenderFeature(Ed25519Address(hexAddress)), + SenderFeature(address), MetadataFeature({'data': utf8_to_hex('Hello, World!')}) ] immutable_features = [ - IssuerFeature(Ed25519Address(hexAddress)), + IssuerFeature(address), MetadataFeature({'data': utf8_to_hex('Hello, World!')}) ] diff --git a/bindings/python/examples/client/build_basic.py b/bindings/python/examples/client/build_basic.py index 76bd61e884..00839eab31 100644 --- a/bindings/python/examples/client/build_basic.py +++ b/bindings/python/examples/client/build_basic.py @@ -2,7 +2,7 @@ import os from dotenv import load_dotenv -from iota_sdk import (AddressUnlockCondition, Client, Ed25519Address, +from iota_sdk import (AddressUnlockCondition, Client, ExpirationUnlockCondition, MetadataFeature, SenderFeature, StorageDepositReturnUnlockCondition, TagFeature, TimelockUnlockCondition, Utils, utf8_to_hex) @@ -14,12 +14,10 @@ # Create a Client instance client = Client(nodes=[node_url]) -hex_address = Utils.bech32_to_hex( +address = Utils.parse_bech32_address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') -address_unlock_condition = AddressUnlockCondition( - Ed25519Address(hex_address) -) +address_unlock_condition = AddressUnlockCondition(address) # Build most basic output with amount and a single address unlock condition basic_output = client.build_basic_output( @@ -47,7 +45,7 @@ unlock_conditions=[ address_unlock_condition, StorageDepositReturnUnlockCondition( - return_address=Ed25519Address(hex_address), + return_address=address, amount=1000000 ) ], @@ -60,7 +58,7 @@ unlock_conditions=[ address_unlock_condition, ExpirationUnlockCondition( - return_address=Ed25519Address(hex_address), + return_address=address, slot_index=1 ) ], @@ -96,7 +94,7 @@ address_unlock_condition ], features=[ - SenderFeature(Ed25519Address(hex_address)) + SenderFeature(address) ], amount=1000000, ) diff --git a/bindings/python/examples/client/build_nft.py b/bindings/python/examples/client/build_nft.py index 8100a7feca..85c95677e3 100644 --- a/bindings/python/examples/client/build_nft.py +++ b/bindings/python/examples/client/build_nft.py @@ -5,7 +5,6 @@ from iota_sdk import ( AddressUnlockCondition, Client, - Ed25519Address, IssuerFeature, MetadataFeature, SenderFeature, @@ -22,7 +21,7 @@ # Create a Client instance client = Client(nodes=[node_url]) -hexAddress = Utils.bech32_to_hex( +address = Utils.parse_bech32_address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') tip_27_immutable_metadata = Irc27Metadata( @@ -34,16 +33,16 @@ # Build NFT output nft_output = client.build_nft_output( unlock_conditions=[ - AddressUnlockCondition(Ed25519Address(hexAddress)) + AddressUnlockCondition(address) ], # NftId needs to be null the first time nft_id='0x0000000000000000000000000000000000000000000000000000000000000000', immutable_features=[ - IssuerFeature(Ed25519Address(hexAddress)), + IssuerFeature(address), tip_27_immutable_metadata.as_feature() ], features=[ - SenderFeature(Ed25519Address(hexAddress)), + SenderFeature(address), MetadataFeature(utf8_to_hex('mutable metadata')), TagFeature(utf8_to_hex('my tag')) ] diff --git a/bindings/python/examples/how_tos/account_output/request_funds.py b/bindings/python/examples/how_tos/account_output/request_funds.py index dd414bddd0..63edd41911 100644 --- a/bindings/python/examples/how_tos/account_output/request_funds.py +++ b/bindings/python/examples/how_tos/account_output/request_funds.py @@ -2,7 +2,7 @@ import time from dotenv import load_dotenv -from iota_sdk import Wallet, WalletOptions, Utils, SyncOptions, WalletSyncOptions +from iota_sdk import AccountAddress, Wallet, WalletOptions, Utils, SyncOptions, WalletSyncOptions # In this example we request funds to the wallet's first account output # address. @@ -27,8 +27,8 @@ print(f'Account Id: {account_id}') # Get Account address -account_address = Utils.account_id_to_bech32( - account_id, wallet.get_client().get_bech32_hrp()) +account_address = Utils.address_to_bech32( + AccountAddress(account_id), wallet.get_client().get_bech32_hrp()) faucet_response = wallet.get_client().request_funds_from_faucet( FAUCET_URL, account_address) print(faucet_response) diff --git a/bindings/python/examples/how_tos/account_output/send_amount.py b/bindings/python/examples/how_tos/account_output/send_amount.py index f21d018bd5..50a1730386 100644 --- a/bindings/python/examples/how_tos/account_output/send_amount.py +++ b/bindings/python/examples/how_tos/account_output/send_amount.py @@ -1,7 +1,7 @@ import os from dotenv import load_dotenv -from iota_sdk import Wallet, WalletOptions, Utils, NodeIndexerAPI, SyncOptions, WalletSyncOptions, SendParams +from iota_sdk import AccountAddress, Wallet, WalletOptions, Utils, NodeIndexerAPI, SyncOptions, WalletSyncOptions, SendParams # In this example we send funds from an account output. @@ -26,8 +26,8 @@ print(f'Account Id: {account_id}') # Get account address -account_address = Utils.account_id_to_bech32( - account_id, wallet.get_client().get_bech32_hrp()) +account_address = Utils.address_to_bech32( + AccountAddress(account_id), wallet.get_client().get_bech32_hrp()) # Find first output unlockable by the account address query_parameters = NodeIndexerAPI.BasicOutputQueryParameters( diff --git a/bindings/python/examples/how_tos/advanced_transactions/advanced_transaction.py b/bindings/python/examples/how_tos/advanced_transactions/advanced_transaction.py index d01b938590..f7151d8c20 100644 --- a/bindings/python/examples/how_tos/advanced_transactions/advanced_transaction.py +++ b/bindings/python/examples/how_tos/advanced_transactions/advanced_transaction.py @@ -6,7 +6,6 @@ from iota_sdk import ( AddressUnlockCondition, Client, - Ed25519Address, Wallet, WalletOptions, Utils, @@ -39,8 +38,8 @@ basic_output = Client().build_basic_output( unlock_conditions=[ AddressUnlockCondition( - Ed25519Address( - Utils.bech32_to_hex('rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy')) + Utils.parse_bech32_address( + 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') ), TimelockUnlockCondition(in_an_hour), ], diff --git a/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py b/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py index 7350e8cde2..53a05f3a50 100644 --- a/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py +++ b/bindings/python/examples/how_tos/nft_collection/01_mint_collection_nft.py @@ -2,7 +2,7 @@ import sys from dotenv import load_dotenv -from iota_sdk import MintNftParams, Utils, Wallet, WalletOptions, Irc27Metadata +from iota_sdk import HexStr, MintNftParams, NFTAddress, Utils, Wallet, WalletOptions, Irc27Metadata load_dotenv() @@ -16,7 +16,7 @@ if len(sys.argv) < 2: raise Exception("missing example argument: ISSUER_NFT_ID") -issuer_nft_id = sys.argv[1] +issuer_nft_id = HexStr(sys.argv[1]) wallet = Wallet(WalletOptions(storage_path=os.environ.get('WALLET_DB_PATH'))) @@ -29,12 +29,12 @@ wallet.sync() bech32_hrp = wallet.get_client().get_bech32_hrp() -issuer = Utils.nft_id_to_bech32(issuer_nft_id, bech32_hrp) +issuer = Utils.address_to_bech32(NFTAddress(issuer_nft_id), bech32_hrp) def get_immutable_metadata(index: int) -> str: """Returns the immutable metadata for the NFT with the given index""" - Irc27Metadata( + return Irc27Metadata( "video/mp4", "https://ipfs.io/ipfs/QmPoYcVm9fx47YXNTkhpMEYSxCD3Bqh7PJYr7eo5YjLgiT", "Shimmer OG NFT #" + str(index), diff --git a/bindings/python/examples/how_tos/outputs/features.py b/bindings/python/examples/how_tos/outputs/features.py index a3bb5482b9..210e82623d 100644 --- a/bindings/python/examples/how_tos/outputs/features.py +++ b/bindings/python/examples/how_tos/outputs/features.py @@ -5,7 +5,6 @@ from iota_sdk import ( AddressUnlockCondition, Client, - Ed25519Address, Utils, SenderFeature, IssuerFeature, @@ -18,12 +17,10 @@ client = Client() -hex_address = Utils.bech32_to_hex( +address = Utils.parse_bech32_address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') -address_unlock_condition = AddressUnlockCondition( - Ed25519Address(hex_address) -) +address_unlock_condition = AddressUnlockCondition(address) # Output with sender feature nft_output = client.build_nft_output( @@ -32,7 +29,7 @@ address_unlock_condition ], features=[ - SenderFeature(Ed25519Address(hex_address)) + SenderFeature(address) ], ) outputs = [nft_output] @@ -44,7 +41,7 @@ address_unlock_condition, ], immutable_features=[ - IssuerFeature(Ed25519Address(hex_address)) + IssuerFeature(address) ], ) outputs.append(nft_output) diff --git a/bindings/python/examples/how_tos/outputs/unlock_conditions.py b/bindings/python/examples/how_tos/outputs/unlock_conditions.py index 01ee5db20b..e0d349cc8c 100644 --- a/bindings/python/examples/how_tos/outputs/unlock_conditions.py +++ b/bindings/python/examples/how_tos/outputs/unlock_conditions.py @@ -3,9 +3,7 @@ from dotenv import load_dotenv from iota_sdk import ( AddressUnlockCondition, - AccountAddress, Client, - Ed25519Address, Utils, ExpirationUnlockCondition, SimpleTokenScheme, @@ -18,15 +16,13 @@ client = Client() -hex_address = Utils.bech32_to_hex( +ed25519_address = Utils.parse_bech32_address( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') -account_hex_address = Utils.bech32_to_hex( +account_address = Utils.parse_bech32_address( 'rms1pr59qm43mjtvhcajfmupqf23x29llam88yecn6pyul80rx099krmv2fnnux') -address_unlock_condition = AddressUnlockCondition( - Ed25519Address(hex_address) -) +address_unlock_condition = AddressUnlockCondition(ed25519_address) token_scheme = SimpleTokenScheme(50, 0, 100) @@ -42,7 +38,7 @@ address_unlock_condition, StorageDepositReturnUnlockCondition( 1000000, - Ed25519Address(hex_address), + ed25519_address, ), ], ) @@ -63,7 +59,7 @@ address_unlock_condition, ExpirationUnlockCondition( 1, - Ed25519Address(hex_address), + ed25519_address, ), ], ) @@ -75,7 +71,7 @@ token_scheme=token_scheme, unlock_conditions=[ ImmutableAccountAddressUnlockCondition( - AccountAddress(account_hex_address), + account_address, ), ], ) diff --git a/bindings/python/examples/how_tos/wallet/consolidate_outputs.py b/bindings/python/examples/how_tos/wallet/consolidate_outputs.py index efb0edb804..1d623980dd 100644 --- a/bindings/python/examples/how_tos/wallet/consolidate_outputs.py +++ b/bindings/python/examples/how_tos/wallet/consolidate_outputs.py @@ -32,7 +32,7 @@ for i, output_data in enumerate(outputs): print(f'OUTPUT #{i}') print( - f'- address: #{Utils.hex_to_bech32(output_data.address.pub_key_hash, "rms")}') + f'- address: #{Utils.address_to_bech32(output_data.address, "rms")}') print(f'- amount: #{output_data.output.amount}') native_tokens = [ @@ -65,7 +65,7 @@ for i, output_data in enumerate(outputs): print(f'OUTPUT #{i}') print( - f'- address: #{Utils.hex_to_bech32(output_data.address.pub_key_hash, "rms")}') + f'- address: #{Utils.address_to_bech32(output_data.address, "rms")}') print(f'- amount: #{output_data.output.amount}') native_tokens = [ diff --git a/bindings/python/examples/wallet/13-check-unlock-conditions.py b/bindings/python/examples/wallet/13-check-unlock-conditions.py index bf354557e9..abe6b6ddb1 100644 --- a/bindings/python/examples/wallet/13-check-unlock-conditions.py +++ b/bindings/python/examples/wallet/13-check-unlock-conditions.py @@ -15,13 +15,13 @@ output = wallet.prepare_output(OutputParams( address, 1000000)) -hexEncodedWalletAddress = Utils.bech32_to_hex(address) +walletAddress = Utils.parse_bech32_address(address) controlled_by_wallet = False if len( output.unlock_conditions) == 1 and output.unlock_conditions[0].type == 0: - if output.unlock_conditions[0].address.pub_key_hash == hexEncodedWalletAddress: + if output.unlock_conditions[0].address.pub_key_hash == walletAddress.pub_key_hash: controlled_by_wallet = True print( diff --git a/bindings/python/iota_sdk/client/_utils.py b/bindings/python/iota_sdk/client/_utils.py index f829cb74cc..6a68fcc9c3 100644 --- a/bindings/python/iota_sdk/client/_utils.py +++ b/bindings/python/iota_sdk/client/_utils.py @@ -6,7 +6,6 @@ from iota_sdk.types.address import Address 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 @@ -35,16 +34,6 @@ def _call_method(self, name, data=None): no payload. """ - # pylint: disable=redefined-builtin - def hex_to_bech32(self, hex_str: HexStr, - bech32_hrp: Optional[str] = None) -> str: - """Transforms a hex encoded address to a bech32 encoded address. - """ - return self._call_method('hexToBech32', { - 'hex': hex_str, - 'bech32Hrp': bech32_hrp - }) - def address_to_bech32(self, address: Address, bech32_hrp: Optional[str] = None) -> str: """Converts an address to its bech32 representation. @@ -54,33 +43,6 @@ def address_to_bech32(self, address: Address, 'bech32Hrp': bech32_hrp }) - def account_id_to_bech32(self, account_id: HexStr, - bech32_hrp: Optional[str] = None) -> str: - """Transforms an account id to a bech32 encoded address. - """ - return self._call_method('accountIdToBech32', { - 'accountId': account_id, - 'bech32Hrp': bech32_hrp - }) - - def anchor_id_to_bech32(self, anchor_id: HexStr, - bech32_hrp: Optional[str] = None) -> str: - """Transforms an anchor id to a bech32 encoded address. - """ - return self._call_method('anchorIdToBech32', { - 'anchorId': anchor_id, - 'bech32Hrp': bech32_hrp - }) - - def nft_id_to_bech32(self, nft_id: HexStr, - bech32_hrp: Optional[str] = None) -> str: - """Transforms an nft id to a bech32 encoded address. - """ - return self._call_method('nftIdToBech32', { - 'nftId': nft_id, - 'bech32Hrp': bech32_hrp - }) - def computer_minimum_output_amount(self, output: Output) -> int: """Minimum required output amount. """ diff --git a/bindings/python/iota_sdk/utils.py b/bindings/python/iota_sdk/utils.py index 7a50f7a0b5..02f27c60fe 100644 --- a/bindings/python/iota_sdk/utils.py +++ b/bindings/python/iota_sdk/utils.py @@ -29,25 +29,6 @@ class Utils: """Utility functions. """ - - @staticmethod - def bech32_to_hex(bech32: str) -> HexStr: - """Convert a Bech32 string to a hex string. - """ - return _call_method('bech32ToHex', { - 'bech32': bech32 - }) - - # pylint: disable=redefined-builtin - @staticmethod - def hex_to_bech32(hex_str: HexStr, bech32_hrp: str) -> str: - """Convert a hex encoded address to a Bech32 encoded address. - """ - return _call_method('hexToBech32', { - 'hex': hex_str, - 'bech32Hrp': bech32_hrp - }) - @staticmethod def address_to_bech32(address: Address, bech32_hrp: str) -> str: """Convert an address to its bech32 representation. @@ -57,33 +38,6 @@ def address_to_bech32(address: Address, bech32_hrp: str) -> str: 'bech32Hrp': bech32_hrp }) - @staticmethod - def account_id_to_bech32(account_id: HexStr, bech32_hrp: str) -> str: - """Convert an account id to a Bech32 encoded address. - """ - return _call_method('accountIdToBech32', { - 'accountId': account_id, - 'bech32Hrp': bech32_hrp - }) - - @staticmethod - def anchor_id_to_bech32(anchor_id: HexStr, bech32_hrp: str) -> str: - """Convert an anchor id to a Bech32 encoded address. - """ - return _call_method('anchorIdToBech32', { - 'anchorId': anchor_id, - 'bech32Hrp': bech32_hrp - }) - - @staticmethod - def nft_id_to_bech32(nft_id: HexStr, bech32_hrp: str) -> str: - """Convert an NFT ID to a Bech32 encoded address. - """ - return _call_method('nftIdToBech32', { - 'nftId': nft_id, - 'bech32Hrp': bech32_hrp - }) - # pylint: disable=redefined-builtin @staticmethod def public_key_hash( diff --git a/bindings/wasm/tests/utilityMethods.spec.ts b/bindings/wasm/tests/utilityMethods.spec.ts index 34f677b70d..fbcf791456 100644 --- a/bindings/wasm/tests/utilityMethods.spec.ts +++ b/bindings/wasm/tests/utilityMethods.spec.ts @@ -1,4 +1,4 @@ -import { Utils } from '../node/lib'; +import { AccountAddress, Ed25519Address, Utils } from '../node/lib'; describe('Utils methods', () => { it('generates and validates mnemonic', async () => { @@ -17,15 +17,15 @@ describe('Utils methods', () => { }); it('converts address to hex and bech32', async () => { - const address = + const bech32Address = 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy'; - const hexAddress = Utils.bech32ToHex(address); + const address = Utils.parseBech32Address(bech32Address) as Ed25519Address; - expect(hexAddress.slice(0, 2)).toBe('0x'); + expect(address.pubKeyHash.slice(0, 2)).toBe('0x'); - const bech32Address = Utils.hexToBech32(hexAddress, 'rms'); + const convertedBech32Address = Utils.addressToBech32(address, 'rms'); - expect(bech32Address).toBe(address); + expect(convertedBech32Address).toBe(bech32Address); }); it('converts hex public key to bech32 address', async () => { @@ -63,7 +63,7 @@ describe('Utils methods', () => { const accountId = '0x0ebc2867a240719a70faacdfc3840e857fa450b37d95297ac4f166c2f70c3345'; - const accountAddress = Utils.accountIdToBech32(accountId, 'rms'); + const accountAddress = Utils.addressToBech32(new AccountAddress(accountId), 'rms'); expect(accountAddress).toBe( 'rms1pq8tc2r85fq8rxnsl2kdlsuyp6zhlfzskd7e22t6cnckdshhpse52a27mlc', diff --git a/sdk/src/client/utils.rs b/sdk/src/client/utils.rs index 1db387bab3..fc705f0137 100644 --- a/sdk/src/client/utils.rs +++ b/sdk/src/client/utils.rs @@ -19,33 +19,12 @@ use crate::{ client::ClientError, types::block::{ address::{Address, Bech32Address, Ed25519Address, Hrp, ToBech32Ext}, - output::{AccountId, AnchorId, NftId}, payload::TaggedDataPayload, Block, BlockId, }, utils::ConvertTo, }; -/// Transforms bech32 to hex -pub fn bech32_to_hex(bech32: impl ConvertTo) -> Result { - Ok(match bech32.convert()?.inner() { - Address::Ed25519(ed) => ed.to_string(), - Address::Account(account) => account.to_string(), - Address::Nft(nft) => nft.to_string(), - Address::Anchor(anchor) => anchor.to_string(), - Address::ImplicitAccountCreation(implicit) => implicit.to_string(), - Address::Multi(multi) => multi.to_string(), - Address::Restricted(restricted) => restricted.to_string(), - }) -} - -/// Transforms a hex encoded address to a bech32 encoded address -pub fn hex_to_bech32(hex: &str, bech32_hrp: impl ConvertTo) -> Result { - let address = hex.parse::()?; - - Ok(Address::Ed25519(address).try_to_bech32(bech32_hrp)?) -} - /// Transforms a prefix hex encoded public key to a bech32 encoded address pub fn hex_public_key_to_bech32_address( hex: &str, @@ -108,18 +87,6 @@ pub async fn request_funds_from_faucet(url: &str, bech32_address: &Bech32Address } impl Client { - /// Transforms a hex encoded address to a bech32 encoded address - pub async fn hex_to_bech32( - &self, - hex: &str, - bech32_hrp: Option>, - ) -> Result { - match bech32_hrp { - Some(hrp) => Ok(hex_to_bech32(hex, hrp)?), - None => Ok(hex_to_bech32(hex, self.get_bech32_hrp().await?)?), - } - } - /// Converts an address to its bech32 representation pub async fn address_to_bech32( &self, @@ -132,42 +99,6 @@ impl Client { } } - /// Transforms an account id to a bech32 encoded address - pub async fn account_id_to_bech32( - &self, - account_id: AccountId, - bech32_hrp: Option>, - ) -> Result { - match bech32_hrp { - Some(hrp) => Ok(account_id.to_bech32(hrp.convert()?)), - None => Ok(account_id.to_bech32(self.get_bech32_hrp().await?)), - } - } - - /// Transforms an anchor id to a bech32 encoded address - pub async fn anchor_id_to_bech32( - &self, - anchor_id: AnchorId, - bech32_hrp: Option>, - ) -> Result { - match bech32_hrp { - Some(hrp) => Ok(anchor_id.to_bech32(hrp.convert()?)), - None => Ok(anchor_id.to_bech32(self.get_bech32_hrp().await?)), - } - } - - /// Transforms an nft id to a bech32 encoded address - pub async fn nft_id_to_bech32( - &self, - nft_id: NftId, - bech32_hrp: Option>, - ) -> Result { - match bech32_hrp { - Some(hrp) => Ok(nft_id.to_bech32(hrp.convert()?)), - None => Ok(nft_id.to_bech32(self.get_bech32_hrp().await?)), - } - } - /// Transforms a hex encoded public key to a bech32 encoded address pub async fn hex_public_key_to_bech32_address( &self, @@ -180,11 +111,6 @@ impl Client { } } - /// Transforms bech32 to hex - pub fn bech32_to_hex(bech32: impl ConvertTo) -> Result { - bech32_to_hex(bech32) - } - /// Generates a new mnemonic. pub fn generate_mnemonic() -> Result { generate_mnemonic() From a61fc007993fecd3f324d0e3b61f93ff3bc87731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Thu, 7 Mar 2024 14:45:26 +0100 Subject: [PATCH 2/2] Fix casing --- bindings/python/examples/wallet/13-check-unlock-conditions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/python/examples/wallet/13-check-unlock-conditions.py b/bindings/python/examples/wallet/13-check-unlock-conditions.py index abe6b6ddb1..66538afcc6 100644 --- a/bindings/python/examples/wallet/13-check-unlock-conditions.py +++ b/bindings/python/examples/wallet/13-check-unlock-conditions.py @@ -15,13 +15,13 @@ output = wallet.prepare_output(OutputParams( address, 1000000)) -walletAddress = Utils.parse_bech32_address(address) +wallet_address = Utils.parse_bech32_address(address) controlled_by_wallet = False if len( output.unlock_conditions) == 1 and output.unlock_conditions[0].type == 0: - if output.unlock_conditions[0].address.pub_key_hash == walletAddress.pub_key_hash: + if output.unlock_conditions[0].address.pub_key_hash == wallet_address.pub_key_hash: controlled_by_wallet = True print(