Expand bip39 usage (#381)
* Bump wasm-crypto for bip39ToSeed * Support bip39ToSeed for ed25519 (backwards-compat) * Update to latest wasm-crypto
This commit is contained in:
@@ -13,6 +13,12 @@ describe('mnemonicToMiniSecret', () => {
|
||||
await cryptoWaitReady();
|
||||
});
|
||||
|
||||
it('generates a valid seed', () => {
|
||||
expect(
|
||||
u8aToHex(mnemonicToMiniSecret('seed sock milk update focus rotate barely fade car face mechanic mercy'))
|
||||
).toEqual('0x4d1ab2a57929edfd018aaa974e62ed557e3f54b4104acabedf73c8f5a1dbb029');
|
||||
});
|
||||
|
||||
tests.forEach(([mnemonic, , seed], index) => {
|
||||
it(`Created correct seed for ${index}`, () => {
|
||||
expect(
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import toSecret from './toSecret';
|
||||
|
||||
describe('mnemonicToSecret', () => {
|
||||
it('generates a valid seed', () => {
|
||||
expect(
|
||||
toSecret('basket actual')
|
||||
).toEqual(new Uint8Array([
|
||||
92, 242, 212, 168, 176, 53, 94, 144, 41, 91, 223, 197, 101, 160, 34, 164, 9, 175, 6, 61, 83, 101, 187, 87, 191, 116, 217, 82, 143, 73, 75, 250, 68, 0, 245, 61, 131, 73, 184, 15, 218, 228, 64, 130, 215, 249, 84, 30, 29, 186, 43, 0, 59, 207, 236, 157, 13, 83, 120, 28, 166, 118, 101, 31
|
||||
]));
|
||||
});
|
||||
});
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { mnemonicToSeed } from 'bip39';
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
export default function toSecret (mnemonic: string): Uint8Array {
|
||||
return bufferToU8a(
|
||||
mnemonicToSeed(mnemonic)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2019 @polkadot/util-crypto authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import toSeed from './toSeed';
|
||||
|
||||
describe('mnemonicToSeed', () => {
|
||||
it('generates a valid seed', () => {
|
||||
expect(
|
||||
u8aToHex(toSeed('seed sock milk update focus rotate barely fade car face mechanic mercy'))
|
||||
).toEqual('0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027');
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import toSecret from './toSecret';
|
||||
import { mnemonicToSeed } from 'bip39';
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
import { bip39ToSeed, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
/**
|
||||
* @name toSeed
|
||||
@@ -21,6 +23,8 @@ import toSecret from './toSecret';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export default function toSeed (mnemonic: string): Uint8Array {
|
||||
return toSecret(mnemonic).subarray(0, 32);
|
||||
export default function toSeed (mnemonic: string, password: string = ''): Uint8Array {
|
||||
return isReady()
|
||||
? bip39ToSeed(mnemonic, password)
|
||||
: bufferToU8a(mnemonicToSeed(mnemonic, password)).subarray(0, 32);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user