Merge pull request #644 from braydonf/ref/default-network
Default the network, if not specified, to `networks.defaultNetwork`. Closes #559
This commit is contained in:
commit
7682cfa359
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -69,6 +69,5 @@ module.exports = {
|
|||
defaultNetwork: livenet,
|
||||
livenet: livenet,
|
||||
testnet: testnet,
|
||||
mainnet: livenet,
|
||||
get: getNetwork
|
||||
};
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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<validAddresses.length;i++){
|
||||
var error = Address.getValidationError(validAddresses[i], 'mainnet');
|
||||
var error = Address.getValidationError(validAddresses[i], 'livenet');
|
||||
should.not.exist(error);
|
||||
}
|
||||
});
|
||||
|
||||
it('should not validate because of an invalid checksum', function() {
|
||||
for(var i=0;i<badChecksums.length;i++){
|
||||
var error = Address.getValidationError(badChecksums[i], 'mainnet', 'pubkeyhash');
|
||||
var error = Address.getValidationError(badChecksums[i], 'livenet', 'pubkeyhash');
|
||||
should.exist(error);
|
||||
error.message.should.equal('Checksum mismatch');
|
||||
}
|
||||
|
@ -152,7 +153,7 @@ describe('Address', function() {
|
|||
|
||||
it('should not validate because of a mismatched type', function() {
|
||||
for(var i=0;i<validAddresses.length;i++){
|
||||
var error = Address.getValidationError(validAddresses[i], 'mainnet', 'scripthash');
|
||||
var error = Address.getValidationError(validAddresses[i], 'livenet', 'scripthash');
|
||||
should.exist(error);
|
||||
error.message.should.equal('Address has mismatched type.');
|
||||
}
|
||||
|
@ -160,7 +161,7 @@ describe('Address', function() {
|
|||
|
||||
it('should not validate because of non-base58 characters', function() {
|
||||
for(var i=0;i<nonBase58.length;i++){
|
||||
var error = Address.getValidationError(nonBase58[i], 'mainnet', 'pubkeyhash');
|
||||
var error = Address.getValidationError(nonBase58[i], 'livenet', 'pubkeyhash');
|
||||
should.exist(error);
|
||||
error.message.should.equal('Non-base58 character');
|
||||
}
|
||||
|
@ -182,7 +183,7 @@ describe('Address', function() {
|
|||
|
||||
it('should not validate testnet addresses because of mismatched network', function() {
|
||||
for(var i=0;i<testValidAddresses.length;i++){
|
||||
var error = Address.getValidationError(testValidAddresses[i], 'mainnet', 'pubkeyhash');
|
||||
var error = Address.getValidationError(testValidAddresses[i], 'livenet', 'pubkeyhash');
|
||||
should.exist(error);
|
||||
error.message.should.equal('Address has mismatched network type.');
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -274,9 +287,9 @@ describe('Address', function() {
|
|||
|
||||
it('should make this address from an uncompressed pubkey', function() {
|
||||
var pubkey = PublicKey.fromDER(new Buffer('0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98', 'hex'));
|
||||
var a = Address.fromPublicKey(pubkey, 'mainnet');
|
||||
var a = Address.fromPublicKey(pubkey, 'livenet');
|
||||
a.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
|
||||
var b = new Address(pubkey, 'mainnet', 'pubkeyhash');
|
||||
var b = new Address(pubkey, 'livenet', 'pubkeyhash');
|
||||
b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
|
||||
});
|
||||
|
||||
|
@ -299,7 +312,7 @@ describe('Address', function() {
|
|||
b.toString().should.equal('347iRqVwks5r493N1rsLN4k9J7Ljg488W7');
|
||||
});
|
||||
|
||||
it('should derive from this known address string mainnet', function() {
|
||||
it('should derive from this known address string livenet', function() {
|
||||
var address = new Address(str);
|
||||
var buffer = address.toBuffer();
|
||||
var slice = buffer.slice(1);
|
||||
|
@ -314,8 +327,8 @@ describe('Address', function() {
|
|||
b.network.should.equal('testnet');
|
||||
});
|
||||
|
||||
it('should derive from this known address string mainnet scripthash', function() {
|
||||
var a = new Address(validp2shAddresses[0], 'mainnet', 'scripthash');
|
||||
it('should derive from this known address string livenet scripthash', function() {
|
||||
var a = new Address(validp2shAddresses[0], 'livenet', 'scripthash');
|
||||
var b = new Address(a.toString());
|
||||
b.toString().should.equal(validp2shAddresses[0]);
|
||||
});
|
||||
|
@ -339,7 +352,7 @@ describe('Address', function() {
|
|||
|
||||
describe('#toString', function() {
|
||||
|
||||
it('should output a mainnet pubkeyhash address', function() {
|
||||
it('should output a livenet pubkeyhash address', function() {
|
||||
var address = new Address(str);
|
||||
address.toString().should.equal(str);
|
||||
});
|
||||
|
@ -365,7 +378,7 @@ describe('Address', function() {
|
|||
|
||||
it('should output formatted output correctly', function() {
|
||||
var address = new Address(str);
|
||||
var output = '<Address: 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r, type: pubkeyhash, network: mainnet>';
|
||||
var output = '<Address: 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r, type: pubkeyhash, network: livenet>';
|
||||
address.inspect().should.equal(output);
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -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() {
|
||||
|
@ -12,7 +13,7 @@ describe('PrivateKey', function() {
|
|||
var buf = new Buffer(hex, 'hex');
|
||||
var enctestnet = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG';
|
||||
var enctu = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu';
|
||||
var encmainnet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy';
|
||||
var enclivenet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy';
|
||||
var encmu = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un';
|
||||
|
||||
it('should create a new random private key', function() {
|
||||
|
@ -51,7 +52,7 @@ describe('PrivateKey', function() {
|
|||
|
||||
it('should not be able to instantiate private key because of compression mismatch', function() {
|
||||
(function() {
|
||||
var a = new PrivateKey('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m', 'mainnet', false);
|
||||
var a = new PrivateKey('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m', 'livenet', false);
|
||||
}).should.throw('Private key compression mismatch');
|
||||
});
|
||||
|
||||
|
@ -86,7 +87,7 @@ describe('PrivateKey', function() {
|
|||
it('should not be able to instantiate with unknown network', function() {
|
||||
(function() {
|
||||
var a = new PrivateKey(null, 'unknown');
|
||||
}).should.throw('Must specify the network ("mainnet" or "testnet")');
|
||||
}).should.throw('Must specify the network ("livenet" or "testnet")');
|
||||
});
|
||||
|
||||
it('should create a 0 private key with this convenience method', function() {
|
||||
|
@ -95,9 +96,20 @@ describe('PrivateKey', function() {
|
|||
privkey.bn.toString().should.equal(bn.toString());
|
||||
});
|
||||
|
||||
it('should create a mainnet private key', function() {
|
||||
var privkey = new PrivateKey(BN.fromBuffer(buf), 'mainnet', true);
|
||||
privkey.toString().should.equal(encmainnet);
|
||||
it('should create a livenet private key', function() {
|
||||
var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', true);
|
||||
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() {
|
||||
|
@ -105,8 +117,8 @@ describe('PrivateKey', function() {
|
|||
privkey.toString().should.equal(enctu);
|
||||
});
|
||||
|
||||
it('should create an uncompressed mainnet private key', function() {
|
||||
var privkey = new PrivateKey(BN.fromBuffer(buf), 'mainnet', false);
|
||||
it('should create an uncompressed livenet private key', function() {
|
||||
var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet', false);
|
||||
privkey.toString().should.equal(encmu);
|
||||
});
|
||||
|
||||
|
@ -129,7 +141,7 @@ describe('PrivateKey', function() {
|
|||
});
|
||||
|
||||
describe('#toAddress', function() {
|
||||
it('should output this known mainnet address correctly', function() {
|
||||
it('should output this known livenet address correctly', function() {
|
||||
var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
|
||||
var address = privkey.toAddress();
|
||||
address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT');
|
||||
|
@ -144,9 +156,9 @@ describe('PrivateKey', function() {
|
|||
});
|
||||
|
||||
describe('#inspect', function() {
|
||||
it('should output known mainnet address for console', function() {
|
||||
it('should output known livenet address for console', function() {
|
||||
var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
|
||||
privkey.inspect().should.equal('<PrivateKey: L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m, compressed: true, network: mainnet>');
|
||||
privkey.inspect().should.equal('<PrivateKey: L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m, compressed: true, network: livenet>');
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue