Internal bip32 mnemonicGenerate takes numWords param (#1494)

* Internal bip32 mnemonicGenerate takes numWords param

* Bump deps

* Calculate Calculate strength

* Remove duplicated assert
This commit is contained in:
Jaco
2022-05-20 13:00:07 +03:00
committed by GitHub
parent cab50dbf7a
commit 7a3e1c23aa
19 changed files with 669 additions and 610 deletions
+1 -1
View File
@@ -21,7 +21,7 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@ledgerhq/hw-transport": "^6.27.1",
"@ledgerhq/hw-transport-webhid": "^6.27.1",
"@ledgerhq/hw-transport-webusb": "^6.27.1",
+2 -2
View File
@@ -22,10 +22,10 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@ledgerhq/hw-transport": "^6.27.1",
"@polkadot/hw-ledger-transports": "9.2.2-3",
"@polkadot/util": "9.2.2-3",
"@zondax/ledger-substrate": "^0.28.0"
"@zondax/ledger-substrate": "^0.29.0"
}
}
+1 -1
View File
@@ -23,7 +23,7 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/util": "9.2.2-3",
"@polkadot/util-crypto": "9.2.2-3"
},
+1 -1
View File
@@ -20,7 +20,7 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/util": "9.2.2-3",
"@substrate/ss58-registry": "^1.18.0"
},
+1 -1
View File
@@ -29,7 +29,7 @@
},
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@noble/hashes": "1.0.0",
"@noble/secp256k1": "1.5.5",
"@polkadot/networks": "9.2.2-3",
+5 -8
View File
@@ -12,6 +12,7 @@
// - Use util-crypto randomAsU8a (instead of randombytes)
// - Remove setting of wordlist passing of wordlist in functions
// - Remove mnemonicToSeed (we only use the sync variant)
// - generateMnemonic takes number of words (instead of strength)
import { assert, stringToU8a, u8aToU8a } from '@polkadot/util';
@@ -73,7 +74,7 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
// calculate the checksum and compare
const entropyBytes = entropyBits.match(/(.{1,8})/g)?.map(binaryToByte);
assert(entropyBytes && entropyBytes.length % 4 === 0 && entropyBytes.length >= 16 && entropyBytes.length <= 32, INVALID_ENTROPY);
assert(entropyBytes && (entropyBytes.length % 4 === 0) && (entropyBytes.length >= 16) && (entropyBytes.length <= 32), INVALID_ENTROPY);
const entropy = u8aToU8a(entropyBytes);
const newChecksum = deriveChecksumBits(entropy);
@@ -85,7 +86,7 @@ export function mnemonicToEntropy (mnemonic: string): Uint8Array {
export function entropyToMnemonic (entropy: Uint8Array): string {
// 128 <= ENT <= 256
assert(entropy.length % 4 === 0 && entropy.length >= 16 && entropy.length <= 32, INVALID_ENTROPY);
assert((entropy.length % 4 === 0) && (entropy.length >= 16) && (entropy.length <= 32), INVALID_ENTROPY);
const entropyBits = bytesToBinary(Array.from(entropy));
const checksumBits = deriveChecksumBits(entropy);
@@ -97,12 +98,8 @@ export function entropyToMnemonic (entropy: Uint8Array): string {
.join(' ');
}
export function generateMnemonic (strength?: number): string {
strength = strength || 128;
assert(strength % 32 === 0, INVALID_ENTROPY);
return entropyToMnemonic(randomAsU8a(strength / 8));
export function generateMnemonic (numWords: 12 | 15 | 18 | 21 | 24): string {
return entropyToMnemonic(randomAsU8a((numWords / 3) * 4));
}
export function validateMnemonic (mnemonic: string): boolean {
+1 -10
View File
@@ -6,15 +6,6 @@ import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
import { generateMnemonic } from './bip39';
// mapping of words to the actual strength (as expected)
const STRENGTH_MAP = {
12: 16 * 8,
15: 20 * 8,
18: 24 * 8,
21: 28 * 8,
24: 32 * 8
};
/**
* @name mnemonicGenerate
* @summary Creates a valid mnemonic string using using [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
@@ -30,5 +21,5 @@ const STRENGTH_MAP = {
export function mnemonicGenerate (numWords: 12 | 15 | 18 | 21 | 24 = 12, onlyJs?: boolean): string {
return !hasBigInt || (!onlyJs && isReady())
? bip39Generate(numWords)
: generateMnemonic(STRENGTH_MAP[numWords]);
: generateMnemonic(numWords);
}
+1 -1
View File
@@ -37,7 +37,7 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-bigint": "9.2.2-3",
"@polkadot/x-global": "9.2.2-3",
"@polkadot/x-textdecoder": "9.2.2-3",
+1 -1
View File
@@ -22,7 +22,7 @@
"type": "module",
"version": "9.2.2-3",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3"
}
}
+1 -1
View File
@@ -20,7 +20,7 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/util": "9.2.2-3",
"buffer-es6": "^4.9.3"
}
+1 -1
View File
@@ -22,7 +22,7 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3",
"@types/node-fetch": "^2.6.1",
"node-fetch": "^2.6.7"
+1 -1
View File
@@ -20,6 +20,6 @@
"version": "9.2.2-3",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.17.9"
"@babel/runtime": "^7.18.0"
}
}
+2 -2
View File
@@ -22,10 +22,10 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3"
},
"devDependencies": {
"@types/react-native": "^0.67.6"
"@types/react-native": "^0.67.7"
}
}
+1 -1
View File
@@ -22,7 +22,7 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3"
}
}
+1 -1
View File
@@ -22,7 +22,7 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3"
}
}
+1 -1
View File
@@ -22,7 +22,7 @@
"main": "node.js",
"react-native": "react-native.js",
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime": "^7.18.0",
"@polkadot/x-global": "9.2.2-3",
"@types/websocket": "^1.0.5",
"websocket": "^1.0.34"