From 4adf1fcb140447c45d99e0467628fc4c97a8e50d Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 17:31:39 -0500 Subject: [PATCH 1/6] Network: Replaced all instances of mainnet with livenet for consistency --- lib/address.js | 38 +++++++++++++++++++------------------- lib/privatekey.js | 18 +++++++++--------- test/address.js | 40 ++++++++++++++++++++-------------------- test/privatekey.js | 32 ++++++++++++++++---------------- 4 files changed, 64 insertions(+), 64 deletions(-) diff --git a/lib/address.js b/lib/address.js index e199a29..a7ccddf 100644 --- a/lib/address.js +++ b/lib/address.js @@ -25,7 +25,7 @@ var Hash = require('./crypto/hash'); * * * @param {String} data - The encoded data in various formats - * @param {String} [network] - The network: 'mainnet' or 'testnet' + * @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type of address: 'script' or 'pubkey' * @returns {Address} A new valid and frozen instance of an Address * @constructor @@ -40,8 +40,8 @@ function Address(data, network, type) { throw new TypeError('First argument is required, please include address data.'); } - if (network && (network !== 'mainnet' && network !== 'testnet')) { - throw new TypeError('Second argument must be "mainnet" or "testnet".'); + if (network && (network !== 'livenet' && network !== 'testnet')) { + throw new TypeError('Second argument must be "livenet" or "testnet".'); } if (type && (type !== 'pubkeyhash' && type !== 'scripthash')) { @@ -66,7 +66,7 @@ function Address(data, network, type) { } // set defaults if not set - info.network = info.network || network || 'mainnet'; + info.network = info.network || network || 'livenet'; info.type = info.type || type || 'pubkeyhash'; // set the validated values @@ -103,7 +103,7 @@ Address._transformHash = function(hash){ * Internal function to transform a bitcoin address buffer * * @param {Buffer} buffer - An instance of a hex encoded address Buffer - * @param {String} [network] - The network: 'mainnet' or 'testnet' + * @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type: 'pubkeyhash' or 'scripthash' * @returns {Object} An object with keys: hashBuffer, network and type * @private @@ -121,13 +121,13 @@ Address._transformBuffer = function(buffer, network, type){ var bufType = false; switch(buffer[0]){ // the version byte - case networks.mainnet.pubkeyhash: - bufNetwork = 'mainnet'; + case networks.livenet.pubkeyhash: + bufNetwork = 'livenet'; bufType = 'pubkeyhash'; break; - case networks.mainnet.scripthash: - bufNetwork = 'mainnet'; + case networks.livenet.scripthash: + bufNetwork = 'livenet'; bufType = 'scripthash'; break; @@ -197,7 +197,7 @@ Address._transformScript = function(script){ * Internal function to transform a bitcoin address string * * @param {String} data - An instance of PublicKey - * @param {String} [network] - The network: 'mainnet' or 'testnet' + * @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type: 'pubkeyhash' or 'scripthash' * @returns {Object} An object with keys: hashBuffer, network and type * @private @@ -216,7 +216,7 @@ Address._transformString = function(data, network, type){ * Instantiate an address from a PublicKey instance * * @param {String} data - An instance of PublicKey - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @returns {Address} A new valid and frozen instance of an Address */ Address.fromPublicKey = function(data, network){ @@ -229,7 +229,7 @@ Address.fromPublicKey = function(data, network){ * Instantiate an address from a ripemd160 public key hash * * @param {Buffer} hash - An instance of buffer of the hash - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @returns {Address} A new valid and frozen instance of an Address */ Address.fromPublicKeyHash = function(hash, network) { @@ -242,7 +242,7 @@ Address.fromPublicKeyHash = function(hash, network) { * Instantiate an address from a ripemd160 script hash * * @param {Buffer} hash - An instance of buffer of the hash - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @returns {Address} A new valid and frozen instance of an Address */ Address.fromScriptHash = function(hash, network) { @@ -255,7 +255,7 @@ Address.fromScriptHash = function(hash, network) { * Instantiate an address from a Script * * @param {Script} script - An instance of Script - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @returns {Address} A new valid and frozen instance of an Address */ Address.fromScript = function(script, network) { @@ -268,7 +268,7 @@ Address.fromScript = function(script, network) { * Instantiate an address from a buffer of the address * * @param {Buffer} buffer - An instance of buffer of the address - * @param {String} [network] - The network: 'mainnet' or 'testnet' + * @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type of address: 'script' or 'pubkey' * @returns {Address} A new valid and frozen instance of an Address */ @@ -282,7 +282,7 @@ Address.fromBuffer = function(buffer, network, type) { * Instantiate an address from an address string * * @param {String} str - An string of the bitcoin address - * @param {String} [network] - The network: 'mainnet' or 'testnet' + * @param {String} [network] - The network: 'livenet' or 'testnet' * @param {String} [type] - The type of address: 'script' or 'pubkey' * @returns {Address} A new valid and frozen instance of an Address */ @@ -301,7 +301,7 @@ Address.fromString = function(str, network, type) { * // a network mismatch error * * @param {String} data - The encoded data - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @param {String} type - The type of address: 'script' or 'pubkey' * @returns {null|Error} The corresponding error message */ @@ -321,11 +321,11 @@ Address.getValidationError = function(data, network, type) { * * @example * - * var valid = Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'mainnet'); + * var valid = Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'livenet'); * // true * * @param {String} data - The encoded data - * @param {String} network - The network: 'mainnet' or 'testnet' + * @param {String} network - The network: 'livenet' or 'testnet' * @param {String} type - The type of address: 'script' or 'pubkey' * @returns {null|Error} The corresponding error message */ diff --git a/lib/privatekey.js b/lib/privatekey.js index eb9a4db..6cf53e7 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -27,7 +27,7 @@ var PublicKey = require('./publickey'); * var imported = PrivateKey.fromWIF(exported); * * @param {String} data - The encoded data in various formats - * @param {String} [network] - Either "mainnet" or "testnet" + * @param {String} [network] - Either "livenet" or "testnet" * @param {Boolean} [compressed] - If the key is in compressed format * @returns {PrivateKey} A new valid instance of an PrivateKey * @constructor @@ -40,7 +40,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { var info = { compressed: typeof(compressed) !== 'undefined' ? compressed : true, - network: network || 'mainnet' + network: network || 'livenet' }; // detect type of data @@ -61,7 +61,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { throw new TypeError('Number must be less than N'); } if (typeof(networks[info.network]) === 'undefined') { - throw new TypeError('Must specify the network ("mainnet" or "testnet")'); + throw new TypeError('Must specify the network ("livenet" or "testnet")'); } if (typeof(info.compressed) !== 'boolean') { throw new TypeError('Must specify whether the corresponding public key is compressed or not (true or false)'); @@ -98,7 +98,7 @@ PrivateKey._getRandomBN = function(){ * Internal function to transform a WIF Buffer into a private key * * @param {Buffer} buf - An WIF string - * @param {String} [network] - Either "mainnet" or "testnet" + * @param {String} [network] - Either "livenet" or "testnet" * @param {String} [compressed] - If the private key is compressed * @returns {Object} An object with keys: bn, network and compressed * @private @@ -115,8 +115,8 @@ PrivateKey._transformBuffer = function(buf, network, compressed) { throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)'); } - if (buf[0] === networks.mainnet.privatekey) { - info.network = 'mainnet'; + if (buf[0] === networks.livenet.privatekey) { + info.network = 'livenet'; } else if (buf[0] === networks.testnet.privatekey) { info.network = 'testnet'; } else { @@ -177,7 +177,7 @@ PrivateKey.fromJSON = function(json) { * * Instantiate a PrivateKey from random bytes * - * @param {String} [network] - Either "mainnet" or "testnet" + * @param {String} [network] - Either "livenet" or "testnet" * @param {String} [compressed] - If the private key is compressed * @returns {PrivateKey} A new valid instance of PrivateKey */ @@ -203,7 +203,7 @@ PrivateKey.fromString = function(str) { * Check if there would be any errors when initializing a PrivateKey * * @param {String} data - The encoded data in various formats - * @param {String} [network] - Either "mainnet" or "testnet" + * @param {String} [network] - Either "livenet" or "testnet" * @param {String} [compressed] - If the private key is compressed * @returns {null|Error} An error if exists */ @@ -223,7 +223,7 @@ PrivateKey.getValidationError = function(data, network, compressed) { * Check if the parameters are valid * * @param {String} data - The encoded data in various formats - * @param {String} [network] - Either "mainnet" or "testnet" + * @param {String} [network] - Either "livenet" or "testnet" * @param {String} [compressed] - If the private key is compressed * @returns {Boolean} If the private key is would be valid */ diff --git a/test/address.js b/test/address.js index 80f363f..00ed0b0 100644 --- a/test/address.js +++ b/test/address.js @@ -23,17 +23,17 @@ describe('Address', function() { it('should throw an error because of bad network param', function() { (function(){ var a = new Address(validAddresses[0], 'main', 'pubkeyhash'); - }).should.throw('Second argument must be "mainnet" or "testnet".'); + }).should.throw('Second argument must be "livenet" or "testnet".'); }); it('should throw an error because of bad type param', function() { (function() { - var a = new Address(validAddresses[0], 'mainnet', 'pubkey'); + var a = new Address(validAddresses[0], 'livenet', 'pubkey'); }).should.throw('Third argument must be "pubkeyhash" or "scripthash"'); }); - // mainnet valid + // livenet valid var validAddresses = [ '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', '1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT', @@ -41,7 +41,7 @@ describe('Address', function() { '1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7' ]; - // mainnet p2sh + // livenet p2sh var validp2shAddresses = [ '342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey', '33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk', @@ -57,7 +57,7 @@ describe('Address', function() { '2NB72XtkjpnATMggui83aEtPawyyKvnbX2o' ]; - //mainnet bad checksums + //livenet bad checksums var badChecksums = [ '15vkcKf7gB23wLAnZLmbVuMiiVDc3nq4a2', '1A6ut1tWnUq1SEQLMr4ttDh24wcbj4w2TT', @@ -65,7 +65,7 @@ describe('Address', function() { '1Jz2yCRd5ST1p2gUqFB5wsSQfdmEJaffg7' ]; - //mainnet non-base58 + //livenet non-base58 var nonBase58 = [ '15vkcKf7g#23wLAnZLmb$uMiiVDc3nq4a2', '1A601ttWnUq1SEQLMr4ttDh24wcbj4w2TT', @@ -83,13 +83,13 @@ describe('Address', function() { describe('validation', function() { - it('should describe this mainnet address as an invalid testnet address', function() { + it('should describe this livenet address as an invalid testnet address', function() { var error = Address.getValidationError('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'testnet'); should.exist(error); }); it('should should return a true boolean', function(){ - var valid = Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'mainnet'); + var valid = Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'livenet'); valid.should.equal(true); }); @@ -128,14 +128,14 @@ describe('Address', function() { it('should validate addresses with params', function() { for(var i=0;i'); + privkey.inspect().should.equal(''); }); it('should output known testnet address for console', function() { @@ -178,7 +178,7 @@ describe('PrivateKey', function() { describe('#toBuffer', function() { it('should output known buffer', function() { - var privkey = new PrivateKey(BN.fromBuffer(buf), 'mainnet', true); + var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', true); var b = privkey.toBuffer().toString('hex').should.equal(buf.toString('hex')); }); }); @@ -186,7 +186,7 @@ describe('PrivateKey', function() { describe('#toBigNumber', function() { it('should output known BN', function() { var a = BN.fromBuffer(buf); - var privkey = new PrivateKey(a, 'mainnet', true); + var privkey = new PrivateKey(a, 'livenet', true); var b = privkey.toBigNumber(); b.toString('hex').should.equal(a.toString('hex')); }); @@ -206,8 +206,8 @@ describe('PrivateKey', function() { describe('#fromWIF', function() { it('should parse this compressed testnet address correctly', function() { - var privkey = PrivateKey.fromWIF(encmainnet); - privkey.toWIF().should.equal(encmainnet); + var privkey = PrivateKey.fromWIF(enclivenet); + privkey.toWIF().should.equal(enclivenet); }); }); @@ -232,7 +232,7 @@ describe('PrivateKey', function() { describe('#toString', function() { - it('should parse this uncompressed mainnet address correctly', function() { + it('should parse this uncompressed livenet address correctly', function() { var privkey = PrivateKey.fromString(encmu); privkey.toString().should.equal(encmu); }); From 6e36baebc6c3c0a860de16062b9b4b1aa5d75518 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 17:55:57 -0500 Subject: [PATCH 2/6] Tests: Enable tests for network, include defaultNetwork, check each constant, build browser tests before running to make sure it's up-to-date --- gulpfile.js | 2 +- lib/networks.js | 1 - test/networks.js | 25 ++++++++++++------------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index b227ceb..142f8db 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -110,7 +110,7 @@ gulp.task('browser-all', ['errors'], function(callback) { runSequence(['browser'], ['browser-test'], callback); }); -gulp.task('karma', testKarma); +gulp.task('karma', ['browser-test'], testKarma); gulp.task('errors', shell.task([ 'node ./lib/errors/build.js' diff --git a/lib/networks.js b/lib/networks.js index 2419252..a37a5cf 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -69,6 +69,5 @@ module.exports = { defaultNetwork: livenet, livenet: livenet, testnet: testnet, - mainnet: livenet, get: getNetwork }; diff --git a/test/networks.js b/test/networks.js index f58e9dd..af09951 100644 --- a/test/networks.js +++ b/test/networks.js @@ -2,24 +2,23 @@ var should = require('chai').should(); var bitcore = require('..'); -var Networks = bitcore.Networks; +var networks = bitcore.Networks; describe('Networks', function() { it('should contain all Networks', function() { - should.exist(Networks.livenet); - should.exist(Networks.testnet); - should.exist(Networks.mainnet); + should.exist(networks.livenet); + should.exist(networks.testnet); + should.exist(networks.defaultNetwork); }); - describe('contain all constants for livenet and testnet', function() { - var makeTest = function(key) { - Networks.testnet.hasOwnProperty(key).should.equal(true); - }; - for (var key in Networks.livenet) { - if (Networks.livenet.hasOwnProperty(key)) { - it('all should contain ' + key, makeTest(key)); - } - } + + var constants = ['name', 'alias', 'pubkeyhash', 'scripthash', 'xpubkey', 'xprivkey']; + + constants.forEach(function(key){ + it('should have constant '+key+' for livenet and testnet', function(){ + networks.testnet.hasOwnProperty(key).should.equal(true); + networks.livenet.hasOwnProperty(key).should.equal(true); + }); }); }); From eb72310a622693eb31efe6cd1fa522635b21c505 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 18:07:39 -0500 Subject: [PATCH 3/6] Address: Use defaultNetwork if network is not specified --- lib/address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/address.js b/lib/address.js index a7ccddf..f22ade8 100644 --- a/lib/address.js +++ b/lib/address.js @@ -66,7 +66,7 @@ function Address(data, network, type) { } // set defaults if not set - info.network = info.network || network || 'livenet'; + info.network = info.network || network || networks.defaultNetwork.name; info.type = info.type || type || 'pubkeyhash'; // set the validated values From b91b0fe4a5ffd3d4361077fd6db7f5662b6520c3 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 18:10:18 -0500 Subject: [PATCH 4/6] PrivateKey: Use defaultNetwork if not specified --- lib/privatekey.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 6cf53e7..5754ffb 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -40,7 +40,7 @@ var PrivateKey = function PrivateKey(data, network, compressed) { var info = { compressed: typeof(compressed) !== 'undefined' ? compressed : true, - network: network || 'livenet' + network: network || networks.defaultNetwork.name }; // detect type of data @@ -116,9 +116,9 @@ PrivateKey._transformBuffer = function(buf, network, compressed) { } if (buf[0] === networks.livenet.privatekey) { - info.network = 'livenet'; + info.network = networks.livenet.name; } else if (buf[0] === networks.testnet.privatekey) { - info.network = 'testnet'; + info.network = networks.testnet.name; } else { throw new Error('Invalid network'); } From 2cbcf7da1ca116d00fa92e10004c281c548f7b67 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 18:19:17 -0500 Subject: [PATCH 5/6] Tests: Check that an Address is using the defaultNetwork --- test/address.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/address.js b/test/address.js index 00ed0b0..3c29c3b 100644 --- a/test/address.js +++ b/test/address.js @@ -6,6 +6,7 @@ var bitcore = require('..'); var PublicKey = bitcore.PublicKey; var Address = bitcore.Address; var Script = bitcore.Script; +var networks = bitcore.Networks; describe('Address', function() { @@ -260,6 +261,18 @@ describe('Address', function() { var c = new Address(hash).toString().should.equal(str); }); + it('should make an address using the default network', function() { + var hash = pubkeyhash; //use the same hash + var a = Address.fromPublicKeyHash(hash); + a.network.should.equal('livenet'); + // change the default + networks.defaultNetwork = networks.testnet; + var b = Address.fromPublicKeyHash(hash); + b.network.should.equal('testnet'); + // restore the default + networks.defaultNetwork = networks.livenet; + }); + it('should throw an error for invalid length hashBuffer', function() { (function() { var a = Address.fromPublicKeyHash(buf); From 4ba53fcb92918b0132bcce64204e655b85376074 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 1 Dec 2014 18:22:51 -0500 Subject: [PATCH 6/6] Tests: Check that PrivateKey is using the defaultNetwork --- test/privatekey.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/privatekey.js b/test/privatekey.js index 6cc5a85..1a69f48 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -5,6 +5,7 @@ var bitcore = require('..'); var BN = bitcore.crypto.BN; var Point = bitcore.crypto.Point; var PrivateKey = bitcore.PrivateKey; +var networks = bitcore.Networks; var base58check = bitcore.encoding.Base58Check; describe('PrivateKey', function() { @@ -100,6 +101,17 @@ describe('PrivateKey', function() { privkey.toString().should.equal(enclivenet); }); + it('should create a default network private key', function() { + var a = new PrivateKey(BN.fromBuffer(buf)); + a.network.should.equal('livenet'); + // change the default + networks.defaultNetwork = networks.testnet; + var b = new PrivateKey(BN.fromBuffer(buf)); + b.network.should.equal('testnet'); + // restore the default + networks.defaultNetwork = networks.livenet; + }); + it('should create an uncompressed testnet private key', function() { var privkey = new PrivateKey(BN.fromBuffer(buf), 'testnet', false); privkey.toString().should.equal(enctu);