From 57b8afbdabc1d2a1872e07f20a987e4358371590 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 5 May 2014 13:23:22 +1000 Subject: [PATCH] Network: rename Network to Networks This change removes the most common ambiguities. As the network module is not representative of a class, the lower case has been used. --- src/eckey.js | 6 +++--- src/hdwallet.js | 12 ++++++------ src/index.js | 2 +- src/{network.js => networks.js} | 0 src/script.js | 8 ++++---- src/transaction.js | 8 ++++---- src/wallet.js | 10 +++++----- test/integration/p2sh.js | 6 +++--- test/message.js | 4 ++-- test/script.js | 4 ++-- test/transaction.js | 6 +++--- 11 files changed, 33 insertions(+), 33 deletions(-) rename src/{network.js => networks.js} (100%) diff --git a/src/eckey.js b/src/eckey.js index 87d2934..fa238a9 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -1,7 +1,7 @@ var assert = require('assert') var base58check = require('./base58check') var ecdsa = require('./ecdsa') -var network = require('./network') +var networks = require('./networks') var secureRandom = require('secure-random') var Address = require('./address') @@ -72,7 +72,7 @@ ECKey.prototype.toHex = function() { } ECKey.prototype.toWIF = function(version) { - version = version || network.bitcoin.wif + version = version || networks.bitcoin.wif var buffer = this.toBuffer() if (this.pub.compressed) { @@ -115,7 +115,7 @@ ECPubKey.prototype.verify = function(hash, sig) { } ECPubKey.prototype.getAddress = function(version) { - version = version || network.bitcoin.pubKeyHash + version = version || networks.bitcoin.pubKeyHash return new Address(crypto.hash160(this.toBuffer()), version) } diff --git a/src/hdwallet.js b/src/hdwallet.js index 146b82d..966185d 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -8,7 +8,7 @@ var CJS = require('crypto-js') var crypto = require('./crypto') var ECKey = require('./eckey').ECKey var ECPubKey = require('./eckey').ECPubKey -var Network = require('./network') +var networks = require('./networks') var sec = require('./sec') var ecparams = sec("secp256k1") @@ -27,7 +27,7 @@ function HDWallet(seed, networkString) { this.chaincode = I.slice(32) this.network = networkString || 'bitcoin' - if(!Network.hasOwnProperty(this.network)) { + if(!networks.hasOwnProperty(this.network)) { throw new Error("Unknown network: " + this.network) } @@ -70,8 +70,8 @@ HDWallet.fromBuffer = function(input) { var version = input.readUInt32BE(0) var type - for(var name in Network) { - var network = Network[name] + for(var name in networks) { + var network = networks[name] for(var t in network.bip32) { if (version != network.bip32[t]) continue @@ -128,7 +128,7 @@ HDWallet.prototype.getAddress = function() { HDWallet.prototype.toBuffer = function(priv) { // Version - var version = Network[this.network].bip32[priv ? 'priv' : 'pub'] + var version = networks[this.network].bip32[priv ? 'priv' : 'pub'] var buffer = new Buffer(HDWallet.LENGTH) // 4 bytes: version bytes @@ -245,7 +245,7 @@ HDWallet.prototype.derivePrivate = function(index) { } HDWallet.prototype.getKeyVersion = function() { - return Network[this.network].pubKeyHash + return networks[this.network].pubKeyHash } HDWallet.prototype.toString = HDWallet.prototype.toBase58 diff --git a/src/index.js b/src/index.js index c236a02..484342d 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,6 @@ module.exports = { Transaction: T.Transaction, TransactionIn: T.TransactionIn, TransactionOut: T.TransactionOut, - network: require('./network'), + networks: require('./networks'), Wallet: require('./wallet') } diff --git a/src/network.js b/src/networks.js similarity index 100% rename from src/network.js rename to src/networks.js diff --git a/src/script.js b/src/script.js index 6fa6269..c5bec6b 100644 --- a/src/script.js +++ b/src/script.js @@ -2,7 +2,7 @@ var assert = require('assert') var Address = require('./address') var crypto = require('./crypto') var convert = require('./convert') -var Network = require('./network') +var networks = require('./networks') var Opcode = require('./opcode') function Script(data) { @@ -193,7 +193,7 @@ Script.prototype.toScriptHash = function() { } Script.prototype.getToAddress = function(network) { - network = network || Network.bitcoin + network = network || networks.bitcoin if(isPubkeyhash.call(this)) { return new Address(new Buffer(this.chunks[2]), network.pubKeyHash) @@ -205,7 +205,7 @@ Script.prototype.getToAddress = function(network) { } Script.prototype.getFromAddress = function(version) { - version = version || Network.bitcoin.pubKeyHash + version = version || networks.bitcoin.pubKeyHash return new Address(this.simpleInHash(), version) } @@ -348,7 +348,7 @@ Script.prototype.writeBytes = function(data) { */ Script.createOutputScript = function(address, network) { assert(address instanceof Address) - network = network || Network.bitcoin + network = network || networks.bitcoin var script = new Script() diff --git a/src/transaction.js b/src/transaction.js index 0b346a4..f6660bc 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -9,7 +9,7 @@ var convert = require('./convert') var crypto = require('./crypto') var ECKey = require('./eckey').ECKey var ecdsa = require('./ecdsa') -var Network = require('./network') +var networks = require('./networks') var Transaction = function (doc) { if (!(this instanceof Transaction)) { return new Transaction(doc) } @@ -112,7 +112,7 @@ Transaction.prototype.addOutput = function (address, value, network) { address = Address.fromBase58Check(address) } - network = network || Network.bitcoin + network = network || networks.bitcoin this.outs.push(new TransactionOut({ value: value, @@ -368,7 +368,7 @@ Transaction.deserialize = function(buffer) { */ Transaction.prototype.sign = function(index, key, type, network) { assert(key instanceof ECKey) - network = network || Network.bitcoin + network = network || networks.bitcoin var address = key.pub.getAddress(network.pubKeyHash) @@ -445,7 +445,7 @@ function TransactionOut(data) { this.value = data.value this.address = data.address - var network = data.network || Network.bitcoin + var network = data.network || networks.bitcoin if (this.script.buffer.length > 0) { this.address = this.script.getToAddress(network) } diff --git a/src/wallet.js b/src/wallet.js index e843dba..c6b7134 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -2,7 +2,7 @@ var convert = require('./convert') var Transaction = require('./transaction').Transaction var HDNode = require('./hdwallet.js') var rng = require('secure-random') -var Network = require('./network') +var networks = require('./networks') function Wallet(seed, options) { if (!(this instanceof Wallet)) { return new Wallet(seed, options); } @@ -174,7 +174,7 @@ function Wallet(seed, options) { checkDust(value) var tx = new Transaction() - tx.addOutput(to, value, Network[network]) + tx.addOutput(to, value, networks[network]) var utxo = getCandidateOutputs(value) var totalInValue = 0 @@ -190,7 +190,7 @@ function Wallet(seed, options) { var change = totalInValue - value - fee if(change > 0 && !isDust(change)) { - tx.addOutput(changeAddress || getChangeAddress(), change, Network[network]) + tx.addOutput(changeAddress || getChangeAddress(), change, networks[network]) } break } @@ -246,7 +246,7 @@ function Wallet(seed, options) { function estimateFeePadChangeOutput(tx){ var tmpTx = tx.clone() - tmpTx.addOutput(getChangeAddress(), 0, Network[network]) + tmpTx.addOutput(getChangeAddress(), 0, networks[network]) return tmpTx.estimateFee() } @@ -266,7 +266,7 @@ function Wallet(seed, options) { tx.ins.forEach(function(inp,i) { var output = me.outputs[inp.outpoint.hash + ':' + inp.outpoint.index] if (output) { - tx.sign(i, me.getPrivateKeyForAddress(output.address), false, Network[network]) + tx.sign(i, me.getPrivateKeyForAddress(output.address), false, networks[network]) } }) return tx diff --git a/test/integration/p2sh.js b/test/integration/p2sh.js index b52f9a6..55978bb 100644 --- a/test/integration/p2sh.js +++ b/test/integration/p2sh.js @@ -5,7 +5,7 @@ var ECKey = require('../../src/eckey').ECKey; var T = require('../../src/transaction'); var Transaction = T.Transaction; var Script = require('../../src/script'); -var network = require('../../src/network'); +var networks = require('../../src/networks'); var crypto = require('../../src/crypto'); var helloblock = require('helloblock-js')({ @@ -31,7 +31,7 @@ describe('p2sh', function() { }) var redeemScript = Script.createMultisigOutputScript(2, pubKeyBuffers) var hash160 = crypto.hash160(redeemScript.buffer) - var multisigAddress = new Address(hash160, network.testnet.scriptHash) + var multisigAddress = new Address(hash160, networks.testnet.scriptHash) // Check what our target address's starting value is var targetAddress = 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r'; @@ -50,7 +50,7 @@ describe('p2sh', function() { var tx = new Transaction() var unspent = resource[0]; tx.addInput(unspent.txHash, unspent.index) - tx.addOutput(targetAddress, 100000, network.testnet) + tx.addOutput(targetAddress, 100000, networks.testnet) var signatures = privKeys.map(function(privKey) { return tx.signScriptSig(0, redeemScript, privKey) diff --git a/test/message.js b/test/message.js index 6589250..12831da 100644 --- a/test/message.js +++ b/test/message.js @@ -1,7 +1,7 @@ var assert = require('assert') var ECKey = require('../src/eckey').ECKey var Message = require('../').Message -var network = require('../').network +var networks = require('../').networks var fixtures = require('./fixtures/message') @@ -86,7 +86,7 @@ describe('Message', function() { var key = ECKey.makeRandom() var sig = Message.sign(key, msg) - var addr = key.pub.getAddress(network.testnet.pubKeyHash) + var addr = key.pub.getAddress(networks.testnet.pubKeyHash) assert(Message.verify(addr, sig, msg)) }) }) diff --git a/test/script.js b/test/script.js index 3d15610..ea84681 100644 --- a/test/script.js +++ b/test/script.js @@ -1,6 +1,6 @@ var assert = require('assert') var crypto = require('..').crypto -var network = require('..').network +var networks = require('..').networks var Address = require('../src/address.js') var Script = require('../src/script.js') @@ -116,7 +116,7 @@ describe('Script', function() { var redeemScript = Script.createMultisigOutputScript(2, pubKeys) var hash160 = crypto.hash160(redeemScript.buffer) - var multisigAddress = new Address(hash160, network.bitcoin.scriptHash) + var multisigAddress = new Address(hash160, networks.bitcoin.scriptHash) assert.equal(multisigAddress.toString(), '32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v') }) diff --git a/test/transaction.js b/test/transaction.js index 234d925..55f74e7 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -2,11 +2,11 @@ var assert = require('assert') var Address = require('../src/address') var ECKey = require('../src/eckey').ECKey +var networks = require('..').networks var T = require('../src/transaction') var Transaction = T.Transaction var TransactionOut = T.TransactionOut var Script = require('../src/script') -var network = require('..').network var fixtureTxes = require('./fixtures/mainnet_tx') var fixtureTx1Hex = fixtureTxes.prevTx @@ -176,7 +176,7 @@ describe('Transaction', function() { it('supports alternative networks', function(){ var addr = 'mkHJaNR7uuwRG1JrmTZsV4MszaTKjCBvCR' - tx.addOutput(addr, 40000, network.testnet) + tx.addOutput(addr, 40000, networks.testnet) verifyTransactionOut() assert.equal(tx.outs[0].address.toString(), addr) @@ -250,7 +250,7 @@ describe('Transaction', function() { it('works for multi-sig redeem script', function() { var tx = new Transaction() tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0) - tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1, network.testnet) + tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1, networks.testnet) var privKeys = [ '5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf',