Skip to content

Commit

Permalink
List wallet accounts (#1583)
Browse files Browse the repository at this point in the history
* Add Wallet::accounts method

* Add to CLI

* Add to bindings core

* Add to nodejs

* Add to Python
  • Loading branch information
thibault-martinez authored Nov 7, 2023
1 parent 8650eb0 commit 6028f8f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use crate::OmittedDebug;
#[serde(tag = "name", content = "data", rename_all = "camelCase")]
#[non_exhaustive]
pub enum WalletMethod {
/// Returns the accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
Accounts,
/// Backup storage. Password must be the current one, when Stronghold is used as SecretManager.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
Expand Down
4 changes: 4 additions & 0 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::{method::WalletMethod, response::Response, Result};
/// Call a wallet method.
pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletMethod) -> Result<Response> {
let response = match method {
WalletMethod::Accounts => {
let accounts = wallet.accounts().await;
Response::OutputsData(accounts.iter().map(OutputDataDto::from).collect())
}
#[cfg(feature = "stronghold")]
WalletMethod::Backup { destination, password } => {
wallet.backup(destination, password).await?;
Expand Down
4 changes: 4 additions & 0 deletions bindings/nodejs/lib/types/wallet/bridge/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export type __ImplicitAccountCreationAddressMethod__ = {
name: 'implicitAccountCreationAddress';
};

export type __AccountsMethod__ = {
name: 'accounts';
};

export type __ImplicitAccountsMethod__ = {
name: 'implicitAccounts';
};
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/wallet/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
__OutputsMethod__,
__PendingTransactionsMethod__,
__ImplicitAccountCreationAddressMethod__,
__AccountsMethod__,
__ImplicitAccountsMethod__,
__IncomingTransactionsMethod__,
__TransactionsMethod__,
Expand Down Expand Up @@ -99,6 +100,7 @@ export type __AccountMethod__ =
| __OutputsMethod__
| __PendingTransactionsMethod__
| __ImplicitAccountCreationAddressMethod__
| __AccountsMethod__
| __ImplicitAccountsMethod__
| __IncomingTransactionsMethod__
| __TransactionsMethod__
Expand Down
17 changes: 17 additions & 0 deletions bindings/nodejs/lib/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,23 @@ export class Account {
return JSON.parse(response).payload;
}

/**
* Returns the accounts of the wallet.
*
* @returns The accounts of the wallet.
*/
async accounts(): Promise<OutputData[]> {
const response = await this.methodHandler.callAccountMethod(
this.meta.index,
{
name: 'accounts',
},
);

const parsed = JSON.parse(response) as Response<OutputData[]>;
return plainToInstance(OutputData, parsed.payload);
}

/**
* Returns the implicit accounts of the wallet.
*
Expand Down
8 changes: 8 additions & 0 deletions bindings/python/iota_sdk/wallet/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ def implicit_account_creation_address(self) -> str:
'implicitAccountCreationAddress'
)

def accounts(self) -> List[OutputData]:
"""Returns the accounts of the wallet.
"""
outputs = self._call_account_method(
'accounts'
)
return [from_dict(OutputData, o) for o in outputs]

def implicit_accounts(self) -> List[OutputData]:
"""Returns the implicit accounts of the wallet.
"""
Expand Down
1 change: 1 addition & 0 deletions cli/src/wallet_cli/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustyline::{
};

const WALLET_COMMANDS: &[&str] = &[
"accounts",
"address",
"balance",
"burn-native-token",
Expand Down
8 changes: 8 additions & 0 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl WalletCli {
#[derive(Debug, Subcommand)]
#[allow(clippy::large_enum_variant)]
pub enum WalletCommand {
/// Lists the accounts of the wallet.
Accounts,
/// Print the wallet address.
Address,
/// Print the wallet balance.
Expand Down Expand Up @@ -304,6 +306,11 @@ impl FromStr for OutputSelector {
}
}

// `accounts` command
pub async fn accounts_command(wallet: &Wallet) -> Result<(), Error> {
print_outputs(wallet.accounts().await, "Accounts:").await
}

// `address` command
pub async fn address_command(wallet: &Wallet) -> Result<(), Error> {
print_wallet_address(wallet).await?;
Expand Down Expand Up @@ -1057,6 +1064,7 @@ pub async fn prompt_internal(
}
};
match protocol_cli.command {
WalletCommand::Accounts => accounts_command(wallet).await,
WalletCommand::Address => address_command(wallet).await,
WalletCommand::Balance => balance_command(wallet).await,
WalletCommand::BurnNativeToken { token_id, amount } => {
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
dto::FoundryOutputDto, AccountId, AnchorId, DelegationId, FoundryId, FoundryOutput, NftId, Output,
OutputId, TokenId,
},
payload::signed_transaction::{dto::TransactionDto, Transaction, TransactionId},
payload::signed_transaction::TransactionId,
},
TryFromDto,
},
Expand Down Expand Up @@ -466,6 +466,17 @@ where
.collect()
}

/// Returns accounts of the wallet.
pub async fn accounts(&self) -> Vec<OutputData> {
self.data()
.await
.unspent_outputs
.values()
.filter(|output_data| output_data.output.is_account())
.cloned()
.collect()
}

/// Returns all incoming transactions of the wallet
pub async fn incoming_transactions(&self) -> Vec<TransactionWithMetadata> {
self.data().await.incoming_transactions.values().cloned().collect()
Expand Down

0 comments on commit 6028f8f

Please sign in to comment.