Swap bip39 to wasm-crypto (#373)

* Swap bip39 to wasm-crypto

* Bump upstream deps
This commit is contained in:
Jaco Greeff
2019-03-20 11:32:05 +01:00
committed by GitHub
parent ee050501af
commit fe0bd25acc
31 changed files with 1134 additions and 449 deletions
+4
View File
@@ -1,3 +1,7 @@
# 0.39.1
- Pull in bip39 generation functions from @poladkot/wasm-crypto with a fallback to the pure JS versions
# 0.38.1
- Align with current substrate master for sr25519 keys and hard/soft derivation
@@ -1,5 +1,5 @@
{
"name": "02_sign_verify_message_nacl",
"name": "03_mnemonic_generate_validate_bip39",
"version": "0.1.0",
"description": "Example showing how to sign and verify a message using NaCl",
"main": "index.js",
+3 -3
View File
@@ -10,7 +10,7 @@
],
"resolutions": {
"babel-core": "^7.0.0-bridge.0",
"typescript": "^3.3.3333"
"typescript": "^3.3.4000"
},
"scripts": {
"build": "polkadot-dev-build-ts && polkadot-dev-build-docs",
@@ -20,8 +20,8 @@
"test": "jest --coverage"
},
"devDependencies": {
"@babel/core": "^7.3.4",
"@polkadot/dev": "^0.27.1",
"@babel/core": "^7.4.0",
"@polkadot/dev": "^0.28.0-beta.0",
"@polkadot/ts": "^0.1.56",
"gh-pages": "^2.0.1"
}
+1 -1
View File
@@ -28,7 +28,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/chainspec#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1"
}
}
+1 -1
View File
@@ -28,7 +28,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/db#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/trie-hash": "^0.38.1",
"@polkadot/util": "^0.38.1",
"@types/mkdirp": "^0.5.2",
+1 -1
View File
@@ -30,7 +30,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1",
"@polkadot/util-crypto": "^0.38.1",
"@types/bs58": "^4.0.0",
+1 -1
View File
@@ -28,7 +28,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-codec#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1"
}
}
+1 -1
View File
@@ -28,7 +28,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-db#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/db": "^0.38.1",
"@polkadot/trie-codec": "^0.38.1",
"@polkadot/trie-hash": "^0.38.1",
+1 -1
View File
@@ -28,7 +28,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/trie-hash#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1"
},
"devDependencies": {
+4 -3
View File
@@ -28,14 +28,15 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1",
"@polkadot/wasm-schnorrkel": "^0.1.1",
"@polkadot/wasm-crypto": "^0.2.0-beta.1",
"@polkadot/wasm-schnorrkel": "^0.2.0-beta.1",
"@types/bip39": "^2.4.2",
"@types/pbkdf2": "^3.0.0",
"@types/secp256k1": "^3.5.0",
"@types/webassembly-js-api": "^0.0.2",
"@types/xxhashjs": "^0.2.0",
"@types/xxhashjs": "^0.2.1",
"bip39": "^2.5.0",
"blakejs": "^1.1.0",
"js-sha3": "^0.8.0",
+7 -1
View File
@@ -2,6 +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 './polyfill';
import { wasmWaitReady } from './ready';
import { schnorrkelWaitReady } from './schnorrkel';
export * from './blake2';
@@ -19,7 +22,10 @@ export function cryptoWaitReady (): Promise<boolean> {
// this is a bit convoluted, but since we can do the same for libsodium,
// prepare for multiples, easy to adapt without thinking
return Promise
.all([schnorrkelWaitReady()])
.all([
wasmWaitReady(),
schnorrkelWaitReady()
])
.then(() => true)
.catch((error) => {
console.error('Unable to initialize @polkadot/util-crypto', error);
@@ -4,8 +4,13 @@
import generate from './generate';
import validate from './validate';
import { cryptoWaitReady } from '..';
describe('mnemonicGenerate', () => {
beforeEach(async () => {
await cryptoWaitReady();
});
it('generates a valid mnemonic (default strength)', () => {
const mnemonic = generate();
@@ -2,7 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../polyfill';
import { generateMnemonic } from 'bip39';
import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
type WordCount = 12 | 15 | 18 | 21 | 24;
@@ -28,5 +31,7 @@ const STRENGTH_MAP = {
* ```
*/
export default function mnemonicGenerate (numWords: WordCount = 12): string {
return generateMnemonic(STRENGTH_MAP[numWords]);
return isReady()
? bip39Generate(numWords)
: generateMnemonic(STRENGTH_MAP[numWords]);
}
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../polyfill';
/**
* @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds
*/
@@ -6,8 +6,13 @@ import { u8aToHex } from '@polkadot/util';
import toEntropy from './toEntropy';
import tests from '../schnorrkel/keypair/testing';
import { cryptoWaitReady } from '..';
describe('mnemonicToEntropy', () => {
beforeEach(async () => {
await cryptoWaitReady();
});
tests.forEach(([mnemonic, entropy], index) => {
it(`Created correct entropy for ${index}`, () => {
expect(u8aToHex(toEntropy(mnemonic))).toEqual(entropy);
+10 -5
View File
@@ -2,13 +2,18 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../polyfill';
import { mnemonicToEntropy } from 'bip39';
import { hexAddPrefix, hexToU8a } from '@polkadot/util';
import { bip39ToEntropy, isReady } from '@polkadot/wasm-crypto';
export default function toEntropy (mnemonic: string): Uint8Array {
return hexToU8a(
hexAddPrefix(
mnemonicToEntropy(mnemonic)
)
);
return isReady()
? bip39ToEntropy(mnemonic)
: hexToU8a(
hexAddPrefix(
mnemonicToEntropy(mnemonic)
)
);
}
@@ -6,8 +6,13 @@ import { u8aToHex } from '@polkadot/util';
import mnemonicToMiniSecret from './toMiniSecret';
import tests from '../schnorrkel/keypair/testing';
import { cryptoWaitReady } from '..';
describe('mnemonicToMiniSecret', () => {
beforeEach(async () => {
await cryptoWaitReady();
});
tests.forEach(([mnemonic, , seed], index) => {
it(`Created correct seed for ${index}`, () => {
expect(
@@ -2,12 +2,19 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../polyfill';
import { pbkdf2Sync } from 'pbkdf2';
import { bufferToU8a, stringToU8a, u8aToBuffer } from '@polkadot/util';
import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
import toEntropy from './toEntropy';
export default function toMiniSecret (mnemonic: string, password: string = ''): Uint8Array {
if (isReady()) {
return bip39ToMiniSecret(mnemonic, password);
}
const entropy = u8aToBuffer(toEntropy(mnemonic));
const salt = u8aToBuffer(stringToU8a(`mnemonic${password}`));
@@ -3,8 +3,13 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import validate from './validate';
import { cryptoWaitReady } from '..';
describe('mnemonicValidate', () => {
beforeEach(async () => {
await cryptoWaitReady();
});
it('returns true on valid', () => {
expect(
validate('seed sock milk update focus rotate barely fade car face mechanic mercy')
@@ -2,7 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../polyfill';
import { validateMnemonic } from 'bip39';
import { bip39Validate, isReady } from '@polkadot/wasm-crypto';
/**
* @name mnemonicValidate
@@ -18,5 +21,7 @@ import { validateMnemonic } from 'bip39';
* ```
*/
export default function mnemonicValidate (mnemonic: string): boolean {
return validateMnemonic(mnemonic);
return isReady()
? bip39Validate(mnemonic)
: validateMnemonic(mnemonic);
}
@@ -2,4 +2,4 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '@polkadot/wasm-schnorrkel/crypto-polyfill';
import '@polkadot/wasm-crypto/crypto-polyfill';
+13
View File
@@ -0,0 +1,13 @@
// 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 { isReady, waitReady } from '@polkadot/wasm-crypto';
export function wasmWaitReady (): Promise<boolean> {
return waitReady();
}
export function wasmIsReady (): boolean {
return isReady();
}
@@ -4,7 +4,7 @@
import { Keypair } from '../types';
import './polyfill';
import '../polyfill';
import schnorrkel from '@polkadot/wasm-schnorrkel';
@@ -4,7 +4,7 @@
import { Keypair } from '../types';
import './polyfill';
import '../polyfill';
import schnorrkel from '@polkadot/wasm-schnorrkel';
+1 -1
View File
@@ -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.
import './polyfill';
import '../polyfill';
export { default as schnorrkelDeriveHard } from './deriveHard';
export { default as schnorrkelDeriveSoft } from './deriveSoft';
@@ -4,7 +4,7 @@
import { Keypair } from '../../types';
import '../polyfill';
import '../../polyfill';
import schnorrkel from '@polkadot/wasm-schnorrkel';
+1 -1
View File
@@ -4,7 +4,7 @@
import { Keypair } from '../types';
import './polyfill';
import '../polyfill';
import { assert } from '@polkadot/util';
import schnorrkel from '@polkadot/wasm-schnorrkel';
@@ -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.
import './polyfill';
import '../polyfill';
import schnorrkel from '@polkadot/wasm-schnorrkel';
/**
+1 -1
View File
@@ -30,7 +30,7 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-rlp#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@polkadot/util": "^0.38.1"
},
"devDependencies": {
+4 -4
View File
@@ -28,15 +28,15 @@
},
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
"dependencies": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.4.0",
"@types/bn.js": "^4.11.4",
"@types/camelcase": "^4.1.0",
"@types/camelcase": "^5.2.0",
"@types/deasync": "^0.1.0",
"@types/ip-regex": "^3.0.0",
"bn.js": "^4.11.8",
"camelcase": "^5.0.0",
"camelcase": "^5.2.0",
"chalk": "^2.4.2",
"core-js": "^2.6.5",
"core-js": "^3.0.0",
"ip-regex": "^4.0.0",
"moment": "^2.24.0"
}
+1033 -416
View File
File diff suppressed because it is too large Load Diff