EC/Key: use network object consistently

This commit is contained in:
Daniel Cousens 2014-06-03 21:43:10 +10:00
parent d39662e375
commit 28dc390377
6 changed files with 16 additions and 16 deletions

View file

@ -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) {

View file

@ -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) {

View file

@ -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) {

View file

@ -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 = {

View file

@ -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)
})

View file

@ -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)
})
})