Skip to content

Commit

Permalink
Into trait in proxy calls
Browse files Browse the repository at this point in the history
We can make our function easier for the caller to work with by using the Into trait.
This trait will can automatically convert a &str into a String. If we already have a String, then no conversion happens.
  • Loading branch information
elmurci committed Mar 23, 2020
1 parent b4c9f0e commit d1d9d8b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 52 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ use xpring::{Xprl};
let mut xrpl = Xprl::new("http://test.xrp.xpring.io:50051")?;

// 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".to_owned(), None, true)?;
let wallet_from_mnemonic = xrpl.wallet_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", None, true)?;

// 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".to_owned(), Some("m/44'/144'/0'/0/1".to_owned()), 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"), true)?;
```

##### 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".to_owned(), None, true)?;
let wallet_from_seed = xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
// XWalletGenerationResult { wallet:
// XWallet
// {
Expand All @@ -99,7 +99,7 @@ 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".to_owned(), false)?; //entropy and mainnet
let random_wallet_with_entropy = xrpl.generate_random_wallet("00000000000000000000000000000000", false)?; //entropy and mainnet

// XWalletGenerationResult { wallet:
// XWallet
Expand All @@ -119,7 +119,7 @@ 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".to_owned(), Some("m/44'/144'/0'/0/1".to_owned()), 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"), false)?;

println!("Address: {}", wallet.address); //XVMFQQBMhdouRqhPMuawgBMN1AVFTofPAdRsXG5RkPtUPNQ
println!("Public Key: {}", wallet.publicKey); //031D68BC1A142E6766B2BDFB006CCFE135EF2E0E2E94ABB5CF5C9AB6104776FBAE
Expand All @@ -132,18 +132,18 @@ A wallet can also sign and verify arbitrary messages. Generally, users should us

```rust
let signed_message = xrpl.wallet_sign(
"mymessage".to_owned(),
"000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389".to_owned()
"mymessage",
"000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389"
).unwrap();

println!("Signed Message: {:?}", signed_message); //304402204146402099809E1F021421569F72BA34DCAFCC832741AB6310F887F60734D9F002203E813AD6A59D67D8EE06C8EA05BCC1BA8F690B631E6F243E8BE60633D27BE05D

...

let verified_message = xrpl.wallet_verify(
"mymessage".to_owned(),
"3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32".to_owned(),
"038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89".to_owned()
"mymessage",
"3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32",
"038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89"
).unwrap();
// true
```
Expand Down Expand Up @@ -184,7 +184,7 @@ An `XrplClient` can send XRP to other [accounts](https://xrpl.org/accounts.html)

```rust
let w = wallet.from_seed(
"shKtxFAYfNUHYayYMYkp3KjQQX2UY".to_owned(),
"shKtxFAYfNUHYayYMYkp3KjQQX2UY",
None,
true
).unwrap();
Expand Down Expand Up @@ -270,7 +270,7 @@ let mut ilp = Ilp::new("http://hermes-grpc.ilpv4.dev", "test", "password")?;
// Send Payment
let payment =
ilp.send_to(
"$money.ilpv4.dev/test2".to_owned(),
"$money.ilpv4.dev/test2",
8,
10
)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/ilp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
// Send Payment
let payment =
ilp.send_to(
"$money.ilpv4.dev/sdk_account2".to_owned(),
"$money.ilpv4.dev/sdk_account2",
13,
10
)?;
Expand Down
16 changes: 10 additions & 6 deletions examples/xrpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ fn main() {

// // Generate a Wallet from a seed
let wallet_from_seed =
xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB".to_owned(), None, true)?;
xrpl.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
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".to_owned(), Some("m/44'/144'/0'/0/1".to_owned()), 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"),
true
)?;
println!("\nWallet from mnemonic {:#?}", wallet_from_mnemonic);

// Sign a message
let signed_message = xrpl.wallet_sign(
"mymessage".to_owned(),
"000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389".to_owned(),
"mymessage",
"000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389",
)?;
println!("\nSigned Message {:?}", signed_message);

