extern crate walletd;
use walletd::{
blockstream::Blockstream, walletd_coin_core::BlockchainConnectorBuilder,
walletd_ethereum::EthClient, Bip39Mnemonic, BitcoinWallet, BlockchainConnector, CryptoWallet,
Error, EthereumWallet, HDNetworkType, KeyPair, Mnemonic, MnemonicBuilder, MnemonicKeyPairType,
};
const BTC_TESTNET_URL: &str = "https://blockstream.info/testnet/api";
const ETH_TESTNET_URL: &str = "https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161";
#[tokio::main]
async fn main() -> Result<(), Error> {
let my_mnemonic_phrase =
"joy tail arena mix other envelope diary achieve short nest true vocal";
let network = HDNetworkType::TestNet;
let my_mnemonic = match Bip39Mnemonic::builder()
.mnemonic_phrase(my_mnemonic_phrase)
.build()
{
Ok(mnemonic) => mnemonic,
Err(e) => panic!("Error: {}", e),
};
let hd_wallet = KeyPair::new(
my_mnemonic.to_seed(),
my_mnemonic.phrase(),
MnemonicKeyPairType::HDBip39,
None,
network,
);
println!("HD Wallet Information:\n{:#?}", hd_wallet);
let btc_blockchain_client = BlockchainConnectorBuilder::<Blockstream>::new()
.url(BTC_TESTNET_URL.into())
.build()?;
let mut btc_wallet = hd_wallet.derive_wallet::<BitcoinWallet>()?;
btc_wallet.set_blockchain_client(btc_blockchain_client);
btc_wallet.sync().await?;
let eth_blockchain_client = EthClient::builder().url(ETH_TESTNET_URL.into()).build()?;
let mut eth_wallet = hd_wallet.derive_wallet::<EthereumWallet>()?;
eth_wallet.set_blockchain_client(eth_blockchain_client);
let current_btc_balance = btc_wallet.balance().await?;
println!(
"Current BTC balance: {} BTC, {} satoshi",
current_btc_balance.btc(),
current_btc_balance.satoshi()
);
let current_eth_balance = eth_wallet.balance().await?;
println!(
"Current ETH balance: {} ETH ({} wei)",
current_eth_balance.eth(),
current_eth_balance.wei()
);
let receive_address_btc = btc_wallet.receive_address()?;
println!(
"Address to use to receive funds to this BTC wallet: {}",
receive_address_btc
);
let receive_address_eth = eth_wallet.receive_address()?;
println!(
"Address to use to receive funds to this ETH wallet: {}",
receive_address_eth
);
Ok(())
}