diff --git a/src/address.js b/src/address.js index 680a561..c99e4bd 100644 --- a/src/address.js +++ b/src/address.js @@ -2,6 +2,7 @@ var base58 = require('./base58'); var Crypto = require('./crypto-js/crypto'); var conv = require('./convert'); var util = require('./util'); +var mainnet = require('./network').mainnet.addressVersion; var Address = function (bytes, version) { if (!(this instanceof Address)) { return new Address(bytes, version); } @@ -15,11 +16,11 @@ var Address = function (bytes, version) { : bytes.length <= 40 ? conv.hexToBytes(bytes) : util.error('Bad input'); - this.version = version || this.hash.version || Address.address_types.prod; + this.version = version || this.hash.version || mainnet; } else { this.hash = bytes; - this.version = version || bytes.version || Address.address_types.prod; + this.version = version || bytes.version || mainnet; } }; @@ -29,7 +30,7 @@ var Address = function (bytes, version) { * Returns the address as a base58-encoded string in the standardized format. */ Address.prototype.toString = function () { - return base58.checkEncode(this.hash.slice(0),this.version); + return base58.checkEncode(this.hash.slice(0), this.version); }; Address.prototype.getHash = function () { @@ -56,14 +57,4 @@ Address.decodeString = function (string) { return base58.checkDecode(string); }; -Address.address_types = { - prod: 0, - testnet: 111 -}; - -Address.p2sh_types = { - prod: 5, - testnet: 196 -}; - module.exports = Address; diff --git a/src/eckey.js b/src/eckey.js index 68aecd1..ddec4f0 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -7,6 +7,9 @@ var conv = require('./convert'); var Address = require('./address'); var ecdsa = require('./ecdsa'); var ECPointFp = require('./jsbn/ec').ECPointFp; +var Network = require('./network') +var mainnet = Network.mainnet.addressVersion +var testnet = Network.testnet.addressVersion var ecparams = sec("secp256k1"); @@ -18,7 +21,7 @@ var ECKey = function (input,compressed,version) { var n = ecparams.getN(); this.priv = ecdsa.getBigRandom(n); this.compressed = compressed || false; - this.version = version || Address.address_types.prod; + this.version = version || mainnet; } else this.import(input,compressed,version) }; @@ -57,16 +60,16 @@ ECKey.prototype.import = function (input,compressed,version) { this.version = version !== undefined ? version : input instanceof ECKey ? input.version - : input instanceof BigInteger ? Address.address_types.prod - : util.isArray(input) ? Address.address_types.prod + : input instanceof BigInteger ? mainnet + : util.isArray(input) ? mainnet : typeof input != "string" ? null - : input.length == 44 ? Address.address_types.prod - : input.length == 51 && input[0] == '5' ? Address.address_types.prod - : input.length == 51 && input[0] == '9' ? Address.address_types.testnet - : input.length == 52 && has('LK',input[0]) ? Address.address_types.prod - : input.length == 52 && input[0] == 'c' ? Address.address_types.testnet - : input.length == 64 ? Address.address_types.prod - : input.length == 65 ? Address.address_types.prod + : input.length == 44 ? mainnet + : input.length == 51 && input[0] == '5' ? mainnet + : input.length == 51 && input[0] == '9' ? testnet + : input.length == 52 && has('LK',input[0]) ? mainnet + : input.length == 52 && input[0] == 'c' ? testnet + : input.length == 64 ? mainnet + : input.length == 65 ? mainnet : null }; @@ -133,7 +136,7 @@ var ECPubKey = function(input,compressed,version) { var n = ecparams.getN(); this.pub = ecparams.getG().multiply(ecdsa.getBigRandom(n)) this.compressed = compressed || false; - this.version = version || Address.address_types.prod; + this.version = version || mainnet; } else this.import(input,compressed,version) } @@ -158,7 +161,7 @@ ECPubKey.prototype.import = function(input,compressed,version) { version ? version : input instanceof ECPointFp ? input.version : input instanceof ECPubKey ? input.version - : Address.address_types.prod + : mainnet } ECPubKey.prototype.add = function(key) { diff --git a/src/hdwallet.js b/src/hdwallet.js index 2d286f9..a65c2bd 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -1,12 +1,15 @@ var convert = require('./convert.js') -, base58 = require('./base58.js') -, assert = require('assert') -, format = require('util').format -, util = require('./util.js') -, Crypto = require('./crypto-js/crypto.js') -, ECKey = require('./eckey.js').ECKey -, ECPubKey = require('./eckey.js').ECPubKey -, Address = require('./address.js') +var base58 = require('./base58.js') +var assert = require('assert') +var format = require('util').format +var util = require('./util.js') +var Crypto = require('./crypto-js/crypto.js') +var ECKey = require('./eckey.js').ECKey +var ECPubKey = require('./eckey.js').ECPubKey +var Address = require('./address.js') +var Network = require('./network') +var mainnet = Network.mainnet.addressVersion +var testnet = Network.testnet.addressVersion var HDWallet = module.exports = function(seed, network) { if (seed === undefined) return @@ -233,7 +236,7 @@ HDWallet.prototype.derivePrivate = function(index) { } HDWallet.prototype.getKeyVersion = function() { - return this.network == 'prod' ? Address.address_types.prod : Address.address_types.testnet + return this.network == 'prod' ? mainnet : testnet } HDWallet.prototype.toString = HDWallet.prototype.toBase58 diff --git a/src/network.js b/src/network.js new file mode 100644 index 0000000..e02fd01 --- /dev/null +++ b/src/network.js @@ -0,0 +1,10 @@ +module.exports = { + mainnet: { + addressVersion: 0, + p2shVersion: 5 + }, + testnet: { + addressVersion: 111, + p2shVersion: 196 + } +}; diff --git a/test/eckey.js b/test/eckey.js index edd86c6..c0caa41 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -5,6 +5,9 @@ var convert = require('../src/convert.js'); var bytesToHex = convert.bytesToHex; var hexToBytes = convert.hexToBytes; var Address = require('../src/address'); +var Network = require('../src/network') +var mainnet = Network.mainnet.addressVersion +var testnet = Network.testnet.addressVersion describe('ECKey', function() { describe('constructor (base58 private) on mainnet', function() { @@ -16,7 +19,7 @@ describe('ECKey', function() { assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, Address.address_types.prod); + assert.equal(key.version, mainnet); }) it('parses base64', function() { @@ -27,7 +30,7 @@ describe('ECKey', function() { assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, Address.address_types.prod); + assert.equal(key.version, mainnet); }) it('parses WIF', function() { @@ -40,7 +43,7 @@ describe('ECKey', function() { assert.equal(key.compressed, false); assert.equal(key.getPub().toHex(), pub); assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, Address.address_types.prod); + assert.equal(key.version, mainnet); }) it('parses compressed WIF', function() { @@ -52,7 +55,7 @@ describe('ECKey', function() { assert.equal(key.compressed, true); assert.equal(key.getPub().toHex(), pub); assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, Address.address_types.prod); + assert.equal(key.version, mainnet); }) }) @@ -61,11 +64,11 @@ describe('ECKey', function() { var priv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458'; var pub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199' + '283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566'; - var key = new ECKey(priv, false, Address.address_types.testnet); + var key = new ECKey(priv, false, testnet); assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, Address.address_types.testnet); + assert.equal(key.version, testnet); assert.equal(key.toHex(), priv); }) @@ -73,11 +76,11 @@ describe('ECKey', function() { var priv = 'VYdB+iv47y5FaUVIPdQInkgATrABeuD1lACUoM4x7tU='; var pub = '042f43c16c08849fed20a35bb7b1947bbf0923c52d613ee13b5c665a1e10d24b2' + '8be909a70f5f87c1adb79fbcd1b3f17d20aa91c04fc355112dba2ce9b1cbf013b'; - var key = new ECKey(priv, false, Address.address_types.testnet); + var key = new ECKey(priv, false, testnet); assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, Address.address_types.testnet); + assert.equal(key.version, testnet); assert.equal(key.toBase64(), priv); }) @@ -91,7 +94,7 @@ describe('ECKey', function() { assert.equal(key.compressed, false); assert.equal(key.getPub().toHex(), pub); assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, Address.address_types.testnet); + assert.equal(key.version, testnet); assert.equal(key.toBase58(), priv); }) @@ -104,7 +107,7 @@ describe('ECKey', function() { assert.equal(key.compressed, true); assert.equal(key.getPub().toHex(), pub); assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, Address.address_types.testnet); + assert.equal(key.version, testnet); assert.equal(key.toBase58(), priv); }) }) diff --git a/test/wallet.js b/test/wallet.js index 45ef084..5617c41 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -1,5 +1,7 @@ var Wallet = require('../src/wallet.js') -var Address = require('../src/address.js') +var Network = require('../src/network') +var mainnet = Network.mainnet.addressVersion +var testnet = Network.testnet.addressVersion var assert = require('assert') describe('Wallet', function() { @@ -38,16 +40,16 @@ describe('Wallet', function() { describe('networkType', function() { it('ensures that a mainnet Wallet has mainnet child keys (pub and priv)', function() { var w = Wallet("foobar", {network: "prod"}) - assert(w.getMasterKey().priv.version == Address.address_types['prod']) + assert(w.getMasterKey().priv.version == mainnet) w.generateAddress() - assert(w.getPrivateKey(0).priv.version == Address.address_types['prod']) + assert(w.getPrivateKey(0).priv.version == mainnet) }) it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() { var w = Wallet("foobar", {network: "testnet"}) - assert(w.getMasterKey().priv.version == Address.address_types['testnet']) + assert(w.getMasterKey().priv.version == testnet) w.generateAddress() - assert(w.getPrivateKey(0).priv.version == Address.address_types['testnet']) + assert(w.getPrivateKey(0).priv.version == testnet) }) }) })