fix references to encoding
This commit is contained in:
parent
deee8c6887
commit
4508fb6765
15
index.js
15
index.js
|
@ -1,12 +1,5 @@
|
||||||
var bitcore = module.exports;
|
var bitcore = module.exports;
|
||||||
|
|
||||||
// protocol
|
|
||||||
bitcore.Constants = require('./lib/protocol/constants');
|
|
||||||
bitcore.Base58 = require('./lib/protocol/base58');
|
|
||||||
bitcore.Base58Check = require('./lib/protocol/base58check');
|
|
||||||
bitcore.BufferReader = require('./lib/protocol/bufferreader');
|
|
||||||
bitcore.BufferWriter = require('./lib/protocol/bufferwriter');
|
|
||||||
bitcore.Varint = require('./lib/protocol/varint');
|
|
||||||
|
|
||||||
// crypto
|
// crypto
|
||||||
bitcore.BN = require('./lib/crypto/bn');
|
bitcore.BN = require('./lib/crypto/bn');
|
||||||
|
@ -15,12 +8,20 @@ bitcore.Hash = require('./lib/crypto/hash');
|
||||||
bitcore.Random = require('./lib/crypto/random');
|
bitcore.Random = require('./lib/crypto/random');
|
||||||
bitcore.Point = require('./lib/crypto/point');
|
bitcore.Point = require('./lib/crypto/point');
|
||||||
|
|
||||||
|
// encoding
|
||||||
|
bitcore.Base58 = require('./lib/encoding/base58');
|
||||||
|
bitcore.Base58Check = require('./lib/encoding/base58check');
|
||||||
|
bitcore.BufferReader = require('./lib/encoding/bufferreader');
|
||||||
|
bitcore.BufferWriter = require('./lib/encoding/bufferwriter');
|
||||||
|
bitcore.Varint = require('./lib/encoding/varint');
|
||||||
|
|
||||||
// main bitcoin library
|
// main bitcoin library
|
||||||
bitcore.Address = require('./lib/address');
|
bitcore.Address = require('./lib/address');
|
||||||
bitcore.BIP32 = require('./lib/bip32');
|
bitcore.BIP32 = require('./lib/bip32');
|
||||||
bitcore.Block = require('./lib/block');
|
bitcore.Block = require('./lib/block');
|
||||||
bitcore.Blockheader = require('./lib/blockheader');
|
bitcore.Blockheader = require('./lib/blockheader');
|
||||||
bitcore.Keypair = require('./lib/keypair');
|
bitcore.Keypair = require('./lib/keypair');
|
||||||
|
bitcore.networks = require('./lib/networks');
|
||||||
bitcore.Opcode = require('./lib/opcode');
|
bitcore.Opcode = require('./lib/opcode');
|
||||||
bitcore.Privkey = require('./lib/privkey');
|
bitcore.Privkey = require('./lib/privkey');
|
||||||
bitcore.Pubkey = require('./lib/pubkey');
|
bitcore.Pubkey = require('./lib/pubkey');
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var base58check = require('./protocol/base58check');
|
var base58check = require('./encoding/base58check');
|
||||||
var constants = require('./protocol/constants');
|
var networks = require('./networks');
|
||||||
var Hash = require('./crypto/hash');
|
var Hash = require('./crypto/hash');
|
||||||
|
|
||||||
function Address(buf) {
|
function Address(buf) {
|
||||||
|
@ -29,16 +29,16 @@ Address.prototype.fromBuffer = function(buf) {
|
||||||
if (buf.length !== 1 + 20)
|
if (buf.length !== 1 + 20)
|
||||||
throw new Error('Address buffers must be exactly 21 bytes');
|
throw new Error('Address buffers must be exactly 21 bytes');
|
||||||
var version = buf[0];
|
var version = buf[0];
|
||||||
if (version === constants['mainnet']['pubkeyhash']) {
|
if (version === networks['mainnet']['pubkeyhash']) {
|
||||||
this.networkstr = 'mainnet';
|
this.networkstr = 'mainnet';
|
||||||
this.typestr = 'pubkeyhash';
|
this.typestr = 'pubkeyhash';
|
||||||
} else if (version === constants['mainnet']['scripthash']) {
|
} else if (version === networks['mainnet']['scripthash']) {
|
||||||
this.networkstr = 'mainnet';
|
this.networkstr = 'mainnet';
|
||||||
this.typestr = 'scripthash';
|
this.typestr = 'scripthash';
|
||||||
} else if (version === constants['testnet']['pubkeyhash']) {
|
} else if (version === networks['testnet']['pubkeyhash']) {
|
||||||
this.networkstr = 'testnet';
|
this.networkstr = 'testnet';
|
||||||
this.typestr = 'pubkeyhash';
|
this.typestr = 'pubkeyhash';
|
||||||
} else if (version === constants['testnet']['scripthash']) {
|
} else if (version === networks['testnet']['scripthash']) {
|
||||||
this.networkstr = 'testnet';
|
this.networkstr = 'testnet';
|
||||||
this.typestr = 'scripthash';
|
this.typestr = 'scripthash';
|
||||||
} else {
|
} else {
|
||||||
|
@ -98,7 +98,7 @@ Address.prototype.isValid = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.toBuffer = function() {
|
Address.prototype.toBuffer = function() {
|
||||||
var version = new Buffer([constants[this.networkstr][this.typestr]]);
|
var version = new Buffer([networks[this.networkstr][this.typestr]]);
|
||||||
var buf = Buffer.concat([version, this.hashbuf]);
|
var buf = Buffer.concat([version, this.hashbuf]);
|
||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
|
36
lib/bip32.js
36
lib/bip32.js
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Base58Check = require('./protocol/base58check');
|
var Base58Check = require('./encoding/base58check');
|
||||||
var constants = require('./protocol/constants');
|
var networks = require('./networks');
|
||||||
var Hash = require('./crypto/hash');
|
var Hash = require('./crypto/hash');
|
||||||
var Point = require('./crypto/point');
|
var Point = require('./crypto/point');
|
||||||
var Random = require('./crypto/random');
|
var Random = require('./crypto/random');
|
||||||
|
@ -38,7 +38,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 = constants[networkstr].bip32privkey;
|
this.version = networks[networkstr].bip32privkey;
|
||||||
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]);
|
||||||
|
@ -72,7 +72,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 = constants[networkstr].bip32privkey;
|
this.version = networks[networkstr].bip32privkey;
|
||||||
this.keypair = new Keypair();
|
this.keypair = new Keypair();
|
||||||
this.keypair.privkey = new Privkey({bn: BN().fromBuffer(hash.slice(0, 32))});
|
this.keypair.privkey = new Privkey({bn: BN().fromBuffer(hash.slice(0, 32))});
|
||||||
this.keypair.privkey2pubkey();
|
this.keypair.privkey2pubkey();
|
||||||
|
@ -99,12 +99,12 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
||||||
var keyBytes = bytes.slice(45, 78);
|
var keyBytes = bytes.slice(45, 78);
|
||||||
|
|
||||||
var isPrivate =
|
var isPrivate =
|
||||||
(this.version == constants.mainnet.bip32privkey ||
|
(this.version == networks.mainnet.bip32privkey ||
|
||||||
this.version == constants.testnet.bip32privkey);
|
this.version == networks.testnet.bip32privkey);
|
||||||
|
|
||||||
var isPublic =
|
var isPublic =
|
||||||
(this.version == constants.mainnet.bip32pubkey ||
|
(this.version == networks.mainnet.bip32pubkey ||
|
||||||
this.version == constants.testnet.bip32pubkey);
|
this.version == networks.testnet.bip32pubkey);
|
||||||
|
|
||||||
if (isPrivate && keyBytes[0] == 0) {
|
if (isPrivate && keyBytes[0] == 0) {
|
||||||
this.keypair = new Keypair();
|
this.keypair = new Keypair();
|
||||||
|
@ -130,13 +130,13 @@ BIP32.prototype.buildxpubkey = function() {
|
||||||
|
|
||||||
var v = null;
|
var v = null;
|
||||||
switch (this.version) {
|
switch (this.version) {
|
||||||
case constants.mainnet.bip32pubkey:
|
case networks.mainnet.bip32pubkey:
|
||||||
case constants.mainnet.bip32privkey:
|
case networks.mainnet.bip32privkey:
|
||||||
v = constants.mainnet.bip32pubkey;
|
v = networks.mainnet.bip32pubkey;
|
||||||
break;
|
break;
|
||||||
case constants.testnet.bip32pubkey:
|
case networks.testnet.bip32pubkey:
|
||||||
case constants.testnet.bip32privkey:
|
case networks.testnet.bip32privkey:
|
||||||
v = constants.testnet.bip32pubkey;
|
v = networks.testnet.bip32pubkey;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown version');
|
throw new Error('Unknown version');
|
||||||
|
@ -248,8 +248,8 @@ BIP32.prototype.deriveChild = function(i) {
|
||||||
var usePrivate = (i & 0x80000000) != 0;
|
var usePrivate = (i & 0x80000000) != 0;
|
||||||
|
|
||||||
var isPrivate =
|
var isPrivate =
|
||||||
(this.version == constants.mainnet.bip32privkey ||
|
(this.version == networks.mainnet.bip32privkey ||
|
||||||
this.version == constants.testnet.bip32privkey);
|
this.version == networks.testnet.bip32privkey);
|
||||||
|
|
||||||
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');
|
||||||
|
@ -316,8 +316,8 @@ BIP32.prototype.deriveChild = function(i) {
|
||||||
|
|
||||||
BIP32.prototype.toString = function() {
|
BIP32.prototype.toString = function() {
|
||||||
var isPrivate =
|
var isPrivate =
|
||||||
(this.version == constants.mainnet.bip32privkey ||
|
(this.version == networks.mainnet.bip32privkey ||
|
||||||
this.version == constants.testnet.bip32privkey);
|
this.version == networks.testnet.bip32privkey);
|
||||||
|
|
||||||
if (isPrivate)
|
if (isPrivate)
|
||||||
return this.xprivkeyString();
|
return this.xprivkeyString();
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Hash = require('./crypto/hash');
|
var Hash = require('./crypto/hash');
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
var Varint = require('./protocol/varint');
|
var Varint = require('./encoding/varint');
|
||||||
var Transaction = require('./transaction');
|
var Transaction = require('./transaction');
|
||||||
var Blockheader = require('./blockheader');
|
var Blockheader = require('./blockheader');
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
|
|
||||||
var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce) {
|
var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce) {
|
||||||
if (!(this instanceof Blockheader))
|
if (!(this instanceof Blockheader))
|
||||||
|
|
|
@ -22,7 +22,4 @@ exports.testnet = {
|
||||||
bip32privkey: 0x04358394,
|
bip32privkey: 0x04358394,
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.ephemeral = {
|
exports.livenet = exports.mainnet;
|
||||||
prefix: 0x0f,
|
|
||||||
identity: 0x02
|
|
||||||
};
|
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
var BN = require('./crypto/bn');
|
var BN = require('./crypto/bn');
|
||||||
var Point = require('./crypto/point');
|
var Point = require('./crypto/point');
|
||||||
var Random = require('./crypto/random');
|
var Random = require('./crypto/random');
|
||||||
var constants = require('./protocol/constants');
|
var networks = require('./networks');
|
||||||
var base58check = require('./protocol/base58check');
|
var base58check = require('./encoding/base58check');
|
||||||
|
|
||||||
var Privkey = function Privkey(bn) {
|
var Privkey = function Privkey(bn) {
|
||||||
if (!(this instanceof Privkey))
|
if (!(this instanceof Privkey))
|
||||||
|
@ -50,7 +50,7 @@ Privkey.prototype.fromRandom = function() {
|
||||||
Privkey.prototype.validate = function() {
|
Privkey.prototype.validate = function() {
|
||||||
if (!this.bn.lt(Point.getN()))
|
if (!this.bn.lt(Point.getN()))
|
||||||
throw new Error('Number must be less than N');
|
throw new Error('Number must be less than N');
|
||||||
if (typeof constants[this.networkstr] === undefined)
|
if (typeof networks[this.networkstr] === undefined)
|
||||||
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');
|
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');
|
||||||
if (typeof this.compressed !== 'boolean')
|
if (typeof this.compressed !== 'boolean')
|
||||||
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)');
|
throw new Error('Must specify whether the corresponding public key is compressed or not (true or false)');
|
||||||
|
@ -68,9 +68,9 @@ Privkey.prototype.toWIF = function() {
|
||||||
var privbuf = this.bn.toBuffer({size: 32});
|
var privbuf = this.bn.toBuffer({size: 32});
|
||||||
var buf;
|
var buf;
|
||||||
if (compressed)
|
if (compressed)
|
||||||
buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]);
|
buf = Buffer.concat([new Buffer([networks[networkstr].privkey]), this.bn.toBuffer({size: 32}), new Buffer([0x01])]);
|
||||||
else
|
else
|
||||||
buf = Buffer.concat([new Buffer([constants[networkstr].privkey]), this.bn.toBuffer({size: 32})]);
|
buf = Buffer.concat([new Buffer([networks[networkstr].privkey]), this.bn.toBuffer({size: 32})]);
|
||||||
|
|
||||||
return base58check.encode(buf);
|
return base58check.encode(buf);
|
||||||
};
|
};
|
||||||
|
@ -85,9 +85,9 @@ Privkey.prototype.fromWIF = function(str) {
|
||||||
else
|
else
|
||||||
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
||||||
|
|
||||||
if (buf[0] === constants.mainnet.privkey)
|
if (buf[0] === networks.mainnet.privkey)
|
||||||
this.networkstr = 'mainnet';
|
this.networkstr = 'mainnet';
|
||||||
else if (buf[0] === constants.testnet.privkey)
|
else if (buf[0] === networks.testnet.privkey)
|
||||||
this.networkstr = 'testnet';
|
this.networkstr = 'testnet';
|
||||||
else
|
else
|
||||||
throw new Error('Invalid networkstr');
|
throw new Error('Invalid networkstr');
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
var Opcode = require('./opcode');
|
var Opcode = require('./opcode');
|
||||||
|
|
||||||
var Script = function Script(buf) {
|
var Script = function Script(buf) {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var Varint = require('./protocol/varint');
|
var Varint = require('./encoding/varint');
|
||||||
var Hash = require('./crypto/hash');
|
var Hash = require('./crypto/hash');
|
||||||
var Txin = require('./txin');
|
var Txin = require('./txin');
|
||||||
var Txout = require('./txout');
|
var Txout = require('./txout');
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
var Varint = require('./protocol/varint');
|
var Varint = require('./encoding/varint');
|
||||||
var Script = require('./script');
|
var Script = require('./script');
|
||||||
|
|
||||||
var Txin = function Txin(txidbuf, txoutnum, scriptvi, script, seqnum) {
|
var Txin = function Txin(txidbuf, txoutnum, scriptvi, script, seqnum) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var BN = require('./crypto/bn');
|
var BN = require('./crypto/bn');
|
||||||
var BufferReader = require('./protocol/bufferreader');
|
var BufferReader = require('./encoding/bufferreader');
|
||||||
var BufferWriter = require('./protocol/bufferwriter');
|
var BufferWriter = require('./encoding/bufferwriter');
|
||||||
var Varint = require('./protocol/varint');
|
var Varint = require('./encoding/varint');
|
||||||
var Script = require('./script');
|
var Script = require('./script');
|
||||||
|
|
||||||
var Txout = function Txout(valuebn, scriptvi, script) {
|
var Txout = function Txout(valuebn, scriptvi, script) {
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var should = require('chai').should();
|
var should = require('chai').should();
|
||||||
var bitcore = require('../../');
|
var bitcore = require('..');
|
||||||
var constants = bitcore.Constants;
|
var networks = bitcore.networks;
|
||||||
|
|
||||||
describe('Constants', function() {
|
describe('networks', function() {
|
||||||
|
|
||||||
it('should contain all constants for livenet and testnet', function() {
|
it('should contain all networks', function() {
|
||||||
for (var key in constants.livenet) {
|
should.exist(networks.livenet);
|
||||||
if (constants.livenet.hasOwnProperty(key)) {
|
should.exist(networks.testnet);
|
||||||
constants.testnet.hasOwnProperty(key).should.equal(true);
|
should.exist(networks.mainnet);
|
||||||
|
});
|
||||||
|
describe('contain all constants for livenet and testnet', function() {
|
||||||
|
for (var key in networks.livenet) {
|
||||||
|
if (networks.livenet.hasOwnProperty(key)) {
|
||||||
|
it('all should contain '+key, function() {
|
||||||
|
networks.testnet.hasOwnProperty(key).should.equal(true);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue