Skip to content

Commit

Permalink
Remove redundant address conversion methods (#2151)
Browse files Browse the repository at this point in the history
* Remove Bech32ToHex, HexToBech32, AccountIdToBech32, AnchorIdToBech32, NftIdToBech32

* Fix casing

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Mar 7, 2024
1 parent 23631d8 commit b0baecd
Show file tree
Hide file tree
Showing 41 changed files with 134 additions and 675 deletions.
32 changes: 0 additions & 32 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hrp>,
},
/// Converts an address to its bech32 representation
#[serde(rename_all = "camelCase")]
AddressToBech32 { address: Address, bech32_hrp: Option<Hrp> },
/// 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<Hrp>,
},
/// 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<Hrp>,
},
/// 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<Hrp>,
},
/// Calculate the minimum required amount for an output.
/// Expected response:
/// [`Amount`](crate::Response::Amount)
Expand Down
30 changes: 1 addition & 29 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 0 additions & 12 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
12 changes: 1 addition & 11 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -27,17 +27,7 @@ use crate::{method::UtilsMethod, response::Response};
/// Call a utils method.
pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response, crate::Error> {
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()),
Expand Down
12 changes: 0 additions & 12 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions bindings/core/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ async fn utils() -> Result<(), Box<dyn std::error::Error>> {

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) => {
Expand Down
14 changes: 5 additions & 9 deletions bindings/nodejs/examples/client/11-build-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SenderFeature,
TagFeature,
StorageDepositReturnUnlockCondition,
Ed25519Address,
ExpirationUnlockCondition,
TimelockUnlockCondition,
utf8ToHex,
Expand All @@ -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({
Expand All @@ -68,7 +67,7 @@ async function run() {
unlockConditions: [
addressUnlockCondition,
new StorageDepositReturnUnlockCondition(
new Ed25519Address(hexAddress),
ed25519Address,
'1000000',
),
],
Expand All @@ -81,10 +80,7 @@ async function run() {
amount: BigInt(1000000),
unlockConditions: [
addressUnlockCondition,
new ExpirationUnlockCondition(
new Ed25519Address(hexAddress),
1,
),
new ExpirationUnlockCondition(ed25519Address, 1),
],
});

Expand Down Expand Up @@ -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));
Expand Down
11 changes: 4 additions & 7 deletions bindings/nodejs/examples/client/13-build-account-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Utils,
MetadataFeature,
SenderFeature,
Ed25519Address,
IssuerFeature,
AddressUnlockCondition,
utf8ToHex,
Expand All @@ -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') }),
],
});
Expand Down
11 changes: 4 additions & 7 deletions bindings/nodejs/examples/client/15-build-nft-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TagFeature,
MetadataFeature,
SenderFeature,
Ed25519Address,
IssuerFeature,
Irc27Metadata,
} from '@iota/sdk';
Expand All @@ -34,7 +33,7 @@ async function run() {
});

try {
const hexAddress = Utils.bech32ToHex(
const ed25519Address = Utils.parseBech32Address(
'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy',
);

Expand All @@ -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'),
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
//
Expand Down Expand Up @@ -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(),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
//
Expand Down Expand Up @@ -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(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import {
AddressUnlockCondition,
Ed25519Address,
TimelockUnlockCondition,
Utils,
Wallet,
Expand Down Expand Up @@ -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),
Expand Down
Loading

0 comments on commit b0baecd

Please sign in to comment.