Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hexPublicKeyToBech32Address by publicKeyHash #2135

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,6 @@ pub enum ClientMethod {
/// Human readable part
bech32_hrp: Option<Hrp>,
},
/// Transforms a hex encoded public key to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexPublicKeyToBech32Address {
/// Hex encoded public key
hex: String,
/// Human readable part
bech32_hrp: Option<Hrp>,
},
/// Calculate the minimum required amount for an output.
/// Expected response:
/// [`Amount`](crate::Response::Amount)
Expand Down
20 changes: 5 additions & 15 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use iota_sdk::{
unlock::Unlock,
BlockDto,
},
utils::serde::{option_mana_rewards, string},
utils::serde::{option_mana_rewards, prefix_hex_bytes, string},
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -65,12 +65,6 @@ pub enum UtilsMethod {
nft_id: NftId,
bech32_hrp: Hrp,
},
/// Transforms a hex encoded public key to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexPublicKeyToBech32Address {
hex: String,
bech32_hrp: Hrp,
},
/// Returns a valid Address parsed from a String.
ParseBech32Address {
address: Bech32Address,
Expand All @@ -96,10 +90,11 @@ pub enum UtilsMethod {
TransactionId {
payload: SignedTransactionPayloadDto,
},
/// Computes the account ID
/// Computes the Blake2b256 hash of the provided hex encoded bytes.
#[serde(rename_all = "camelCase")]
ComputeAccountId {
output_id: OutputId,
Blake2b256Hash {
#[serde(with = "prefix_hex_bytes")]
bytes: Vec<u8>,
},
/// Computes the Foundry ID
#[serde(rename_all = "camelCase")]
Expand All @@ -108,11 +103,6 @@ pub enum UtilsMethod {
serial_number: u32,
token_scheme_type: u8,
},
/// Computes the NFT ID
#[serde(rename_all = "camelCase")]
ComputeNftId {
output_id: OutputId,
},
/// Computes the output ID from transaction id and output index
ComputeOutputId {
id: TransactionId,
Expand Down
3 changes: 0 additions & 3 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ pub(crate) async fn call_client_method_internal(
ClientMethod::NftIdToBech32 { nft_id, bech32_hrp } => {
Response::Bech32Address(client.nft_id_to_bech32(nft_id, bech32_hrp).await?)
}
ClientMethod::HexPublicKeyToBech32Address { hex, bech32_hrp } => {
Response::Bech32Address(client.hex_public_key_to_bech32_address(&hex, bech32_hrp).await?)
}
ClientMethod::ComputeMinimumOutputAmount { output } => {
let storage_score_params = client.get_storage_score_parameters().await?;

Expand Down
16 changes: 8 additions & 8 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crypto::keys::bip39::Mnemonic;
use crypto::{
hashes::{blake2b::Blake2b256, Digest},
keys::bip39::Mnemonic,
};
use iota_sdk::{
client::{hex_public_key_to_bech32_address, hex_to_bech32, verify_mnemonic, Client},
client::{hex_to_bech32, verify_mnemonic, Client},
types::{
block::{
address::{AccountAddress, Address, ToBech32Ext},
input::UtxoInput,
output::{AccountId, FoundryId, MinimumOutputAmount, NftId, Output, OutputId, TokenId},
output::{FoundryId, MinimumOutputAmount, Output, OutputId, TokenId},
payload::{signed_transaction::Transaction, SignedTransactionPayload},
semantic::SemanticValidationContext,
signature::SignatureError,
Expand All @@ -34,9 +37,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
Response::Bech32Address(anchor_id.to_bech32(bech32_hrp))
}
UtilsMethod::NftIdToBech32 { nft_id, bech32_hrp } => Response::Bech32Address(nft_id.to_bech32(bech32_hrp)),
UtilsMethod::HexPublicKeyToBech32Address { hex, bech32_hrp } => {
Response::Bech32Address(hex_public_key_to_bech32_address(&hex, 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 All @@ -55,7 +56,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let payload = SignedTransactionPayload::try_from_dto(payload)?;
Response::TransactionId(payload.transaction().id())
}
UtilsMethod::ComputeAccountId { output_id } => Response::AccountId(AccountId::from(&output_id)),
UtilsMethod::Blake2b256Hash { bytes } => Response::Hash(prefix_hex::encode(Blake2b256::digest(bytes).to_vec())),
UtilsMethod::ComputeFoundryId {
account_id,
serial_number,
Expand All @@ -65,7 +66,6 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
serial_number,
token_scheme_type,
)),
UtilsMethod::ComputeNftId { output_id } => Response::NftId(NftId::from(&output_id)),
UtilsMethod::ComputeOutputId { id, index } => Response::OutputId(OutputId::new(id, index)),
UtilsMethod::ComputeTokenId {
account_id,
Expand Down
10 changes: 2 additions & 8 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use iota_sdk::{
block::{
address::{Address, Bech32Address, Hrp},
input::UtxoInput,
output::{AccountId, DecayedMana, FoundryId, NftId, Output, OutputId, OutputMetadata, TokenId},
output::{DecayedMana, FoundryId, Output, OutputId, OutputMetadata, TokenId},
payload::{dto::SignedTransactionPayloadDto, signed_transaction::TransactionId},
protocol::ProtocolParameters,
signature::Ed25519Signature,
Expand Down Expand Up @@ -213,15 +213,10 @@ pub enum Response {
/// - [`TransactionId`](crate::method::UtilsMethod::TransactionId)
TransactionId(TransactionId),
/// Response for:
/// - [`ComputeAccountId`](crate::method::UtilsMethod::ComputeAccountId)
AccountId(AccountId),
/// Response for:
/// - [`ComputeNftId`](crate::method::UtilsMethod::ComputeNftId)
NftId(NftId),
/// Response for:
/// - [`ComputeFoundryId`](crate::method::UtilsMethod::ComputeFoundryId)
FoundryId(FoundryId),
/// Response for:
/// - [`Blake2b256Hash`](crate::method::UtilsMethod::Blake2b256Hash)
/// - [`TransactionSigningHash`](crate::method::UtilsMethod::TransactionSigningHash)
Hash(String),
/// Response for [`Bech32ToHex`](crate::method::UtilsMethod::Bech32ToHex)
Expand Down Expand Up @@ -251,7 +246,6 @@ pub enum Response {
/// - [`AnchorIdToBech32`](crate::method::UtilsMethod::AnchorIdToBech32)
/// - [`NftIdToBech32`](crate::method::ClientMethod::NftIdToBech32)
/// - [`NftIdToBech32`](crate::method::UtilsMethod::NftIdToBech32)
/// - [`HexPublicKeyToBech32Address`](crate::method::ClientMethod::HexPublicKeyToBech32Address)
/// - [`HexToBech32`](crate::method::ClientMethod::HexToBech32)
/// - [`ImplicitAccountCreationAddress`](crate::method::WalletMethod::ImplicitAccountCreationAddress)
Bech32Address(Bech32Address),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async function run() {
`Public key: ${ed25519Signature.publicKey}\nSignature: ${ed25519Signature.signature}`,
);

const bech32Address = Utils.hexPublicKeyToBech32Address(
ed25519Signature.publicKey,
const bech32Address = Utils.addressToBech32(
Utils.publicKeyHash(ed25519Signature.publicKey),
'rms',
);
console.log('Address: ' + bech32Address);
Expand Down
22 changes: 0 additions & 22 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,28 +946,6 @@ export class Client {
return JSON.parse(response).payload;
}

/**
* Convert a hex encoded public key to a Bech32 encoded address.
*
* @param hex The hexadecimal string representation of a public key.
* @param bech32Hrp The Bech32 HRP (human readable part) to be used.
* @returns The corresponding Bech32 address.
*/
async hexPublicKeyToBech32Address(
hex: HexEncodedString,
bech32Hrp?: string,
): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'hexPublicKeyToBech32Address',
data: {
hex,
bech32Hrp,
},
});

return JSON.parse(response).payload;
}

/**
* Return the unhealthy nodes.
*/
Expand Down
8 changes: 0 additions & 8 deletions bindings/nodejs/lib/types/client/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,6 @@ export interface __NftIdToBech32Method__ {
};
}

export interface __HexPublicKeyToBech32AddressMethod__ {
name: 'hexPublicKeyToBech32Address';
data: {
hex: HexEncodedString;
bech32Hrp?: string;
};
}

export interface __FindBlocksMethod__ {
name: 'findBlocks';
data: {
Expand Down
2 changes: 0 additions & 2 deletions bindings/nodejs/lib/types/client/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import type {
__AccountIdToBech32Method__,
__AnchorIdToBech32Method__,
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__AccountOutputIdsMethod__,
__AccountOutputIdMethod__,
__AnchorOutputIdsMethod__,
Expand Down Expand Up @@ -121,7 +120,6 @@ export type __ClientMethods__ =
| __AccountIdToBech32Method__
| __AnchorIdToBech32Method__
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __AccountOutputIdsMethod__
| __AccountOutputIdMethod__
| __AnchorOutputIdsMethod__
Expand Down
8 changes: 2 additions & 6 deletions bindings/nodejs/lib/types/utils/bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {
__GenerateMnemonicMethod__,
__MnemonicToHexSeedMethod__,
__ComputeAccountIdMethod__,
__ComputeOutputIdMethod__,
__ComputeTokenIdMethod__,
__ComputeNftIdMethod__,
__ComputeFoundryIdMethod__,
__ComputeMinimumOutputAmountMethod__,
__ParseBech32AddressMethod__,
__Blake2b256HashMethod__,
__BlockIdMethod__,
__TransactionIdMethod__,
__Bech32ToHexMethod__,
Expand All @@ -16,7 +15,6 @@ import type {
__AccountIdToBech32Method__,
__AnchorIdToBech32Method__,
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__IsAddressValidMethod__,
__ProtocolParametersHashMethod__,
__TransactionSigningHashMethod__,
Expand All @@ -40,13 +38,12 @@ import type {
export type __UtilsMethods__ =
| __GenerateMnemonicMethod__
| __MnemonicToHexSeedMethod__
| __ComputeAccountIdMethod__
| __ComputeNftIdMethod__
| __ComputeFoundryIdMethod__
| __ComputeOutputIdMethod__
| __ComputeTokenIdMethod__
| __ComputeMinimumOutputAmountMethod__
| __ParseBech32AddressMethod__
| __Blake2b256HashMethod__
| __BlockIdMethod__
| __TransactionIdMethod__
| __Bech32ToHexMethod__
Expand All @@ -55,7 +52,6 @@ export type __UtilsMethods__ =
| __AccountIdToBech32Method__
| __AnchorIdToBech32Method__
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __IsAddressValidMethod__
| __ProtocolParametersHashMethod__
| __TransactionSigningHashMethod__
Expand Down
21 changes: 3 additions & 18 deletions bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ export interface __MnemonicToHexSeedMethod__ {
};
}

export interface __ComputeAccountIdMethod__ {
name: 'computeAccountId';
data: {
outputId: OutputId;
};
}

export interface __ComputeFoundryIdMethod__ {
name: 'computeFoundryId';
data: {
Expand All @@ -47,10 +40,10 @@ export interface __ComputeFoundryIdMethod__ {
};
}

export interface __ComputeNftIdMethod__ {
name: 'computeNftId';
export interface __Blake2b256HashMethod__ {
name: 'blake2b256Hash';
data: {
outputId: OutputId;
bytes: HexEncodedString;
};
}

Expand Down Expand Up @@ -148,14 +141,6 @@ export interface __NftIdToBech32Method__ {
};
}

export interface __HexPublicKeyToBech32AddressMethod__ {
name: 'hexPublicKeyToBech32Address';
data: {
hex: HexEncodedString;
bech32Hrp: string;
};
}

export interface __IsAddressValidMethod__ {
name: 'isAddressValid';
data: {
Expand Down
36 changes: 17 additions & 19 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Unlock,
DecayedMana,
NumericString,
Ed25519Address,
} from '../types';
import {
AccountId,
Expand Down Expand Up @@ -70,9 +71,9 @@ export class Utils {
*/
static computeAccountId(outputId: OutputId): AccountId {
return callUtilsMethod({
name: 'computeAccountId',
name: 'blake2b256Hash',
data: {
outputId,
bytes: outputId,
},
});
}
Expand Down Expand Up @@ -108,9 +109,9 @@ export class Utils {
*/
static computeNftId(outputId: OutputId): NftId {
return callUtilsMethod({
name: 'computeNftId',
name: 'blake2b256Hash',
data: {
outputId,
bytes: outputId,
},
});
}
Expand Down Expand Up @@ -369,23 +370,20 @@ export class Utils {
}

/**
* Convert a hex-encoded public key to a Bech32-encoded address string.
* Hashes a hex encoded public key with Blake2b256.
*
* @param hex A hex-encoded public key.
* @param bech32Hrp The Bech32 HRP (human readable part) to use.
* @returns The Bech32-encoded address string.
* @param hex The hexadecimal string representation of a public key.
* @returns The Ed25519 address with the hashed public key.
*/
static hexPublicKeyToBech32Address(
hex: HexEncodedString,
bech32Hrp: string,
): Bech32Address {
return callUtilsMethod({
name: 'hexPublicKeyToBech32Address',
data: {
hex,
bech32Hrp,
},
});
static publicKeyHash(hex: HexEncodedString): Ed25519Address {
return new Ed25519Address(
callUtilsMethod({
name: 'blake2b256Hash',
data: {
bytes: hex,
},
}),
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/tests/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe('Utils methods', () => {
const hexPublicKey =
'0x2baaf3bca8ace9f862e60184bd3e79df25ff230f7eaaa4c7f03daa9833ba854a';

const address = Utils.hexPublicKeyToBech32Address(hexPublicKey, 'rms');
const address = Utils.publicKeyHash(hexPublicKey);

expect(address).toBeValidAddress();
expect(address.pubKeyHash).toBe('0x96f9de0989e77d0e150e850a5a600e83045fa57419eaf3b20225b763d4e23813');
});

it('validates address', () => {
Expand Down
Loading
Loading