Create seed from mnemenic (#225)

This commit is contained in:
Jaco Greeff
2018-10-13 21:42:39 +02:00
committed by GitHub
parent 9d43119ed2
commit 91c7058284
2 changed files with 7 additions and 4 deletions
+6 -2
View File
@@ -6,6 +6,7 @@ import hexToU8a from '@polkadot/util/hex/toU8a';
import u8aFromString from '@polkadot/util/u8a/fromString';
import Keyring from './index';
import setPrefix from './address/setPrefix';
describe('keypair', () => {
const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]);
@@ -18,6 +19,7 @@ describe('keypair', () => {
keypair = new Keyring();
keypair.addFromSeed(seedOne);
setPrefix(42);
});
it('adds the pair', () => {
@@ -27,9 +29,11 @@ describe('keypair', () => {
});
it('adds from a mnemonic', () => {
setPrefix(68);
expect(
keypair.addFromMnemonic('seed sock milk update focus rotate barely fade car face mechanic mercy').address()
).toEqual('5E2DuAdUMXXu8yvuydaEtcGBN6MFJpa9cCmvR1wh9WS3GLg4');
keypair.addFromMnemonic('moral movie very draw assault whisper awful rebuild speed purity repeat card').address()
).toEqual('7pDZKLEixRnF6Q5jzr7DsCEiNPt3d6Rknc14SyUcnRwTQK14');
});
it('allows publicKeys retrieval', () => {
+1 -2
View File
@@ -5,7 +5,6 @@
import { KeyringInstance, KeyringPair, KeyringPair$Json, KeyringPair$Meta } from './types';
import mnemonicToSecret from '@polkadot/util-crypto/mnemonic/toSecret';
import naclKeypairFromSecret from '@polkadot/util-crypto/nacl/keypair/fromSecret';
import naclKeypairFromSeed from '@polkadot/util-crypto/nacl/keypair/fromSeed';
import hexToU8a from '@polkadot/util/hex/toU8a';
@@ -34,7 +33,7 @@ export default class Keyring implements KeyringInstance {
}
addFromMnemonic (mnemonic: string, meta?: KeyringPair$Meta): KeyringPair {
return this.addPair(createPair(naclKeypairFromSecret(mnemonicToSecret(mnemonic)), meta));
return this.addFromSeed(mnemonicToSecret(mnemonic).subarray(0, 32), meta);
}
addFromSeed (seed: Uint8Array, meta?: KeyringPair$Meta): KeyringPair {