Skip to content

Commit

Permalink
Merge branch '2.0' into feat/iterable-wallet-data
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 23, 2023
2 parents d828070 + cae50ff commit bf0f6b7
Show file tree
Hide file tree
Showing 114 changed files with 1,014 additions and 1,561 deletions.
1 change: 1 addition & 0 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl WalletOptions {
self
}

#[cfg(feature = "storage")]
pub fn with_storage_path(mut self, storage_path: impl Into<Option<String>>) -> Self {
self.storage_path = storage_path.into();
self
Expand Down
6 changes: 1 addition & 5 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use iota_sdk::{
address::{Bech32Address, Hrp},
output::{
dto::OutputDto, feature::Feature, unlock_condition::dto::UnlockConditionDto, AccountId, AnchorId,
DelegationId, FoundryId, NativeToken, NftId, OutputId, TokenScheme,
DelegationId, FoundryId, NftId, OutputId, TokenScheme,
},
payload::{dto::PayloadDto, signed_transaction::TransactionId},
BlockDto, BlockId, IssuerId,
Expand All @@ -43,7 +43,6 @@ pub enum ClientMethod {
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
account_id: AccountId,
foundry_counter: Option<u32>,
unlock_conditions: Vec<UnlockConditionDto>,
Expand All @@ -61,7 +60,6 @@ pub enum ClientMethod {
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
unlock_conditions: Vec<UnlockConditionDto>,
features: Option<Vec<Feature>>,
},
Expand All @@ -73,7 +71,6 @@ pub enum ClientMethod {
// If not provided, minimum amount will be used
#[serde(default, with = "option_string")]
amount: Option<u64>,
native_tokens: Option<Vec<NativeToken>>,
serial_number: u32,
token_scheme: TokenScheme,
unlock_conditions: Vec<UnlockConditionDto>,
Expand All @@ -91,7 +88,6 @@ pub enum ClientMethod {
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
nft_id: NftId,
unlock_conditions: Vec<UnlockConditionDto>,
features: Option<Vec<Feature>>,
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use iota_sdk::{
},
wallet::{
ClientOptions, ConsolidationParams, CreateAccountParams, CreateNativeTokenParams, FilterOptions, MintNftParams,
OutputParams, OutputsToClaim, SendNativeTokensParams, SendNftParams, SendParams, SyncOptions,
OutputParams, OutputsToClaim, SendNativeTokenParams, SendNftParams, SendParams, SyncOptions,
TransactionOptions,
},
U256,
Expand Down Expand Up @@ -295,7 +295,7 @@ pub enum WalletMethod {
/// Prepare to send native tokens.
/// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction)
PrepareSendNativeTokens {
params: Vec<SendNativeTokensParams>,
params: Vec<SendNativeTokenParams>,
options: Option<TransactionOptions>,
},
/// Prepare to Send nft.
Expand Down
10 changes: 1 addition & 9 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::BuildAccountOutput {
amount,
mana,
native_tokens,
account_id,
foundry_counter,
unlock_conditions,
Expand All @@ -73,7 +72,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?)
},
mana,
native_tokens,
&account_id,
foundry_counter,
unlock_conditions,
Expand All @@ -86,7 +84,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::BuildBasicOutput {
amount,
mana,
native_tokens,
unlock_conditions,
features,
} => {
Expand All @@ -97,7 +94,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?)
},
mana,
native_tokens,
unlock_conditions,
features,
)?);
Expand All @@ -106,7 +102,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
}
ClientMethod::BuildFoundryOutput {
amount,
native_tokens,
serial_number,
token_scheme,
unlock_conditions,
Expand All @@ -119,7 +114,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
} else {
OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?)
},
native_tokens,
serial_number,
token_scheme,
unlock_conditions,
Expand All @@ -132,7 +126,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::BuildNftOutput {
amount,
mana,
native_tokens,
nft_id,
unlock_conditions,
features,
Expand All @@ -145,7 +138,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?)
},
mana,
native_tokens,
&nft_id,
unlock_conditions,
features,
Expand Down Expand Up @@ -259,7 +251,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
}
ClientMethod::GetOutputsIgnoreErrors { output_ids } => {
let outputs_response = client
.get_outputs_with_metadata_ignore_errors(&output_ids)
.get_outputs_with_metadata_ignore_not_found(&output_ids)
.await?
.iter()
.map(OutputWithMetadataResponse::from)
Expand Down
10 changes: 3 additions & 7 deletions bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,9 @@ where
if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else if let Some(secret_manager) = secret_manager.downcast::<SecretManager>() {
if let SecretManager::Stronghold(secret_manager) = secret_manager {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
} else if let Some(SecretManager::Stronghold(secret_manager)) = secret_manager.downcast::<SecretManager>() {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
Expand Down
1 change: 0 additions & 1 deletion bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ pub enum Response {
FoundryId(FoundryId),
/// Response for:
/// - [`TransactionSigningHash`](crate::method::UtilsMethod::TransactionSigningHash)
/// - [`ComputeInputsCommitment`](crate::method::UtilsMethod::ComputeInputsCommitment)
Hash(String),
/// Response for [`GetNodeInfo`](crate::method::ClientMethod::GetNodeInfo)
NodeInfoWrapper(NodeInfoWrapper),
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use iota_sdk_bindings_core::{
};
use pretty_assertions::assert_eq;

#[cfg(feature = "storage")]
#[tokio::test]
async fn create_wallet() -> Result<()> {
let storage_path = "test-storage/create_wallet";
Expand Down Expand Up @@ -59,6 +60,7 @@ async fn create_wallet() -> Result<()> {
Ok(())
}

#[cfg(feature = "storage")]
#[tokio::test]
async fn client_from_wallet() -> Result<()> {
let storage_path = "test-storage/client_from_wallet";
Expand Down Expand Up @@ -100,6 +102,7 @@ async fn client_from_wallet() -> Result<()> {
}

// TODO reenable
// #[cfg(feature = "storage")]
// #[tokio::test]
// async fn build_and_sign_block() -> Result<()> {
// let storage_path = "test-storage/build_and_sign_block";
Expand Down
13 changes: 8 additions & 5 deletions bindings/nodejs/examples/client/05-get-address-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ async function run() {
for (const outputResponse of addressOutputs) {
const output = outputResponse['output'];
if (output instanceof CommonOutput) {
(output as CommonOutput).getNativeTokens()?.forEach((token) => {
totalNativeTokens[token.id] =
(totalNativeTokens[token.id] || BigInt(0)) +
token.amount;
});
const nativeTokenFeature = (
output as CommonOutput
).getNativeToken();
if (nativeTokenFeature != undefined) {
totalNativeTokens[nativeTokenFeature.id] =
(totalNativeTokens[nativeTokenFeature.id] ||
BigInt(0)) + nativeTokenFeature.amount;
}
}

totalAmount += output.getAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ async function run() {
outputs.forEach(({ output, address }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native tokens: %s',
'- address: %s\n- amount: %d\n- native token: %s',
Utils.hexToBech32(address.toString(), 'rms'),
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeTokens()
? (output as CommonOutput).getNativeToken() ?? []
: [],
);
});
Expand Down Expand Up @@ -91,7 +91,7 @@ async function run() {
Utils.hexToBech32(address.toString(), 'rms'),
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeTokens()
? (output as CommonOutput).getNativeToken()
: undefined,
);
});
Expand Down
10 changes: 5 additions & 5 deletions bindings/nodejs/examples/how_tos/native_tokens/send.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { SendNativeTokensParams } from '@iota/sdk';
import { SendNativeTokenParams } from '@iota/sdk';

import { getUnlockedWallet } from '../../wallet/common';

// The native token amount to send.
const SEND_NATIVE_TOKEN_AMOUNT = BigInt(10);
// The address to send the tokens to
// The address to send the token to
const RECV_ADDRESS =
'rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu';

// In this example we will send native tokens.
// In this example we will send a native token.
//
// Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
// running the `how_tos/accounts_and_addresses/create-wallet` example!
Expand All @@ -32,10 +32,10 @@ async function run() {
)?.tokenId;

if (tokenId != null) {
const outputs: SendNativeTokensParams[] = [
const outputs: SendNativeTokenParams[] = [
{
address: RECV_ADDRESS,
nativeTokens: [[tokenId, SEND_NATIVE_TOKEN_AMOUNT]],
nativeToken: [tokenId, SEND_NATIVE_TOKEN_AMOUNT],
},
];

Expand Down
70 changes: 70 additions & 0 deletions bindings/nodejs/lib/types/block/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ enum AddressType {
Anchor = 24,
/** An implicit account creation address. */
ImplicitAccountCreation = 32,
/** A Multi address. */
Multi = 40,
/** An address with restricted capabilities. */
Restricted = 48,
}
Expand Down Expand Up @@ -78,6 +80,7 @@ abstract class Address {
throw new Error('Invalid JSON');
}
}

/**
* An Ed25519 Address.
*/
Expand Down Expand Up @@ -256,6 +259,70 @@ class RestrictedAddress extends Address {
}
}

/**
* A weighted address.
*/
class WeightedAddress {
/**
* The unlocked address.
*/
@Type(() => Address, {
discriminator: {
property: 'type',
subTypes: [
{ value: Ed25519Address, name: AddressType.Ed25519 as any },
{ value: AccountAddress, name: AddressType.Account as any },
{ value: NftAddress, name: AddressType.Nft as any },
{ value: AnchorAddress, name: AddressType.Anchor as any },
],
},
})
readonly address: Address;
/**
* The weight of the unlocked address.
*/
readonly weight: number;

/**
* @param address The unlocked address.
* @param weight The weight of the unlocked address.
*/
constructor(address: Address, weight: number) {
this.address = address;
this.weight = weight;
}
}

/**
* An address that consists of addresses with weights and a threshold value.
* The Multi Address can be unlocked if the cumulative weight of all unlocked addresses is equal to or exceeds the
* threshold.
*/
class MultiAddress extends Address {
/**
* The weighted unlocked addresses.
*/
readonly addresses: WeightedAddress[];
/**
* The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address.
*/
readonly threshold: number;

/**
* @param addresses The weighted unlocked addresses.
* @param threshold The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address.
*/
constructor(addresses: WeightedAddress[], threshold: number) {
super(AddressType.Multi);
this.addresses = addresses;
this.threshold = threshold;
}

toString(): string {
return JSON.stringify(this);
}
}

const AddressDiscriminator = {
property: 'type',
subTypes: [
Expand All @@ -267,6 +334,7 @@ const AddressDiscriminator = {
value: ImplicitAccountCreationAddress,
name: AddressType.ImplicitAccountCreation as any,
},
{ value: MultiAddress, name: AddressType.Multi as any },
{ value: RestrictedAddress, name: AddressType.Restricted as any },
],
};
Expand All @@ -281,5 +349,7 @@ export {
NftAddress,
AnchorAddress,
ImplicitAccountCreationAddress,
WeightedAddress,
MultiAddress,
RestrictedAddress,
};
Loading

0 comments on commit bf0f6b7

Please sign in to comment.