Browse Source

Refactor network.js

patch-2
Esteban Ordano 10 years ago
parent
commit
73265d9089
  1. 32
      lib/bip32.js
  2. 89
      lib/networks.js
  3. 2
      package.json

32
lib/bip32.js

@ -36,7 +36,7 @@ BIP32.prototype.set = function(obj) {
BIP32.prototype.fromRandom = function(networkstr) { BIP32.prototype.fromRandom = function(networkstr) {
if (!networkstr) if (!networkstr)
networkstr = 'mainnet'; networkstr = 'mainnet';
this.version = networks[networkstr].bip32privkey; this.version = networks[networkstr].xprivkey;
this.depth = 0x00; this.depth = 0x00;
this.parentfingerprint = new Buffer([0, 0, 0, 0]); this.parentfingerprint = new Buffer([0, 0, 0, 0]);
this.childindex = 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.parentfingerprint = new Buffer([0, 0, 0, 0]);
this.childindex = new Buffer([0, 0, 0, 0]); this.childindex = new Buffer([0, 0, 0, 0]);
this.chaincode = hash.slice(32, 64); 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.privkey = new PrivateKey(BN().fromBuffer(hash.slice(0, 32)));
this.pubkey = PublicKey.fromPrivateKey(this.privkey); this.pubkey = PublicKey.fromPrivateKey(this.privkey);
this.hasprivkey = true; this.hasprivkey = true;
@ -97,12 +97,12 @@ BIP32.prototype.initFromBytes = function(bytes) {
var keyBytes = bytes.slice(45, 78); var keyBytes = bytes.slice(45, 78);
var isPrivate = var isPrivate =
(this.version == networks.mainnet.bip32privkey || (this.version == networks.mainnet.xprivkey ||
this.version == networks.testnet.bip32privkey); this.version == networks.testnet.xprivkey);
var isPublic = var isPublic =
(this.version == networks.mainnet.bip32pubkey || (this.version == networks.mainnet.xpubkey ||
this.version == networks.testnet.bip32pubkey); this.version == networks.testnet.xpubkey);
if (isPrivate && keyBytes[0] == 0) { if (isPrivate && keyBytes[0] == 0) {
this.privkey = new PrivateKey(BN().fromBuffer(keyBytes.slice(1, 33))); this.privkey = new PrivateKey(BN().fromBuffer(keyBytes.slice(1, 33)));
@ -126,13 +126,13 @@ BIP32.prototype.buildxpubkey = function() {
var v = null; var v = null;
switch (this.version) { switch (this.version) {
case networks.mainnet.bip32pubkey: case networks.mainnet.xpubkey:
case networks.mainnet.bip32privkey: case networks.mainnet.xprivkey:
v = networks.mainnet.bip32pubkey; v = networks.mainnet.xpubkey;
break; break;
case networks.testnet.bip32pubkey: case networks.testnet.xpubkey:
case networks.testnet.bip32privkey: case networks.testnet.xprivkey:
v = networks.testnet.bip32pubkey; v = networks.testnet.xpubkey;
break; break;
default: default:
throw new Error('Unknown version'); throw new Error('Unknown version');
@ -244,8 +244,8 @@ BIP32.prototype.deriveChild = function(i) {
var usePrivate = (i & 0x80000000) != 0; var usePrivate = (i & 0x80000000) != 0;
var isPrivate = var isPrivate =
(this.version == networks.mainnet.bip32privkey || (this.version == networks.mainnet.xprivkey ||
this.version == networks.testnet.bip32privkey); this.version == networks.testnet.xprivkey);
if (usePrivate && (!this.hasprivkey || !isPrivate)) if (usePrivate && (!this.hasprivkey || !isPrivate))
throw new Error('Cannot do private key derivation without private key'); throw new Error('Cannot do private key derivation without private key');
@ -308,8 +308,8 @@ BIP32.prototype.deriveChild = function(i) {
BIP32.prototype.toString = function() { BIP32.prototype.toString = function() {
var isPrivate = var isPrivate =
(this.version == networks.mainnet.bip32privkey || (this.version == networks.mainnet.xprivkey ||
this.version == networks.testnet.bip32privkey); this.version == networks.testnet.xprivkey);
if (isPrivate) if (isPrivate)
return this.xprivkeyString(); return this.xprivkeyString();

89
lib/networks.js

@ -1,25 +1,72 @@
'use strict'; 'use strict';
var _ = require('lodash');
exports.mainnet = { /**
pubkeyhash: 0x00, * @constructor
identity: 0x0f, * A network is merely a map containing values that correspond to version
identephem: 0x02, * numbers for each bitcoin network. Currently only supporting "livenet"
identpersist: 0x01, * (a.k.a. "mainnet") and "testnet".
privatekey: 0x80, */
scripthash: 0x05, function Network() {}
bip32pubkey: 0x0488b21e,
bip32privkey: 0x0488ade4,
};
exports.testnet = { /**
pubkeyhash: 0x6f, * @instance
identity: 0x0f, * @member Network#livenet
identephem: 0x02, */
identpersist: 0x11, var livenet = new Network();
privatekey: 0xef, _.extend(livenet, {
scripthash: 0xc4, name: 'livenet',
bip32pubkey: 0x043587cf, alias: 'mainnet',
bip32privkey: 0x04358394, 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 = {};
exports.livenet = exports.mainnet; _.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
};

2
package.json

@ -68,6 +68,7 @@
"bs58": "=2.0.0", "bs58": "=2.0.0",
"elliptic": "=0.15.14", "elliptic": "=0.15.14",
"hash.js": "=0.3.2", "hash.js": "=0.3.2",
"lodash": "=2.4.1",
"sha512": "=0.0.1" "sha512": "=0.0.1"
}, },
"devDependencies": { "devDependencies": {
@ -77,6 +78,7 @@
"grunt-contrib-watch": "^0.6.1", "grunt-contrib-watch": "^0.6.1",
"grunt-markdown": "^0.6.1", "grunt-markdown": "^0.6.1",
"grunt-shell": "^1.1.1", "grunt-shell": "^1.1.1",
"lodash": "=2.4.1",
"mocha": "~2.0.1" "mocha": "~2.0.1"
}, },
"license": "MIT" "license": "MIT"

Loading…
Cancel
Save