Skip to content

Commit

Permalink
split online and offline wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Dec 6, 2023
1 parent 4b75dd8 commit 556bfea
Show file tree
Hide file tree
Showing 88 changed files with 890 additions and 408 deletions.
6 changes: 3 additions & 3 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use iota_sdk;
use iota_sdk::{
client::secret::{SecretManager, SecretManagerDto},
types::block::address::Bech32Address,
wallet::{ClientOptions, Wallet},
wallet::{core::Online, ClientOptions, Wallet, WalletBuilder},
};
use serde::Deserialize;

Expand Down Expand Up @@ -91,9 +91,9 @@ impl WalletOptions {
self
}

pub async fn build(self) -> iota_sdk::wallet::Result<Wallet<SecretManager>> {
pub async fn build(self) -> iota_sdk::wallet::Result<Wallet<Online<SecretManager>>> {
log::debug!("wallet options: {self:?}");
let mut builder = Wallet::builder()
let mut builder = WalletBuilder::new()
.with_address(self.address)
.with_alias(self.alias)
.with_public_key_options(self.public_key_options)
Expand Down
6 changes: 3 additions & 3 deletions bindings/core/src/method_handler/call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::pin::Pin;
use futures::Future;
use iota_sdk::{
client::{secret::SecretManager, Client},
wallet::Wallet,
wallet::{core::Online, Wallet},
};

use crate::{
Expand Down Expand Up @@ -35,7 +35,7 @@ impl CallMethod for Client {
}
}

impl CallMethod for Wallet<SecretManager> {
impl CallMethod for Wallet<Online<SecretManager>> {
type Method = WalletMethod;

fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>> {
Expand All @@ -55,7 +55,7 @@ pub async fn call_client_method(client: &Client, method: ClientMethod) -> Respon
}

/// Call a wallet method.
pub async fn call_wallet_method(wallet: &Wallet<SecretManager>, method: WalletMethod) -> Response {
pub async fn call_wallet_method(wallet: &Wallet<Online<SecretManager>>, method: WalletMethod) -> Response {
log::debug!("Wallet method: {method:?}");
let result = convert_async_panics(|| async { call_wallet_method_internal(wallet, method).await }).await;

Expand Down
5 changes: 3 additions & 2 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ use iota_sdk::{
},
types::TryFromDto,
wallet::{
types::TransactionWithMetadataDto, BlockIssuerKeySource, PreparedCreateNativeTokenTransactionDto, Wallet,
core::Online, types::TransactionWithMetadataDto, BlockIssuerKeySource, PreparedCreateNativeTokenTransactionDto,
Wallet,
},
};

use crate::{method::WalletMethod, response::Response};

/// Call a wallet method.
pub(crate) async fn call_wallet_method_internal(
wallet: &Wallet<SecretManager>,
wallet: &Wallet<Online<SecretManager>>,
method: WalletMethod,
) -> crate::Result<Response> {
let response = match method {
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use iota_sdk_bindings_core::{
call_wallet_method as rust_call_wallet_method,
iota_sdk::{
client::secret::SecretManager,
wallet::{events::WalletEventType, Wallet},
wallet::{core::Online, events::WalletEventType, Wallet},
},
Response, WalletMethod, WalletOptions,
};
Expand All @@ -17,7 +17,7 @@ use tokio::sync::RwLock;

use crate::{client::ClientMethodHandler, secret_manager::SecretManagerMethodHandler, NodejsError};

pub type WalletMethodHandler = Arc<RwLock<Option<Wallet<SecretManager>>>>;
pub type WalletMethodHandler = Arc<RwLock<Option<Wallet<Online<SecretManager>>>>>;

#[napi(js_name = "createWallet")]
pub async fn create_wallet(options: String) -> Result<External<WalletMethodHandler>> {
Expand Down
3 changes: 2 additions & 1 deletion bindings/wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use iota_sdk_bindings_core::{
iota_sdk::{
client::secret::SecretManager,
wallet::{
core::Online,
events::types::{WalletEvent, WalletEventType},
Wallet,
},
Expand All @@ -25,7 +26,7 @@ use crate::{client::ClientMethodHandler, secret_manager::SecretManagerMethodHand
/// The Wallet method handler.
#[wasm_bindgen(js_name = WalletMethodHandler)]
pub struct WalletMethodHandler {
wallet: Arc<Mutex<Option<Wallet<SecretManager>>>>,
wallet: Arc<Mutex<Option<Wallet<Online<SecretManager>>>>>,
}

/// Creates a method handler with the given options.
Expand Down
8 changes: 4 additions & 4 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use iota_sdk::{
},
crypto::keys::bip44::Bip44,
types::block::address::Bech32Address,
wallet::ClientOptions,
wallet::{ClientOptions, WalletBuilder},
};
use log::LevelFilter;

Expand Down Expand Up @@ -291,7 +291,7 @@ pub async fn init_command(
.with_address_index(bip.address_index)
});

Ok(Wallet::builder()
Ok(WalletBuilder::new()
.with_secret_manager(secret_manager)
.with_client_options(ClientOptions::new().with_node(init_params.node_url.as_str())?)
.with_storage_path(storage_path.to_str().expect("invalid unicode"))
Expand Down Expand Up @@ -332,7 +332,7 @@ pub async fn node_info_command(storage_path: &Path) -> Result<Wallet, Error> {
pub async fn restore_command(storage_path: &Path, snapshot_path: &Path, backup_path: &Path) -> Result<Wallet, Error> {
check_file_exists(backup_path).await?;

let mut builder = Wallet::builder();
let mut builder = WalletBuilder::new().into_online();
if check_file_exists(snapshot_path).await.is_ok() {
println!(
"Detected a stronghold file at {}. Enter password to unlock:",
Expand Down Expand Up @@ -400,7 +400,7 @@ pub async fn unlock_wallet(
None
};

let maybe_wallet = Wallet::builder()
let maybe_wallet = WalletBuilder::new()
.with_secret_manager(secret_manager)
.with_storage_path(storage_path.to_str().expect("invalid unicode"))
.finish()
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ mod wallet_cli;

use clap::Parser;
use fern_logger::{LoggerConfigBuilder, LoggerOutputConfigBuilder};
use iota_sdk::client::secret::stronghold::StrongholdSecretManager;
use iota_sdk::{client::secret::stronghold::StrongholdSecretManager, wallet::core::Online};
use log::LevelFilter;

use self::{
cli::{new_wallet, Cli},
error::Error,
};

pub type Wallet = iota_sdk::wallet::Wallet<StrongholdSecretManager>;
pub type Wallet = iota_sdk::wallet::Wallet<Online<StrongholdSecretManager>>;

#[macro_export]
macro_rules! println_log_info {
Expand Down
8 changes: 6 additions & 2 deletions sdk/examples/how_tos/account/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
//! cargo run --release --all-features --example create_account_output
//! ```
use iota_sdk::{wallet::Result, Wallet};
use iota_sdk::{
client::secret::SecretManager,
wallet::{Result, WalletBuilder},
};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -23,7 +26,8 @@ async fn main() -> Result<()> {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
8 changes: 6 additions & 2 deletions sdk/examples/how_tos/account/destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
//! cargo run --release --all-features --example destroy_account_output
//! ```
use iota_sdk::{wallet::Result, Wallet};
use iota_sdk::{
client::secret::SecretManager,
wallet::{Result, WalletBuilder},
};

#[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 wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
8 changes: 4 additions & 4 deletions sdk/examples/how_tos/account_wallet/request_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//! `cargo run --release --all-features --example account_wallet_request_funds`
use iota_sdk::{
client::request_funds_from_faucet,
client::{request_funds_from_faucet, secret::mnemonic::MnemonicSecretManager},
types::block::address::{AccountAddress, ToBech32Ext},
wallet::{AccountSyncOptions, Result, SyncOptions},
Wallet,
wallet::{AccountSyncOptions, Result, SyncOptions, WalletBuilder},
};

#[tokio::main]
Expand All @@ -25,7 +24,8 @@ async fn main() -> Result<()> {
let faucet_url = std::env::var("FAUCET_URL").unwrap();

// Create the wallet
let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<MnemonicSecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
7 changes: 4 additions & 3 deletions sdk/examples/how_tos/account_wallet/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//! `cargo run --release --all-features --example account_wallet_transaction`
use iota_sdk::{
client::node_api::indexer::query_parameters::BasicOutputQueryParameters,
client::{node_api::indexer::query_parameters::BasicOutputQueryParameters, secret::SecretManager},
types::block::address::{AccountAddress, ToBech32Ext},
wallet::{AccountSyncOptions, Result, SyncOptions, TransactionOptions},
wallet::{AccountSyncOptions, Result, SyncOptions, TransactionOptions, WalletBuilder},
Wallet,
};

Expand All @@ -31,7 +31,8 @@ async fn main() -> Result<()> {
};

// Create the wallet
let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
9 changes: 7 additions & 2 deletions sdk/examples/how_tos/accounts_and_addresses/check_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
//! cargo run --release --all-features --example check_balance
//! ```
use iota_sdk::{wallet::Result, Wallet};
use iota_sdk::{
client::secret::SecretManager,
wallet::{Result, WalletBuilder},
Wallet,
};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -22,7 +26,8 @@ async fn main() -> Result<()> {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
//! ```
use iota_sdk::{
client::secret::SecretManager,
types::block::address::ToBech32Ext,
wallet::{ConsolidationParams, Result},
wallet::{ConsolidationParams, Result, WalletBuilder},
Wallet,
};

Expand All @@ -29,7 +30,8 @@ async fn main() -> Result<()> {
}
}

let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
9 changes: 7 additions & 2 deletions sdk/examples/how_tos/accounts_and_addresses/list_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
//! cargo run --release --all-features --example list_outputs
//! ```
use iota_sdk::{wallet::Result, Wallet};
use iota_sdk::{
client::secret::SecretManager,
wallet::{Result, WalletBuilder},
Wallet,
};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,7 +24,8 @@ async fn main() -> Result<()> {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
//! ```
use iota_sdk::{
wallet::{Result, SyncOptions},
client::secret::SecretManager,
wallet::{Result, SyncOptions, WalletBuilder},
Wallet,
};

Expand All @@ -23,7 +24,8 @@ async fn main() -> Result<()> {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! `cargo run --release --all-features --example advanced_transaction`
use iota_sdk::{
client::secret::SecretManager,
types::block::{
address::Bech32Address,
output::{
Expand All @@ -16,7 +17,7 @@ use iota_sdk::{
},
slot::SlotIndex,
},
wallet::Result,
wallet::{Result, WalletBuilder},
Wallet,
};

Expand All @@ -30,7 +31,8 @@ async fn main() -> Result<()> {
}

// Get the wallet we generated with `create_wallet`.
let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//! `cargo run --release --all-features --example claim_transaction`
use iota_sdk::{
wallet::{OutputsToClaim, Result},
client::secret::SecretManager,
wallet::{OutputsToClaim, Result, WalletBuilder},
Wallet,
};

Expand All @@ -21,7 +22,8 @@ async fn main() -> Result<()> {
}

// Get the wallet we generated with `create_wallet`.
let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
//! ```
use iota_sdk::{
wallet::{Result, TransactionOptions},
client::secret::SecretManager,
wallet::{Result, TransactionOptions, WalletBuilder},
Wallet,
};

Expand All @@ -31,7 +32,8 @@ async fn main() -> Result<()> {
}

// Get the wallet we generated with `create_wallet`.
let wallet = Wallet::builder()
let wallet = WalletBuilder::new()
.into_online::<SecretManager>()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
Expand Down
Loading

0 comments on commit 556bfea

Please sign in to comment.