Browse Source

ECPair: cease using Address for address encoding

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
588b346f5d
  1. 14
      src/ecpair.js
  2. 2
      src/message.js
  3. 7
      src/transaction_builder.js
  4. 2
      test/ecpair.js
  5. 2
      test/integration/advanced.js
  6. 8
      test/integration/basic.js
  7. 2
      test/integration/crypto.js
  8. 2
      test/integration/multisig.js
  9. 6
      test/scripts.js
  10. 4
      test/transaction_builder.js

14
src/ecpair.js

@ -1,5 +1,5 @@
var assert = require('assert')
var base58check = require('bs58check')
var bs58check = require('bs58check')
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var ecurve = require('ecurve')
@ -7,7 +7,6 @@ var networks = require('./networks')
var randomBytes = require('randombytes')
var typeForce = require('typeforce')
var Address = require('./address')
var BigInteger = require('bigi')
function findNetworkByWIFVersion (version) {
@ -69,7 +68,7 @@ ECPair.fromPublicKeyBuffer = function (buffer, network) {
}
ECPair.fromWIF = function (string) {
var payload = base58check.decode(string)
var payload = bs58check.decode(string)
var version = payload.readUInt8(0)
var compressed
@ -125,13 +124,18 @@ ECPair.prototype.toWIF = function () {
buffer.writeUInt8(0x01, 33)
}
return base58check.encode(buffer)
return bs58check.encode(buffer)
}
ECPair.prototype.getAddress = function () {
var pubKey = this.getPublicKeyBuffer()
var pubKeyHash = bcrypto.hash160(pubKey)
return new Address(bcrypto.hash160(pubKey), this.network.pubKeyHash)
var payload = new Buffer(21)
payload.writeUInt8(this.network.pubKeyHash, 0)
pubKeyHash.copy(payload, 1)
return bs58check.encode(payload)
}
ECPair.prototype.getPublicKeyBuffer = function () {

2
src/message.js

@ -47,7 +47,7 @@ function verify (address, signature, message, network) {
network: network
})
return keyPair.getAddress().toString() === address.toString()
return keyPair.getAddress() === address.toString()
}
module.exports = {

7
src/transaction_builder.js

@ -1,4 +1,5 @@
var assert = require('assert')
var bcrypto = require('./crypto')
var bufferutils = require('./bufferutils')
var ops = require('./opcodes')
var scripts = require('./scripts')
@ -36,7 +37,7 @@ function extractInput (txIn) {
hashType = parsed.hashType
pubKeys = scriptSig.chunks.slice(1)
signatures = [parsed.signature]
prevOutScript = ECPair.fromPublicKeyBuffer(pubKeys[0]).getAddress().toOutputScript()
prevOutScript = Address.fromBase58Check(ECPair.fromPublicKeyBuffer(pubKeys[0]).getAddress()).toOutputScript()
break
}
@ -330,7 +331,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
case 'pubkeyhash': {
var pkh1 = redeemScript.chunks[2]
var pkh2 = keyPair.getAddress().hash
var pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer())
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
pubKeys = [kpPubKey]
@ -362,7 +363,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// we know nothin' Jon Snow, assume pubKeyHash
} else {
input.prevOutScript = keyPair.getAddress().toOutputScript()
input.prevOutScript = Address.fromBase58Check(keyPair.getAddress()).toOutputScript()
input.prevOutType = 'pubkeyhash'
input.pubKeys = [kpPubKey]
input.scriptType = input.prevOutType

2
test/ecpair.js

@ -171,7 +171,7 @@ describe('ECPair', function () {
it('returns ' + f.address + ' for ' + f.WIF, function () {
var keyPair = ECPair.fromWIF(f.WIF)
assert.strictEqual(keyPair.getAddress().toString(), f.address)
assert.strictEqual(keyPair.getAddress(), f.address)
})
})
})

2
test/integration/advanced.js

@ -29,7 +29,7 @@ describe('bitcoinjs-lib (advanced)', function () {
var keyPair = bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet
})
var address = keyPair.getAddress().toString()
var address = keyPair.getAddress()
faucetWithdraw(address, 2e4, function (err) {
if (err) return done(err)

8
test/integration/basic.js

@ -11,7 +11,7 @@ describe('bitcoinjs-lib (basic)', function () {
// generate random keyPair
var keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
var address = keyPair.getAddress().toString()
var address = keyPair.getAddress()
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
})
@ -21,7 +21,7 @@ describe('bitcoinjs-lib (basic)', function () {
var d = bigi.fromBuffer(hash)
var keyPair = new bitcoin.ECPair(d)
var address = keyPair.getAddress().toString()
var address = keyPair.getAddress()
assert.strictEqual(address, '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8')
})
@ -34,7 +34,7 @@ describe('bitcoinjs-lib (basic)', function () {
var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng })
var wif = keyPair.toWIF()
var address = keyPair.getAddress().toString()
var address = keyPair.getAddress()
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn')
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS')
@ -42,7 +42,7 @@ describe('bitcoinjs-lib (basic)', function () {
it('can import an address via WIF', function () {
var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
var address = keyPair.getAddress().toString()
var address = keyPair.getAddress()
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31')
})

2
test/integration/crypto.js

@ -35,7 +35,7 @@ describe('bitcoinjs-lib (crypto)', function () {
assert.deepEqual(QprimeR.getEncoded(), QprimeS.getEncoded())
// derived shared-secret address
var address = new bitcoin.ECPair(null, QprimeS).getAddress().toString()
var address = new bitcoin.ECPair(null, QprimeS).getAddress()
assert.strictEqual(address, '1EwCNJNZM5q58YPPTnjR1H5BvYRNeyZi47')
})

2
test/integration/multisig.js

@ -58,7 +58,7 @@ describe('bitcoinjs-lib (multisig)', function () {
// make a random destination address
var targetAddress = bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet
}).getAddress().toString()
}).getAddress()
var txb = new bitcoin.TransactionBuilder()
txb.addInput(unspent.txId, unspent.vout)

6
test/scripts.js

@ -1,10 +1,10 @@
/* global describe, it */
var assert = require('assert')
var bcrypto = require('../src/crypto')
var ops = require('../src/opcodes')
var scripts = require('../src/scripts')
var ECPair = require('../src/ecpair')
var Script = require('../src/script')
var fixtures = require('./fixtures/scripts.json')
@ -177,10 +177,10 @@ describe('Scripts', function () {
if (f.type !== 'pubkeyhash') return
var pubKey = new Buffer(f.pubKey, 'hex')
var address = ECPair.fromPublicKeyBuffer(pubKey).getAddress()
var pubKeyHash = bcrypto.hash160(pubKey)
it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash)
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
})
})

4
test/transaction_builder.js

@ -71,7 +71,7 @@ describe('TransactionBuilder', function () {
keyPair = new ECPair(BigInteger.ONE)
privAddress = keyPair.getAddress()
privScript = privAddress.toOutputScript()
privScript = Address.fromBase58Check(privAddress).toOutputScript()
})
describe('addInput', function () {
@ -125,7 +125,7 @@ describe('TransactionBuilder', function () {
describe('addOutput', function () {
it('accepts an address string and value', function () {
var vout = txb.addOutput(privAddress.toBase58Check(), 1000)
var vout = txb.addOutput(privAddress, 1000)
assert.strictEqual(vout, 0)
var txout = txb.tx.outs[0]

Loading…
Cancel
Save