Skip to content

Commit

Permalink
move test boolean to Xrpl constructor
Browse files Browse the repository at this point in the history
Moving the test boolean parameter in functions to the Xprl constructor will make things easier as we won't have to repeat it for every method.
  • Loading branch information
elmurci committed Mar 23, 2020
1 parent d1d9d8b commit 217b1af
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 56 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,28 @@ A hierarchical deterministic wallet is created using a mnemonic and a derivation
use xpring::{Xprl};

...

let mut xrpl = Xprl::new("http://test.xrp.xpring.io:50051")?;
// TestNet
let mut xrpl = Xprl::new("http://test.xrp.xpring.io:50051", false)?;

// With mnemonic and default derivation path
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", None, true)?;
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
None
)?;

// With mnemonic and custom derivation path
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", Some("m/44'/144'/0'/0/1"), true)?;
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
Some("m/44'/144'/0'/0/1")
)?;
```

##### Seed-Based Wallets

You can construct a seed based wallet by passing a base58check encoded seed string.

```rust
let wallet_from_seed = xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
let wallet_from_seed = xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None)?;
// XWalletGenerationResult { wallet:
// XWallet
// {
Expand All @@ -98,8 +104,8 @@ xpring-rs can generate a new and random HD Wallet. The result of a wallet genera

```rust
// Generate a random wallet.
let random_wallet = xrpl.generate_random_wallet(None, false)?; //no entropy and testnet
let random_wallet_with_entropy = xrpl.generate_random_wallet("00000000000000000000000000000000", false)?; //entropy and mainnet
let random_wallet = xrpl.generate_random_wallet(None)?; //no entropy and testnet
let random_wallet_with_entropy = xrpl.generate_random_wallet(Some("00000000000000000000000000000000"))?; //entropy and mainnet

// XWalletGenerationResult { wallet:
// XWallet
Expand All @@ -119,7 +125,10 @@ let random_wallet_with_entropy = xrpl.generate_random_wallet("000000000000000000
A generated wallet can provide its public key, private key, and address on the XRP ledger.

```rust
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", Some("m/44'/144'/0'/0/1"), false)?;
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
Some("m/44'/144'/0'/0/1")
)?;

println!("Address: {}", wallet.address); //XVMFQQBMhdouRqhPMuawgBMN1AVFTofPAdRsXG5RkPtUPNQ
println!("Public Key: {}", wallet.publicKey); //031D68BC1A142E6766B2BDFB006CCFE135EF2E0E2E94ABB5CF5C9AB6104776FBAE
Expand Down Expand Up @@ -186,7 +195,6 @@ An `XrplClient` can send XRP to other [accounts](https://xrpl.org/accounts.html)
let w = wallet.from_seed(
"shKtxFAYfNUHYayYMYkp3KjQQX2UY",
None,
true
).unwrap();
let response = client.send(12.12, "T7jkn8zYC2NhPdcbVxkiEXZGy56YiEE4P7uXRgpy5j4Q6S1","T7QqSicoC1nB4YRyzWzctWW7KjwiYUtDzVaLwFd4N7W1AUU", w);
//XrplReliableSendResponse {
Expand Down
13 changes: 6 additions & 7 deletions examples/xrpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use xpring::Xrpl;
#[throws(_)]
fn main() {

// Xrpl instance
let mut xrpl = Xrpl::new("http://test.xrp.xrpl.io:50051")?;
// Xrpl instance (TestNet)
let mut xrpl = Xrpl::new("http://test.xrp.xrpl.io:50051", false)?;

// Encode an X-Address
let x_address =
Expand Down Expand Up @@ -39,19 +39,18 @@ fn main() {
);

// Generate a Random Wallet
let random_wallet = xrpl.generate_random_wallet(None, false)?;
let random_wallet = xrpl.generate_random_wallet(None)?;
println!("\nRandom Wallet {:#?}", random_wallet);

// // Generate a Wallet from a seed
let wallet_from_seed =
xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None)?;
println!("\nWallet from seed {:#?}", wallet_from_seed);

// Generate a Wallet from mnemonic
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
Some("m/44'/144'/0'/0/1"),
true
Some("m/44'/144'/0'/0/1")
)?;
println!("\nWallet from mnemonic {:#?}", wallet_from_mnemonic);

