diff --git a/src/eckey.js b/src/eckey.js index 58954a5..15e78a5 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -53,13 +53,13 @@ ECKey.makeRandom = function(compressed, rng) { } // Export functions -ECKey.prototype.toWIF = function(version) { - version = version || networks.bitcoin.wif +ECKey.prototype.toWIF = function(network) { + network = network || networks.bitcoin var bufferLen = this.pub.compressed ? 34 : 33 var buffer = new Buffer(bufferLen) - buffer.writeUInt8(version, 0) + buffer.writeUInt8(network.wif, 0) this.D.toBuffer(32).copy(buffer, 1) if (this.pub.compressed) { diff --git a/src/ecpubkey.js b/src/ecpubkey.js index db4c07c..9f7d2fb 100644 --- a/src/ecpubkey.js +++ b/src/ecpubkey.js @@ -30,10 +30,10 @@ ECPubKey.fromHex = function(hex) { } // Operations -ECPubKey.prototype.getAddress = function(version) { - version = version || networks.bitcoin.pubKeyHash +ECPubKey.prototype.getAddress = function(network) { + network = network || networks.bitcoin - return new Address(crypto.hash160(this.toBuffer()), version) + return new Address(crypto.hash160(this.toBuffer()), network.pubKeyHash) } ECPubKey.prototype.verify = function(hash, signature) { diff --git a/src/hdnode.js b/src/hdnode.js index b418e9d..80fee26 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -136,7 +136,7 @@ HDNode.prototype.getFingerprint = function() { } HDNode.prototype.getAddress = function() { - return this.pubKey.getAddress(this.network.pubKeyHash) + return this.pubKey.getAddress(this.network) } HDNode.prototype.toBase58 = function(isPrivate) { diff --git a/src/message.js b/src/message.js index bdd7e78..53f4ea8 100644 --- a/src/message.js +++ b/src/message.js @@ -37,10 +37,9 @@ function sign(key, message, network) { // TODO: network could be implied from address function verify(address, compactSig, message, network) { - if (typeof address === 'string') { - address = Address.fromBase58Check(address) + if (address instanceof Address) { + address = address.toString() } - network = network || networks.bitcoin var hash = magicHash(message, network) @@ -49,7 +48,7 @@ function verify(address, compactSig, message, network) { var Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i) var pubKey = new ECPubKey(Q, parsed.compressed) - return pubKey.getAddress(address.version).toString() === address.toString() + return pubKey.getAddress(network).toString() === address } module.exports = { diff --git a/test/eckey.js b/test/eckey.js index 7d17416..0bc65a8 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -1,11 +1,11 @@ var assert = require('assert') var crypto = require('../src/crypto') +var networks = require('../src/networks') var BigInteger = require('bigi') var ECKey = require('../src/eckey') var fixtures = require('./fixtures/eckey.json') -var networks = require('../src/networks') describe('ECKey', function() { describe('constructor', function() { @@ -66,8 +66,8 @@ describe('ECKey', function() { f.WIFs.forEach(function(wif) { it('exports ' + wif.string + ' correctly', function() { var privKey = ECKey.fromWIF(wif.string) - var version = networks[wif.network].wif - var result = privKey.toWIF(version) + var network = networks[wif.network] + var result = privKey.toWIF(network) assert.equal(result, wif.string) }) diff --git a/test/ecpubkey.js b/test/ecpubkey.js index 00f51ff..8d22f1b 100644 --- a/test/ecpubkey.js +++ b/test/ecpubkey.js @@ -1,5 +1,6 @@ var assert = require('assert') var crypto = require('../src/crypto') +var networks = require('../src/networks') var sec = require('../src/sec') var ecparams = sec('secp256k1') @@ -71,9 +72,9 @@ describe('ECPubKey', function() { it('supports alternative networks', function() { var pubKey = new ECPubKey(Q) - var address = pubKey.getAddress(0x09) + var address = pubKey.getAddress(networks.testnet) - assert.equal(address.version, 0x09) + assert.equal(address.version, networks.testnet.pubKeyHash) assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160) }) })