pub mod exceptions;
#[cfg(feature = "helpers")]
pub mod faucet_generation;
use crate::constants::CryptoAlgorithm;
use crate::core::addresscodec::classic_address_to_xaddress;
use crate::core::keypairs::derive_classic_address;
use crate::core::keypairs::derive_keypair;
use crate::core::keypairs::generate_seed;
use alloc::string::String;
use core::fmt::Display;
use exceptions::XRPLWalletResult;
use zeroize::Zeroize;
#[derive(Debug)]
pub struct Wallet {
pub seed: String,
pub public_key: String,
pub private_key: String,
pub classic_address: String,
pub sequence: u64,
}
impl Drop for Wallet {
fn drop(&mut self) {
self.seed.zeroize();
self.public_key.zeroize();
self.private_key.zeroize();
self.classic_address.zeroize();
self.sequence.zeroize();
}
}
impl Wallet {
pub fn new(seed: &str, sequence: u64) -> XRPLWalletResult<Self> {
let (public_key, private_key) = derive_keypair(seed, false)?;
let classic_address = derive_classic_address(&public_key)?;
Ok(Wallet {
seed: seed.into(),
public_key,
private_key,
classic_address,
sequence,
})
}
pub fn create(crypto_algorithm: Option<CryptoAlgorithm>) -> XRPLWalletResult<Self> {
Self::new(&generate_seed(None, crypto_algorithm)?, 0)
}
pub fn get_xaddress(
&self,
tag: Option<u64>,
is_test_network: bool,
) -> XRPLWalletResult<String> {
Ok(classic_address_to_xaddress(
&self.classic_address,
tag,
is_test_network,
)?)
}
}
impl Display for Wallet {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(
f,
"Wallet {{ public_key: {}, private_key: -HIDDEN-, classic_address: {} }}",
self.public_key, self.classic_address
)
}
}