Cleanup Buffer.from usages, expand tests (#869)

* Cleanup Bffer.from usages, expand tests

* validate mnemonic tests & cleanups
This commit is contained in:
Jaco Greeff
2021-02-04 08:50:07 +01:00
committed by GitHub
parent 9621d90613
commit ca1362c595
8 changed files with 68 additions and 30 deletions
@@ -11,15 +11,10 @@ const ED25519_CRYPTO = 'ed25519 seed';
// gets an xprv from a mnemonic
export function ledgerMaster (mnemonic: string): Uint8Array {
const seed = mnemonicToSeedSync(mnemonic);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const chainCode = hmacSha256(ED25519_CRYPTO, new Uint8Array([1, ...seed]));
let priv;
while (!priv || (priv[31] & 0b0010_0000)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
priv = hmacSha512(ED25519_CRYPTO, priv || seed);
}
+3 -7
View File
@@ -15,7 +15,7 @@
import hash from 'hash.js';
import { assert, u8aToU8a } from '@polkadot/util';
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
import { pbkdf2Encode } from '../pbkdf2';
import { randomAsU8a } from '../random/asU8a';
@@ -45,13 +45,9 @@ function deriveChecksumBits (entropyBuffer: Uint8Array): string {
return bytesToBinary(Array.from(result)).slice(0, CS);
}
function salt (password?: string): string {
return 'mnemonic' + (password || '');
}
export function mnemonicToSeedSync (mnemonic: string, password?: string): Uint8Array {
const mnemonicBuffer = Buffer.from(normalize(mnemonic), 'utf8');
const saltBuffer = Buffer.from(salt(normalize(password)), 'utf8');
const mnemonicBuffer = stringToU8a(normalize(mnemonic));
const saltBuffer = stringToU8a(`mnemonic${normalize(password)}`);
return pbkdf2Encode(mnemonicBuffer, saltBuffer).password;
}
@@ -1,12 +1,12 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aToHex } from '@polkadot/util';
import { u8aEq, u8aToHex } from '@polkadot/util';
import { waitReady } from '@polkadot/wasm-crypto';
import { mnemonicToLegacySeed } from './';
const MENMONIC = 'seed sock milk update focus rotate barely fade car face mechanic mercy';
const MNEMONIC = 'seed sock milk update focus rotate barely fade car face mechanic mercy';
const SEED = '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027';
describe('mnemonicToLegacySeed', (): void => {
@@ -14,11 +14,30 @@ describe('mnemonicToLegacySeed', (): void => {
await waitReady();
});
[undefined, 'foo', 'bar'].forEach((password): void => {
it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => {
expect(
u8aEq(
mnemonicToLegacySeed(MNEMONIC, password, true),
mnemonicToLegacySeed(MNEMONIC, password, false)
)
).toEqual(true);
});
});
[false, true].forEach((onlyJs): void => {
it(`generates a valid seed (onlyJs = ${onlyJs.toString()})`, (): void => {
expect(
u8aToHex(mnemonicToLegacySeed(MENMONIC, undefined, onlyJs))
u8aToHex(mnemonicToLegacySeed(MNEMONIC, undefined, onlyJs))
).toEqual(SEED);
});
});
[false, true].forEach((onlyJs): void => {
it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => {
expect(
() => mnemonicToLegacySeed('foo bar baz', undefined, onlyJs)
).toThrow(/mnemonic specified/);
});
});
});
@@ -1,9 +1,11 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '@polkadot/util';
import { bip39ToSeed, isReady } from '@polkadot/wasm-crypto';
import { mnemonicToSeedSync } from './bip39';
import { mnemonicValidate } from './validate';
/**
* @name toSeed
@@ -23,6 +25,8 @@ import { mnemonicToSeedSync } from './bip39';
* ```
*/
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs = false): Uint8Array {
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
return isReady() && !onlyJs
? bip39ToSeed(mnemonic, password)
: mnemonicToSeedSync(mnemonic, password).subarray(0, 32);
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aToHex } from '@polkadot/util';
import { u8aEq, u8aToHex } from '@polkadot/util';
import tests from '../schnorrkel/keypair/testing';
import { cryptoWaitReady } from '..';
@@ -15,6 +15,17 @@ describe('mnemonicToMiniSecret', (): void => {
await cryptoWaitReady();
});
[undefined, 'foo', 'bar'].forEach((password): void => {
it(`generates Wasm & Js equivalents (password = ${password || 'undefined'})`, (): void => {
expect(
u8aEq(
mnemonicToMiniSecret(MNEMONIC, password, true),
mnemonicToMiniSecret(MNEMONIC, password, false)
)
).toEqual(true);
});
});
[false, true].forEach((onlyJs): void => {
it(`generates a valid seed (onlyJs = ${onlyJs.toString()})`, (): void => {
expect(
@@ -35,4 +46,12 @@ describe('mnemonicToMiniSecret', (): void => {
});
});
});
[false, true].forEach((onlyJs): void => {
it(`fails with non-mnemonics (onlyJs = ${onlyJs.toString()})`, (): void => {
expect(
() => mnemonicToMiniSecret('foo bar baz', undefined, onlyJs)
).toThrow(/mnemonic specified/);
});
});
});
@@ -1,19 +1,22 @@
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { stringToU8a, u8aToBuffer } from '@polkadot/util';
import { assert, stringToU8a } from '@polkadot/util';
import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
import { pbkdf2Encode } from '../pbkdf2';
import { mnemonicToEntropy } from './toEntropy';
import { mnemonicValidate } from './validate';
export function mnemonicToMiniSecret (mnemonic: string, password = '', onlyJs = false): Uint8Array {
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
if (isReady() && !onlyJs) {
return bip39ToMiniSecret(mnemonic, password);
}
const entropy = u8aToBuffer(mnemonicToEntropy(mnemonic));
const salt = u8aToBuffer(stringToU8a(`mnemonic${password}`));
const entropy = mnemonicToEntropy(mnemonic);
const salt = stringToU8a(`mnemonic${password}`);
// return the first 32 bytes as the seed
return pbkdf2Encode(entropy, salt).password.slice(0, 32);
+10 -8
View File
@@ -15,15 +15,17 @@ describe('pbkdf2Encode', (): void => {
await waitReady();
});
it('has equivalent Wasm & JS results', (): void => {
const salt = randomAsU8a();
[256, 1024, 2048].forEach((rounds): void => {
it(`has equivalent Wasm & JS results (${rounds} rounds)`, (): void => {
const salt = randomAsU8a();
expect(
u8aEq(
pbkdf2Encode(TEST_PASSWORD, salt, 2048, false).password,
pbkdf2Encode(TEST_PASSWORD, salt, 2048, true).password
)
).toBe(true);
expect(
u8aEq(
pbkdf2Encode(TEST_PASSWORD, salt, rounds, false).password,
pbkdf2Encode(TEST_PASSWORD, salt, rounds, true).password
)
).toBe(true);
});
});
[false, true].forEach((onlyJs): void => {
+3 -3
View File
@@ -3,18 +3,18 @@
// Adapted from https://gist.github.com/calvinmetcalf/91e8e84dc63c75f2aa53
import { u8aConcat } from '@polkadot/util';
import { bnToU8a, u8aConcat } from '@polkadot/util';
import { hmacSha512 } from '../hmac';
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
let out = new Uint8Array();
let num = 0;
const block = Buffer.concat([salt, Buffer.from([0, 0, 0, 0])]);
const block = u8aConcat(salt, new Uint8Array(4));
while (out.length < len) {
num++;
block.writeUInt32BE(num, salt.length);
block.set(bnToU8a(num, { bitLength: 32, isLe: false }), salt.length);
let prev = hmacSha512(password, block);
const md = prev;