Skip to content

Commit 18d222b

Browse files
committed
Add bip39 submodule
1 parent 8afd467 commit 18d222b

File tree

23 files changed

+18780
-6
lines changed

23 files changed

+18780
-6
lines changed

packages/ethereum-cryptography/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
/node_modules
99
/scryptsy-without-crypto-build
1010
/hdkey-without-crypto-build
11+
/bip39-without-wordlists
1112
/vendor
13+
/bip39
1214
elliptic-secp256k1.js
1315
scryptsy-without-crypto.js
1416
hdkey-without-crypto.js
17+
bip39-without-wordlists.js

packages/ethereum-cryptography/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The cryptographic primitives included are:
3030
* [AES](#aes-submodule)
3131
* [Secp256k1](#secp256k1-submodule)
3232
* [Hierarchical Deterministic keys derivation](#hierarchical-deterministic-keys-submodule)
33+
* [Seed recovery phrases](#seed-recovery-phrases)
3334

3435
## Installation
3536

@@ -409,6 +410,64 @@ const childkey = hdkey.derive("m/0/2147483647'/1");
409410
console.log(childkey.privateExtendedKey);
410411
```
411412
413+
## Seed recovery phrases
414+
415+
The `bip39` submodule provides functions to generate, validate and use seed
416+
recovery phrases according to [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki).
417+
418+
### Function types
419+
420+
```ts
421+
function generateMnemonic(wordlist: string[], strength: number = 128): string;
422+
423+
function mnemonicToEntropy(mnemonic: string, wordlist: string[]): Buffer;
424+
425+
function entropyToMnemonic(entropy: Buffer, wordlist: string[]): string;
426+
427+
function validateMnemonic(mnemonic: string, wordlist: string[]): boolean;
428+
429+
async function mnemonicToSeed(mnemonic: string, passphrase: string = ""): Promise<Buffer>;
430+
431+
function mnemonicToSeedSync(mnemonic: string, passphrase: string = ""): Buffer;
432+
```
433+
434+
### Word lists
435+
436+
This submodule also contains the word lists defined by BIP39 for Czech, English,
437+
French, Italian, Japanese, Korean, Simplified and Traditional Chinese, and
438+
Spanish. These are not imported by default, as that would increase bundle sizes
439+
too much. Instead, you should import and use them explicitly.
440+
441+
The word lists are exported as a `wordlist` variable in each of these submodules:
442+
443+
* `ethereum-cryptography/bip39/wordlists/czech.js`
444+
445+
* `ethereum-cryptography/bip39/wordlists/english.js`
446+
447+
* `ethereum-cryptography/bip39/wordlists/french.js`
448+
449+
* `ethereum-cryptography/bip39/wordlists/italian.js`
450+
451+
* `ethereum-cryptography/bip39/wordlists/japanese.js`
452+
453+
* `ethereum-cryptography/bip39/wordlists/korean.js`
454+
455+
* `ethereum-cryptography/bip39/wordlists/simplified-chinese.js`
456+
457+
* `ethereum-cryptography/bip39/wordlists/spanish.js`
458+
459+
* `ethereum-cryptography/bip39/wordlists/traditional-chinese.js`
460+
461+
### Example usage
462+
463+
```js
464+
465+
const { generateMnemonic } = require("ethereum-cryptography/bip39");
466+
const { wordlist } = require("ethereum-cryptography/bip39/wordlists/english");
467+
468+
console.log(generateMnemonic(wordlist));
469+
```
470+
412471
## Browser usage
413472
414473
This package works with all the major Javascript bundlers. It is
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# hdkey-without-crypto
2+
3+
This folder contains the necessary config to bundle `bip39` with Rollup, but
4+
without including its wordlits.
5+
6+
## Why do we do this?
7+
8+
Including its wordlists would make this module huge.
9+
10+
## How does this work?
11+
12+
We have a git submodule with the latests release of `bip39`, and bundle it
13+
with Rollup.
14+
15+
We replace the imports of `./_wordlists` with an empty module.
16+

packages/ethereum-cryptography/bip39-without-wordlists-config/empty-module.js

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import commonjs from "rollup-plugin-commonjs";
2+
import alias from "rollup-plugin-alias";
3+
4+
export default {
5+
input: __dirname + "/../bip39-lib/src/index.js",
6+
output: {
7+
file:
8+
__dirname +
9+
"/../bip39-without-wordlists-build/bip39-without-wordlists.js",
10+
format: "cjs",
11+
sourcemap: false,
12+
exports: "named"
13+
},
14+
external: ["create-hash", "randombytes", "pbkdf2"],
15+
plugins: [
16+
alias({
17+
entries: [
18+
{ find: "./_wordlists", replacement: __dirname + "/empty-module.js" }
19+
]
20+
}),
21+
commonjs()
22+
]
23+
};

packages/ethereum-cryptography/package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"src",
3939
"vendor",
4040
"shims",
41+
"bip39",
4142
"*.js",
4243
"*.js.map",
4344
"*.d.ts",
@@ -57,16 +58,17 @@
5758
"./aes.js": "./pure/aes.js",
5859
"./hdkey.js": "./pure/hdkey.js",
5960
"./blake2b.js": "./pure/blake2b.js",
60-
"./random.js": "./pure/random.js"
61+
"./random.js": "./pure/random.js",
62+
"./bip39/index.js": "./pure/bip39/index.js"
6163
},
6264
"sideEffects": false,
6365
"scripts": {
6466
"prepare": "npm run build",
65-
"build": "npm-run-all build:tsc elliptic-secp256k1:build elliptic-secp256k1:copy scryptsy-without-crypto:build scryptsy-without-crypto:copy hdkey-without-crypto:build hdkey-without-crypto:copy",
67+
"build": "npm-run-all build:tsc elliptic-secp256k1:build elliptic-secp256k1:copy scryptsy-without-crypto:build scryptsy-without-crypto:copy hdkey-without-crypto:build hdkey-without-crypto:copy bip39-without-wordlists:build bip39-without-wordlists:copy",
6668
"build:tsc": "tsc --project tsconfig.prod.json",
67-
"test": "npm-run-all elliptic-secp256k1:build scryptsy-without-crypto:build hdkey-without-crypto:build test:node",
69+
"test": "npm-run-all elliptic-secp256k1:build scryptsy-without-crypto:build hdkey-without-crypto:build bip39-without-wordlists:build test:node",
6870
"test:node": "mocha",
69-
"clean": "rimraf vendor test-builds pure shims scryptsy-without-crypto-build hdkey-without-crypto-build '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'",
71+
"clean": "rimraf vendor test-builds pure shims scryptsy-without-crypto-build hdkey-without-crypto-build bip39-without-wordlists-build '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'",
7072
"lint": "tslint --project tsconfig.json",
7173
"lint:fix": "tslint --fix --project tsconfig.json",
7274
"browser-tests": "npm-run-all browser-tests:build browser-tests:test",
@@ -81,7 +83,9 @@
8183
"scryptsy-without-crypto:build": "bash -x scripts/build-scryptsy-without-crypto.sh",
8284
"scryptsy-without-crypto:copy": "mkdir -p vendor pure/vendor && cp src/vendor/scryptsy-without-crypto.js ./vendor && cp src/pure/vendor/scryptsy-without-crypto.js ./pure/vendor",
8385
"hdkey-without-crypto:build": "bash -x scripts/build-hdkey-without-crypto.sh",
84-
"hdkey-without-crypto:copy": "mkdir -p vendor pure/vendor && cp src/vendor/hdkey-without-crypto.js ./vendor && cp src/pure/vendor/hdkey-without-crypto.js ./pure/vendor"
86+
"hdkey-without-crypto:copy": "mkdir -p vendor pure/vendor && cp src/vendor/hdkey-without-crypto.js ./vendor && cp src/pure/vendor/hdkey-without-crypto.js ./pure/vendor",
87+
"bip39-without-wordlists:build": "bash -x scripts/build-bip39-without-wordlists.sh",
88+
"bip39-without-wordlists:copy": "mkdir -p pure/vendor && cp src/pure/vendor/bip39-without-wordlists.js ./pure/vendor"
8589
},
8690
"devDependencies": {
8791
"@types/chai": "^4.2.1",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set -e
2+
3+
echo "Bundling bip39 with Rollup"
4+
npx rollup -c bip39-without-wordlists-config/rollup.config.js
5+
6+
echo "Copying output"
7+
cp bip39-without-wordlists-build/bip39-without-wordlists.js ./src/pure/vendor

packages/ethereum-cryptography/scripts/build-browser-tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ mkdir -p test-builds/tsc/src/vendor test-builds/tsc/src/pure/vendor
2020
cp src/vendor/hdkey-without-crypto.js test-builds/tsc/src/vendor
2121
cp src/pure/vendor/hdkey-without-crypto.js test-builds/tsc/src/pure/vendor
2222

23+
echo "Building bip39-without-wordlists"
24+
npm run bip39-without-wordlists:build
25+
mkdir -p test-builds/tsc/src/pure/vendor
26+
cp src/pure/vendor/bip39-without-wordlists.js test-builds/tsc/src/pure/vendor
27+
2328
echo "Building tests with Parcel"
2429
npx parcel build --no-cache --no-minify test-builds/tsc/src/pure/*.js test-builds/tsc/test/pure/*.js -d test-builds/parcel
2530

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as bip39Pure from "../pure/bip39";
2+
3+
let bip39Module: typeof bip39Pure;
4+
5+
try {
6+
// tslint:disable-next-line no-implicit-dependencies
7+
bip39Module = require("ethereum-cryptography-native/bip39");
8+
} catch {
9+
bip39Module = require("../pure/bip39");
10+
}
11+
12+
export const generateMnemonic = bip39Module.generateMnemonic;
13+
export const mnemonicToEntropy = bip39Module.mnemonicToEntropy;
14+
export const entropyToMnemonic = bip39Module.entropyToMnemonic;
15+
export const validateMnemonic = bip39Module.validateMnemonic;
16+
export const mnemonicToSeed = bip39Module.mnemonicToSeed;
17+
export const mnemonicToSeedSync = bip39Module.mnemonicToSeedSync;

0 commit comments

Comments
 (0)