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:
Jaco Greeff
2019-03-23 19:17:29 +01:00
committed by GitHub
parent 41cf7e6c47
commit 364a5d93f0
9 changed files with 58 additions and 45 deletions
+8
View File
@@ -32,6 +32,14 @@ describe('keypair', () => {
).toEqual(publicKeyTwo);
});
it('creates a ed25519 pair via mneminicToSeed', () => {
expect(
keypair.addFromUri(
'seed sock milk update focus rotate barely fade car face mechanic mercy'
).address()
).toEqual('5DkQP32jP4DVJLWWBRBoZF2tpWjqFrcrTBo6H5NcSk7MxFyP');
});
it('adds from a mnemonic', () => {
setPrefix(68);
+7
View File
@@ -147,6 +147,7 @@ export default class Keyring implements KeyringInstance {
* @description This creates a pair from the suri, but does not add it to the keyring
*/
createFromUri (_suri: string, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
// here we only aut-add the dev phrase if we have a hard-derived path
const suri = _suri.indexOf('//') === 0
? `${DEV_PHRASE}${_suri}`
: _suri;
@@ -160,8 +161,14 @@ export default class Keyring implements KeyringInstance {
const parts = str.split(' ');
if ([12, 15, 18, 21, 24].includes(parts.length)) {
// FIXME This keeps compat with older versions, but breaks compat with subkey
// seed = type === 'sr25519'
// ? mnemonicToMiniSecret(phrase, password)
// : mnemonicToSeed(phrase, password);
seed = mnemonicToMiniSecret(phrase, password);
} else {
assert(str.length <= 32, 'specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');
seed = stringToU8a(str.padEnd(32));
}
}
+3 -3
View File
@@ -30,9 +30,9 @@
"dependencies": {
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.40.1",
"@polkadot/wasm-crypto": "^0.3.1",
"@polkadot/wasm-dalek-ed25519": "^0.3.1",
"@polkadot/wasm-schnorrkel": "^0.3.1",
"@polkadot/wasm-crypto": "^0.5.1",
"@polkadot/wasm-dalek-ed25519": "^0.5.1",
"@polkadot/wasm-schnorrkel": "^0.5.1",
"@types/bip39": "^2.4.2",
"@types/pbkdf2": "^3.0.0",
"@types/secp256k1": "^3.5.0",
@@ -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');
});
});
+7 -3
View File
@@ -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);
}
+12 -12
View File
@@ -1584,20 +1584,20 @@
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
integrity sha512-wnt4zXxZXyz6WaubTO/I+nUElwV2DogFzdl6CrKfVn2PTWp8uHN06W9s40FH57ORtmQfDr9rLRP8Nq+oIyElbg==
"@polkadot/wasm-crypto@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.3.1.tgz#d605d18557076f01b00610844ba41bf12298e502"
integrity sha512-G1QEKC8o6SkKc65xjgMzujoA9rEQpMp/RZCVqrrmevgBhxTSohVsbXsBO3ooNtQLdq0DfFqUH13FatV0RxVdHQ==
"@polkadot/wasm-crypto@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-0.5.1.tgz#3d5734d6dc2f5174a411689f78538287d21bd710"
integrity sha512-8h7uz85bvLVirtbxLkELr9H25oqAfgnQuDP6FP3QJeag7VfMtVvRF5U2JWGgsiUSiRg+UJnTuCleOSDnageRmg==
"@polkadot/wasm-dalek-ed25519@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-dalek-ed25519/-/wasm-dalek-ed25519-0.3.1.tgz#52b86a3fabbbdeb60273b51685d089d5be2bfe6a"
integrity sha512-TvFMpE0y2RTCDRcVAer/94bCT4X8qHTU/K5GEhiqHtsLNZ9igJeSn27PRHUF3rzq1YgwCLvG6/wQ+4p+4DWTlQ==
"@polkadot/wasm-dalek-ed25519@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-dalek-ed25519/-/wasm-dalek-ed25519-0.5.1.tgz#7b59f5381c8e16335d4de31e2e30ac7d7592762f"
integrity sha512-wBz1/tJJHylQ8cfB0J11h8T46m6trAq+tyNzGCaoDNFyb7XDg3wNxpyWNHg1PGrI59sjaDiTfvr3RUiodWgcng==
"@polkadot/wasm-schnorrkel@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-schnorrkel/-/wasm-schnorrkel-0.3.1.tgz#08287848f769dee4955517348905888bd289f277"
integrity sha512-2J/lNZe7oDWvzUbsEfvKfiiAvBIBbIsppeBGahwSg2Y+cLweQgThXGizPpQoPo0tHxgMKdTi6jhPu7tLzCW/cA==
"@polkadot/wasm-schnorrkel@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@polkadot/wasm-schnorrkel/-/wasm-schnorrkel-0.5.1.tgz#9db91db2e262668bdc1b037ef6d010ba2a29bf41"
integrity sha512-uFlYRtMYeuVzW72+I01ToKqbPmn/3gFJzMc2MJcpOgOEPHNrQ1erXkqy0s1BxJYIDaO5SlvAQK/lRQbAWEYUDA==
"@types/babel__core@^7.1.0":
version "7.1.0"