Re-add bip39ToSeed interface (#104)
* Re-add bip39ToSeed interface * import
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 1.4.0-beta.x
|
||||
|
||||
- Rea-add the BTC/ETH compatible `bip39ToSeed` dropped in the previous version (ETH-compatible pairs)
|
||||
|
||||
## 1.3.1 Aug 9, 2020
|
||||
|
||||
- Add `scrypt` function for KDF generation
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
use bip39::{Mnemonic, MnemonicType, Language};
|
||||
use bip39::{Mnemonic, MnemonicType, Language, Seed};
|
||||
use hmac::Hmac;
|
||||
use pbkdf2::pbkdf2;
|
||||
use sha2::Sha512;
|
||||
@@ -48,6 +48,20 @@ pub fn ext_bip39_to_mini_secret(phrase: &str, password: &str) -> Vec<u8> {
|
||||
result[..32].to_vec()
|
||||
}
|
||||
|
||||
/// Creates a BTC/ETH compatible seed from a bip-39 phrase
|
||||
///
|
||||
/// @phrase: mnemonic phrase
|
||||
///
|
||||
/// Returns a 32-byte seed
|
||||
#[wasm_bindgen]
|
||||
pub fn ext_bip39_to_seed(phrase: &str, password: &str) -> Vec<u8> {
|
||||
let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();
|
||||
|
||||
Seed::new(&mnemonic, password)
|
||||
.as_bytes()[..32]
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
/// Validates a bip39 phrase
|
||||
///
|
||||
/// * phrase: mnemonic phrase
|
||||
@@ -85,6 +99,15 @@ pub mod tests {
|
||||
assert_eq!(result[..], mini[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_bip39_seed() {
|
||||
let phrase = "seed sock milk update focus rotate barely fade car face mechanic mercy";
|
||||
let seed = hex!("3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027");
|
||||
let result = ext_bip39_to_seed(phrase, "");
|
||||
|
||||
assert_eq!(result[..], seed[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_bip39_generate() {
|
||||
let phrase = ext_bip39_generate(12);
|
||||
|
||||
+1
@@ -5,6 +5,7 @@
|
||||
export function bip39Generate (words: 12 | 15 | 18 | 21 | 24): string;
|
||||
export function bip39ToEntropy (phrase: string): Uint8Array;
|
||||
export function bip39ToMiniSecret (phrase: string, password: string): Uint8Array;
|
||||
export function bip39ToSeed (phrase: string, password: string): Uint8Array;
|
||||
export function bip39Validate (phrase: string): boolean;
|
||||
|
||||
export function ed25519KeypairFromSeed (seed: Uint8Array): Uint8Array;
|
||||
|
||||
@@ -39,9 +39,18 @@ function bip39_toMiniSecret (wasm) {
|
||||
assert(RESULT === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret');
|
||||
}
|
||||
|
||||
function bip39_toSeed (wasm) {
|
||||
const RESULT = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', ''));
|
||||
|
||||
console.log('\tSEE', RESULT);
|
||||
|
||||
assert(RESULT === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
bip39_generate,
|
||||
bip39_toEntropy,
|
||||
bip39_toMiniSecret,
|
||||
bip39_toSeed,
|
||||
bip39_validate
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user