Expand Down Expand Up @@ -80,7 +79,7 @@ fn main() {
//Send Payment
println!("\nSending payment...");
let sending_wallet =
xrpl.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY", None, true)?;
xrpl.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY", None)?;
println!("sending_wallet {:?}", sending_wallet);
let payment = xrpl.send(
12.12,
Expand Down
85 changes: 45 additions & 40 deletions src/xpring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn copy_js_to_exec_path() -> String {
pub struct Xrpl {
pub(crate) jscontext: JavaScript,
pub(crate) xrplclient: XrplClient,
pub(crate) test: bool
}

impl Xrpl {
Expand All @@ -31,6 +32,7 @@ impl Xrpl {
/// # Arguments
///
/// * `xrplclient_url` - `&str` Url for the XRP Ledger node.
/// * `test` - `bool` true for TestNet, false for MainNet.
///
/// # Remarks
///
Expand All @@ -41,15 +43,16 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// # Ok(())
/// # }
/// ```
pub fn new<S: Into<String>>(xrplclient_url: S) -> Xrpl {
pub fn new<S: Into<String>>(xrplclient_url: S, test: bool) -> Xrpl {
let xrpljs_path = copy_js_to_exec_path()?;
Xrpl {
jscontext: JavaScript::new(xrpljs_path)?,
xrplclient: XrplClient::connect(xrplclient_url.into())?,
test
}
}

Expand All @@ -60,7 +63,6 @@ impl Xrpl {
/// # Arguments
///
/// * `entropy` - `Option<String>` (Optional) Entropy.
/// * `test` - `bool` true for TestNet, false for MainNet.
///
/// # Remarks
///
Expand All @@ -72,8 +74,8 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::wallet::{XWalletGenerationResult};
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let random_wallet = xpring.generate_random_wallet(None, false)?;
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let random_wallet = xpring.generate_random_wallet(None)?;
/// # Ok(())
/// # }
///
Expand All @@ -93,17 +95,15 @@ impl Xrpl {
pub fn generate_random_wallet<S: Into<Option<String>>>(
&mut self,
entropy: S,
test: bool,
) -> XWalletGenerationResult {
wallet::generate_random(&mut self.jscontext, entropy.into(), test)?
wallet::generate_random(&mut self.jscontext, entropy.into(), self.test)?
}

/// Generates a wallet from a mnemonic (and derivation path).
///
/// # Arguments
///
/// * `mnemonic` - `Option<String>` Mnemonic.
/// * `test` - `bool` true for TestNet, false for MainNet.
///
/// # Remarks
///
Expand All @@ -115,14 +115,16 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::wallet::{XWallet};
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let wallet_from_mnemonic = xpring.wallet_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", Some("m/44'/144'/0'/0/1"), true)?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", true)?;
/// let wallet_from_mnemonic = xrpl.wallet_from_mnemonic(
/// "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
/// Some("m/44'/144'/0'/0/1")
/// )?;
/// # Ok(())
/// # }
/// // XWallet {
/// // public_key: "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89",
/// // private_key: "000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389",
/// // test: true,
/// // address: Some("T7FxQEtaiNkq6ELhqGk3Pz2ov5aEoaGo6V642R74aaywJNT")
/// // }
/// ```
Expand All @@ -131,14 +133,13 @@ impl Xrpl {
&mut self,
mnemonic: S,
derivation_path: Option<&str>,
test: bool,
) -> XWallet {
let derivation_path = if derivation_path.is_some() {
Some(derivation_path.unwrap().to_owned())
} else {
None
};
wallet::from_mnemonic(&mut self.jscontext, mnemonic.into(), derivation_path, test)?
wallet::from_mnemonic(&mut self.jscontext, mnemonic.into(), derivation_path, self.test)?
}

/// Generates a wallet from a seed.
Expand All @@ -147,7 +148,6 @@ impl Xrpl {
///
/// * `seed` - `String` Seed
/// * `derivation_path` - `Option<String>` (Optional) Derivation path.
/// * `test` - `bool` true for TestNet, false for MainNet.
///
/// # Remarks
///
Expand All @@ -159,16 +159,15 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::wallet::{XWallet};
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xprl = Xrpl::new("http://test.xrp.xpring.io:50051", true)?;
/// let wallet_from_seed =
/// xpring.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
/// xprl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None)?;
/// # Ok(())
/// # }
///
/// // XWallet {
/// // public_key: "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89",
/// // private_key: "000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389",
/// // test: true,
/// // address: Some("T7FxQEtaiNkq6ELhqGk3Pz2ov5aEoaGo6V642R74aaywJNT")
/// // }
/// ```
Expand All @@ -177,14 +176,13 @@ impl Xrpl {
&mut self,
seed: S,
derivation_path: Option<&str>,
test: bool,
) -> XWallet {
let derivation_path = if derivation_path.is_some() {
Some(derivation_path.unwrap().to_owned())
} else {
None
};
wallet::from_seed(&mut self.jscontext, seed.into(), derivation_path, test)?
wallet::from_seed(&mut self.jscontext, seed.into(), derivation_path, self.test)?
}

/// Signs a message with a private key.
Expand All @@ -203,8 +201,8 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let signed_message = xpring.wallet_sign(
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let signed_message = xrpl.wallet_sign(
/// "mymessage",
/// "000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389",
/// )?;
Expand Down Expand Up @@ -235,8 +233,12 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let message_verification_result = xpring.wallet_verify("mymessage", "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32", "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let message_verification_result = xrpl.wallet_verify(
/// "mymessage",
/// "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32",
/// "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89"
/// )?;
/// # Ok(())
/// # }
///
Expand Down Expand Up @@ -269,9 +271,9 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let is_address_valid =
/// xpring.validate_address("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// xrpl.validate_address("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// # Ok(())
/// # }
///
Expand All @@ -297,9 +299,9 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let is_address_valid =
/// xpring.validate_x_address("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// xrpl.validate_x_address("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// # Ok(())
/// # }
///
Expand All @@ -325,9 +327,9 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let is_address_valid =
/// xpring.validate_classic_address("rU6K7V3Po4snVhBBaU29sesqs2qTQJWDw1")?;
/// xrpl.validate_classic_address("rU6K7V3Po4snVhBBaU29sesqs2qTQJWDw1")?;
/// # Ok(())
/// # }
///
Expand All @@ -353,9 +355,9 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let x_address =
/// xpring.encode_classic_address("rU6K7V3Po4snVhBBaU29sesqs2qTQJWDw1", Some(12345), None)?;
/// xrpl.encode_classic_address("rU6K7V3Po4snVhBBaU29sesqs2qTQJWDw1", Some(12345), None)?;
/// # Ok(())
///
/// # }
Expand Down Expand Up @@ -388,9 +390,9 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::address::XClassicAddress;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let classic_address =
/// xpring.decode_x_address("XVfC9CTCJh6GN2x8bnrw3LtdbqiVCUvtU3HnooQDgBnUpQT")?;
/// xrpl.decode_x_address("XVfC9CTCJh6GN2x8bnrw3LtdbqiVCUvtU3HnooQDgBnUpQT")?;
/// # Ok(())
/// # }
///
Expand Down Expand Up @@ -422,8 +424,8 @@ impl Xrpl {
/// ```
/// # use xpring::Xrpl;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let balance = xpring.get_balance("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let balance = xrpl.get_balance("TVr7v7JGN5suv7Zgdu9aL4PtCkwayZNYWvjSG23uMMWMvzZ")?;
/// # Ok(())
/// # }
///
Expand Down Expand Up @@ -453,10 +455,13 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::xrplclient::{XrplReliableSendResponse};
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let sending_wallet =
/// xpring.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY", None, true)?;
/// let payment = xpring.send(
/// xrpl.wallet_from_seed(
/// "shKtxFAYfNUHYayYMYkp3KjQQX2UY",
/// None
/// )?;
/// let payment = xrpl.send(
/// 12.12,
/// "T7jkn8zYC2NhPdcbVxkiEXZGy56YiEE4P7uXRgpy5j4Q6S1",
/// "T7QqSicoC1nB4YRyzWzctWW7KjwiYUtDzVaLwFd4N7W1AUU",
Expand Down Expand Up @@ -504,8 +509,8 @@ impl Xrpl {
/// # use xpring::Xrpl;
/// # use xpring::transaction::XTransactionStatus;
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let transaction_status = xpring.get_transaction_status(
/// # let mut xrpl = Xrpl::new("http://test.xrp.xpring.io:50051", false)?;
/// let transaction_status = xrpl.get_transaction_status(
/// "51338E39369AECBA05B5826D77BD4C9092BAD6B578664548FE742C75D3C187CE",
/// )?;
/// # Ok(())
Expand Down

0 comments on commit 217b1af

Please sign in to comment.