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/address.js b/lib/address.js index fe3d6ec..31c68fa 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')) { @@ -68,7 +68,7 @@ function Address(data, network, type) { } // set defaults if not set - info.network = info.network || network || 'mainnet'; + info.network = info.network || network || networks.defaultNetwork.name; info.type = info.type || type || 'pubkeyhash'; // set the validated values @@ -105,7 +105,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 @@ -123,13 +123,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; @@ -199,7 +199,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 @@ -218,7 +218,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){ @@ -231,7 +231,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) { @@ -244,7 +244,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) { @@ -257,7 +257,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) { @@ -270,7 +270,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 */ @@ -284,7 +284,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 */ @@ -303,7 +303,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 */ @@ -323,11 +323,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 {boolean} The corresponding error message */ 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/lib/privatekey.js b/lib/privatekey.js index eb9a4db..5754ffb 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 || networks.defaultNetwork.name }; // 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,10 +115,10 @@ 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 = networks.livenet.name; } else if (buf[0] === networks.testnet.privatekey) { - info.network = 'testnet'; + info.network = networks.testnet.name; } else { throw new Error('Invalid network'); } @@ -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..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() { @@ -23,17 +24,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 +42,7 @@ describe('Address', function() { '1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7' ]; - // mainnet p2sh + // livenet p2sh var validp2shAddresses = [ '342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey', '33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk', @@ -57,7 +58,7 @@ describe('Address', function() { '2NB72XtkjpnATMggui83aEtPawyyKvnbX2o' ]; - //mainnet bad checksums + //livenet bad checksums var badChecksums = [ '15vkcKf7gB23wLAnZLmbVuMiiVDc3nq4a2', '1A6ut1tWnUq1SEQLMr4ttDh24wcbj4w2TT', @@ -65,7 +66,7 @@ describe('Address', function() { '1Jz2yCRd5ST1p2gUqFB5wsSQfdmEJaffg7' ]; - //mainnet non-base58 + //livenet non-base58 var nonBase58 = [ '15vkcKf7g#23wLAnZLmb$uMiiVDc3nq4a2', '1A601ttWnUq1SEQLMr4ttDh24wcbj4w2TT', @@ -83,13 +84,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 +129,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 +190,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 +198,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 +218,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 +244,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); });