diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2021-03-18 08:42:35 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2021-03-18 08:42:35 +0100 |
| commit | 1ce0c196c10dc92977114279180ea714056c587d (patch) | |
| tree | b726917e32f285bf4a63892cbd932e5b5dbb71f9 | |
| parent | 0bb365d3e0ec9663e8f011b31c3d4c52d168ab6f (diff) | |
add transaction tests and bump version
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/account.rs | 8 | ||||
| -rw-r--r-- | src/ganache.rs | 12 | ||||
| -rw-r--r-- | src/hdk.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/transaction.rs | 48 |
7 files changed, 70 insertions, 11 deletions
@@ -118,7 +118,7 @@ dependencies = [ [[package]] name = "hdwallet" -version = "0.0.1" +version = "0.1.0" dependencies = [ "anyhow", "ethnum", @@ -1,6 +1,6 @@ [package] name = "hdwallet" -version = "0.0.1" +version = "0.1.0" authors = ["Nicholas Rodrigues Lordello <[email protected]>"] edition = "2018" license = "GPL-3.0-or-later" diff --git a/src/account.rs b/src/account.rs index a518753..7d1eeb5 100644 --- a/src/account.rs +++ b/src/account.rs @@ -78,14 +78,12 @@ impl Drop for PrivateKey { #[cfg(test)] mod tests { use super::*; + use crate::ganache::DETERMINISTIC_PRIVATE_KEY; use hex_literal::hex; - const GANACHE_DETERMINISTIC_PRIVATE_KEY: [u8; 32] = - hex!("4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"); - #[test] fn ganache_determinitic_address() { - let key = PrivateKey::new(GANACHE_DETERMINISTIC_PRIVATE_KEY).unwrap(); + let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap(); assert_eq!( *key.address(), hex!("90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"), @@ -94,7 +92,7 @@ mod tests { #[test] fn ganache_deterministic_signature() { - let key = PrivateKey::new(GANACHE_DETERMINISTIC_PRIVATE_KEY).unwrap(); + let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap(); let message = hash::keccak256(b"\x19Ethereum Signed Message:\n12Hello World!"); assert_eq!( key.sign(message), diff --git a/src/ganache.rs b/src/ganache.rs new file mode 100644 index 0000000..f6710d8 --- /dev/null +++ b/src/ganache.rs @@ -0,0 +1,12 @@ +//! Module containing Ganache deterministic secrets for testing. + +use hex_literal::hex; + +/// The Ganache deterministic mnemonic. +pub const DETERMINISTIC_MNEMONIC: &str = + "myth like bonus scare over problem client lizard pioneer submit female collect"; + +/// The private key of the account at index 0 derived from the Ganache +/// deterministic mnemonic. +pub const DETERMINISTIC_PRIVATE_KEY: [u8; 32] = + hex!("4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"); @@ -70,14 +70,12 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{account::Address, mnemonic::Mnemonic}; + use crate::{account::Address, ganache::DETERMINISTIC_MNEMONIC, mnemonic::Mnemonic}; use hex_literal::hex; #[test] fn ganache_deterministic_mnemonic() { - const GANACHE_DETERMINISTIC_MNEMONIC: &str = "myth like bonus scare over problem \ - client lizard pioneer submit female collect"; - let mnemonic = GANACHE_DETERMINISTIC_MNEMONIC.parse::<Mnemonic>().unwrap(); + let mnemonic = DETERMINISTIC_MNEMONIC.parse::<Mnemonic>().unwrap(); let path = "m/44'/60'/0'/0/0".parse::<Path>().unwrap(); let account = derive(mnemonic.seed(""), &path).unwrap(); diff --git a/src/main.rs b/src/main.rs index 4aa453e..c96228b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ mod mnemonic; mod rand; mod transaction; +#[cfg(test)] +mod ganache; + use crate::cmd::*; use std::process; use structopt::StructOpt; diff --git a/src/transaction.rs b/src/transaction.rs index 3229478..9aa95ea 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -112,3 +112,51 @@ fn replay_protection(chain_id: u64, v: u8) -> u64 { let v = v as u64; v + 8 + chain_id * 2 } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{account::PrivateKey, ganache::DETERMINISTIC_PRIVATE_KEY, hash}; + use hex_literal::hex; + + const CHAIN_ID: u64 = 0x1337; + + fn transaction() -> Transaction { + Transaction { + nonce: U256::new(7777), + gas_price: U256::new(100_000_000_000), + gas: U256::new(150_000), + to: Some(Address(hex!("7070707070707070707070707070707070707070"))), + value: U256::new(42_000_000_000_000_000_000), + data: hex!("01020304").into(), + } + } + + #[test] + fn rlp_encode_for_signing() { + assert_eq!( + transaction().encode(CHAIN_ID, None), + hex!( + "f6821e6185174876e800830249f0947070707070707070707070707070707070 + 707070890246ddf9797668000084010203048213378080" + ), + ) + } + + #[test] + fn rlp_encode_with_signature() { + let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap(); + let message = hash::keccak256(transaction().encode(CHAIN_ID, None)); + let signature = key.sign(message); + + assert_eq!( + transaction().encode(CHAIN_ID, Some(signature)), + hex!( + "f876821e6185174876e800830249f09470707070707070707070707070707070 + 70707070890246ddf979766800008401020304822691a076382953503398303f + e8c3e3d235e8f71adc39fb90fcda99514e324d96bad253a044351903896d0b6f + 8a3ee9543c1e23ec3bab29a4ae5cae1a6b7b2c811105264f" + ), + ) + } +} |