// Verify a message
let message_verification_result = xrpl.wallet_verify("mymessage".to_owned(), "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32".to_owned(), "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89".to_owned())?;
let message_verification_result = xrpl.wallet_verify("mymessage", "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32", "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89")?;
println!(
"\nSigned Message Verification {:?}",
message_verification_result
Expand All @@ -76,7 +80,7 @@ fn main() {
//Send Payment
println!("\nSending payment...");
let sending_wallet =
xrpl.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY".to_owned(), None, true)?;
xrpl.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY", None, true)?;
println!("sending_wallet {:?}", sending_wallet);
let payment = xrpl.send(
12.12,
Expand Down
74 changes: 42 additions & 32 deletions src/xpring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl Xrpl {
/// # Ok(())
/// # }
/// ```
pub fn new(xrplclient_url: &'static str) -> Xrpl {
pub fn new<S: Into<String>>(xrplclient_url: S) -> Xrpl {
let xrpljs_path = copy_js_to_exec_path()?;
Xrpl {
jscontext: JavaScript::new(xrpljs_path)?,
xrplclient: XrplClient::connect(xrplclient_url)?,
xrplclient: XrplClient::connect(xrplclient_url.into())?,
}
}

Expand Down Expand Up @@ -90,12 +90,12 @@ impl Xrpl {
/// // }
/// ```
#[throws(_)]
pub fn generate_random_wallet(
pub fn generate_random_wallet<S: Into<Option<String>>>(
&mut self,
entropy: Option<String>,
entropy: S,
test: bool,
) -> XWalletGenerationResult {
wallet::generate_random(&mut self.jscontext, entropy, test)?
wallet::generate_random(&mut self.jscontext, entropy.into(), test)?
}

/// Generates a wallet from a mnemonic (and derivation path).
Expand All @@ -116,7 +116,7 @@ impl 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".to_owned(), Some("m/44'/144'/0'/0/1".to_owned()), true)?;
/// 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)?;
/// # Ok(())
/// # }
/// // XWallet {
Expand All @@ -127,13 +127,18 @@ impl Xrpl {
/// // }
/// ```
#[throws(_)]
pub fn wallet_from_mnemonic(
pub fn wallet_from_mnemonic<S: Into<String>>(
&mut self,
mnemonic: String,
derivation_path: Option<String>,
mnemonic: S,
derivation_path: Option<&str>,
test: bool,
) -> XWallet {
wallet::from_mnemonic(&mut self.jscontext, mnemonic, derivation_path, test)?
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)?
}

/// Generates a wallet from a seed.
Expand All @@ -156,7 +161,7 @@ impl Xrpl {
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let wallet_from_seed =
/// xpring.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB".to_owned(), None, true)?;
/// xpring.wallet_from_seed("snYP7oArxKepd3GPDcrjMsJYiJeJB", None, true)?;
/// # Ok(())
/// # }
///
Expand All @@ -168,13 +173,18 @@ impl Xrpl {
/// // }
/// ```
#[throws(_)]
pub fn wallet_from_seed(
pub fn wallet_from_seed<S: Into<String>>(
&mut self,
seed: String,
derivation_path: Option<String>,
seed: S,
derivation_path: Option<&str>,
test: bool,
) -> XWallet {
wallet::from_seed(&mut self.jscontext, seed, derivation_path, test)?
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)?
}

/// Signs a message with a private key.
Expand All @@ -195,17 +205,17 @@ impl Xrpl {
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let signed_message = xpring.wallet_sign(
/// "mymessage".to_owned(),
/// "000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389".to_owned(),
/// "mymessage",
/// "000974B4CFE004A2E6C4364CBF3510A36A352796728D0861F6B555ED7E54A70389",
/// )?;
/// # Ok(())
/// # }
///
/// // "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32"
/// ```
#[throws(_)]
pub fn wallet_sign(&mut self, message: String, private_key: String) -> String {
wallet::sign(&mut self.jscontext, message, private_key)?
pub fn wallet_sign<S: Into<String>>(&mut self, message: S, private_key: S) -> String {
wallet::sign(&mut self.jscontext, message.into(), private_key.into())?
}

/// Verifies with a public key a signed message.
Expand All @@ -226,20 +236,20 @@ 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".to_owned(), "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32".to_owned(), "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89".to_owned())?;
/// let message_verification_result = xpring.wallet_verify("mymessage", "3045022100DD88E31FF9AFD2A6DA48D40C4B4E8F11725E11C9D9E52388710E35ED19212EF6022068CFA9C09071322751C11DD21E89088879DC28B3B683D3F863090FB7C331EC32", "038BF420B5271ADA2D7479358FF98A29954CF18DC25155184AEAD05796DA737E89")?;
/// # Ok(())
/// # }
///
/// // true
/// ```
#[throws(_)]
pub fn wallet_verify(
pub fn wallet_verify<S: Into<String>>(
&mut self,
message: String,
signature: String,
public_key: String,
message: S,
signature: S,
public_key: S,
) -> bool {
wallet::verify(&mut self.jscontext, message, signature, public_key)?
wallet::verify(&mut self.jscontext, message.into(), signature.into(), public_key.into())?
}

// Util
Expand Down Expand Up @@ -268,8 +278,8 @@ impl Xrpl {
/// // true
/// ```
#[throws(_)]
pub fn validate_address(&mut self, address: &str) -> bool {
util::is_valid_address(&mut self.jscontext, address)?
pub fn validate_address(&mut self, address: &'static str) -> bool {
util::is_valid_address(&mut self.jscontext, address.into())?
}

/// Validates an X-Address
Expand Down Expand Up @@ -445,7 +455,7 @@ impl Xrpl {
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut xpring = Xrpl::new("http://test.xrp.xpring.io:50051")?;
/// let sending_wallet =
/// xpring.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY".to_owned(), None, true)?;
/// xpring.wallet_from_seed("shKtxFAYfNUHYayYMYkp3KjQQX2UY", None, true)?;
/// let payment = xpring.send(
/// 12.12,
/// "T7jkn8zYC2NhPdcbVxkiEXZGy56YiEE4P7uXRgpy5j4Q6S1",
Expand Down Expand Up @@ -565,7 +575,7 @@ impl Ilp {
/// # fn main() -> Result<(), anyhow::Error> {
/// # let mut ilp = Ilp::new("http://hermes-grpc.ilpv4.dev", "sdk_account1", "password")?;
/// let payment = ilp.send_to(
/// "$money.ilpv4.dev/sdk_account2".to_owned(),
/// "$money.ilpv4.dev/sdk_account2",
/// 13,
/// 10
/// )?;
Expand All @@ -582,13 +592,13 @@ impl Ilp {
/// // }
/// ```
#[throws(_)]
pub fn send_to(&mut self,
destination_payment_pointer: String,
pub fn send_to<S: Into<String>>(&mut self,
destination_payment_pointer: S,
amount: u64,
timeout_seconds: u64
) -> IlpSendResponse {
self.ilpclient.send(
destination_payment_pointer,
destination_payment_pointer.into(),
amount,
timeout_seconds
)?
Expand Down
1 change: 0 additions & 1 deletion src/xrplclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ mod tests {
fn test_send() {
let mut client = XrplClient::connect(DEFAULT_SERVER_URL)?;
let out_dir = std::env::var("OUT_DIR").unwrap();
println!("----OUT_DIR-----> {:?}", out_dir);
let mut jscontext = JavaScript::new(format!("{}/xpring.js", out_dir))?;
let w = XWallet::new(
"0314ACE51F9B116BCF3C1E38A9BD92706AF4334165870139144E947B27BB0103E8".to_owned(),
Expand Down

0 comments on commit d1d9d8b

Please sign in to comment.