From e66635b40e28cbd84e228363bd505ebf9ace81a8 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Mon, 6 Nov 2023 14:06:26 +0100 Subject: [PATCH] List implicit accounts (#1574) * Add Wallet::implicit_accounts * Add to CLI * Add to bindings core * Add to nodejs bindings * Add to python bindings --- bindings/core/src/method/wallet.rs | 3 +++ bindings/core/src/method_handler/wallet.rs | 4 ++++ .../nodejs/lib/types/wallet/bridge/account.ts | 4 ++++ .../nodejs/lib/types/wallet/bridge/index.ts | 2 ++ bindings/nodejs/lib/wallet/account.ts | 17 +++++++++++++++++ bindings/python/iota_sdk/wallet/account.py | 8 ++++++++ cli/src/wallet_cli/completer.rs | 1 + cli/src/wallet_cli/mod.rs | 16 ++++++++++++---- sdk/src/wallet/core/mod.rs | 11 +++++++++++ 9 files changed, 62 insertions(+), 4 deletions(-) diff --git a/bindings/core/src/method/wallet.rs b/bindings/core/src/method/wallet.rs index 2748e34864..3a578919f0 100644 --- a/bindings/core/src/method/wallet.rs +++ b/bindings/core/src/method/wallet.rs @@ -188,6 +188,9 @@ pub enum WalletMethod { /// Returns the implicit account creation address of the wallet if it is Ed25519 based. /// Expected response: [`Bech32Address`](crate::Response::Bech32Address) ImplicitAccountCreationAddress, + /// Returns the implicit accounts of the wallet. + /// Expected response: [`OutputsData`](crate::Response::OutputsData) + ImplicitAccounts, /// Returns all incoming transactions of the wallet /// Expected response: /// [`Transactions`](crate::Response::Transactions) diff --git a/bindings/core/src/method_handler/wallet.rs b/bindings/core/src/method_handler/wallet.rs index 58169a8b80..50948dadb7 100644 --- a/bindings/core/src/method_handler/wallet.rs +++ b/bindings/core/src/method_handler/wallet.rs @@ -206,6 +206,10 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM let implicit_account_creation_address = wallet.implicit_account_creation_address().await?; Response::Bech32Address(implicit_account_creation_address) } + WalletMethod::ImplicitAccounts => { + let implicit_accounts = wallet.implicit_accounts().await; + Response::OutputsData(implicit_accounts.iter().map(OutputDataDto::from).collect()) + } WalletMethod::IncomingTransactions => { let transactions = wallet.incoming_transactions().await; Response::Transactions(transactions.iter().map(TransactionWithMetadataDto::from).collect()) diff --git a/bindings/nodejs/lib/types/wallet/bridge/account.ts b/bindings/nodejs/lib/types/wallet/bridge/account.ts index 363f743486..408216fcc9 100644 --- a/bindings/nodejs/lib/types/wallet/bridge/account.ts +++ b/bindings/nodejs/lib/types/wallet/bridge/account.ts @@ -149,6 +149,10 @@ export type __ImplicitAccountCreationAddressMethod__ = { name: 'implicitAccountCreationAddress'; }; +export type __ImplicitAccountsMethod__ = { + name: 'implicitAccounts'; +}; + export type __IncomingTransactionsMethod__ = { name: 'incomingTransactions'; }; diff --git a/bindings/nodejs/lib/types/wallet/bridge/index.ts b/bindings/nodejs/lib/types/wallet/bridge/index.ts index f7cfaee903..ae459e428c 100644 --- a/bindings/nodejs/lib/types/wallet/bridge/index.ts +++ b/bindings/nodejs/lib/types/wallet/bridge/index.ts @@ -16,6 +16,7 @@ import type { __OutputsMethod__, __PendingTransactionsMethod__, __ImplicitAccountCreationAddressMethod__, + __ImplicitAccountsMethod__, __IncomingTransactionsMethod__, __TransactionsMethod__, __UnspentOutputsMethod__, @@ -98,6 +99,7 @@ export type __AccountMethod__ = | __OutputsMethod__ | __PendingTransactionsMethod__ | __ImplicitAccountCreationAddressMethod__ + | __ImplicitAccountsMethod__ | __IncomingTransactionsMethod__ | __TransactionsMethod__ | __UnspentOutputsMethod__ diff --git a/bindings/nodejs/lib/wallet/account.ts b/bindings/nodejs/lib/wallet/account.ts index 23cbc888d4..117975e1d7 100644 --- a/bindings/nodejs/lib/wallet/account.ts +++ b/bindings/nodejs/lib/wallet/account.ts @@ -774,6 +774,23 @@ export class Account { return JSON.parse(response).payload; } + /** + * Returns the implicit accounts of the wallet. + * + * @returns The implicit accounts of the wallet. + */ + async implicitAccounts(): Promise { + const response = await this.methodHandler.callAccountMethod( + this.meta.index, + { + name: 'implicitAccounts', + }, + ); + + const parsed = JSON.parse(response) as Response; + return plainToInstance(OutputData, parsed.payload); + } + /** * List all incoming transactions of the account. * diff --git a/bindings/python/iota_sdk/wallet/account.py b/bindings/python/iota_sdk/wallet/account.py index d5fd533047..8f13adbf12 100644 --- a/bindings/python/iota_sdk/wallet/account.py +++ b/bindings/python/iota_sdk/wallet/account.py @@ -277,6 +277,14 @@ def implicit_account_creation_address(self) -> str: 'implicitAccountCreationAddress' ) + def implicit_accounts(self) -> List[OutputData]: + """Returns the implicit accounts of the wallet. + """ + outputs = self._call_account_method( + 'implicitAccounts' + ) + return [from_dict(OutputData, o) for o in outputs] + def incoming_transactions(self) -> List[TransactionWithMetadata]: """Returns all incoming transactions of the account. """ diff --git a/cli/src/wallet_cli/completer.rs b/cli/src/wallet_cli/completer.rs index c7ee9763b4..b11cb140bb 100644 --- a/cli/src/wallet_cli/completer.rs +++ b/cli/src/wallet_cli/completer.rs @@ -24,6 +24,7 @@ const WALLET_COMMANDS: &[&str] = &[ "exit", "faucet", "implicit-account-creation-address", + "implicit-accounts", "melt-native-token", "mint-native-token", "mint-nft", diff --git a/cli/src/wallet_cli/mod.rs b/cli/src/wallet_cli/mod.rs index 987e7204e6..f78c3dc5ce 100644 --- a/cli/src/wallet_cli/mod.rs +++ b/cli/src/wallet_cli/mod.rs @@ -118,6 +118,8 @@ pub enum WalletCommand { }, /// Returns the implicit account creation address of the wallet if it is Ed25519 based. ImplicitAccountCreationAddress, + /// Lists the implicit accounts of the wallet. + ImplicitAccounts, /// Mint additional native tokens. MintNativeToken { /// Token ID to be minted, e.g. 0x087d205988b733d97fb145ae340e27a8b19554d1ceee64574d7e5ff66c45f69e7a0100000000. @@ -562,12 +564,17 @@ pub async fn faucet_command(wallet: &Wallet, address: Option, url } // `implicit-account-creation-address` command -pub async fn implicit_account_creation_address(wallet: &Wallet) -> Result<(), Error> { +pub async fn implicit_account_creation_address_command(wallet: &Wallet) -> Result<(), Error> { println_log_info!("{}", wallet.implicit_account_creation_address().await?); Ok(()) } +// `implicit-accounts` command +pub async fn implicit_accounts_command(wallet: &Wallet) -> Result<(), Error> { + print_outputs(wallet.implicit_accounts().await, "Implicit accounts:").await +} + // `melt-native-token` command pub async fn melt_native_token_command(wallet: &Wallet, token_id: String, amount: String) -> Result<(), Error> { let transaction = wallet @@ -588,7 +595,7 @@ pub async fn melt_native_token_command(wallet: &Wallet, token_id: String, amount } // `mint-native-token` command -pub async fn mint_native_token(wallet: &Wallet, token_id: String, amount: String) -> Result<(), Error> { +pub async fn mint_native_token_command(wallet: &Wallet, token_id: String, amount: String) -> Result<(), Error> { let mint_transaction = wallet .mint_native_token( TokenId::from_str(&token_id)?, @@ -1085,13 +1092,14 @@ pub async fn prompt_internal( } WalletCommand::Faucet { address, url } => faucet_command(wallet, address, url).await, WalletCommand::ImplicitAccountCreationAddress => { - implicit_account_creation_address(wallet).await + implicit_account_creation_address_command(wallet).await } + WalletCommand::ImplicitAccounts => implicit_accounts_command(wallet).await, WalletCommand::MeltNativeToken { token_id, amount } => { melt_native_token_command(wallet, token_id, amount).await } WalletCommand::MintNativeToken { token_id, amount } => { - mint_native_token(wallet, token_id, amount).await + mint_native_token_command(wallet, token_id, amount).await } WalletCommand::MintNft { address, diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index 3314b6b0f9..f8a83acea3 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -455,6 +455,17 @@ where .cloned() } + /// Returns implicit accounts of the wallet. + pub async fn implicit_accounts(&self) -> Vec { + self.data() + .await + .unspent_outputs + .values() + .filter(|output_data| output_data.output.is_implicit_account()) + .cloned() + .collect() + } + /// Returns all incoming transactions of the wallet pub async fn incoming_transactions(&self) -> Vec { self.data().await.incoming_transactions.values().cloned().collect()