diff --git a/src/ecpair.js b/src/ecpair.js index cfc3649..b491f53 100644 --- a/src/ecpair.js +++ b/src/ecpair.js @@ -1,16 +1,14 @@ -var baddress = require('./address') -var bcrypto = require('./crypto') -var ecdsa = require('./ecdsa') -var randomBytes = require('randombytes') -var typeforce = require('typeforce') -var types = require('./types') -var wif = require('wif') +let ecdsa = require('./ecdsa') +let randomBytes = require('randombytes') +let typeforce = require('typeforce') +let types = require('./types') +let wif = require('wif') -var NETWORKS = require('./networks') -var BigInteger = require('bigi') +let NETWORKS = require('./networks') +let BigInteger = require('bigi') -var ecurve = require('ecurve') -var secp256k1 = ecdsa.__curve +let ecurve = require('ecurve') +let secp256k1 = ecdsa.__curve function ECPair (d, Q, options) { if (options) { @@ -100,10 +98,6 @@ ECPair.makeRandom = function (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 () { return this.network } diff --git a/test/ecpair.js b/test/ecpair.js index 046808f..52e5499 100644 --- a/test/ecpair.js +++ b/test/ecpair.js @@ -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 () { fixtures.valid.forEach(function (f) { it('returns ' + f.network + ' for ' + f.WIF, function () { diff --git a/test/integration/_regtest.js b/test/integration/_regtest.js index 0bb1a40..0baaffa 100644 --- a/test/integration/_regtest.js +++ b/test/integration/_regtest.js @@ -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 () { - return bitcoin.ECPair.makeRandom({ + return getAddress(bitcoin.ECPair.makeRandom({ network: bitcoin.networks.testnet - }).getAddress() + }), bitcoin.networks.testnet) } module.exports = { diff --git a/test/integration/addresses.js b/test/integration/addresses.js index 3b45836..0f8ae52 100644 --- a/test/integration/addresses.js +++ b/test/integration/addresses.js @@ -1,27 +1,34 @@ /* global describe, it */ -var assert = require('assert') -var bigi = require('bigi') -var bitcoin = require('../../') -var dhttp = require('dhttp/200') +let assert = require('assert') +let bitcoin = require('../../') +let dhttp = require('dhttp/200') // deterministic RNG for testing only 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 () { it('can generate a random address', function () { var keyPair = bitcoin.ECPair.makeRandom({ rng: rng }) - var address = keyPair.getAddress() + var address = getAddress(keyPair) assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64') }) it('can generate an address from a SHA256 hash', function () { var hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple')) - var d = bigi.fromBuffer(hash) - var keyPair = new bitcoin.ECPair(d) - var address = keyPair.getAddress() + var keyPair = bitcoin.ECPair.makeRandom({ rng: () => hash }) + var address = getAddress(keyPair) + // Generating addresses from SHA256 hashes is not secure if the input to the hash function is predictable // Do not use with predictable inputs assert.strictEqual(address, '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') @@ -29,7 +36,7 @@ describe('bitcoinjs-lib (addresses)', function () { it('can import an address via WIF', function () { var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct') - var address = keyPair.getAddress() + var address = getAddress(keyPair) 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) { var keyPair = bitcoin.ECPair.makeRandom() - var address = keyPair.getAddress() + var address = getAddress(keyPair) dhttp({ method: 'POST', @@ -120,20 +127,20 @@ describe('bitcoinjs-lib (addresses)', function () { // other networks it('can generate a Testnet address', function () { - var testnet = bitcoin.networks.testnet - var keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) - var wif = keyPair.toWIF() - var address = keyPair.getAddress() + let testnet = bitcoin.networks.testnet + let keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng }) + let wif = keyPair.toWIF() + let address = getAddress(keyPair, testnet) assert.strictEqual(address, 'mubSzQNtZfDj1YdNP6pNDuZy6zs6GDn61L') assert.strictEqual(wif, 'cRgnQe9MUu1JznntrLaoQpB476M8PURvXVQB5R2eqms5tXnzNsrr') }) it('can generate a Litecoin address', function () { - var litecoin = bitcoin.networks.litecoin - var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng }) - var wif = keyPair.toWIF() - var address = keyPair.getAddress() + let litecoin = bitcoin.networks.litecoin + let keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng }) + let wif = keyPair.toWIF() + let address = getAddress(keyPair, litecoin) assert.strictEqual(address, 'LZJSxZbjqJ2XVEquqfqHg1RQTDdfST5PTn') assert.strictEqual(wif, 'T7A4PUSgTDHecBxW1ZiYFrDNRih2o7M8Gf9xpoCgudPF9gDiNvuS') diff --git a/test/integration/bip32.js b/test/integration/bip32.js index 6695a96..effdec1 100644 --- a/test/integration/bip32.js +++ b/test/integration/bip32.js @@ -1,14 +1,16 @@ /* global describe, it */ -var assert = require('assert') +let assert = require('assert') let bip32 = require('bip32') -var bip39 = require('bip39') -var bitcoin = require('../../') +let bip39 = require('bip39') +let bitcoin = require('../../') -var baddress = bitcoin.address -var bcrypto = bitcoin.crypto -function getAddress (node) { - return baddress.toBase58Check(bcrypto.hash160(node.publicKey), bitcoin.networks.bitcoin.pubKeyHash) +// 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.publicKey), network.pubKeyHash) } describe('bitcoinjs-lib (BIP32)', function () { diff --git a/test/integration/cltv.js b/test/integration/cltv.js index 14937c8..2b51ed7 100644 --- a/test/integration/cltv.js +++ b/test/integration/cltv.js @@ -43,10 +43,10 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () { this.timeout(30000) // 3 hours ago - var lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) }) - var redeemScript = cltvCheckSigOutput(alice, bob, lockTime) - var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) - var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest) + let lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) }) + let redeemScript = cltvCheckSigOutput(alice, bob, lockTime) + let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) + let address = bitcoin.address.fromOutputScript(scriptPubKey, regtest) // fund the P2SH(CLTV) address regtestUtils.faucet(address, 1e5, function (err, unspent) { diff --git a/test/integration/stealth.js b/test/integration/stealth.js index 0eabac1..b3cea41 100644 --- a/test/integration/stealth.js +++ b/test/integration/stealth.js @@ -1,13 +1,20 @@ /* global describe, it */ -var assert = require('assert') -var bigi = require('bigi') -var bitcoin = require('../../') +let assert = require('assert') +let bigi = require('bigi') +let bitcoin = require('../../') -var ecurve = require('ecurve') -var secp256k1 = ecurve.getCurveByName('secp256k1') -var G = secp256k1.G -var n = secp256k1.n +let ecurve = require('ecurve') +let secp256k1 = ecurve.getCurveByName('secp256k1') +let G = secp256k1.G +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) function stealthSend (e, Q) { @@ -74,16 +81,16 @@ describe('bitcoinjs-lib (crypto)', function () { // ... recipient reveals public key (recipient.Q) to sender 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/) // ... sender reveals nonce public key (nonce.Q) to recipient var forRecipient = stealthReceive(recipient.d, nonce.Q) - assert.equal(forRecipient.getAddress(), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE') + assert.equal(getAddress(forRecipient), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE') assert.equal(forRecipient.toWIF(), 'L1yjUN3oYyCXV3LcsBrmxCNTa62bZKWCybxVJMvqjMmmfDE8yk7n') // 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 () { @@ -99,7 +106,7 @@ describe('bitcoinjs-lib (crypto)', function () { assert.doesNotThrow(function () { forRecipient.toWIF() }) // 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 () { @@ -138,8 +145,8 @@ describe('bitcoinjs-lib (crypto)', function () { assert.doesNotThrow(function () { forRecipient.toWIF() }) // scanner, sender and recipient, all derived same address - assert.equal(forSender.getAddress(), forScanner.getAddress()) - assert.equal(forSender.getAddress(), forRecipient.getAddress()) + assert.equal(getAddress(forSender), getAddress(forScanner)) + assert.equal(getAddress(forSender), getAddress(forRecipient)) }) it('can generate a dual-key stealth address (randomly)', function () { @@ -160,7 +167,7 @@ describe('bitcoinjs-lib (crypto)', function () { assert.doesNotThrow(function () { forRecipient.toWIF() }) // scanner, sender and recipient, all derived same address - assert.equal(forSender.getAddress(), forScanner.getAddress()) - assert.equal(forSender.getAddress(), forRecipient.getAddress()) + assert.equal(getAddress(forSender), getAddress(forScanner)) + assert.equal(getAddress(forSender), getAddress(forRecipient)) }) }) diff --git a/test/integration/transactions.js b/test/integration/transactions.js index fb3fd03..c1e4862 100644 --- a/test/integration/transactions.js +++ b/test/integration/transactions.js @@ -1,9 +1,17 @@ /* global describe, it */ -var assert = require('assert') -var bitcoin = require('../../') -var regtestUtils = require('./_regtest') -var regtest = regtestUtils.network +let assert = require('assert') +let bitcoin = require('../../') +let regtestUtils = require('./_regtest') +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 () { return Buffer.from('YT8dAtK4d16A3P1z+TpwB2jJ4aFH3g9M1EioIBkLEV4=', 'base64') @@ -52,17 +60,17 @@ describe('bitcoinjs-lib (transactions)', function () { var aliceChange = bitcoin.ECPair.makeRandom({ network: regtest, rng: rng }) // 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) - regtestUtils.faucet(alice2.getAddress(), 7e4, function (err, unspent1) { + regtestUtils.faucet(getAddress(alice2, regtest), 7e4, function (err, unspent1) { if (err) return done(err) var txb = new bitcoin.TransactionBuilder(regtest) txb.addInput(unspent0.txId, unspent0.vout) // alice1 unspent txb.addInput(unspent1.txId, unspent1.vout) // alice2 unspent 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 // 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 }) - regtestUtils.faucet(keyPair.getAddress(), 2e5, function (err, unspent) { + regtestUtils.faucet(getAddress(keyPair, regtest), 2e5, function (err, unspent) { if (err) return done(err) var txb = new bitcoin.TransactionBuilder(regtest) @@ -228,7 +236,7 @@ describe('bitcoinjs-lib (transactions)', function () { tx.ins.forEach(function (input, 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 ss = bitcoin.script.signature.decode(scriptSig.signature) var hash = tx.hashForSignature(i, prevOutScript, ss.hashType) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 1a98c3d..86f7eca 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -1,18 +1,24 @@ /* global describe, it, beforeEach */ -var assert = require('assert') -var baddress = require('../src/address') -var bscript = require('../src/script') -var btemplates = require('../src/templates') -var ops = require('bitcoin-ops') +let assert = require('assert') +let baddress = require('../src/address') +let bcrypto = require('../src/crypto') +let bscript = require('../src/script') +let btemplates = require('../src/templates') +let ops = require('bitcoin-ops') -var BigInteger = require('bigi') -var ECPair = require('../src/ecpair') -var Transaction = require('../src/transaction') -var TransactionBuilder = require('../src/transaction_builder') -var NETWORKS = require('../src/networks') +let BigInteger = require('bigi') +let ECPair = require('../src/ecpair') +let Transaction = require('../src/transaction') +let TransactionBuilder = require('../src/transaction_builder') +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) { var network = NETWORKS[f.network] @@ -220,7 +226,8 @@ describe('TransactionBuilder', 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) var txout = txb.__tx.outs[0]