fix references to encoding

This commit is contained in:
Manuel Araoz 2014-11-21 12:54:56 -03:00
parent deee8c6887
commit 4508fb6765
12 changed files with 71 additions and 67 deletions

View File

@ -1,12 +1,5 @@
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
bitcore.BN = require('./lib/crypto/bn');
@ -15,12 +8,20 @@ bitcore.Hash = require('./lib/crypto/hash');
bitcore.Random = require('./lib/crypto/random');
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
bitcore.Address = require('./lib/address');
bitcore.BIP32 = require('./lib/bip32');
bitcore.Block = require('./lib/block');
bitcore.Blockheader = require('./lib/blockheader');
bitcore.Keypair = require('./lib/keypair');
bitcore.networks = require('./lib/networks');
bitcore.Opcode = require('./lib/opcode');
bitcore.Privkey = require('./lib/privkey');
bitcore.Pubkey = require('./lib/pubkey');

View File

@ -1,7 +1,7 @@
'use strict';
var base58check = require('./protocol/base58check');
var constants = require('./protocol/constants');
var base58check = require('./encoding/base58check');
var networks = require('./networks');
var Hash = require('./crypto/hash');
function Address(buf) {
@ -29,16 +29,16 @@ Address.prototype.fromBuffer = function(buf) {
if (buf.length !== 1 + 20)
throw new Error('Address buffers must be exactly 21 bytes');
var version = buf[0];
if (version === constants['mainnet']['pubkeyhash']) {
if (version === networks['mainnet']['pubkeyhash']) {
this.networkstr = 'mainnet';
this.typestr = 'pubkeyhash';
} else if (version === constants['mainnet']['scripthash']) {
} else if (version === networks['mainnet']['scripthash']) {
this.networkstr = 'mainnet';
this.typestr = 'scripthash';
} else if (version === constants['testnet']['pubkeyhash']) {
} else if (version === networks['testnet']['pubkeyhash']) {
this.networkstr = 'testnet';
this.typestr = 'pubkeyhash';
} else if (version === constants['testnet']['scripthash']) {
} else if (version === networks['testnet']['scripthash']) {
this.networkstr = 'testnet';
this.typestr = 'scripthash';
} else {
@ -98,7 +98,7 @@ Address.prototype.isValid = 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]);
return buf;
};

View File

@ -1,7 +1,7 @@
'use strict';
var Base58Check = require('./protocol/base58check');
var constants = require('./protocol/constants');
var Base58Check = require('./encoding/base58check');
var networks = require('./networks');
var Hash = require('./crypto/hash');
var Point = require('./crypto/point');
var Random = require('./crypto/random');
@ -38,7 +38,7 @@ BIP32.prototype.set = function(obj) {
BIP32.prototype.fromRandom = function(networkstr) {
if (!networkstr)
networkstr = 'mainnet';
this.version = constants[networkstr].bip32privkey;
this.version = networks[networkstr].bip32privkey;
this.depth = 0x00;
this.parentfingerprint = 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.childindex = new Buffer([0, 0, 0, 0]);
this.chaincode = hash.slice(32, 64);
this.version = constants[networkstr].bip32privkey;
this.version = networks[networkstr].bip32privkey;
this.keypair = new Keypair();
this.keypair.privkey = new Privkey({bn: BN().fromBuffer(hash.slice(0, 32))});
this.keypair.privkey2pubkey();
@ -99,12 +99,12 @@ BIP32.prototype.initFromBytes = function(bytes) {
var keyBytes = bytes.slice(45, 78);
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
(this.version == networks.mainnet.bip32privkey ||
this.version == networks.testnet.bip32privkey);
var isPublic =
(this.version == constants.mainnet.bip32pubkey ||
this.version == constants.testnet.bip32pubkey);
(this.version == networks.mainnet.bip32pubkey ||
this.version == networks.testnet.bip32pubkey);
if (isPrivate && keyBytes[0] == 0) {
this.keypair = new Keypair();
@ -130,13 +130,13 @@ BIP32.prototype.buildxpubkey = function() {
var v = null;
switch (this.version) {
case constants.mainnet.bip32pubkey:
case constants.mainnet.bip32privkey:
v = constants.mainnet.bip32pubkey;
case networks.mainnet.bip32pubkey:
case networks.mainnet.bip32privkey:
v = networks.mainnet.bip32pubkey;
break;
case constants.testnet.bip32pubkey:
case constants.testnet.bip32privkey:
v = constants.testnet.bip32pubkey;
case networks.testnet.bip32pubkey:
case networks.testnet.bip32privkey:
v = networks.testnet.bip32pubkey;
break;
default:
throw new Error('Unknown version');
@ -248,8 +248,8 @@ BIP32.prototype.deriveChild = function(i) {
var usePrivate = (i & 0x80000000) != 0;
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
(this.version == networks.mainnet.bip32privkey ||
this.version == networks.testnet.bip32privkey);
if (usePrivate && (!this.hasprivkey || !isPrivate))
throw new Error('Cannot do private key derivation without private key');
@ -316,8 +316,8 @@ BIP32.prototype.deriveChild = function(i) {
BIP32.prototype.toString = function() {
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
(this.version == networks.mainnet.bip32privkey ||
this.version == networks.testnet.bip32privkey);
if (isPrivate)
return this.xprivkeyString();

View File

@ -1,9 +1,9 @@
'use strict';
var Hash = require('./crypto/hash');
var BufferReader = require('./protocol/bufferreader');
var BufferWriter = require('./protocol/bufferwriter');
var Varint = require('./protocol/varint');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Varint = require('./encoding/varint');
var Transaction = require('./transaction');
var Blockheader = require('./blockheader');

View File

@ -1,7 +1,7 @@
'use strict';
var BufferReader = require('./protocol/bufferreader');
var BufferWriter = require('./protocol/bufferwriter');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce) {
if (!(this instanceof Blockheader))

View File

@ -22,7 +22,4 @@ exports.testnet = {
bip32privkey: 0x04358394,
};
exports.ephemeral = {
prefix: 0x0f,
identity: 0x02
};
exports.livenet = exports.mainnet;

View File

@ -3,8 +3,8 @@
var BN = require('./crypto/bn');
var Point = require('./crypto/point');
var Random = require('./crypto/random');
var constants = require('./protocol/constants');
var base58check = require('./protocol/base58check');
var networks = require('./networks');
var base58check = require('./encoding/base58check');
var Privkey = function Privkey(bn) {
if (!(this instanceof Privkey))
@ -50,7 +50,7 @@ Privkey.prototype.fromRandom = function() {
Privkey.prototype.validate = function() {
if (!this.bn.lt(Point.getN()))
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")');
if (typeof this.compressed !== 'boolean')
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 buf;
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
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);
};
@ -85,9 +85,9 @@ Privkey.prototype.fromWIF = function(str) {
else
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';
else if (buf[0] === constants.testnet.privkey)
else if (buf[0] === networks.testnet.privkey)
this.networkstr = 'testnet';
else
throw new Error('Invalid networkstr');

View File

@ -1,7 +1,7 @@
'use strict';
var BufferReader = require('./protocol/bufferreader');
var BufferWriter = require('./protocol/bufferwriter');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Opcode = require('./opcode');
var Script = function Script(buf) {

View File

@ -1,8 +1,8 @@
'use strict';
var BufferWriter = require('./protocol/bufferwriter');
var BufferReader = require('./protocol/bufferreader');
var Varint = require('./protocol/varint');
var BufferWriter = require('./encoding/bufferwriter');
var BufferReader = require('./encoding/bufferreader');
var Varint = require('./encoding/varint');
var Hash = require('./crypto/hash');
var Txin = require('./txin');
var Txout = require('./txout');

View File

@ -1,8 +1,8 @@
'use strict';
var BufferReader = require('./protocol/bufferreader');
var BufferWriter = require('./protocol/bufferwriter');
var Varint = require('./protocol/varint');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Varint = require('./encoding/varint');
var Script = require('./script');
var Txin = function Txin(txidbuf, txoutnum, scriptvi, script, seqnum) {

View File

@ -1,9 +1,9 @@
'use strict';
var BN = require('./crypto/bn');
var BufferReader = require('./protocol/bufferreader');
var BufferWriter = require('./protocol/bufferwriter');
var Varint = require('./protocol/varint');
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
var Varint = require('./encoding/varint');
var Script = require('./script');
var Txout = function Txout(valuebn, scriptvi, script) {

View File

@ -1,18 +1,24 @@
'use strict';
var should = require('chai').should();
var bitcore = require('../../');
var constants = bitcore.Constants;
var bitcore = require('..');
var networks = bitcore.networks;
describe('Constants', function() {
describe('networks', function() {
it('should contain all constants for livenet and testnet', function() {
for (var key in constants.livenet) {
if (constants.livenet.hasOwnProperty(key)) {
constants.testnet.hasOwnProperty(key).should.equal(true);
it('should contain all networks', function() {
should.exist(networks.livenet);
should.exist(networks.testnet);
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);
});
}
}
});
});