rm getAddress

This commit is contained in:
Daniel Cousens 2018-05-22 17:43:25 +10:00
parent 4ed1a49dc5
commit c2a5d9dc1a
9 changed files with 115 additions and 92 deletions

View file

@ -1,16 +1,14 @@
var baddress = require('./address') let ecdsa = require('./ecdsa')
var bcrypto = require('./crypto') let randomBytes = require('randombytes')
var ecdsa = require('./ecdsa') let typeforce = require('typeforce')
var randomBytes = require('randombytes') let types = require('./types')
var typeforce = require('typeforce') let wif = require('wif')
var types = require('./types')
var wif = require('wif')
var NETWORKS = require('./networks') let NETWORKS = require('./networks')
var BigInteger = require('bigi') let BigInteger = require('bigi')
var ecurve = require('ecurve') let ecurve = require('ecurve')
var secp256k1 = ecdsa.__curve let secp256k1 = ecdsa.__curve
function ECPair (d, Q, options) { function ECPair (d, Q, options) {
if (options) { if (options) {
@ -100,10 +98,6 @@ ECPair.makeRandom = function (options) {
return new ECPair(d, null, options) return new ECPair(d, null, options)
} }
ECPair.prototype.getAddress = function () {
return baddress.toBase58Check(bcrypto.hash160(this.getPublicKeyBuffer()), this.getNetwork().pubKeyHash)
}
ECPair.prototype.getNetwork = function () { ECPair.prototype.getNetwork = function () {
return this.network return this.network
} }

View file

@ -196,16 +196,6 @@ describe('ECPair', function () {
})) }))
}) })
describe('getAddress', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + f.address + ' for ' + f.WIF, function () {
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
assert.strictEqual(keyPair.getAddress(), f.address)
})
})
})
describe('getNetwork', function () { describe('getNetwork', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('returns ' + f.network + ' for ' + f.WIF, function () { it('returns ' + f.network + ' for ' + f.WIF, function () {

View file

@ -67,10 +67,18 @@ function verify (txo, callback) {
}) })
} }
// TODO: remove
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKeyBuffer()), network.pubKeyHash)
}
function randomAddress () { function randomAddress () {
return bitcoin.ECPair.makeRandom({ return getAddress(bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet network: bitcoin.networks.testnet
}).getAddress() }), bitcoin.networks.testnet)
} }
module.exports = { module.exports = {

View file

@ -1,27 +1,34 @@
/* global describe, it */ /* global describe, it */
var assert = require('assert') let assert = require('assert')
var bigi = require('bigi') let bitcoin = require('../../')
var bitcoin = require('../../') let dhttp = require('dhttp/200')
var dhttp = require('dhttp/200')
// deterministic RNG for testing only // deterministic RNG for testing only
function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') } function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') }
// TODO: remove
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKeyBuffer()), network.pubKeyHash)
}
describe('bitcoinjs-lib (addresses)', function () { describe('bitcoinjs-lib (addresses)', function () {
it('can generate a random address', function () { it('can generate a random address', function () {
var keyPair = bitcoin.ECPair.makeRandom({ rng: rng }) var keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
var address = keyPair.getAddress() var address = getAddress(keyPair)
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64') assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
}) })
it('can generate an address from a SHA256 hash', function () { it('can generate an address from a SHA256 hash', function () {
var hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple')) var hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple'))
var d = bigi.fromBuffer(hash)
var keyPair = new bitcoin.ECPair(d) var keyPair = bitcoin.ECPair.makeRandom({ rng: () => hash })
var address = keyPair.getAddress() var address = getAddress(keyPair)
// Generating addresses from SHA256 hashes is not secure if the input to the hash function is predictable // Generating addresses from SHA256 hashes is not secure if the input to the hash function is predictable
// Do not use with predictable inputs // Do not use with predictable inputs
assert.strictEqual(address, '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') assert.strictEqual(address, '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8')
@ -29,7 +36,7 @@ describe('bitcoinjs-lib (addresses)', function () {
it('can import an address via WIF', function () { it('can import an address via WIF', function () {
var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
var address = keyPair.getAddress() var address = getAddress(keyPair)
assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31') assert.strictEqual(address, '19AAjaTUbRjQCMuVczepkoPswiZRhjtg31')
}) })
@ -100,7 +107,7 @@ describe('bitcoinjs-lib (addresses)', function () {
it('can support the retrieval of transactions for an address (via 3PBP)', function (done) { it('can support the retrieval of transactions for an address (via 3PBP)', function (done) {
var keyPair = bitcoin.ECPair.makeRandom() var keyPair = bitcoin.ECPair.makeRandom()
var address = keyPair.getAddress() var address = getAddress(keyPair)
dhttp({ dhttp({
method: 'POST', method: 'POST',
@ -120,20 +127,20 @@ describe('bitcoinjs-lib (addresses)', function () {
// other networks // other networks
it('can generate a Testnet address', function () { it('can generate a Testnet address', function () {
var testnet = bitcoin.networks.testnet let testnet = bitcoin.networks.testnet
var keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) let keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng })
var wif = keyPair.toWIF() let wif = keyPair.toWIF()
var address = keyPair.getAddress() let address = getAddress(keyPair, testnet)
assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L') assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L')
assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr') assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr')
}) })
it('can generate a Litecoin address', function () { it('can generate a Litecoin address', function () {
var litecoin = bitcoin.networks.litecoin let litecoin = bitcoin.networks.litecoin
var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng }) let keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng })
var wif = keyPair.toWIF() let wif = keyPair.toWIF()
var address = keyPair.getAddress() let address = getAddress(keyPair, litecoin)
assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn') assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn')
assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS') assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS')

View file

@ -1,14 +1,16 @@
/* global describe, it */ /* global describe, it */
var assert = require('assert') let assert = require('assert')
let bip32 = require('bip32') let bip32 = require('bip32')
var bip39 = require('bip39') let bip39 = require('bip39')
var bitcoin = require('../../') let bitcoin = require('../../')
var baddress = bitcoin.address // TODO: remove
var bcrypto = bitcoin.crypto let baddress = bitcoin.address
function getAddress (node) { let bcrypto = bitcoin.crypto
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), bitcoin.networks.bitcoin.pubKeyHash) function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
} }
describe('bitcoinjs-lib (BIP32)', function () { describe('bitcoinjs-lib (BIP32)', function () {

View file

@ -43,10 +43,10 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
this.timeout(30000) this.timeout(30000)
// 3 hours ago // 3 hours ago
var lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) }) let lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) })
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime) let redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest) let address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
// fund the P2SH(CLTV) address // fund the P2SH(CLTV) address
regtestUtils.faucet(address, 1e5, function (err, unspent) { regtestUtils.faucet(address, 1e5, function (err, unspent) {

View file

@ -1,13 +1,20 @@
/* global describe, it */ /* global describe, it */
var assert = require('assert') let assert = require('assert')
var bigi = require('bigi') let bigi = require('bigi')
var bitcoin = require('../../') let bitcoin = require('../../')
var ecurve = require('ecurve') let ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1') let secp256k1 = ecurve.getCurveByName('secp256k1')
var G = secp256k1.G let G = secp256k1.G
var n = secp256k1.n let n = secp256k1.n
// TODO: remove
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKeyBuffer()), bitcoin.networks.bitcoin.pubKeyHash)
}
// vG = (dG \+ sha256(e * dG)G) // vG = (dG \+ sha256(e * dG)G)
function stealthSend (e, Q) { function stealthSend (e, Q) {
@ -74,16 +81,16 @@ describe('bitcoinjs-lib (crypto)', function () {
// ... recipient reveals public key (recipient.Q) to sender // ... recipient reveals public key (recipient.Q) to sender
var forSender = stealthSend(nonce.d, recipient.Q) var forSender = stealthSend(nonce.d, recipient.Q)
assert.equal(forSender.getAddress(), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE') assert.equal(getAddress(forSender), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE')
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/) assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)
// ... sender reveals nonce public key (nonce.Q) to recipient // ... sender reveals nonce public key (nonce.Q) to recipient
var forRecipient = stealthReceive(recipient.d, nonce.Q) var forRecipient = stealthReceive(recipient.d, nonce.Q)
assert.equal(forRecipient.getAddress(), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE') assert.equal(getAddress(forRecipient), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE')
assert.equal(forRecipient.toWIF(), 'L1yjUN3oYyCXV3LcsBrmxCNTa62bZKWCybxVJMvqjMmmfDE8yk7n') assert.equal(forRecipient.toWIF(), 'L1yjUN3oYyCXV3LcsBrmxCNTa62bZKWCybxVJMvqjMmmfDE8yk7n')
// sender and recipient, both derived same address // sender and recipient, both derived same address
assert.equal(forSender.getAddress(), forRecipient.getAddress()) assert.equal(getAddress(forSender), getAddress(forRecipient))
}) })
it('can generate a single-key stealth address (randomly)', function () { it('can generate a single-key stealth address (randomly)', function () {
@ -99,7 +106,7 @@ describe('bitcoinjs-lib (crypto)', function () {
assert.doesNotThrow(function () { forRecipient.toWIF() }) assert.doesNotThrow(function () { forRecipient.toWIF() })
// sender and recipient, both derived same address // sender and recipient, both derived same address
assert.equal(forSender.getAddress(), forRecipient.getAddress()) assert.equal(getAddress(forSender), getAddress(forRecipient))
}) })
it('can recover parent recipient.d, if a derived private key is leaked [and nonce was revealed]', function () { it('can recover parent recipient.d, if a derived private key is leaked [and nonce was revealed]', function () {
@ -138,8 +145,8 @@ describe('bitcoinjs-lib (crypto)', function () {
assert.doesNotThrow(function () { forRecipient.toWIF() }) assert.doesNotThrow(function () { forRecipient.toWIF() })
// scanner, sender and recipient, all derived same address // scanner, sender and recipient, all derived same address
assert.equal(forSender.getAddress(), forScanner.getAddress()) assert.equal(getAddress(forSender), getAddress(forScanner))
assert.equal(forSender.getAddress(), forRecipient.getAddress()) assert.equal(getAddress(forSender), getAddress(forRecipient))
}) })
it('can generate a dual-key stealth address (randomly)', function () { it('can generate a dual-key stealth address (randomly)', function () {
@ -160,7 +167,7 @@ describe('bitcoinjs-lib (crypto)', function () {
assert.doesNotThrow(function () { forRecipient.toWIF() }) assert.doesNotThrow(function () { forRecipient.toWIF() })
// scanner, sender and recipient, all derived same address // scanner, sender and recipient, all derived same address
assert.equal(forSender.getAddress(), forScanner.getAddress()) assert.equal(getAddress(forSender), getAddress(forScanner))
assert.equal(forSender.getAddress(), forRecipient.getAddress()) assert.equal(getAddress(forSender), getAddress(forRecipient))
}) })
}) })

View file

@ -1,9 +1,17 @@
/* global describe, it */ /* global describe, it */
var assert = require('assert') let assert = require('assert')
var bitcoin = require('../../') let bitcoin = require('../../')
var regtestUtils = require('./_regtest') let regtestUtils = require('./_regtest')
var regtest = regtestUtils.network let regtest = regtestUtils.network
// TODO: remove
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKeyBuffer()), network.pubKeyHash)
}
function rng () { function rng () {
return Buffer.from('YT8dAtK4d16A3P1z+TpwB2jJ4aFH3g9M1EioIBkLEV4=', 'base64') return Buffer.from('YT8dAtK4d16A3P1z+TpwB2jJ4aFH3g9M1EioIBkLEV4=', 'base64')
@ -52,17 +60,17 @@ describe('bitcoinjs-lib (transactions)', function () {
var aliceChange = bitcoin.ECPair.makeRandom({ network: regtest, rng: rng }) var aliceChange = bitcoin.ECPair.makeRandom({ network: regtest, rng: rng })
// give Alice 2 unspent outputs // give Alice 2 unspent outputs
regtestUtils.faucet(alice1.getAddress(), 5e4, function (err, unspent0) { regtestUtils.faucet(getAddress(alice1, regtest), 5e4, function (err, unspent0) {
if (err) return done(err) if (err) return done(err)
regtestUtils.faucet(alice2.getAddress(), 7e4, function (err, unspent1) { regtestUtils.faucet(getAddress(alice2, regtest), 7e4, function (err, unspent1) {
if (err) return done(err) if (err) return done(err)
var txb = new bitcoin.TransactionBuilder(regtest) var txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent0.txId, unspent0.vout) // alice1 unspent txb.addInput(unspent0.txId, unspent0.vout) // alice1 unspent
txb.addInput(unspent1.txId, unspent1.vout) // alice2 unspent txb.addInput(unspent1.txId, unspent1.vout) // alice2 unspent
txb.addOutput('mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf', 8e4) // the actual "spend" txb.addOutput('mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf', 8e4) // the actual "spend"
txb.addOutput(aliceChange.getAddress(), 1e4) // Alice's change txb.addOutput(getAddress(aliceChange, regtest), 1e4) // Alice's change
// (in)(4e4 + 2e4) - (out)(1e4 + 3e4) = (fee)2e4 = 20000, this is the miner fee // (in)(4e4 + 2e4) - (out)(1e4 + 3e4) = (fee)2e4 = 20000, this is the miner fee
// Alice signs each input with the respective private keys // Alice signs each input with the respective private keys
@ -81,7 +89,7 @@ describe('bitcoinjs-lib (transactions)', function () {
var keyPair = bitcoin.ECPair.makeRandom({ network: regtest }) var keyPair = bitcoin.ECPair.makeRandom({ network: regtest })
regtestUtils.faucet(keyPair.getAddress(), 2e5, function (err, unspent) { regtestUtils.faucet(getAddress(keyPair, regtest), 2e5, function (err, unspent) {
if (err) return done(err) if (err) return done(err)
var txb = new bitcoin.TransactionBuilder(regtest) var txb = new bitcoin.TransactionBuilder(regtest)
@ -228,7 +236,7 @@ describe('bitcoinjs-lib (transactions)', function () {
tx.ins.forEach(function (input, i) { tx.ins.forEach(function (input, i) {
var keyPair = keyPairs[i] var keyPair = keyPairs[i]
var prevOutScript = bitcoin.address.toOutputScript(keyPair.getAddress()) var prevOutScript = bitcoin.address.toOutputScript(getAddress(keyPair))
var scriptSig = bitcoin.script.pubKeyHash.input.decode(input.script) var scriptSig = bitcoin.script.pubKeyHash.input.decode(input.script)
var ss = bitcoin.script.signature.decode(scriptSig.signature) var ss = bitcoin.script.signature.decode(scriptSig.signature)
var hash = tx.hashForSignature(i, prevOutScript, ss.hashType) var hash = tx.hashForSignature(i, prevOutScript, ss.hashType)

View file

@ -1,18 +1,24 @@
/* global describe, it, beforeEach */ /* global describe, it, beforeEach */
var assert = require('assert') let assert = require('assert')
var baddress = require('../src/address') let baddress = require('../src/address')
var bscript = require('../src/script') let bcrypto = require('../src/crypto')
var btemplates = require('../src/templates') let bscript = require('../src/script')
var ops = require('bitcoin-ops') let btemplates = require('../src/templates')
let ops = require('bitcoin-ops')
var BigInteger = require('bigi') let BigInteger = require('bigi')
var ECPair = require('../src/ecpair') let ECPair = require('../src/ecpair')
var Transaction = require('../src/transaction') let Transaction = require('../src/transaction')
var TransactionBuilder = require('../src/transaction_builder') let TransactionBuilder = require('../src/transaction_builder')
var NETWORKS = require('../src/networks') let NETWORKS = require('../src/networks')
var fixtures = require('./fixtures/transaction_builder') let fixtures = require('./fixtures/transaction_builder')
// TODO: remove
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKeyBuffer()), NETWORKS.bitcoin.pubKeyHash)
}
function construct (f, dontSign) { function construct (f, dontSign) {
var network = NETWORKS[f.network] var network = NETWORKS[f.network]
@ -220,7 +226,8 @@ describe('TransactionBuilder', function () {
}) })
it('accepts an address string and value', function () { it('accepts an address string and value', function () {
var vout = txb.addOutput(keyPair.getAddress(), 1000) let address = getAddress(keyPair)
var vout = txb.addOutput(address, 1000)
assert.strictEqual(vout, 0) assert.strictEqual(vout, 0)
var txout = txb.__tx.outs[0] var txout = txb.__tx.outs[0]