Skip to content

Commit

Permalink
feat: add test for shielded from mnemonic seed
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Sep 19, 2024
1 parent d40a535 commit d5e383f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/crypto/lib/src/crypto/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ mod tests {
assert_eq!(seed.vec.len(), 64);
}

#[wasm_bindgen_test]
fn can_restore_seed_from_phrase() {
let phrase = "caught pig embody hip goose like become worry face oval manual flame \
pizza steel viable proud eternal speed chapter sunny boat because view bullet";
let seed_bytes = vec![
178, 64, 160, 168, 33, 68, 84, 63, 0, 137, 121, 29, 66, 47, 123, 36, 64, 38, 160, 236,
93, 38, 53, 157, 169, 119, 42, 153, 188, 80, 209, 149, 51, 92, 251, 168, 150, 220, 70,
78, 230, 16, 152, 160, 85, 248, 115, 82, 183, 126, 96, 112, 58, 238, 230, 63, 89, 239,
0, 250, 163, 169, 166, 174,
];
let mnemonic = Mnemonic::from_phrase(phrase.into()).unwrap();
let seed = mnemonic
.to_seed(None)
.expect("Should return seed from mnemonic phrase");

assert_eq!(seed.vec, seed_bytes);
}

#[wasm_bindgen_test]
fn invalid_phrase_should_panic() {
let bad_phrase = "caught pig embody hip goose like become";
Expand Down
33 changes: 33 additions & 0 deletions packages/crypto/lib/src/crypto/zip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl ShieldedHDWallet {
#[cfg(test)]
mod tests {
use super::*;
use crate::crypto::bip39;
use masp_primitives::sapling::PaymentAddress;
use wasm_bindgen_test::*;

Expand Down Expand Up @@ -134,4 +135,36 @@ mod tests {
assert_eq!(xsk.expsk.to_bytes().len(), KEY_SIZE);
assert_eq!(xfvk.fvk.to_bytes().len(), KEY_SIZE);
}

#[wasm_bindgen_test]
fn can_restore_keys_from_mnemonic() {
let phrase = "great sphere inmate december menu warrior adjust glass flat heavy act mail";
let mnemonic = bip39::Mnemonic::from_phrase(phrase.into()).unwrap();
let seed = mnemonic
.to_seed(None)
.expect("Should return seed from mnemonic phrase");

let shielded_wallet = ShieldedHDWallet::new(JsValue::from(seed))
.expect("Instantiating ShieldedHDWallet should not fail");

let shielded_account = shielded_wallet
.derive(vec![32, 877, 0], None)
.expect("Deriving from ExtendedKeys should not fail");

let payment_address = PaymentAddress::try_from_slice(&shielded_account.payment_address())
.expect("should instantiate from serialized bytes");
let xfvk = ExtendedFullViewingKey::try_from_slice(&shielded_account.xfvk())
.expect("should instantiate from serialized bytes");

assert_eq!(payment_address.to_string(), "0a918bd974d1abddcc2e15eddec2557abae385ed59fb3089ed4aff418819a63bf4a7890591ff107a2b7569");
assert_eq!(
xfvk.fvk.to_string(),
format!(
"{}{}{}",
"9f89bdaf176f8528f43303ae793ce128af3436902a39ebbe46509f6fef11eb",
"585270060da4c12f1a52b63c7c6906dddcabb0ecd00735e11b0d7cbee277342dd89a10e9c7",
"e69bca34b50a3c8525bbee96347a7cadfd6c32d3af5b92ad1ecf07da"
)
);
}
}

0 comments on commit d5e383f

Please sign in to comment.