Skip to content

Commit

Permalink
Merge branch '2.0' of https://github.com/iotaledger/iota-sdk into 500…
Browse files Browse the repository at this point in the history
…-error-patch
  • Loading branch information
marc2332 committed Feb 22, 2024
2 parents fea61fb + 6b3e64f commit 974377d
Show file tree
Hide file tree
Showing 28 changed files with 259 additions and 117 deletions.
12 changes: 6 additions & 6 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub fn init_logger(config: String) -> std::result::Result<(), fern_logger::Error
#[serde(rename_all = "camelCase")]
pub struct WalletOptions {
pub address: Option<Bech32Address>,
pub alias: Option<String>,
#[serde(with = "option_bip44", default)]
pub bip_path: Option<Bip44>,
pub alias: Option<String>,
pub client_options: Option<ClientOptions>,
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
pub secret_manager: Option<SecretManagerDto>,
Expand All @@ -60,13 +60,13 @@ impl WalletOptions {
self
}

pub fn with_alias(mut self, alias: impl Into<Option<String>>) -> Self {
self.alias = alias.into();
pub fn with_bip_path(mut self, bip_path: impl Into<Option<Bip44>>) -> Self {
self.bip_path = bip_path.into();
self
}

pub fn with_bip_path(mut self, bip_path: impl Into<Option<Bip44>>) -> Self {
self.bip_path = bip_path.into();
pub fn with_alias(mut self, alias: impl Into<Option<String>>) -> Self {
self.alias = alias.into();
self
}

Expand All @@ -90,8 +90,8 @@ impl WalletOptions {
log::debug!("wallet options: {self:?}");
let mut builder = Wallet::builder()
.with_address(self.address)
.with_alias(self.alias)
.with_bip_path(self.bip_path)
.with_alias(self.alias)
.with_client_options(self.client_options);

#[cfg(feature = "storage")]
Expand Down
8 changes: 8 additions & 0 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ pub enum ClientMethod {
/// 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 {
Expand Down
5 changes: 4 additions & 1 deletion 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::{Bech32Address, Hrp},
output::{AccountId, NftId, Output, OutputId, StorageScoreParameters},
output::{AccountId, AnchorId, NftId, Output, OutputId, StorageScoreParameters},
payload::signed_transaction::{
dto::{SignedTransactionPayloadDto, TransactionDto},
TransactionId,
Expand Down Expand Up @@ -39,6 +39,9 @@ pub enum UtilsMethod {
/// 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 },
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
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?)
}
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
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::HexPublicKeyToBech32Address { hex, bech32_hrp } => {
Response::Bech32Address(hex_public_key_to_bech32_address(&hex, bech32_hrp)?)
Expand Down
6 changes: 5 additions & 1 deletion bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,13 @@ pub enum Response {
Output(Output),
/// Response for:
/// - [`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)
/// - [`HexPublicKeyToBech32Address`](crate::method::ClientMethod::HexPublicKeyToBech32Address)
/// - [`HexToBech32`](crate::method::ClientMethod::HexToBech32)
/// - [`NftIdToBech32`](crate::method::ClientMethod::NftIdToBech32)
/// - [`ImplicitAccountCreationAddress`](crate::method::WalletMethod::ImplicitAccountCreationAddress)
Bech32Address(Bech32Address),
/// - [`Faucet`](crate::method::ClientMethod::RequestFundsFromFaucet)
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/tests/secrets_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn method_interface_secrets_debug() {
let wallet_options = WalletOptions::default().with_secret_manager(SecretManagerDto::Placeholder);
assert_eq!(
format!("{:?}", wallet_options),
"WalletOptions { address: None, alias: None, bip_path: None, client_options: None, secret_manager: Some(<omitted>), storage_path: None }"
"WalletOptions { address: None, bip_path: None, alias: None, client_options: None, secret_manager: Some(<omitted>), storage_path: None }"
);
}
22 changes: 22 additions & 0 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,28 @@ export class Client {
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<Bech32Address> {
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.
*
Expand Down
8 changes: 8 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ export interface __AccountIdToBech32Method__ {
};
}

export interface __AnchorIdToBech32Method__ {
name: 'anchorIdToBech32';
data: {
anchorId: AnchorId;
bech32Hrp?: string;
};
}

export interface __NftIdToBech32Method__ {
name: 'nftIdToBech32';
data: {
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
__GetUtxoChangesFullByIndexMethod__,
__HexToBech32Method__,
__AccountIdToBech32Method__,
__AnchorIdToBech32Method__,
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__AccountOutputIdsMethod__,
Expand Down Expand Up @@ -105,6 +106,7 @@ export type __ClientMethods__ =
| __GetUtxoChangesFullByIndexMethod__
| __HexToBech32Method__
| __AccountIdToBech32Method__
| __AnchorIdToBech32Method__
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __AccountOutputIdsMethod__
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
__Bech32ToHexMethod__,
__HexToBech32Method__,
__AccountIdToBech32Method__,
__AnchorIdToBech32Method__,
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__IsAddressValidMethod__,
Expand Down Expand Up @@ -48,6 +49,7 @@ export type __UtilsMethods__ =
| __Bech32ToHexMethod__
| __HexToBech32Method__
| __AccountIdToBech32Method__
| __AnchorIdToBech32Method__
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __IsAddressValidMethod__
Expand Down
10 changes: 9 additions & 1 deletion bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Bech32Address,
Unlock,
} from '../../';
import { AccountId } from '../../block/id';
import { AccountId, AnchorId } from '../../block/id';
import { SlotCommitment } from '../../block/slot';
import { InputSigningData } from '../../client';
import { NumericString } from '../numeric';
Expand Down Expand Up @@ -123,6 +123,14 @@ export interface __AccountIdToBech32Method__ {
};
}

export interface __AnchorIdToBech32Method__ {
name: 'anchorIdToBech32';
data: {
anchorId: AnchorId;
bech32Hrp: string;
};
}

export interface __NftIdToBech32Method__ {
name: 'nftIdToBech32';
data: {
Expand Down
21 changes: 21 additions & 0 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '../types';
import {
AccountId,
AnchorId,
BlockId,
FoundryId,
NftId,
Expand Down Expand Up @@ -313,6 +314,26 @@ export class Utils {
});
}

/**
* 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.
*
Expand Down
14 changes: 11 additions & 3 deletions bindings/python/iota_sdk/client/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@ def _call_method(self, name, data=None):
"""

# pylint: disable=redefined-builtin
def hex_to_bech32(self, hex_str: HexStr, bech32_hrp: str) -> str:
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 account_id_to_bech32(self, account_id: HexStr, bech32_hrp: str) -> str:
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 nft_id_to_bech32(self, nft_id: HexStr, bech32_hrp: str) -> str:
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', {
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/iota_sdk/client/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class ValidatorsResponse:
Response of GET /api/core/v3/validators
Attributes:
stakers: List of registered validators ready for the next epoch.
validators: List of registered validators ready for the next epoch.
page_size: The number of validators returned per one API request with pagination.
cursor: The cursor that needs to be provided as cursor query parameter to request the next page. If empty, this was the last page.
"""
stakers: List[ValidatorResponse]
validators: List[ValidatorResponse]
page_size: int
cursor: Optional[str] = None

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 @@ -58,6 +58,15 @@ def account_id_to_bech32(account_id: HexStr, bech32_hrp: str) -> str:
'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.
Expand Down
8 changes: 5 additions & 3 deletions bindings/python/tests/test_api_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import Generic, TypeVar
from json import load, loads, dumps
from iota_sdk import RoutesResponse, CongestionResponse, ManaRewardsResponse, ValidatorResponse, ValidatorsResponse, CommitteeResponse, IssuanceBlockHeaderResponse, Block, OutputResponse, SlotCommitment
from iota_sdk import RoutesResponse, CongestionResponse, ManaRewardsResponse, ValidatorResponse, CommitteeResponse, IssuanceBlockHeaderResponse, Block, OutputResponse, SlotCommitment


base_path = '../../sdk/tests/types/api/fixtures/'
Expand All @@ -17,7 +17,8 @@ def test_api_response(cls_type: Generic[T], path: str):
fixture = load(payload)
cls = cls_type.from_dict(fixture)

# We need to sort the keys because optional fields in classes must be last in Python
# We need to sort the keys because optional fields in classes must be
# last in Python
fixture_str = dumps(fixture, sort_keys=True)
recreated = dumps(
loads(cls.to_json()), sort_keys=True)
Expand All @@ -34,7 +35,8 @@ def test_api_response(cls_type: Generic[T], path: str):
# GET /api/core/v3/rewards/{outputId}
test_api_response(ManaRewardsResponse, "get-mana-rewards-example.json")
# GET /api/core/v3/validators
test_api_response(ValidatorsResponse, "get-validators-example.json")
# TODO: enable when TIP is updated
# test_api_response(ValidatorsResponse, "get-validators-example.json")
# GET /api/core/v3/validators/{bech32Address}
test_api_response(ValidatorResponse, "get-validator-example.json")
# GET /api/core/v3/committee
Expand Down
11 changes: 6 additions & 5 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,18 @@ pub async fn init_command(
} else {
None
};
let address = init_params
.address
.map(|addr| Bech32Address::from_str(&addr))
.transpose()?;

Ok(Wallet::builder()
.with_secret_manager(secret_manager)
.with_client_options(ClientOptions::new().with_node(init_params.node_url.as_str())?)
.with_storage_path(storage_path.to_str().expect("invalid unicode"))
.with_address(
init_params
.address
.map(|addr| Bech32Address::from_str(&addr))
.transpose()?,
)
.with_bip_path(init_params.bip_path)
.with_address(address)
.with_alias(alias)
.finish()
.await?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn main() -> Result<()> {
.with_secret_manager(SecretManager::Placeholder)
.with_storage_path(ONLINE_WALLET_DB_PATH)
.with_client_options(client_options.clone())
.with_bip_path(Bip44::new(SHIMMER_COIN_TYPE))
.with_address(address)
.with_bip_path(Bip44::new(SHIMMER_COIN_TYPE))
.finish()
.await?;

Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/spammer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ async fn main() -> Result<()> {
let wallet = Wallet::builder()
.with_secret_manager(SecretManager::Mnemonic(secret_manager))
.with_client_options(client_options)
.with_bip_path(bip_path)
.with_address(address)
.with_bip_path(bip_path)
.finish()
.await?;

Expand Down
Loading

0 comments on commit 974377d

Please sign in to comment.