diff --git a/README.md b/README.md index 4f57fe450..57f0d7272 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,11 @@ goals: fromString, toJSON, fromJSON, toBuffer, fromBuffer, toHex, fromHex methods. 4. All standard features of the blockchain are implemented (or will be) and - saved in lib/. All BIPs are correctly implemented and, where appropriate, saved - as bip-xx.js in lib/ (since that is their standard name). In order to allow - rapid development, Yours Bitcoin includes non-standard and experimental - features. Any non-standard features (such as colored coins or stealth - addresses) are labeled as such in index.js as well as in comments. + saved in lib/. All BIPs are correctly implemented and saved + under their functional names to make them easy to find when using the lib. + In order to allow rapid development, Yours Bitcoin includes non-standard + and experimental features. Any non-standard features (such as colored coins + or stealth addresses) are labeled as such in index.js as well as in comments. 5. Expose everything, including dependencies. This makes it possible to develop apps that require fine-grained control over the basics, such as big numbers and diff --git a/entry.js b/entry.js index c638c5198..bff3aeef7 100644 --- a/entry.js +++ b/entry.js @@ -35,9 +35,9 @@ export { version } // Main bitcoin library - bitcoin protocols, standards, cryptography, and // utilities. export * from './lib/address' -export * from './lib/bip-32' -export * from './lib/bip-39' -export * from './lib/bip-39-words' +export * from './lib/hd-wallet' +export * from './lib/mnemonic' +export * from './lib/mnemonic/bip-39-words' export * from './lib/bn' export * from './lib/br' export * from './lib/bsm' diff --git a/index.js b/index.js index 77ac6bd2c..c102a03df 100644 --- a/index.js +++ b/index.js @@ -26,8 +26,6 @@ bsv.version = require('./package').version // Main bitcoin library - bitcoin protocols, standards, cryptography, and // utilities. bsv.Address = require('./lib/address') -bsv.Bip32 = require('./lib/bip-32') -bsv.Bip39 = require('./lib/bip-39') bsv.Bn = require('./lib/bn') bsv.Br = require('./lib/br') bsv.Bsm = require('./lib/bsm') @@ -61,6 +59,10 @@ bsv.Workers = require('./lib/workers') bsv.WorkersResult = require('./lib/workers-result') bsv.cmp = require('./lib/cmp') +// BIPs, accessible by their functional names +bsv.HDWallet = require('./lib/hd-wallet') +bsv.Mnemonic = require('./lib/mnemonic') + // Encryption tools. Some bitcoin standards use Aes encryption, so that's why // these are available. bsv.Ach = require('./lib/ach') diff --git a/lib/constants.js b/lib/constants.js index fd13058dd..72c4e6cdc 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -16,7 +16,7 @@ Constants.Mainnet = { pubKeyHash: 0x00, payToScriptHash: 0x05 }, - Bip32: { + HDWallet: { pubKey: 0x0488b21e, privKey: 0x0488ade4 }, @@ -49,7 +49,7 @@ Constants.Testnet = Object.assign({}, Constants.Mainnet, { pubKeyHash: 0x6f, payToScriptHash: 0xc4 }, - Bip32: { + HDWallet: { pubKey: 0x043587cf, privKey: 0x04358394 }, @@ -72,7 +72,7 @@ Constants.Regtest = Object.assign({}, Constants.Mainnet, { pubKeyHash: 0x6f, payToScriptHash: 0xc4 }, - Bip32: { + HDWallet: { pubKey: 0x043587cf, privKey: 0x04358394 }, @@ -95,7 +95,7 @@ Constants.STN = Object.assign({}, Constants.Mainnet, { pubKeyHash: 0x6f, payToScriptHash: 0xc4 }, - Bip32: { + HDWallet: { pubKey: 0x043587cf, privKey: 0x04358394 }, diff --git a/lib/bip-32.js b/lib/hd-wallet.js similarity index 92% rename from lib/bip-32.js rename to lib/hd-wallet.js index 1c9916b0d..afc0033b4 100644 --- a/lib/bip-32.js +++ b/lib/hd-wallet.js @@ -1,13 +1,13 @@ /** - * Bip32: HD Wallets + * HDWallet: Bip 32 HD Wallets * ================= * * Bip32 is hierarchical deterministic wallets. The standard way to use this is: - * const bip32 = new Bip32().fromRandom() - * const bip32 = new Bip32().fromSeed(buf) - * const bip32 = new Bip32().fromString(string) - * const xprv = bip32.toString() - * const xpub = bip32.toPublic().toString() + * const hdwallet = new HDWallet().fromRandom() + * const hdwallet = new HDWallet().fromSeed(buf) + * const hdwallet = new HDWallet().fromString(string) + * const xprv = hdwallet.toString() + * const xpub = hdwallet.toPublic().toString() * * This code was originally copied from here: * @@ -29,8 +29,8 @@ import { Random } from './random' import { Struct } from './struct' import { Workers } from './workers' -class Bip32 extends Struct { - constructor ( +class HDWallet extends Struct { + constructor( versionBytesNum, depth, parentFingerPrint, @@ -50,12 +50,12 @@ class Bip32 extends Struct { privKey, pubKey }) - constants = constants || Constants.Default.Bip32 + constants = constants || Constants.Default.HDWallet this.Constants = constants this.PrivKey = PrivKey } - fromRandom () { + fromRandom() { this.versionBytesNum = this.Constants.privKey this.depth = 0x00 this.parentFingerPrint = Buffer.from([0, 0, 0, 0]) @@ -66,11 +66,11 @@ class Bip32 extends Struct { return this } - static fromRandom () { + static fromRandom() { return new this().fromRandom() } - fromString (str) { + fromString(str) { return this.fromBuffer(Base58Check.decode(str)) } @@ -78,7 +78,7 @@ class Bip32 extends Struct { * Use workers to convert a bip32 string into a bip32 object without * blocking. */ - async asyncFromString (str) { + async asyncFromString(str) { const args = [str] const workersResult = await Workers.asyncObjectMethod( this, @@ -88,7 +88,7 @@ class Bip32 extends Struct { return this.fromFastBuffer(workersResult.resbuf) } - fromSeed (bytes) { + fromSeed(bytes) { if (!Buffer.isBuffer(bytes)) { throw new Error('bytes must be a buffer') } @@ -111,22 +111,22 @@ class Bip32 extends Struct { return this } - static fromSeed (bytes) { + static fromSeed(bytes) { return new this().fromSeed(bytes) } - async asyncFromSeed (bytes) { + async asyncFromSeed(bytes) { const workersResult = await Workers.asyncObjectMethod(this, 'fromSeed', [ bytes ]) return this.fromFastBuffer(workersResult.resbuf) } - static asyncFromSeed (bytes) { + static asyncFromSeed(bytes) { return new this().asyncFromSeed(bytes) } - fromBuffer (buf) { + fromBuffer(buf) { // Both pub and private extended keys are 78 buf if (buf.length !== 78) { throw new Error('incorrect bip32 data length') @@ -167,7 +167,7 @@ class Bip32 extends Struct { * .fromFastBuffer transmit the public key in uncompressed form, we want it * to be set to compressed when stored in memory. */ - fromFastBuffer (buf) { + fromFastBuffer(buf) { if (buf.length === 0) { return this } @@ -201,7 +201,7 @@ class Bip32 extends Struct { return this } - derive (path) { + derive(path) { const e = path.split('/') if (path === 'm') { @@ -237,14 +237,14 @@ class Bip32 extends Struct { return bip32 } - async asyncDerive (path) { + async asyncDerive(path) { const workersResult = await Workers.asyncObjectMethod(this, 'derive', [ path ]) return new this.constructor().fromFastBuffer(workersResult.resbuf) } - deriveChild (i) { + deriveChild(i) { if (typeof i !== 'number') { throw new Error('i must be a number') } @@ -318,14 +318,14 @@ class Bip32 extends Struct { return ret } - toPublic () { + toPublic() { const bip32 = new this.constructor().fromObject(this) bip32.versionBytesNum = this.Constants.pubKey bip32.privKey = undefined return bip32 } - toBuffer () { + toBuffer() { const isPrivate = this.versionBytesNum === this.Constants.privKey const isPublic = this.versionBytesNum === this.Constants.pubKey if (isPrivate) { @@ -365,7 +365,7 @@ class Bip32 extends Struct { * fancy, slow point multiplication to derive the y value of the public key. * Thus, although .toFastBuffer is not any faster, .fromFastBuffer is faster. */ - toFastBuffer () { + toFastBuffer() { if (!this.versionBytesNum) { return Buffer.alloc(0) } @@ -395,7 +395,7 @@ class Bip32 extends Struct { } } - toString () { + toString() { return Base58Check.encode(this.toBuffer()) } @@ -403,26 +403,26 @@ class Bip32 extends Struct { * Use workers to convert a bip32 object into a bip32 string without * blocking. */ - async asyncToString () { + async asyncToString() { const workersResult = await Workers.asyncObjectMethod(this, 'toString', []) return JSON.parse(workersResult.resbuf.toString()) } - toJSON () { + toJSON() { return this.toFastHex() } - fromJSON (json) { + fromJSON(json) { return this.fromFastHex(json) } - isPrivate () { + isPrivate() { return this.versionBytesNum === this.Constants.privKey } } -Bip32.Mainnet = class extends Bip32 { - constructor ( +HDWallet.Mainnet = class extends HDWallet { + constructor( versionBytesNum, depth, parentFingerPrint, @@ -439,14 +439,14 @@ Bip32.Mainnet = class extends Bip32 { chainCode, privKey, pubKey, - Constants.Mainnet.Bip32, + Constants.Mainnet.HDWallet, PrivKeyClass.Mainnet ) } } -Bip32.Testnet = class extends Bip32 { - constructor ( +HDWallet.Testnet = class extends HDWallet { + constructor( versionBytesNum, depth, parentFingerPrint, @@ -463,10 +463,10 @@ Bip32.Testnet = class extends Bip32 { chainCode, privKey, pubKey, - Constants.Testnet.Bip32, + Constants.Testnet.HDWallet, PrivKeyClass.Testnet ) } } -export { Bip32 } +export { HDWallet } diff --git a/lib/mainnet-bip-32.js b/lib/mainnet-bip-32.js deleted file mode 100644 index 9359e553e..000000000 --- a/lib/mainnet-bip-32.js +++ /dev/null @@ -1,5 +0,0 @@ -import { Bip32 } from './bip-32' - -const MainnetBip32 = Bip32.Mainnet - -export { MainnetBip32 } diff --git a/lib/mainnet-hd-wallet.js b/lib/mainnet-hd-wallet.js new file mode 100644 index 000000000..7607578e5 --- /dev/null +++ b/lib/mainnet-hd-wallet.js @@ -0,0 +1,5 @@ +import { HDWallet } from './hd-wallet' + +const MainnetHDWallet = HDWallet.Mainnet + +export { MainnetHDWallet } diff --git a/lib/bip-39.js b/lib/mnemonic.js similarity index 86% rename from lib/bip-39.js rename to lib/mnemonic.js index de0024ef0..3f553aa76 100644 --- a/lib/bip-39.js +++ b/lib/mnemonic.js @@ -1,5 +1,5 @@ /** - * Bip39: Mnemonic Seeds + * Mnemonic: Bip39 Mnemonic Seeds * ===================== * * Bip39 is a way to turn random entropy into a mnemonic (a string of words @@ -8,11 +8,11 @@ * other way around (i.e., you cannot turn a seed into a mnemonic). The usual * way to use it is either to generate a new one, like this: * - * const mnemonic = new Bip39().fromRandom().toString() + * const mnemonic = new Mnemonic().fromRandom().toString() * * or from a known mnemonic: * - * const seed = new Bip39().fromString(mnemonic).toSeed() + * const seed = new Mnemonic().fromString(mnemonic).toSeed() */ 'use strict' @@ -21,16 +21,16 @@ import { Hash } from './hash' import pbkdf2 from 'pbkdf2' import { Random } from './random' import { Struct } from './struct' -import { wordList } from './bip-39-en-wordlist' +import { wordList } from './mnemonic/bip-39-en-wordlist' import { Workers } from './workers' -class Bip39 extends Struct { - constructor (mnemonic, seed, wordlist = wordList) { +class Mnemonic extends Struct { + constructor(mnemonic, seed, wordlist = wordList) { super({ mnemonic, seed }) this.Wordlist = wordlist } - toBw (bw) { + toBw(bw) { if (!bw) { bw = new Bw() } @@ -50,7 +50,7 @@ class Bip39 extends Struct { return bw } - fromBr (br) { + fromBr(br) { const mnemoniclen = br.readVarIntNum() if (mnemoniclen > 0) { this.mnemonic = br.read(mnemoniclen).toString() @@ -65,7 +65,7 @@ class Bip39 extends Struct { /** * Generate a random new mnemonic from the wordlist. */ - fromRandom (bits) { + fromRandom(bits) { if (!bits) { bits = 128 } @@ -81,11 +81,11 @@ class Bip39 extends Struct { return this } - static fromRandom (bits) { + static fromRandom(bits) { return new this().fromRandom(bits) } - async asyncFromRandom (bits) { + async asyncFromRandom(bits) { if (!bits) { bits = 128 } @@ -95,7 +95,7 @@ class Bip39 extends Struct { 'entropy2Mnemonic', [buf] ) - const bip39 = new Bip39().fromFastBuffer(workersResult.resbuf) + const bip39 = new Mnemonic().fromFastBuffer(workersResult.resbuf) workersResult = await Workers.asyncObjectMethod( bip39, 'mnemonic2Seed', @@ -104,45 +104,45 @@ class Bip39 extends Struct { return this.fromFastBuffer(workersResult.resbuf) } - static asyncFromRandom (bits) { + static asyncFromRandom(bits) { return new this().asyncFromRandom(bits) } - fromEntropy (buf) { + fromEntropy(buf) { this.entropy2Mnemonic(buf) return this } - static fromEntropy (buf) { + static fromEntropy(buf) { return new this().fromEntropy(buf) } - async asyncFromEntropy (buf) { + async asyncFromEntropy(buf) { const workersResult = await Workers.asyncObjectMethod(this, 'fromEntropy', [ buf ]) return this.fromFastBuffer(workersResult.resbuf) } - static asyncFromEntropy (buf) { + static asyncFromEntropy(buf) { return new this().asyncFromEntropy(buf) } - fromString (mnemonic) { + fromString(mnemonic) { this.mnemonic = mnemonic return this } - toString () { + toString() { return this.mnemonic } - toSeed (passphrase) { + toSeed(passphrase) { this.mnemonic2Seed(passphrase) return this.seed } - async asyncToSeed (passphrase) { + async asyncToSeed(passphrase) { if (passphrase === undefined) { passphrase = '' } @@ -155,7 +155,7 @@ class Bip39 extends Struct { * Generate a new mnemonic from some entropy generated somewhere else. The * entropy must be at least 128 bits. */ - entropy2Mnemonic (buf) { + entropy2Mnemonic(buf) { if (!Buffer.isBuffer(buf) || buf.length < 128 / 8) { throw new Error( 'Entropy is less than 128 bits. It must be 128 bits or more.' @@ -175,7 +175,7 @@ class Bip39 extends Struct { if (bin.length % 11 !== 0) { throw new Error( 'internal error - entropy not an even multiple of 11 bits - ' + - bin.length + bin.length ) } @@ -196,7 +196,7 @@ class Bip39 extends Struct { * Check that a mnemonic is valid. This means there should be no superfluous * whitespace, no invalid words, and the checksum should match. */ - check () { + check() { const mnemonic = this.mnemonic // confirm no invalid words @@ -213,7 +213,7 @@ class Bip39 extends Struct { if (bin.length % 11 !== 0) { throw new Error( 'internal error - entropy not an even multiple of 11 bits - ' + - bin.length + bin.length ) } @@ -236,7 +236,7 @@ class Bip39 extends Struct { * Convert a mnemonic to a seed. Does not check for validity of the mnemonic - * for that, you should manually run check() first. */ - mnemonic2Seed (passphrase = '') { + mnemonic2Seed(passphrase = '') { let mnemonic = this.mnemonic if (!this.check()) { throw new Error( @@ -257,7 +257,7 @@ class Bip39 extends Struct { return this } - isValid (passphrase = '') { + isValid(passphrase = '') { let isValid try { isValid = !!this.mnemonic2Seed(passphrase) @@ -267,9 +267,9 @@ class Bip39 extends Struct { return isValid } - static isValid (mnemonic, passphrase = '') { - return new Bip39(mnemonic).isValid(passphrase) + static isValid(mnemonic, passphrase = '') { + return new Mnemonic(mnemonic).isValid(passphrase) } } -export { Bip39 } +export { Mnemonic } diff --git a/lib/bip-39-en-wordlist.js b/lib/mnemonic/bip-39-en-wordlist.js similarity index 100% rename from lib/bip-39-en-wordlist.js rename to lib/mnemonic/bip-39-en-wordlist.js diff --git a/lib/bip-39-en.js b/lib/mnemonic/bip-39-en.js similarity index 66% rename from lib/bip-39-en.js rename to lib/mnemonic/bip-39-en.js index ca3a1faa4..5aa1253cc 100644 --- a/lib/bip-39-en.js +++ b/lib/mnemonic/bip-39-en.js @@ -1,7 +1,7 @@ -import { Bip39 } from './bip-39' +import { Mnemonic } from '../mnemonic' import { wordList } from './bip-39-en-wordlist' -class Bip39En extends Bip39 { +class Bip39En extends Mnemonic { constructor (mnemonic, seed) { super(mnemonic, seed, wordList) } diff --git a/lib/bip-39-jp-wordlist.js b/lib/mnemonic/bip-39-jp-wordlist.js similarity index 100% rename from lib/bip-39-jp-wordlist.js rename to lib/mnemonic/bip-39-jp-wordlist.js diff --git a/lib/bip-39-jp.js b/lib/mnemonic/bip-39-jp.js similarity index 66% rename from lib/bip-39-jp.js rename to lib/mnemonic/bip-39-jp.js index 6b7aacb22..d2b7df80a 100644 --- a/lib/bip-39-jp.js +++ b/lib/mnemonic/bip-39-jp.js @@ -1,7 +1,7 @@ -import { Bip39 } from './bip-39' +import { Mnemonic } from '../mnemonic' import { wordList } from './bip-39-jp-wordlist' -class Bip39Jp extends Bip39 { +class Bip39Jp extends Mnemonic { constructor (mnemonic, seed) { super(mnemonic, seed, wordList) } diff --git a/lib/bip-39-words.js b/lib/mnemonic/bip-39-words.js similarity index 100% rename from lib/bip-39-words.js rename to lib/mnemonic/bip-39-words.js diff --git a/lib/testnet-bip-32.js b/lib/testnet-bip-32.js deleted file mode 100644 index 4ba8c1c83..000000000 --- a/lib/testnet-bip-32.js +++ /dev/null @@ -1,5 +0,0 @@ -import { Bip32 } from './bip-32' - -const MainnetBip32 = Bip32.Testnet - -export { MainnetBip32 } diff --git a/lib/testnet-hd-wallet.js b/lib/testnet-hd-wallet.js new file mode 100644 index 000000000..8e7b3fd3b --- /dev/null +++ b/lib/testnet-hd-wallet.js @@ -0,0 +1,5 @@ +import { HDWallet } from './hd-wallet' + +const MainnetHDWallet = HDWallet.Testnet + +export { MainnetHDWallet } diff --git a/package.json b/package.json index b6a941d86..db0deef7e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bsv", "description": "Javascript library for Bitcoin SV (BSV).", - "version": "2.0.10", + "version": "2.0.11", "author": "Yours Inc.", "homepage": "https://github.com/moneybutton/bsv", "source": "entry.js", @@ -73,6 +73,7 @@ "blockchain", "bip32", "bip39", + "mnemonic", "bip68", "multisig", "ecies" diff --git a/test/bip-32.js b/test/hd-wallet similarity index 77% rename from test/bip-32.js rename to test/hd-wallet index 5f84695d9..0a6b70ea1 100644 --- a/test/bip-32.js +++ b/test/hd-wallet @@ -1,26 +1,26 @@ /* global describe,it */ 'use strict' -import { Bip32 } from '../lib/bip-32' +import { HDWallet } from '../lib/hd-wallet' import { Base58Check } from '../lib/base-58-check' import { PrivKey } from '../lib/priv-key' import should from 'should' -describe('Bip32', function () { +describe('HDWallet', function () { it('should satisfy these basic API features', function () { - Bip32.fromRandom() + HDWallet.fromRandom() .toString() .slice(0, 4) .should.equal('xprv') - Bip32.fromRandom() + HDWallet.fromRandom() .toPublic() .toString() .slice(0, 4) .should.equal('xpub') - Bip32.Testnet.fromRandom() + HDWallet.Testnet.fromRandom() .toString() .slice(0, 4) .should.equal('tprv') - Bip32.Testnet.fromRandom() + HDWallet.Testnet.fromRandom() .toPublic() .toString() .slice(0, 4) @@ -82,36 +82,36 @@ describe('Bip32', function () { it('should make a new a bip32', function () { let bip32 - bip32 = new Bip32() + bip32 = new HDWallet() should.exist(bip32) - bip32 = new Bip32() + bip32 = new HDWallet() should.exist(bip32) - new Bip32() + new HDWallet() .fromString(vector1mPrivate) .toString() .should.equal(vector1mPrivate) - new Bip32() + new HDWallet() .fromString(vector1mPrivate) .toString() .should.equal(vector1mPrivate) - new Bip32() - .fromString(new Bip32().fromString(vector1mPrivate).toString()) + new HDWallet() + .fromString(new HDWallet().fromString(vector1mPrivate).toString()) .toString() .should.equal(vector1mPrivate) }) it('should initialize test vector 1 from the extended public key', function () { - const bip32 = new Bip32().fromString(vector1mPublic) + const bip32 = new HDWallet().fromString(vector1mPublic) should.exist(bip32) }) it('should initialize test vector 1 from the extended private key', function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) should.exist(bip32) }) it('should get the extended public key from the extended private key for test vector 1', function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) bip32 .toPublic() .toString() @@ -119,21 +119,21 @@ describe('Bip32', function () { }) it("should get m/0' ext. private key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'") should.exist(child) child.toString().should.equal(vector1m0hPrivate) }) it("should asynchronously get m/0' ext. private key from test vector 1", async function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = await bip32.asyncDerive("m/0'") should.exist(child) child.toString().should.equal(vector1m0hPrivate) }) it("should get m/0' ext. public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'") should.exist(child) child @@ -143,14 +143,14 @@ describe('Bip32', function () { }) it("should get m/0'/1 ext. private key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1") should.exist(child) child.toString().should.equal(vector1m0h1Private) }) it("should get m/0'/1 ext. public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1") should.exist(child) child @@ -160,9 +160,9 @@ describe('Bip32', function () { }) it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/1') should.exist(child2) child2 @@ -172,9 +172,9 @@ describe('Bip32', function () { }) it("should asynchronously get m/0'/1 ext. public key from m/0' public key from test vector 1", async function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = await childPub.asyncDerive('m/1') should.exist(child2) child2 @@ -184,14 +184,14 @@ describe('Bip32', function () { }) it("should get m/0'/1/2h ext. private key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'") should.exist(child) child.toString().should.equal(vector1m0h12hPrivate) }) it("should get m/0'/1/2h ext. public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'") should.exist(child) child @@ -201,16 +201,16 @@ describe('Bip32', function () { }) it("should get m/0'/1/2h/2 ext. private key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'/2") should.exist(child) child.toString().should.equal(vector1m0h12h2Private) }) it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/2') should.exist(child2) child2 @@ -220,7 +220,7 @@ describe('Bip32', function () { }) it("should get m/0'/1/2h/2 ext. public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'/2") should.exist(child) child @@ -230,14 +230,14 @@ describe('Bip32', function () { }) it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'/2/1000000000") should.exist(child) child.toString().should.equal(vector1m0h12h21000000000Private) }) it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'/2/1000000000") should.exist(child) child @@ -247,9 +247,9 @@ describe('Bip32', function () { }) it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function () { - const bip32 = new Bip32().fromString(vector1mPrivate) + const bip32 = new HDWallet().fromString(vector1mPrivate) const child = bip32.derive("m/0'/1/2'/2") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/1000000000') should.exist(child2) child2 @@ -259,17 +259,17 @@ describe('Bip32', function () { }) it('should initialize test vector 2 from the extended public key', function () { - const bip32 = new Bip32().fromString(vector2mPublic) + const bip32 = new HDWallet().fromString(vector2mPublic) should.exist(bip32) }) it('should initialize test vector 2 from the extended private key', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) should.exist(bip32) }) it('should get the extended public key from the extended private key for test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) bip32 .toPublic() .toString() @@ -277,14 +277,14 @@ describe('Bip32', function () { }) it('should get m/0 ext. private key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive('m/0') should.exist(child) child.toString().should.equal(vector2m0Private) }) it('should get m/0 ext. public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive('m/0') should.exist(child) child @@ -294,9 +294,9 @@ describe('Bip32', function () { }) it('should get m/0 ext. public key from m public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive('m') - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/0') should.exist(child2) child2 @@ -306,14 +306,14 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h ext. private key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'") should.exist(child) child.toString().should.equal(vector2m02147483647hPrivate) }) it('should get m/0/2147483647h ext. public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'") should.exist(child) child @@ -323,14 +323,14 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h/1 ext. private key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1") should.exist(child) child.toString().should.equal(vector2m02147483647h1Private) }) it('should get m/0/2147483647h/1 ext. public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1") should.exist(child) child @@ -340,9 +340,9 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/1') should.exist(child2) child2 @@ -352,14 +352,14 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1/2147483646'") should.exist(child) child.toString().should.equal(vector2m02147483647h12147483646hPrivate) }) it('should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1/2147483646'") should.exist(child) child @@ -369,14 +369,14 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1/2147483646'/2") should.exist(child) child.toString().should.equal(vector2m02147483647h12147483646h2Private) }) it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1/2147483646'/2") should.exist(child) child @@ -386,9 +386,9 @@ describe('Bip32', function () { }) it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2', function () { - const bip32 = new Bip32().fromString(vector2mPrivate) + const bip32 = new HDWallet().fromString(vector2mPrivate) const child = bip32.derive("m/0/2147483647'/1/2147483646'") - const childPub = new Bip32().fromString(child.toPublic().toString()) + const childPub = new HDWallet().fromString(child.toPublic().toString()) const child2 = childPub.derive('m/2') should.exist(child2) child2 @@ -398,11 +398,11 @@ describe('Bip32', function () { }) describe('testnet', function () { - it('should initialize a new Bip32 correctly from a random Bip32', function () { - const b1 = new Bip32.Testnet() + it('should initialize a new HDWallet correctly from a random Bip32', function () { + const b1 = new HDWallet.Testnet() b1.fromRandom() - ;(b1.privKey instanceof PrivKey.Testnet).should.equal(true) - const b2 = new Bip32.Testnet().fromString(b1.toPublic().toString()) + ; (b1.privKey instanceof PrivKey.Testnet).should.equal(true) + const b2 = new HDWallet.Testnet().fromString(b1.toPublic().toString()) b2 .toPublic() .toString() @@ -410,9 +410,9 @@ describe('Bip32', function () { }) it('should generate valid ext pub key for testnet', function () { - const b = new Bip32.Testnet() + const b = new HDWallet.Testnet() b.fromRandom() - ;(b.privKey instanceof PrivKey.Testnet).should.equal(true) + ; (b.privKey instanceof PrivKey.Testnet).should.equal(true) b .toPublic() .toString() @@ -423,8 +423,8 @@ describe('Bip32', function () { describe('#fromObject', function () { it('should set this bip32', function () { - const bip32 = new Bip32().fromString(vector1mPrivate) - const bip322 = new Bip32().fromObject({ + const bip32 = new HDWallet().fromString(vector1mPrivate) + const bip322 = new HDWallet().fromObject({ versionBytesNum: bip32.versionBytesNum, depth: bip32.depth, parentFingerPrint: bip32.parentFingerPrint, @@ -444,24 +444,24 @@ describe('Bip32', function () { describe('#fromRandom', function () { it('should not return the same one twice', function () { - const bip32a = new Bip32().fromRandom() - const bip32b = new Bip32().fromRandom() + const bip32a = new HDWallet().fromRandom() + const bip32b = new HDWallet().fromRandom() bip32a.toString().should.not.equal(bip32b.toString()) }) }) describe('@fromRandom', function () { it('should not return the same one twice', function () { - const bip32a = Bip32.fromRandom() - const bip32b = Bip32.fromRandom() + const bip32a = DWallet.fromRandom() + const bip32b = DWallet.fromRandom() bip32a.toString().should.not.equal(bip32b.toString()) }) }) describe('#fromSeed', function () { - it('should initialize a new Bip32 correctly from test vector 1 seed', function () { + it('should initialize a new HDWallet correctly from test vector 1 seed', function () { const hex = vector1master - const bip32 = new Bip32().fromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = new HDWallet().fromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector1mPrivate) bip32 @@ -470,9 +470,9 @@ describe('Bip32', function () { .should.equal(vector1mPublic) }) - it('should initialize a new Bip32 correctly from test vector 2 seed', function () { + it('should initialize a new HDWallet correctly from test vector 2 seed', function () { const hex = vector2master - const bip32 = new Bip32().fromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = new HDWallet().fromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector2mPrivate) bip32 @@ -483,9 +483,9 @@ describe('Bip32', function () { }) describe('@fromSeed', function () { - it('should initialize a new Bip32 correctly from test vector 1 seed', function () { + it('should initialize a new HDWallet correctly from test vector 1 seed', function () { const hex = vector1master - const bip32 = Bip32.fromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = DWallet.fromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector1mPrivate) bip32 @@ -494,9 +494,9 @@ describe('Bip32', function () { .should.equal(vector1mPublic) }) - it('should initialize a new Bip32 correctly from test vector 2 seed', function () { + it('should initialize a new HDWallet correctly from test vector 2 seed', function () { const hex = vector2master - const bip32 = Bip32.fromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = DWallet.fromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector2mPrivate) bip32 @@ -507,9 +507,9 @@ describe('Bip32', function () { }) describe('#asyncFromSeed', function () { - it('should initialize a new Bip32 correctly from test vector 1 seed', async function () { + it('should initialize a new HDWallet correctly from test vector 1 seed', async function () { const hex = vector1master - const bip32 = await new Bip32().asyncFromSeed( + const bip32 = await new HDWallet().asyncFromSeed( Buffer.from(hex, 'hex'), 'mainnet' ) @@ -521,9 +521,9 @@ describe('Bip32', function () { .should.equal(vector1mPublic) }) - it('should initialize a new Bip32 correctly from test vector 2 seed', async function () { + it('should initialize a new HDWallet correctly from test vector 2 seed', async function () { const hex = vector2master - const bip32 = await new Bip32().asyncFromSeed( + const bip32 = await new HDWallet().asyncFromSeed( Buffer.from(hex, 'hex'), 'mainnet' ) @@ -537,9 +537,9 @@ describe('Bip32', function () { }) describe('@asyncFromSeed', function () { - it('should initialize a new Bip32 correctly from test vector 1 seed', async function () { + it('should initialize a new HDWallet correctly from test vector 1 seed', async function () { const hex = vector1master - const bip32 = await Bip32.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = await HDWallet.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector1mPrivate) bip32 @@ -548,9 +548,9 @@ describe('Bip32', function () { .should.equal(vector1mPublic) }) - it('should initialize a new Bip32 correctly from test vector 2 seed', async function () { + it('should initialize a new HDWallet correctly from test vector 2 seed', async function () { const hex = vector2master - const bip32 = await Bip32.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet') + const bip32 = await HDWallet.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet') should.exist(bip32) bip32.toString().should.equal(vector2mPrivate) bip32 @@ -566,12 +566,12 @@ describe('Bip32', function () { 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' const buf = Base58Check.decode(str) const hex = buf.toString('hex') - let bip32 = new Bip32().fromHex(hex) + let bip32 = new HDWallet().fromHex(hex) should.exist(bip32) bip32.toString().should.equal(str) bip32 = bip32.toPublic() const xpub = bip32.toString() - bip32 = new Bip32().fromHex(bip32.toHex()) + bip32 = new HDWallet().fromHex(bip32.toHex()) bip32.toString().should.equal(xpub) }) }) @@ -581,12 +581,12 @@ describe('Bip32', function () { const str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' const buf = Base58Check.decode(str) - let bip32 = new Bip32().fromBuffer(buf) + let bip32 = new HDWallet().fromBuffer(buf) should.exist(bip32) bip32.toString().should.equal(str) bip32 = bip32.toPublic() const xpub = bip32.toString() - bip32 = new Bip32().fromBuffer(bip32.toBuffer()) + bip32 = new HDWallet().fromBuffer(bip32.toBuffer()) bip32.toString().should.equal(xpub) }) }) @@ -596,7 +596,7 @@ describe('Bip32', function () { const str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' const hex = Base58Check.decode(str).toString('hex') - const bip32 = new Bip32().fromString(str) + const bip32 = new HDWallet().fromString(str) bip32.toHex().should.equal(hex) }) }) @@ -606,7 +606,7 @@ describe('Bip32', function () { const str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' const buf = Base58Check.decode(str) - const bip32 = new Bip32().fromString(str) + const bip32 = new HDWallet().fromString(str) bip32 .toBuffer() .toString('hex') @@ -618,7 +618,7 @@ describe('Bip32', function () { it('should make a bip32 from a string', function () { const str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' - const bip32 = new Bip32().fromString(str) + const bip32 = new HDWallet().fromString(str) should.exist(bip32) bip32.toString().should.equal(str) }) @@ -628,18 +628,18 @@ describe('Bip32', function () { it('should make a bip32 from a string asynchronously', async function () { const str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' - const bip32 = new Bip32().fromString(str) + const bip32 = new HDWallet().fromString(str) should.exist(bip32) - const bip32b = await new Bip32().asyncFromString(str) + const bip32b = await new HDWallet().asyncFromString(str) bip32.toString().should.equal(str) bip32.toString().should.equal(bip32b.toString()) }) }) describe('#toString', function () { - const bip32 = new Bip32() + const bip32 = new HDWallet() bip32.fromRandom() - const tip32 = new Bip32.Testnet() + const tip32 = new HDWallet.Testnet() tip32.fromRandom() it('should return an xprv string', function () { @@ -662,7 +662,7 @@ describe('Bip32', function () { .toString() .slice(0, 4) .should.equal('tprv') - ;(tip32.privKey instanceof PrivKey.Testnet).should.equal(true) + ; (tip32.privKey instanceof PrivKey.Testnet).should.equal(true) }) it('should return a tpub string', function () { @@ -676,7 +676,7 @@ describe('Bip32', function () { describe('#asyncToString', function () { it('should convert to a string same as toString', async function () { - const bip32 = new Bip32().fromRandom() + const bip32 = new HDWallet().fromRandom() const str1 = bip32.toString() const str2 = await bip32.asyncToString() str1.should.equal(str2) @@ -685,34 +685,34 @@ describe('Bip32', function () { describe('#toJSON', function () { it('should be the same as toFastHex', function () { - const bip32 = Bip32.fromRandom() + const bip32 = DWallet.fromRandom() bip32.toJSON().should.equal(bip32.toFastHex()) }) }) describe('#fromJSON', function () { it('should be the same as fromFastHex', function () { - const bip32 = Bip32.fromRandom() + const bip32 = DWallet.fromRandom() const hex = bip32.toHex() - const bip32a = new Bip32().fromJSON(hex) - const bip32b = new Bip32().fromFastHex(hex) + const bip32a = new HDWallet().fromJSON(hex) + const bip32b = new HDWallet().fromFastHex(hex) bip32a.toString().should.equal(bip32b.toString()) }) }) describe('@fromJSON', function () { it('should be the same as fromFastHex', function () { - const bip32 = Bip32.fromRandom() + const bip32 = DWallet.fromRandom() const hex = bip32.toHex() - const bip32a = Bip32.fromJSON(hex) - const bip32b = Bip32.fromFastHex(hex) + const bip32a = DWallet.fromJSON(hex) + const bip32b = DWallet.fromFastHex(hex) bip32a.toString().should.equal(bip32b.toString()) }) }) describe('#isPrivate', function () { it('should know if this bip32 is private', function () { - const bip32priv = new Bip32().fromRandom() + const bip32priv = new HDWallet().fromRandom() const bip32pub = bip32priv.toPublic() bip32priv.isPrivate().should.equal(true) bip32pub.isPrivate().should.equal(false) diff --git a/test/bip-39.js b/test/mnemonic.js similarity index 76% rename from test/bip-39.js rename to test/mnemonic.js index 954d4859d..30913328f 100644 --- a/test/bip-39.js +++ b/test/mnemonic.js @@ -1,22 +1,22 @@ /* global describe,it */ 'use strict' import { Address } from '../lib/address' -import { Bip32 } from '../lib/bip-32' -import { Bip39 } from '../lib/bip-39' -import { Bip39Jp } from '../lib/bip-39-jp' -import { Bip39En } from '../lib/bip-39-en' +import { HDWallet } from '../lib/hd-wallet' +import { Mnemonic } from '../lib/mnemonic' +import { Bip39Jp } from '../lib/mnemonic/bip-39-jp' +import { Bip39En } from '../lib/mnemonic/bip-39-en' import { Random } from '../lib/random' import should from 'should' import vectors from './vectors/bip39.json' -import { wordList as enWordList } from '../lib/bip-39-en-wordlist' -import { wordList as jpWordList } from '../lib/bip-39-jp-wordlist' +import { wordList as enWordList } from '../lib/mnemonic/bip-39-en-wordlist' +import { wordList as jpWordList } from '../lib/mnemonic/bip-39-jp-wordlist' -describe('Bip39', function () { +describe('Mnemonic', function () { this.timeout(5000) it('should initialize the class', function () { - should.exist(Bip39) - should.exist(new Bip39()) + should.exist(Mnemonic) + should.exist(new Mnemonic()) }) it('should have a wordlist of length 2048', function () { @@ -31,10 +31,10 @@ describe('Bip39', function () { // // More information here: // https://github.com/iancoleman/bip39/issues/58 - const seed = Bip39.fromString( + const seed = Mnemonic.fromString( 'fruit wave dwarf banana earth journey tattoo true farm silk olive fence' ).toSeed('banana') - let bip32 = Bip32.fromSeed(seed) + let bip32 = HDWallet.fromSeed(seed) bip32 = bip32.derive("m/44'/0'/0'/0/0") const address = Address.fromPubKey(bip32.pubKey) address.toString().should.equal('17rxURoF96VhmkcEGCj5LNQkmN9HVhWb7F') @@ -46,7 +46,7 @@ describe('Bip39', function () { // should be able to make a mnemonic with or without the default wordlist let bip39 = new Bip39En().fromRandom(128) bip39.check().should.equal(true) - bip39 = new Bip39().fromRandom(128) + bip39 = new Mnemonic().fromRandom(128) bip39.check().should.equal(true) const entropy = Buffer.alloc(32) @@ -73,7 +73,7 @@ describe('Bip39', function () { describe('#toFastBuffer', function () { it('should convert to a buffer', function () { - const bip39 = new Bip39().fromRandom() + const bip39 = new Mnemonic().fromRandom() should.exist(bip39.seed) should.exist(bip39.mnemonic) const buf = bip39.toFastBuffer() @@ -83,8 +83,8 @@ describe('Bip39', function () { describe('#fromFastBuffer', function () { it('should convert from a buffer', function () { - const bip39a = new Bip39().fromRandom() - const bip39b = new Bip39().fromFastBuffer(bip39a.toFastBuffer()) + const bip39a = new Mnemonic().fromRandom() + const bip39b = new Mnemonic().fromFastBuffer(bip39a.toFastBuffer()) bip39a.mnemonic.should.equal(bip39b.mnemonic) bip39b.seed.toString('hex').should.equal(bip39b.seed.toString('hex')) }) @@ -92,35 +92,35 @@ describe('Bip39', function () { describe('#fromRandom', function () { it('should throw an error if bits is too low', function () { - ;(function () { - new Bip39().fromRandom(127) + ; (function () { + new Mnemonic().fromRandom(127) }.should.throw('bits must be multiple of 32')) }) it('should throw an error if bits is not a multiple of 32', function () { - ;(function () { - new Bip39().fromRandom(256 - 1) + ; (function () { + new Mnemonic().fromRandom(256 - 1) }.should.throw('bits must be multiple of 32')) }) }) describe('@fromRandom', function () { it('should throw an error if bits is too low', function () { - ;(function () { - Bip39.fromRandom(127) + ; (function () { + Mnemonic.fromRandom(127) }.should.throw('bits must be multiple of 32')) }) it('should throw an error if bits is not a multiple of 32', function () { - ;(function () { - Bip39.fromRandom(256 - 1) + ; (function () { + Mnemonic.fromRandom(256 - 1) }.should.throw('bits must be multiple of 32')) }) }) describe('#asyncFromRandom', function () { it('should have a seed and a mnemonic', async function () { - const bip39 = await new Bip39().asyncFromRandom() + const bip39 = await new Mnemonic().asyncFromRandom() should.exist(bip39.mnemonic) should.exist(bip39.seed) const seed = bip39.seed @@ -131,7 +131,7 @@ describe('Bip39', function () { describe('@asyncFromRandom', function () { it('should have a seed and a mnemonic', async function () { - const bip39 = await Bip39.asyncFromRandom() + const bip39 = await Mnemonic.asyncFromRandom() should.exist(bip39.mnemonic) should.exist(bip39.seed) const seed = bip39.seed @@ -142,14 +142,14 @@ describe('Bip39', function () { describe('#fromEntropy', function () { it('should build from entropy', function () { - const bip39 = new Bip39().fromEntropy(Random.getRandomBuffer(32)) + const bip39 = new Mnemonic().fromEntropy(Random.getRandomBuffer(32)) should.exist(bip39) }) }) describe('@fromEntropy', function () { it('should build from entropy', function () { - const bip39 = Bip39.fromEntropy(Random.getRandomBuffer(32)) + const bip39 = Mnemonic.fromEntropy(Random.getRandomBuffer(32)) should.exist(bip39) }) }) @@ -157,8 +157,8 @@ describe('Bip39', function () { describe('#asyncFromEntropy', function () { it('should return same as fromEntropy', async function () { const entropy = Random.getRandomBuffer(32) - const bip39a = await new Bip39().asyncFromEntropy(entropy) - const bip39b = await new Bip39().fromEntropy(entropy) + const bip39a = await new Mnemonic().asyncFromEntropy(entropy) + const bip39b = await new Mnemonic().fromEntropy(entropy) bip39a .toSeed() .toString('hex') @@ -169,8 +169,8 @@ describe('Bip39', function () { describe('@asyncFromEntropy', function () { it('should return same as fromEntropy', async function () { const entropy = Random.getRandomBuffer(32) - const bip39a = await Bip39.asyncFromEntropy(entropy) - const bip39b = await Bip39.fromEntropy(entropy) + const bip39a = await Mnemonic.asyncFromEntropy(entropy) + const bip39b = await Mnemonic.fromEntropy(entropy) bip39a .toSeed() .toString('hex') @@ -182,17 +182,17 @@ describe('Bip39', function () { it('should throw an error if you do not use enough entropy', function () { const buf = Buffer.alloc(128 / 8 - 1) buf.fill(0) - ;(function () { - new Bip39().entropy2Mnemonic(buf) - }.should.throw( - 'Entropy is less than 128 bits. It must be 128 bits or more.' - )) + ; (function () { + new Mnemonic().entropy2Mnemonic(buf) + }.should.throw( + 'Entropy is less than 128 bits. It must be 128 bits or more.' + )) }) it('should work with or without the wordlist', function () { const buf = Buffer.alloc(128 / 8) buf.fill(0) - const mnemonic1 = new Bip39().entropy2Mnemonic(buf).mnemonic + const mnemonic1 = new Mnemonic().entropy2Mnemonic(buf).mnemonic const mnemonic2 = new Bip39En().entropy2Mnemonic(buf).mnemonic mnemonic1.should.equal(mnemonic2) }) @@ -202,8 +202,8 @@ describe('Bip39', function () { it('should work with or without optional wordlist', function () { const buf = Buffer.alloc(128 / 8) buf.fill(0) - const mnemonic = new Bip39().entropy2Mnemonic(buf).mnemonic - new Bip39() + const mnemonic = new Mnemonic().entropy2Mnemonic(buf).mnemonic + new Mnemonic() .fromString(mnemonic) .check() .should.equal(true) @@ -216,8 +216,8 @@ describe('Bip39', function () { describe('#fromString', function () { it('should throw an error in invalid mnemonic', function () { - ;(function () { - new Bip39().fromString('invalid mnemonic').toSeed() + ; (function () { + new Mnemonic().fromString('invalid mnemonic').toSeed() }.should.throw( 'Mnemonic does not pass the check - was the mnemonic typed incorrectly? Are there extra spaces?' )) @@ -226,7 +226,7 @@ describe('Bip39', function () { describe('#asyncToSeed', function () { it('should result the same as toSeed', async function () { - const bip39 = new Bip39().fromRandom() + const bip39 = new Mnemonic().fromRandom() const seed1a = bip39.toSeed() const seed2a = await bip39.asyncToSeed() seed1a.toString('hex').should.equal(seed2a.toString('hex')) @@ -238,12 +238,12 @@ describe('Bip39', function () { describe('@isValid', function () { it('should know this is valid', function () { - const isValid = Bip39.isValid('abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR') + const isValid = Mnemonic.isValid('abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR') isValid.should.equal(true) }) it('should know this is invalid', function () { - const isValid = Bip39.isValid('abandonnnnnnn abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR') + const isValid = Mnemonic.isValid('abandonnnnnnn abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR') isValid.should.equal(false) }) }) @@ -257,7 +257,7 @@ describe('Bip39', function () { bip39.check().should.equal(true) const seed = bip39.toSeed(vector.passphrase) seed.toString('hex').should.equal(vector.seed) - new Bip32() + new HDWallet() .fromSeed(seed) .toString() .should.equal(vector.bip32_xprv) @@ -272,7 +272,7 @@ describe('Bip39', function () { bip39.check().should.equal(true) const seed = bip39.toSeed(vector.passphrase) seed.toString('hex').should.equal(vector.seed) - new Bip32() + new HDWallet() .fromSeed(seed) .toString() .should.equal(vector.bip32_xprv) diff --git a/test/workers.js b/test/workers.js index be254e530..6780cd0f3 100644 --- a/test/workers.js +++ b/test/workers.js @@ -1,6 +1,6 @@ /* global describe,it */ 'use strict' -import { Bip32 } from '../lib/bip-32' +import { HDWallet } from '../lib/hd-wallet' import { Ecies } from '../lib/ecies' import { Hash } from '../lib/hash' import { PrivKey } from '../lib/priv-key' @@ -19,12 +19,12 @@ describe('Workers', function () { it('should handle a bunch of things at the same time', async function () { this.timeout(6000) const n = 100 - const str = Bip32.fromString( + const str = HDWallet.fromString( 'xprv9s21ZrQH143K2SX7qL4vxLnbDHtupmfrkv96s1N3eeqKa4LnS5ZNMzhiSQWqL1fmkeF1rF7ndtvDoYH4sKJLMMpaJup21c7C3kyAg8DfbPQ' ).toString() const arr = [] for (let i = 0; i < n; i++) { - arr[i] = Bip32.asyncFromString(str) + arr[i] = HDWallet.asyncFromString(str) } arr.length.should.equal(n) await Promise.all(arr) @@ -59,7 +59,7 @@ describe('Workers', function () { describe('#asyncObjectMethod', function () { it('should compute this method in the workers', async function () { - const bip32 = new Bip32().fromRandom() + const bip32 = new HDWallet().fromRandom() const workersResult = await Workers.asyncObjectMethod(bip32, 'toString', []) const str = JSON.parse(workersResult.resbuf.toString()) str[0].should.equal('x')