network refactors
This commit is contained in:
parent
54bbc42e7d
commit
71353426f6
|
@ -31,16 +31,16 @@ Address.prototype.network = function() {
|
|||
var testnet = networks.testnet;
|
||||
|
||||
var answer;
|
||||
if (version === livenet.addressPubkey || version === livenet.addressScript)
|
||||
if (version === livenet.addressVersion || version === livenet.P2SHVersion)
|
||||
answer = livenet;
|
||||
else if (version === testnet.addressPubkey || version === testnet.addressScript)
|
||||
else if (version === testnet.addressVersion || version === testnet.P2SHVersion)
|
||||
answer = testnet;
|
||||
|
||||
return answer;
|
||||
};
|
||||
|
||||
Address.prototype.isScript = function() {
|
||||
return this.isValid() && this.version() === this.network().addressScript;
|
||||
return this.isValid() && this.version() === this.network().P2SHVersion;
|
||||
};
|
||||
|
||||
|
||||
|
|
30
BIP32.js
30
BIP32.js
|
@ -12,9 +12,9 @@ var secp256k1_Gx = new bignum("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D95
|
|||
|
||||
var BIP32 = function(bytes) {
|
||||
if (bytes == 'mainnet' || bytes == 'livenet')
|
||||
this.version = networks['livenet'].bip32private;
|
||||
this.version = networks['livenet'].bip32privateVersion;
|
||||
else if (bytes == 'testnet')
|
||||
this.version = networks['testnet'].bip32private;
|
||||
this.version = networks['testnet'].bip32privateVersion;
|
||||
|
||||
if (bytes == 'mainnet' || bytes == 'livenet' || bytes == 'testnet') {
|
||||
this.depth = 0x00;
|
||||
|
@ -63,7 +63,7 @@ BIP32.seed = function(bytes, network) {
|
|||
bip32.parentFingerprint = new Buffer([0, 0, 0, 0]);
|
||||
bip32.childIndex = new Buffer([0, 0, 0, 0]);
|
||||
bip32.chainCode = hash.slice(32, 64);
|
||||
bip32.version = networks[network].bip32private;
|
||||
bip32.version = networks[network].bip32privateVersion;
|
||||
bip32.eckey = new Key();
|
||||
bip32.eckey.private = hash.slice(0, 32);
|
||||
bip32.eckey.regenerateSync();
|
||||
|
@ -89,12 +89,12 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
|||
var keyBytes = bytes.slice(45, 78);
|
||||
|
||||
var isPrivate =
|
||||
(this.version == networks['livenet'].bip32private ||
|
||||
this.version == networks['testnet'].bip32private );
|
||||
(this.version == networks['livenet'].bip32privateVersion ||
|
||||
this.version == networks['testnet'].bip32privateVersion );
|
||||
|
||||
var isPublic =
|
||||
(this.version == networks['livenet'].bip32public ||
|
||||
this.version == networks['testnet'].bip32public );
|
||||
(this.version == networks['livenet'].bip32publicVersion ||
|
||||
this.version == networks['testnet'].bip32publicVersion );
|
||||
|
||||
if (isPrivate && keyBytes[0] == 0) {
|
||||
this.eckey = new Key();
|
||||
|
@ -121,13 +121,13 @@ BIP32.prototype.buildExtendedPublicKey = function() {
|
|||
|
||||
var v = null;
|
||||
switch(this.version) {
|
||||
case networks['livenet'].bip32public:
|
||||
case networks['livenet'].bip32private:
|
||||
v = networks['livenet'].bip32public;
|
||||
case networks['livenet'].bip32publicVersion:
|
||||
case networks['livenet'].bip32privateVersion:
|
||||
v = networks['livenet'].bip32publicVersion;
|
||||
break;
|
||||
case networks['testnet'].bip32public:
|
||||
case networks['testnet'].bip32private:
|
||||
v = networks['testnet'].bip32public;
|
||||
case networks['testnet'].bip32publicVersion:
|
||||
case networks['testnet'].bip32privateVersion:
|
||||
v = networks['testnet'].bip32publicVersion;
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unknown version");
|
||||
|
@ -256,8 +256,8 @@ BIP32.prototype.deriveChild = function(i) {
|
|||
var usePrivate = (i & 0x80000000) != 0;
|
||||
|
||||
var isPrivate =
|
||||
(this.version == networks['livenet'].bip32private ||
|
||||
this.version == networks['testnet'].bip32private );
|
||||
(this.version == networks['livenet'].bip32privateVersion ||
|
||||
this.version == networks['testnet'].bip32privateVersion );
|
||||
|
||||
if (usePrivate && (!this.hasPrivateKey || !isPrivate))
|
||||
throw new Error("Cannot do private key derivation without private key");
|
||||
|
|
|
@ -70,9 +70,9 @@ PrivateKey.prototype.network = function() {
|
|||
var testnet = networks.testnet;
|
||||
|
||||
var answer;
|
||||
if (version === livenet.keySecret)
|
||||
if (version === livenet.privKeyVersion)
|
||||
answer = livenet;
|
||||
else if (version === testnet.keySecret)
|
||||
else if (version === testnet.privKeyVersion)
|
||||
answer = testnet;
|
||||
|
||||
return answer;
|
||||
|
|
|
@ -232,21 +232,21 @@ var getAddrStr = function(s) {
|
|||
switch (type) {
|
||||
case Script.TX_PUBKEY:
|
||||
var chunk = s.captureOne();
|
||||
addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
|
||||
addr = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk));
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_PUBKEYHASH:
|
||||
addr = new Address(network.addressPubkey, s.captureOne());
|
||||
addr = new Address(network.addressVersion, s.captureOne());
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_SCRIPTHASH:
|
||||
addr = new Address(network.addressScript, s.captureOne());
|
||||
addr = new Address(network.P2SHVersion, s.captureOne());
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_MULTISIG:
|
||||
var chunks = s.capture();
|
||||
chunks.forEach(function(chunk) {
|
||||
var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
|
||||
var a = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk));
|
||||
addrStrs.push(a.toString());
|
||||
});
|
||||
break;
|
||||
|
|
10
Sign.js
10
Sign.js
|
@ -39,7 +39,7 @@ function signTxIn(nIn, tx, txInputs, network, keys, scripts)
|
|||
var subType = undefined;
|
||||
var subData = undefined;
|
||||
if (txType == TX_SCRIPTHASH) {
|
||||
var addr = new Address(network.addressScript, scriptData[0]);
|
||||
var addr = new Address(network.P2SHVersion, scriptData[0]);
|
||||
var addrStr = addr.toString();
|
||||
if (!(addrStr in scripts))
|
||||
throw new Error("unknown script hash address");
|
||||
|
@ -61,7 +61,7 @@ function signTxIn(nIn, tx, txInputs, network, keys, scripts)
|
|||
return;
|
||||
|
||||
var pubkeyhash = util.sha256ripe160(scriptData[0]);
|
||||
var addr = new Address(network.addressPubkey, pubkeyhash);
|
||||
var addr = new Address(network.addressVersion, pubkeyhash);
|
||||
var addrStr = addr.toString();
|
||||
if (!(addrStr in keys))
|
||||
throw new Error("unknown pubkey");
|
||||
|
@ -75,7 +75,7 @@ function signTxIn(nIn, tx, txInputs, network, keys, scripts)
|
|||
if (scriptSig.chunks.length > 0)
|
||||
return;
|
||||
|
||||
var addr = new Address(network.addressPubkey, scriptData[0]);
|
||||
var addr = new Address(network.addressVersion, scriptData[0]);
|
||||
var addrStr = addr.toString();
|
||||
if (!(addrStr in keys))
|
||||
throw new Error("unknown pubkey hash address");
|
||||
|
@ -90,7 +90,7 @@ function signTxIn(nIn, tx, txInputs, network, keys, scripts)
|
|||
if (scriptSig.chunks.length > 0)
|
||||
return;
|
||||
|
||||
var addr = new Address(network.addressPubkey, subData[0]);
|
||||
var addr = new Address(network.addressVersion, subData[0]);
|
||||
var addrStr = addr.toString();
|
||||
if (!(addrStr in keys))
|
||||
throw new Error("unknown script(pubkey hash) address");
|
||||
|
@ -110,7 +110,7 @@ function signTxIn(nIn, tx, txInputs, network, keys, scripts)
|
|||
continue;
|
||||
|
||||
var pubkeyhash = util.sha256ripe160(scriptSig.chunks[i]);
|
||||
var addr = new Address(network.addressPubkey, pubkeyhash);
|
||||
var addr = new Address(network.addressVersion, pubkeyhash);
|
||||
var addrStr = addr.toString();
|
||||
if (!(addrStr in keys))
|
||||
continue;
|
||||
|
|
|
@ -128,9 +128,9 @@ TransactionBuilder.scriptForAddress = function(addressString) {
|
|||
|
||||
var version = address.version();
|
||||
var script;
|
||||
if (version === livenet.addressPubkey || version === testnet.addressPubkey)
|
||||
if (version === livenet.addressVersion || version === testnet.addressVersion)
|
||||
script = Script.createPubKeyHashOut(address.payload());
|
||||
else if (version === livenet.addressScript || version === testnet.addressScript)
|
||||
else if (version === livenet.P2SHVersion || version === testnet.P2SHVersion)
|
||||
script = Script.createP2SH(address.payload());
|
||||
else
|
||||
throw new Error('invalid output address');
|
||||
|
@ -169,7 +169,7 @@ TransactionBuilder.infoForP2sh = function(opts, networkName) {
|
|||
var hash = util.sha256ripe160(script.getBuffer());
|
||||
|
||||
var version = networkName === 'testnet' ?
|
||||
networks.testnet.addressScript : networks.livenet.addressScript;
|
||||
networks.testnet.P2SHVersion : networks.livenet.P2SHVersion;
|
||||
|
||||
var addr = new Address(version, hash);
|
||||
var addrStr = addr.as('base58');
|
||||
|
@ -439,7 +439,7 @@ TransactionBuilder.prototype._checkTx = function() {
|
|||
TransactionBuilder.prototype._multiFindKey = function(walletKeyMap,pubKeyHash) {
|
||||
var wk;
|
||||
[ networks.livenet, networks.testnet].forEach(function(n) {
|
||||
[ n.addressPubkey, n.addressScript].forEach(function(v) {
|
||||
[ n.addressVersion, n.P2SHVersion].forEach(function(v) {
|
||||
var a = new Address(v,pubKeyHash);
|
||||
if (!wk && walletKeyMap[a]) {
|
||||
wk = walletKeyMap[a];
|
||||
|
|
|
@ -128,7 +128,7 @@ Wallet.prototype.expandKeys = function(keys) {
|
|||
Wallet.prototype.addScript = function(script) {
|
||||
var buf = script.getBuffer();
|
||||
var hash = util.sha256ripe160(buf);
|
||||
var addr = new Address(this.network.addressScript, hash);
|
||||
var addr = new Address(this.network.P2SHVersion, hash);
|
||||
var addrStr = addr.as('base58');
|
||||
this.datastore.scripts[addrStr] = buf.toString('hex');
|
||||
this.dirty = true;
|
||||
|
|
|
@ -22,8 +22,8 @@ WalletKey.prototype.generate = function() {
|
|||
WalletKey.prototype.storeObj = function() {
|
||||
var pubKey = this.privKey.public.toString('hex');
|
||||
var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public);
|
||||
var addr = new Address(this.network.addressPubkey, pubKeyHash);
|
||||
var priv = new PrivateKey(this.network.keySecret, this.privKey.private, this.privKey.compressed);
|
||||
var addr = new Address(this.network.addressVersion, pubKeyHash);
|
||||
var priv = new PrivateKey(this.network.privKeyVersion, this.privKey.private, this.privKey.compressed);
|
||||
var obj = {
|
||||
created: this.created,
|
||||
priv: priv.toString(),
|
||||
|
|
|
@ -63,7 +63,7 @@ var run = function() {
|
|||
p('\tHex : ' + buffertools.toHex(s.buffer));
|
||||
p('\tHuman : ' + s.toHumanReadable());
|
||||
p('\tScript Hash: ' + buffertools.toHex(hash));
|
||||
var a = new Address(networks.livenet.addressScript,hash);
|
||||
var a = new Address(networks.livenet.P2SHVersion,hash);
|
||||
p('\tp2sh Addr: ' + a.toString());
|
||||
|
||||
};
|
||||
|
|
|
@ -16,21 +16,21 @@ var run = function() {
|
|||
switch (type) {
|
||||
case Script.TX_PUBKEY:
|
||||
var chunk = s.captureOne();
|
||||
addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
|
||||
addr = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk));
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_PUBKEYHASH:
|
||||
addr = new Address(network.addressPubkey, s.captureOne());
|
||||
addr = new Address(network.addressVersion, s.captureOne());
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_SCRIPTHASH:
|
||||
addr = new Address(network.addressScript, s.captureOne());
|
||||
addr = new Address(network.P2SHVersion, s.captureOne());
|
||||
addrStrs.push(addr.toString());
|
||||
break;
|
||||
case Script.TX_MULTISIG:
|
||||
var chunks = s.capture();
|
||||
chunks.forEach(function(chunk) {
|
||||
var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk));
|
||||
var a = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk));
|
||||
addrStrs.push(a.toString());
|
||||
});
|
||||
break;
|
||||
|
|
63
networks.js
63
networks.js
|
@ -4,69 +4,40 @@ var hex = function(hex) {return new Buffer(hex, 'hex');};
|
|||
|
||||
exports.livenet = {
|
||||
name: 'livenet',
|
||||
addressVersion: 0x00,
|
||||
magic: hex('f9beb4d9'),
|
||||
addressVersion: 0x00,
|
||||
privKeyVersion: 128,
|
||||
P2SHVersion: 5,
|
||||
bip32publicVersion: 0x0488b21e,
|
||||
bip32privateVersion: 0x0488ade4,
|
||||
genesisBlock: {
|
||||
hash: hex('6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000'),
|
||||
merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'),
|
||||
height: 0,
|
||||
nonce: 2083236893,
|
||||
version: 1,
|
||||
hash: hex('6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000'),
|
||||
prev_hash: buffertools.fill(new Buffer(32), 0),
|
||||
timestamp: 1231006505,
|
||||
merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'),
|
||||
bits: 486604799
|
||||
},
|
||||
genesisBlockTx: {
|
||||
outs: [{
|
||||
v: hex('00F2052A01000000'), // 50 BTC
|
||||
s: new Put()
|
||||
.word8(65) // 65 bytes of data follow
|
||||
.put(hex('04678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5F'))
|
||||
.word8(0xAC) // OP_CHECKSIG
|
||||
.buffer()
|
||||
}],
|
||||
lock_time: 0,
|
||||
version: 1,
|
||||
hash: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'),
|
||||
ins: [{
|
||||
q: 0xFFFFFFFF,
|
||||
o: hex("0000000000000000000000000000000000000000000000000000000000000000FFFFFFFF"),
|
||||
s: new Put()
|
||||
.put(hex('04FFFF001D010445'))
|
||||
.put(new Buffer('The Times 03/Jan/2009 Chancellor on brink of ' +
|
||||
'second bailout for banks', 'ascii'))
|
||||
.buffer()
|
||||
}]
|
||||
},
|
||||
proofOfWorkLimit: hex("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
|
||||
checkpoints: [], // need to put checkpoint blocks here
|
||||
addressPubkey: 0,
|
||||
addressScript: 5,
|
||||
bip32public: 0x0488b21e,
|
||||
bip32private: 0x0488ade4,
|
||||
keySecret: 128,
|
||||
bits: 486604799,
|
||||
}
|
||||
};
|
||||
|
||||
exports.testnet = {
|
||||
name: 'testnet',
|
||||
addressVersion: 0x6f,
|
||||
magic: hex('0b110907'),
|
||||
addressVersion: 0x6f,
|
||||
privKeyVersion: 239,
|
||||
P2SHVersion: 196,
|
||||
bip32publicVersion: 0x043587cf,
|
||||
bip32privateVersion: 0x04358394,
|
||||
genesisBlock: {
|
||||
hash: hex('43497FD7F826957108F4A30FD9CEC3AEBA79972084E90EAD01EA330900000000'),
|
||||
merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'),
|
||||
height: 0,
|
||||
nonce: 414098458,
|
||||
version: 1,
|
||||
hash: hex('43497FD7F826957108F4A30FD9CEC3AEBA79972084E90EAD01EA330900000000'),
|
||||
prev_hash: buffertools.fill(new Buffer(32), 0),
|
||||
timestamp: 1296688602,
|
||||
merkle_root: hex('3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A'),
|
||||
bits: 486604799,
|
||||
},
|
||||
genesisBlockTx: module.exports.livenet.genesisBlockTx,
|
||||
proofOfWorkLimit: module.exports.livenet.proofOfWorkLimit,
|
||||
checkpoints: [], // need to put checkput blocks here
|
||||
addressPubkey: 111,
|
||||
addressScript: 196,
|
||||
bip32public: 0x043587cf,
|
||||
bip32private: 0x04358394,
|
||||
keySecret: 239,
|
||||
}
|
||||
};
|
||||
|
|
|
@ -25,7 +25,7 @@ describe('PrivateKey', function() {
|
|||
it('should convert hex testnet private key with compressed public key to base58check format', function() {
|
||||
var hex = 'b9f4892c9e8282028fea1d2667c4dc5213564d41fc5783896a0d843fc15089f3';
|
||||
var buf = new Buffer(hex, 'hex');
|
||||
var privkey = new PrivateKey(networks.testnet.keySecret, buf, true);
|
||||
var privkey = new PrivateKey(networks.testnet.privKeyVersion, buf, true);
|
||||
|
||||
privkey.as('base58').should.equal('cTpB4YiyKiBcPxnefsDpbnDxFDffjqJob8wGCEDXxgQ7zQoMXJdH');
|
||||
});
|
||||
|
|
|
@ -15,7 +15,7 @@ var Key = bitcore.Key;
|
|||
|
||||
function test_encode_priv(b58, payload, isTestnet, isCompressed) {
|
||||
var network = isTestnet ? networks.testnet : networks.livenet;
|
||||
var version = network.keySecret;
|
||||
var version = network.privKeyVersion;
|
||||
|
||||
var buf_pl = new Buffer(payload, 'hex');
|
||||
var buf;
|
||||
|
@ -37,7 +37,7 @@ function test_encode_priv(b58, payload, isTestnet, isCompressed) {
|
|||
function test_encode_pub(b58, payload, isTestnet, addrType) {
|
||||
var isScript = (addrType === 'script');
|
||||
var network = isTestnet ? networks.testnet : networks.livenet;
|
||||
var version = isScript ? network.addressScript : network.addressPubkey;
|
||||
var version = isScript ? network.P2SHVersion : network.addressVersion;
|
||||
var buf = new Buffer(payload, 'hex');
|
||||
var addr = new Address(version, buf);
|
||||
addr.toString().should.equal(b58);
|
||||
|
@ -46,7 +46,7 @@ function test_encode_pub(b58, payload, isTestnet, addrType) {
|
|||
|
||||
function test_decode_priv(b58, payload, isTestnet, isCompressed) {
|
||||
var network = isTestnet ? networks.testnet : networks.livenet;
|
||||
var version = network.keySecret;
|
||||
var version = network.privKeyVersion;
|
||||
|
||||
var buf_pl = new Buffer(payload, 'hex');
|
||||
var buf;
|
||||
|
@ -65,7 +65,7 @@ function test_decode_priv(b58, payload, isTestnet, isCompressed) {
|
|||
function test_decode_pub(b58, payload, isTestnet, addrType) {
|
||||
var isScript = (addrType === 'script');
|
||||
var network = isTestnet ? networks.testnet : networks.livenet;
|
||||
var version = isScript ? network.addressScript : network.addressPubkey;
|
||||
var version = isScript ? network.P2SHVersion : network.addressVersion;
|
||||
var buf = new Buffer(payload, 'hex');
|
||||
var addr = new Address(b58);
|
||||
|
||||
|
|
|
@ -23,6 +23,18 @@ describe('Miscelaneous stuff', function() {
|
|||
it('should initialze the log object', function() {
|
||||
should.exist(bitcore.log);
|
||||
});
|
||||
it('should initialze the network object', function() {
|
||||
should.exist(networks);
|
||||
var nets = [networks.livenet, networks.testnet];
|
||||
for (var i=0; i<2; i++) {
|
||||
var net = nets[i];
|
||||
should.exist(net.addressVersion);
|
||||
should.exist(net.privKeyVersion);
|
||||
should.exist(net.P2SHVersion);
|
||||
should.exist(net.bip32publicVersion);
|
||||
should.exist(net.bip32privateVersion);
|
||||
}
|
||||
});
|
||||
it('should initialze the const object', function() {
|
||||
should.exist(bitcore.const);
|
||||
});
|
||||
|
@ -122,7 +134,7 @@ describe('Miscelaneous stuff', function() {
|
|||
a.network().should.equal(network);
|
||||
});
|
||||
it('should generate correctly from hex', function() {
|
||||
var version = shouldBeScript ? network.addressScript : network.addressPubkey;
|
||||
var version = shouldBeScript ? network.P2SHVersion : network.addressVersion;
|
||||
var b = new Address(version, new Buffer(hexPayload, 'hex'));
|
||||
b.toString().should.equal(b58);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue