EC/Key: use network object consistently
This commit is contained in:
parent
d39662e375
commit
28dc390377
6 changed files with 16 additions and 16 deletions
|
@ -53,13 +53,13 @@ ECKey.makeRandom = function(compressed, rng) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export functions
|
// Export functions
|
||||||
ECKey.prototype.toWIF = function(version) {
|
ECKey.prototype.toWIF = function(network) {
|
||||||
version = version || networks.bitcoin.wif
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
var bufferLen = this.pub.compressed ? 34 : 33
|
var bufferLen = this.pub.compressed ? 34 : 33
|
||||||
var buffer = new Buffer(bufferLen)
|
var buffer = new Buffer(bufferLen)
|
||||||
|
|
||||||
buffer.writeUInt8(version, 0)
|
buffer.writeUInt8(network.wif, 0)
|
||||||
this.D.toBuffer(32).copy(buffer, 1)
|
this.D.toBuffer(32).copy(buffer, 1)
|
||||||
|
|
||||||
if (this.pub.compressed) {
|
if (this.pub.compressed) {
|
||||||
|
|
|
@ -30,10 +30,10 @@ ECPubKey.fromHex = function(hex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operations
|
// Operations
|
||||||
ECPubKey.prototype.getAddress = function(version) {
|
ECPubKey.prototype.getAddress = function(network) {
|
||||||
version = version || networks.bitcoin.pubKeyHash
|
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) {
|
ECPubKey.prototype.verify = function(hash, signature) {
|
||||||
|
|
|
@ -136,7 +136,7 @@ HDNode.prototype.getFingerprint = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.prototype.getAddress = function() {
|
HDNode.prototype.getAddress = function() {
|
||||||
return this.pubKey.getAddress(this.network.pubKeyHash)
|
return this.pubKey.getAddress(this.network)
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.prototype.toBase58 = function(isPrivate) {
|
HDNode.prototype.toBase58 = function(isPrivate) {
|
||||||
|
|
|
@ -37,10 +37,9 @@ function sign(key, message, network) {
|
||||||
|
|
||||||
// TODO: network could be implied from address
|
// TODO: network could be implied from address
|
||||||
function verify(address, compactSig, message, network) {
|
function verify(address, compactSig, message, network) {
|
||||||
if (typeof address === 'string') {
|
if (address instanceof Address) {
|
||||||
address = Address.fromBase58Check(address)
|
address = address.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
network = network || networks.bitcoin
|
network = network || networks.bitcoin
|
||||||
|
|
||||||
var hash = magicHash(message, network)
|
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 Q = ecdsa.recoverPubKey(ecparams, e, parsed.signature, parsed.i)
|
||||||
|
|
||||||
var pubKey = new ECPubKey(Q, parsed.compressed)
|
var pubKey = new ECPubKey(Q, parsed.compressed)
|
||||||
return pubKey.getAddress(address.version).toString() === address.toString()
|
return pubKey.getAddress(network).toString() === address
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('../src/crypto')
|
var crypto = require('../src/crypto')
|
||||||
|
var networks = require('../src/networks')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECKey = require('../src/eckey')
|
var ECKey = require('../src/eckey')
|
||||||
|
|
||||||
var fixtures = require('./fixtures/eckey.json')
|
var fixtures = require('./fixtures/eckey.json')
|
||||||
var networks = require('../src/networks')
|
|
||||||
|
|
||||||
describe('ECKey', function() {
|
describe('ECKey', function() {
|
||||||
describe('constructor', function() {
|
describe('constructor', function() {
|
||||||
|
@ -66,8 +66,8 @@ describe('ECKey', function() {
|
||||||
f.WIFs.forEach(function(wif) {
|
f.WIFs.forEach(function(wif) {
|
||||||
it('exports ' + wif.string + ' correctly', function() {
|
it('exports ' + wif.string + ' correctly', function() {
|
||||||
var privKey = ECKey.fromWIF(wif.string)
|
var privKey = ECKey.fromWIF(wif.string)
|
||||||
var version = networks[wif.network].wif
|
var network = networks[wif.network]
|
||||||
var result = privKey.toWIF(version)
|
var result = privKey.toWIF(network)
|
||||||
|
|
||||||
assert.equal(result, wif.string)
|
assert.equal(result, wif.string)
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var crypto = require('../src/crypto')
|
var crypto = require('../src/crypto')
|
||||||
|
var networks = require('../src/networks')
|
||||||
var sec = require('../src/sec')
|
var sec = require('../src/sec')
|
||||||
var ecparams = sec('secp256k1')
|
var ecparams = sec('secp256k1')
|
||||||
|
|
||||||
|
@ -71,9 +72,9 @@ describe('ECPubKey', function() {
|
||||||
|
|
||||||
it('supports alternative networks', function() {
|
it('supports alternative networks', function() {
|
||||||
var pubKey = new ECPubKey(Q)
|
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)
|
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue