-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a module "wasm" with structures to be called from wasm to use bdk-cli in repl mode
- Loading branch information
1 parent
51c42d1
commit 54d4804
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::commands::OfflineWalletSubCommand::*; | ||
#[cfg(any( | ||
feature = "electrum", | ||
feature = "esplora", | ||
feature = "compact_filters", | ||
feature = "rpc" | ||
))] | ||
use crate::commands::OnlineWalletSubCommand::*; | ||
use crate::commands::*; | ||
use crate::handlers::*; | ||
use crate::utils::*; | ||
use crate::Nodes; | ||
use bdk::database::AnyDatabase; | ||
use bdk::{bitcoin::Network, Error, Wallet}; | ||
use js_sys::Promise; | ||
use std::rc::Rc; | ||
use std::str::FromStr; | ||
use structopt::StructOpt; | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_futures::future_to_promise; | ||
|
||
#[wasm_bindgen] | ||
pub struct WasmWallet { | ||
wallet: Rc<Wallet<AnyDatabase>>, | ||
wallet_opts: Rc<WalletOpts>, | ||
network: Network, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn log_init() { | ||
wasm_logger::init(wasm_logger::Config::default()); | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WasmWallet { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(network: JsValue, wallet_opts: Vec<JsValue>) -> Result<WasmWallet, String> { | ||
let wallet_opts = wallet_opts | ||
.into_iter() | ||
.map(|a| a.as_string().expect("Invalid type")); | ||
let wallet_opts: WalletOpts = | ||
WalletOpts::from_iter_safe(wallet_opts).map_err(|e| e.to_string())?; | ||
let network = network | ||
.as_string() | ||
.ok_or_else(|| "Invalid network :)".to_string())?; | ||
let network = Network::from_str(&network).map_err(|e| e.to_string())?; | ||
let wallet_opts = | ||
maybe_descriptor_wallet_name(wallet_opts, network).map_err(|e| e.to_string())?; | ||
let database = open_database(&wallet_opts).map_err(|e| e.to_string())?; | ||
let wallet = new_wallet(network, &wallet_opts, database).map_err(|e| e.to_string())?; | ||
Ok(WasmWallet { | ||
wallet: Rc::new(wallet), | ||
wallet_opts: Rc::new(wallet_opts), | ||
network, | ||
}) | ||
} | ||
|
||
pub fn run_command(&self, command: Vec<JsValue>) -> Promise { | ||
let wallet = Rc::clone(&self.wallet); | ||
let wallet_opts = Rc::clone(&self.wallet_opts); | ||
let network = self.network; | ||
|
||
// The promise returned contains a Result<JsValue, String> | ||
future_to_promise(async move { | ||
let command = command | ||
.into_iter() | ||
.map(|a| a.as_string().expect("Invalid type")); | ||
let repl_subcommand = | ||
ReplSubCommand::from_iter_safe(command).map_err(|e| e.to_string())?; | ||
log::debug!("repl_subcommand = {:?}", repl_subcommand); | ||
|
||
let result = match repl_subcommand { | ||
ReplSubCommand::OnlineWalletSubCommand(online_subcommand) => { | ||
let blockchain = new_blockchain(network, &wallet_opts, &Nodes::None) | ||
.map_err(|e| e.to_string())?; | ||
handle_online_wallet_subcommand(&wallet, &blockchain, online_subcommand).await | ||
} | ||
ReplSubCommand::OfflineWalletSubCommand(offline_subcommand) => { | ||
handle_offline_wallet_subcommand(&wallet, &wallet_opts, offline_subcommand) | ||
} | ||
ReplSubCommand::KeySubCommand(key_subcommand) => { | ||
handle_key_subcommand(network, key_subcommand) | ||
} | ||
ReplSubCommand::Exit => return Ok(JsValue::NULL), | ||
}; | ||
|
||
result | ||
.map(|v| JsValue::from_serde(&v).expect("Serde serialization failed")) | ||
.map_err(|e| e.to_string().into()) | ||
}) | ||
} | ||
} |