From 7c720e2b53a817c1e679402d9be171cfa0e99951 Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Tue, 9 Dec 2014 20:21:28 +0100 Subject: [PATCH 1/2] fix example in README First example failed with AssertionError: Invalid mnemonic --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7a60471..deb869b 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,12 @@ JavaScript implementation of [Bitcoin BIP39](https://github.com/bitcoin/bips/blo ```javascript var bip39 = require('bip39') -var mnemonic = bip39.entropyToMnemonic('1337') // hex input, defaults to BIP39 English word list -// 'basket actual' +var mnemonic = bip39.entropyToMnemonic('133755ff') // hex input, defaults to BIP39 English word list +// 'basket rival lemon' + bip39.mnemonicToEntropy(mnemonic) // hex input, defaults to BIP39 English word list -// '1337' +// '133755ff' // Generate a random mnemonic using crypto.randomBytes mnemonic = bip39.generateMnemonic() // strength defaults to 128 bits From 2c962e773571e9f22582b7a5328440bb8dcd951b Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Tue, 9 Dec 2014 20:23:15 +0100 Subject: [PATCH 2/2] add testcases mimicing the examples in README --- test/index.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/index.js b/test/index.js index f6fa690..66ca095 100644 --- a/test/index.js +++ b/test/index.js @@ -134,4 +134,38 @@ describe('BIP39', function() { }) }) }) + + describe('Examples in readme', function() { + var bip39 = BIP39 + + var mnemonic = bip39.entropyToMnemonic('133755ff') // hex input, defaults to BIP39 English word list + // 'basket rival lemon' + assert.ok((/^\w+ \w+ \w+$/).test(mnemonic)) + + var temp = bip39.mnemonicToEntropy(mnemonic) // hex input, defaults to BIP39 English word list + // '133755ff' + assert.equal(temp, '133755ff') + + // Generate a random mnemonic using crypto.randomBytes + mnemonic = bip39.generateMnemonic() // strength defaults to 128 bits + //'bench maximum balance appear cousin negative muscle inform enjoy chief vocal hello' + assert.ok(/^(\w+ ){11}\w+$/.test(mnemonic)) + + var str = bip39.mnemonicToSeedHex('basket actual') + //'5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f' + assert.equal(str, '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f') + + var buff = bip39.mnemonicToSeed('basket actual') + var fiveC = 5*16+12 + assert.equal(buff[0], fiveC) + // + + var bool = bip39.validateMnemonic(mnemonic) + // true + assert.ok(bool) + + bool = bip39.validateMnemonic('basket actual') + // false + assert.ok(! bool) + }) })