Skip to content

Commit

Permalink
tests: geth-compat keystore serde
Browse files Browse the repository at this point in the history
  • Loading branch information
roynalnaruto committed Feb 14, 2022
1 parent 6db0e55 commit 0fa5a7d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ uuid = { version = "0.8", features = ["serde", "v4"] }
# feature = "geth-compat"
ethereum-types = { version = "0.13.1", optional = true }
k256 = { version = "0.10.2", optional = true }
tiny-keccak = { version = "2.0.2", optional = true, default-features = false }

[features]
geth-compat = ["ethereum-types", "k256", "k256/ecdsa", "tiny-keccak"]
geth-compat = ["ethereum-types", "k256", "k256/ecdsa"]
34 changes: 34 additions & 0 deletions src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,39 @@ where
mod tests {
use super::*;

#[cfg(feature = "geth-compat")]
#[test]
fn deserialize_geth_compat_keystore() {
let data = r#"
{
"address": "00000398232e2064f896018496b4b44b3d62751f",
"crypto": {
"cipher": "aes-128-ctr",
"ciphertext": "4f784cd629a7caf34b488e36fb96aad8a8f943a6ce31c7deab950c5e3a5b1c43",
"cipherparams": {
"iv": "76f07196b3c94f25b8f34d869493f640"
},
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"n": 262144,
"p": 1,
"r": 8,
"salt": "1e7be4ce8351dd1710b0885438414b1748a81f1af510eda11e4d1f99c8d43975"
},
"mac": "5b5433575a2418c1c813337a88b4099baa2f534e5dabeba86979d538c1f594d8"
},
"id": "6c4485f3-3cc0-4081-848e-8bf489f2c262",
"version": 3
}"#;
let keystore: EthKeystore = serde_json::from_str(data).unwrap();
assert_eq!(
keystore.address.as_bytes().to_vec(),
hex::decode("00000398232e2064f896018496b4b44b3d62751f").unwrap()
);
}

#[cfg(not(feature = "geth-compat"))]
#[test]
fn test_deserialize_pbkdf2() {
let data = r#"
Expand Down Expand Up @@ -145,6 +178,7 @@ mod tests {
);
}

#[cfg(not(feature = "geth-compat"))]
#[test]
fn test_deserialize_scrypt() {
let data = r#"
Expand Down
8 changes: 3 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod geth_compat {
use ethereum_types::H160 as Address;
use k256::{ecdsa::SigningKey, elliptic_curve::sec1::ToEncodedPoint, PublicKey};
use tiny_keccak::{Hasher, Keccak};
use sha3::{Digest, Keccak256};

use crate::KeystoreError;

Expand All @@ -25,10 +25,8 @@ pub mod geth_compat {
where
S: AsRef<[u8]>,
{
let mut output = [0u8; 32];
let mut hasher = Keccak::v256();
let mut hasher = Keccak256::new();
hasher.update(bytes.as_ref());
hasher.finalize(&mut output);
output
hasher.finalize().into()
}
}

0 comments on commit 0fa5a7d

Please sign in to comment.