Merge pull request #578 from eordano/refactor/networks
Refactor/networks
This commit is contained in:
commit
3cf7a54845
32
lib/bip32.js
32
lib/bip32.js
@ -36,7 +36,7 @@ BIP32.prototype.set = function(obj) {
|
||||
BIP32.prototype.fromRandom = function(networkstr) {
|
||||
if (!networkstr)
|
||||
networkstr = 'mainnet';
|
||||
this.version = networks[networkstr].bip32privkey;
|
||||
this.version = networks[networkstr].xprivkey;
|
||||
this.depth = 0x00;
|
||||
this.parentfingerprint = new Buffer([0, 0, 0, 0]);
|
||||
this.childindex = new Buffer([0, 0, 0, 0]);
|
||||
@ -71,7 +71,7 @@ BIP32.prototype.fromSeed = function(bytes, networkstr) {
|
||||
this.parentfingerprint = new Buffer([0, 0, 0, 0]);
|
||||
this.childindex = new Buffer([0, 0, 0, 0]);
|
||||
this.chaincode = hash.slice(32, 64);
|
||||
this.version = networks[networkstr].bip32privkey;
|
||||
this.version = networks[networkstr].xprivkey;
|
||||
this.privkey = new PrivateKey(BN().fromBuffer(hash.slice(0, 32)));
|
||||
this.pubkey = PublicKey.fromPrivateKey(this.privkey);
|
||||
this.hasprivkey = true;
|
||||
@ -97,12 +97,12 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
||||
var keyBytes = bytes.slice(45, 78);
|
||||
|
||||
var isPrivate =
|
||||
(this.version == networks.mainnet.bip32privkey ||
|
||||
this.version == networks.testnet.bip32privkey);
|
||||
(this.version == networks.mainnet.xprivkey ||
|
||||
this.version == networks.testnet.xprivkey);
|
||||
|
||||
var isPublic =
|
||||
(this.version == networks.mainnet.bip32pubkey ||
|
||||
this.version == networks.testnet.bip32pubkey);
|
||||
(this.version == networks.mainnet.xpubkey ||
|
||||
this.version == networks.testnet.xpubkey);
|
||||
|
||||
if (isPrivate && keyBytes[0] == 0) {
|
||||
this.privkey = new PrivateKey(BN().fromBuffer(keyBytes.slice(1, 33)));
|
||||
@ -126,13 +126,13 @@ BIP32.prototype.buildxpubkey = function() {
|
||||
|
||||
var v = null;
|
||||
switch (this.version) {
|
||||
case networks.mainnet.bip32pubkey:
|
||||
case networks.mainnet.bip32privkey:
|
||||
v = networks.mainnet.bip32pubkey;
|
||||
case networks.mainnet.xpubkey:
|
||||
case networks.mainnet.xprivkey:
|
||||
v = networks.mainnet.xpubkey;
|
||||
break;
|
||||
case networks.testnet.bip32pubkey:
|
||||
case networks.testnet.bip32privkey:
|
||||
v = networks.testnet.bip32pubkey;
|
||||
case networks.testnet.xpubkey:
|
||||
case networks.testnet.xprivkey:
|
||||
v = networks.testnet.xpubkey;
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unknown version');
|
||||
@ -244,8 +244,8 @@ BIP32.prototype.deriveChild = function(i) {
|
||||
var usePrivate = (i & 0x80000000) != 0;
|
||||
|
||||
var isPrivate =
|
||||
(this.version == networks.mainnet.bip32privkey ||
|
||||
this.version == networks.testnet.bip32privkey);
|
||||
(this.version == networks.mainnet.xprivkey ||
|
||||
this.version == networks.testnet.xprivkey);
|
||||
|
||||
if (usePrivate && (!this.hasprivkey || !isPrivate))
|
||||
throw new Error('Cannot do private key derivation without private key');
|
||||
@ -308,8 +308,8 @@ BIP32.prototype.deriveChild = function(i) {
|
||||
|
||||
BIP32.prototype.toString = function() {
|
||||
var isPrivate =
|
||||
(this.version == networks.mainnet.bip32privkey ||
|
||||
this.version == networks.testnet.bip32privkey);
|
||||
(this.version == networks.mainnet.xprivkey ||
|
||||
this.version == networks.testnet.xprivkey);
|
||||
|
||||
if (isPrivate)
|
||||
return this.xprivkeyString();
|
||||
|
||||
@ -1,25 +1,72 @@
|
||||
'use strict';
|
||||
var _ = require('lodash');
|
||||
|
||||
exports.mainnet = {
|
||||
pubkeyhash: 0x00,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x01,
|
||||
privatekey: 0x80,
|
||||
scripthash: 0x05,
|
||||
bip32pubkey: 0x0488b21e,
|
||||
bip32privkey: 0x0488ade4,
|
||||
/**
|
||||
* @constructor
|
||||
* A network is merely a map containing values that correspond to version
|
||||
* numbers for each bitcoin network. Currently only supporting "livenet"
|
||||
* (a.k.a. "mainnet") and "testnet".
|
||||
*/
|
||||
function Network() {}
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Network#livenet
|
||||
*/
|
||||
var livenet = new Network();
|
||||
_.extend(livenet, {
|
||||
name: 'livenet',
|
||||
alias: 'mainnet',
|
||||
pubkeyhash: 0x00,
|
||||
privatekey: 0x80,
|
||||
scripthash: 0x05,
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4
|
||||
});
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Network#testnet
|
||||
*/
|
||||
var testnet = new Network();
|
||||
_.extend(testnet, {
|
||||
name: 'testnet',
|
||||
pubkeyhash: 0x6f,
|
||||
privatekey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
xpubkey: 0x043587cf,
|
||||
xprivkey: 0x04358394
|
||||
});
|
||||
|
||||
var networkMaps = {};
|
||||
|
||||
_.each(_.values(livenet), function(value) {
|
||||
networkMaps[value] = livenet;
|
||||
});
|
||||
_.each(_.values(testnet), function(value) {
|
||||
networkMaps[value] = testnet;
|
||||
});
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @member Network#getNetwork
|
||||
* Retrieves the network associated with a magic number or string.
|
||||
* @param {string|number|Network} arg
|
||||
* @return Network
|
||||
*/
|
||||
function getNetwork(arg) {
|
||||
if (arg === livenet || arg === testnet) {
|
||||
return arg;
|
||||
}
|
||||
return networkMaps[arg];
|
||||
}
|
||||
|
||||
/**
|
||||
* @namespace Network
|
||||
*/
|
||||
module.exports = {
|
||||
livenet: livenet,
|
||||
testnet: testnet,
|
||||
mainnet: livenet,
|
||||
get: getNetwork
|
||||
};
|
||||
|
||||
exports.testnet = {
|
||||
pubkeyhash: 0x6f,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x11,
|
||||
privatekey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
bip32pubkey: 0x043587cf,
|
||||
bip32privkey: 0x04358394,
|
||||
};
|
||||
|
||||
exports.livenet = exports.mainnet;
|
||||
|
||||
@ -68,6 +68,7 @@
|
||||
"bs58": "=2.0.0",
|
||||
"elliptic": "=0.15.14",
|
||||
"hash.js": "=0.3.2",
|
||||
"lodash": "=2.4.1",
|
||||
"sha512": "=0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -77,6 +78,7 @@
|
||||
"grunt-contrib-watch": "^0.6.1",
|
||||
"grunt-markdown": "^0.6.1",
|
||||
"grunt-shell": "^1.1.1",
|
||||
"lodash": "=2.4.1",
|
||||
"mocha": "~2.0.1"
|
||||
},
|
||||
"license": "MIT"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user