Skip to content

Commit

Permalink
Merge pull request #104 from bitcoinjs/typescript
Browse files Browse the repository at this point in the history
v3 (typescript included)
  • Loading branch information
junderw committed Apr 3, 2019
2 parents 2729d2a + 981b010 commit cd6bcdb
Show file tree
Hide file tree
Showing 37 changed files with 17,250 additions and 1,678 deletions.
Empty file added .prettierignore
Empty file.
4 changes: 4 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,10 @@ node_js:
- "lts/*"
- "9"
- "10"
matrix:
include:
- node_js: "lts/*"
env: TEST_SUITE=gitdiff:ci
env:
- TEST_SUITE=test
script: npm run-script $TEST_SUITE
10 changes: 10 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,10 @@
# 3.0.0
__added__
- Added TypeScript support (#104)
- Added support for excluding wordlists from packages (#105)

__changed__
- Changed `mnemonicToSeed` to use async, sync version moved to `mnemonicToSeedSync` (#104)

__removed__
- Removed explicit hex methods (use `toString('hex')` on the Buffer) (#104)
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,12 @@
# Check the CONTRIBUTING doc on bitcoinjs-lib for info

[Contributing to BitcoinJS](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/CONTRIBUTING.md)

# TypeScript

* `npm install`
* Modify TypeScript in ts_src folder
* Run `npm run format`
* Run `npm run lint` and make any necessary changes to remove errors
* Run `npm test` and make sure the tests pass
* Run `git diff` and make sure the diff in the JS files in the src folder match the changes you made to the TS files
74 changes: 65 additions & 9 deletions README.md
Expand Up @@ -16,6 +16,62 @@ When a checksum is invalid, warn the user that the phrase is not something gener

However, there should be other checks in place, such as checking to make sure the user is inputting 12 words or more separated by a space. ie. `phrase.trim().split(/\s+/g).length >= 12`

## Removing wordlists from webpack/browserify

Browserify/Webpack bundles can get very large if you include all the wordlists, so you can now exclude wordlists to make your bundle lighter.

For example, if we want to exclude all wordlists besides chinese_simplified, you could build using the browserify command below.

```bash
$ browserify -r bip39 -s bip39 \
--exclude=./wordlists/english.json \
--exclude=./wordlists/japanese.json \
--exclude=./wordlists/spanish.json \
--exclude=./wordlists/italian.json \
--exclude=./wordlists/french.json \
--exclude=./wordlists/korean.json \
--exclude=./wordlists/chinese_traditional.json \
> bip39.browser.js
```

This will create a bundle that only contains the chinese_simplified wordlist, and it will be the default wordlist for all calls without explicit wordlists.

This is how it will look in the browser console.

```javascript
> bip39.entropyToMnemonic('00000000000000000000000000000000')
"的 的 的 的 的 的 的 的 的 的 的 在"
> bip39.wordlists.chinese_simplified
Array(2048) [ "", "", "", "", "", "", "", "", "", "", … ]
> bip39.wordlists.english
undefined
> bip39.wordlists.japanese
undefined
> bip39.wordlists.spanish
undefined
> bip39.wordlists.italian
undefined
> bip39.wordlists.french
undefined
> bip39.wordlists.korean
undefined
> bip39.wordlists.chinese_traditional
undefined
```

For a list of supported wordlists check the wordlists folder. The name of the json file (minus the extension) is the name of the key to access the wordlist.

You can also change the default wordlist at runtime if you dislike the wordlist you were given as default.

```javascript
> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あまい ろんり"
> bip39.setDefaultWordlist('italian')
undefined
> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"abaco abaco abaco abaco abaco abaco abaco abaco abaco abaco aforisma zibetto"
```

## Installation
``` bash
npm install bip39
Expand All @@ -27,21 +83,21 @@ npm install bip39
const mnemonic = bip39.generateMnemonic()
// => 'seed sock milk update focus rotate barely fade car face mechanic mercy'

bip39.mnemonicToSeedHex('basket actual')
bip39.mnemonicToSeedSync('basket actual').toString('hex')
// => '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f'

bip39.mnemonicToSeed('basket actual')
bip39.mnemonicToSeedSync('basket actual')
// => <Buffer 5c f2 d4 a8 b0 35 5e 90 29 5b df c5 65 a0 22 a4 09 af 06 3d 53 65 bb 57 bf 74 d9 52 8f 49 4b fa 44 00 f5 3d 83 49 b8 0f da e4 40 82 d7 f9 54 1e 1d ba 2b ...>

// mnemonicToSeed and mnemonicToSeedHex have async versions
// mnemonicToSeedAsync mnemonicToSeedHexAsync are more performance oriented
bip39.mnemonicToSeedAsync('basket actual').then(console.log)
// mnemonicToSeed has an synchronous version
// mnemonicToSeedSync is less performance oriented
bip39.mnemonicToSeed('basket actual').then(console.log)
// => <Buffer 5c f2 d4 a8 b0 35 5e 90 29 5b df c5 65 a0 22 a4 09 af 06 3d 53 65 bb 57 bf 74 d9 52 8f 49 4b fa 44 00 f5 3d 83 49 b8 0f da e4 40 82 d7 f9 54 1e 1d ba 2b ...>

bip39.mnemonicToSeedHexAsync('basket actual').then(console.log)
bip39.mnemonicToSeed('basket actual').then(bytes => bytes.toString('hex')).then(console.log)
// => '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f'

bip39.mnemonicToSeed('basket actual', 'a password')
bip39.mnemonicToSeedSync('basket actual', 'a password')
// => <Buffer 46 16 a4 4f 2c 90 b9 69 02 14 b8 fd 43 5b b4 14 62 43 de 10 7b 30 87 59 0a 3b b8 d3 1b 2f 3a ef ab 1d 4b 52 6d 21 e5 0a 04 02 3d 7a d0 66 43 ea 68 3b ... >

bip39.validateMnemonic(mnemonic)
Expand All @@ -53,11 +109,11 @@ bip39.validateMnemonic('basket actual')


``` js
var bip39 = require('bip39')
const bip39 = require('bip39')

// defaults to BIP39 English word list
// uses HEX strings for entropy
var mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000')
const mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000')
// => abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about

// reversible
Expand Down
178 changes: 0 additions & 178 deletions index.js

This file was deleted.

0 comments on commit cd6bcdb

Please sign in to comment.