Skip to content

Commit

Permalink
Add implicit_account_creation_address method
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 2, 2023
1 parent 0b1ef16 commit b8c085e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
9 changes: 8 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ wasm-bindgen-futures = { version = "0.4.37", default-features = false, optional

[dev-dependencies]
iota-sdk = { path = ".", default-features = false, features = ["rand"] }
pretty_assertions = { version = "1.4.0", default-features = false, features = [ "alloc" ] }
pretty_assertions = { version = "1.4.0", default-features = false, features = [
"alloc",
] }

dotenvy = { version = "0.15.7", default-features = false }
fern-logger = { version = "0.5.0", default-features = false }
Expand Down Expand Up @@ -340,6 +342,11 @@ name = "destroy_account_output"
path = "examples/how_tos/account/destroy.rs"
required-features = ["wallet", "stronghold"]

[[example]]
name = "implicit_account_creation"
path = "examples/how_tos/account/implicit_account_creation.rs"
required-features = ["wallet", "stronghold"]

# Outputs

[[example]]
Expand Down
42 changes: 42 additions & 0 deletions sdk/examples/how_tos/account/implicit_account_creation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! In this example we will create an account output.
//!
//! Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
//! running the `./how_tos/accounts_and_addresses/create_wallet.rs` example and that funds are available by running
//! the `get_funds` example!
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example implicit_account_creation
//! ```

use iota_sdk::{
client::{constants::SHIMMER_COIN_TYPE, secret::SecretManager},
crypto::keys::bip44::Bip44,
wallet::{ClientOptions, Result, Wallet},
};

#[tokio::main]
async fn main() -> Result<()> {
//  This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

let secret_manager = SecretManager::try_from_mnemonic(std::env::var("MNEMONIC").unwrap())?;
let client_options = ClientOptions::new().with_node("https://api.testnet.shimmer.network")?;

let wallet = Wallet::builder()
.with_secret_manager(secret_manager)
.with_client_options(client_options)
.with_storage_path("implicit_account_creation")
.with_bip_path(Bip44::new(SHIMMER_COIN_TYPE))
.finish()
.await?;

let implicit_account_creation_address = wallet.implicit_account_creation_address().await?;

println!("{implicit_account_creation_address}");

Ok(())
}
17 changes: 15 additions & 2 deletions sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
},
types::{
block::{
address::{Bech32Address, Hrp},
address::{Address, Bech32Address, Hrp, ImplicitAccountCreationAddress},
output::{dto::FoundryOutputDto, AccountId, FoundryId, FoundryOutput, NftId, Output, OutputId, TokenId},
payload::signed_transaction::{dto::TransactionDto, Transaction, TransactionId},
},
Expand All @@ -41,7 +41,7 @@ use crate::{
wallet::{
operations::syncing::SyncOptions,
types::{OutputData, OutputDataDto},
FilterOptions, Result,
Error, FilterOptions, Result,
},
};

Expand Down Expand Up @@ -252,6 +252,19 @@ where
self.data().await.address.clone()
}

pub async fn implicit_account_creation_address(&self) -> Result<Bech32Address> {
let bech32_address = &self.data().await.address;

if let Address::Ed25519(address) = bech32_address.inner() {
Ok(Bech32Address::new(
*bech32_address.hrp(),
ImplicitAccountCreationAddress::from(address.clone()),
))
} else {
return Err(Error::NonEd25519Address);
}
}

/// Get the wallet's configured Bech32 HRP.
pub async fn bech32_hrp(&self) -> Hrp {
self.data().await.address.hrp
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub enum Error {
/// Address not the wallet address
#[error("address {0} is not the wallet address")]
WalletAddressMismatch(Bech32Address),
/// Action requires the wallet to be Ed25519 address based
#[error("tried to perform an action that requires the wallet to be Ed25519 address based")]
NonEd25519Address,
}

// Serialize type with Display error
Expand Down

0 comments on commit b8c085e

Please sign in to comment.