Skip to content

Commit

Permalink
List implicit accounts (#1574)
Browse files Browse the repository at this point in the history
* Add Wallet::implicit_accounts

* Add to CLI

* Add to bindings core

* Add to nodejs bindings

* Add to python bindings
  • Loading branch information
thibault-martinez authored Nov 6, 2023
1 parent 1bf1a53 commit e66635b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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())
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 __ImplicitAccountsMethod__ = {
name: 'implicitAccounts';
};

export type __IncomingTransactionsMethod__ = {
name: 'incomingTransactions';
};
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__,
__ImplicitAccountsMethod__,
__IncomingTransactionsMethod__,
__TransactionsMethod__,
__UnspentOutputsMethod__,
Expand Down Expand Up @@ -98,6 +99,7 @@ export type __AccountMethod__ =
| __OutputsMethod__
| __PendingTransactionsMethod__
| __ImplicitAccountCreationAddressMethod__
| __ImplicitAccountsMethod__
| __IncomingTransactionsMethod__
| __TransactionsMethod__
| __UnspentOutputsMethod__
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 implicit accounts of the wallet.
*
* @returns The implicit accounts of the wallet.
*/
async implicitAccounts(): Promise<OutputData[]> {
const response = await this.methodHandler.callAccountMethod(
this.meta.index,
{
name: 'implicitAccounts',
},
);

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

/**
* List all incoming transactions of the account.
*
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 @@ -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.
"""
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 @@ -24,6 +24,7 @@ const WALLET_COMMANDS: &[&str] = &[
"exit",
"faucet",
"implicit-account-creation-address",
"implicit-accounts",
"melt-native-token",
"mint-native-token",
"mint-nft",
Expand Down
16 changes: 12 additions & 4 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -562,12 +564,17 @@ pub async fn faucet_command(wallet: &Wallet, address: Option<Bech32Address>, 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
Expand All @@ -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)?,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ where
.cloned()
}

/// Returns implicit accounts of the wallet.
pub async fn implicit_accounts(&self) -> Vec<OutputData> {
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<TransactionWithMetadata> {
self.data().await.incoming_transactions.values().cloned().collect()
Expand Down

0 comments on commit e66635b

Please sign in to comment.