• Stars
    star
    983
  • Rank 46,580 (Top 1.0 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys

BIP39

Build Status NPM

js-standard-style

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys

Reminder for developers

Please remember to allow recovery from mnemonic phrases that have invalid checksums (or that you don't have the wordlist)

When a checksum is invalid, warn the user that the phrase is not something generated by your app, and ask if they would like to use it anyway. This way, your app only needs to hold the wordlists for your supported languages, but you can recover phrases made by other apps in other languages.

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.

$ 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/czech.json \
 --exclude=./wordlists/portuguese.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.

You can also do this in Webpack 5 using the IgnorePlugin. Here is an example of excluding all non-English wordlists

...
plugins: [
  new webpack.IgnorePlugin({
     checkResource(resource) {
       return /.*\/wordlists\/(?!english).*\.json/.test(resource)
     }
   }),
],
...

This is how it will look in the browser console.

> 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.czech
undefined
> bip39.wordlists.portuguese
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.

> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γ„γ“γγ—γ‚“γ€€γ‚γΎγ„γ€€γ‚γ‚“γ‚Š"
> bip39.setDefaultWordlist('italian')
undefined
> bip39.entropyToMnemonic('00000000000000000000000000000fff')
"abaco abaco abaco abaco abaco abaco abaco abaco abaco abaco aforisma zibetto"

Installation

npm install bip39

Examples

// Generate a random mnemonic (uses crypto.randomBytes under the hood), defaults to 128-bits of entropy
const mnemonic = bip39.generateMnemonic()
// => 'seed sock milk update focus rotate barely fade car face mechanic mercy'

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

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 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.mnemonicToSeed('basket actual').then(bytes => bytes.toString('hex')).then(console.log)
// => '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f'

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)
// => true

bip39.validateMnemonic('basket actual')
// => false
const bip39 = require('bip39')

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

// reversible
bip39.mnemonicToEntropy(mnemonic)
// => '00000000000000000000000000000000'

More Repositories

1

bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
TypeScript
5,592
star
2

bip38

BIP38 is a standard process to encrypt Bitcoin and crypto currency private keys that is less susceptible to brute force attacks thus protecting the user.
JavaScript
206
star
3

coinselect

An unspent transaction output (UTXO) selection module for bitcoin.
JavaScript
178
star
4

bip32

A BIP32 compatible library.
JavaScript
151
star
5

bitcoinjs-message

JavaScript
126
star
6

bolt11

A library for encoding and decoding lightning network payment requests as defined in BOLT #11.
JavaScript
93
star
7

bip44-constants

This package provides BIP44 coin constants as found here: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
JavaScript
87
star
8

bs58check

A straight forward implementation of base58check extending upon bs58.
JavaScript
85
star
9

bech32

Bech32 encoding / decoding
TypeScript
84
star
10

wif

Bitcoin Wallet Import Format JS encoding/decoding module
JavaScript
75
star
11

bip32-utils

A small set of utilities for use with BIP32 HD key nodes
JavaScript
74
star
12

fast-dat-parser

Superfast blockchain parser for stats
C++
73
star
13

tiny-secp256k1

A tiny secp256k1 native/JS wrapper
JavaScript
58
star
14

bip21

A BIP21 compatible URL encoding utility library
JavaScript
55
star
15

indexd

An external bitcoind index management service module
JavaScript
53
star
16

merkle-lib

A performance conscious library for merkle root and tree calculations.
JavaScript
29
star
17

bitcoinjs.org

HTML
20
star
18

bip32-wallet

A BIP32 Wallet backed by bitcoinjs-lib, lite on features but heavily tested.
JavaScript
19
star
19

regtest-client

A client library based off of the integration tests of bitcoinjs-lib. This requires a server component.
JavaScript
18
star
20

regtest-server

A regtest server for bitcoinjs-lib testing
JavaScript
18
star
21

bip174

A BIP174 compatible partial Transaction encoding library.
TypeScript
17
star
22

electrum-mnemonic

Electrum Mnemonics (electrum v2 and greater)
TypeScript
15
star
23

payjoin-client

A Payjoin Client Library in JS with TypeScript types
TypeScript
15
star
24

blkdat-stream

A blk*.dat streaming module, useful for parsing the Bitcoin blockchain
JavaScript
14
star
25

aezeed

A package for encoding, decoding, and generating mnemonics of the aezeed specification. (WIP)
TypeScript
13
star
26

bip66

Strict DER signatures
JavaScript
12
star
27

bitcoin-ops

bitcoin OP codes
JavaScript
11
star
28

ecpair

The ECPair module for bitcoinjs-lib
TypeScript
9
star
29

varuint-bitcoin

encode/decode number as bitcoin variable length integer https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
JavaScript
6
star
30

bip69

Lexicographical Indexing of Transaction Inputs and Outputs
JavaScript
4
star
31

playground

Go nuts! PRs can be useful for discussion, and won't be rejected
JavaScript
3
star
32

bip65

A BIP65 absolute lock-time encoding library.
JavaScript
2
star
33

utxo

JavaScript
2
star
34

bip68

A BIP68 relative lock-time encoding library.
JavaScript
2
star
35

minimaldata

A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA
JavaScript
2
star
36

pushdata-bitcoin

encode/decode number as bitcoin pushdata integer
JavaScript
2
star
37

uint8array-tools

A set of tools for Uint8Array to aide in the move from Buffers.
JavaScript
1
star
38

tif

DEPRECATED: Bitcoin Transaction Interchange Format (TIF) decoding/encoding module
JavaScript
1
star