Browse Source

Merge pull request #1085 from bitcoinjs/payments

rm getAddress
addLowRGrinding
Jonathan Underwood 7 years ago
committed by GitHub
parent
commit
49422672cf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      src/ecpair.js
  2. 10
      test/ecpair.js
  3. 12
      test/integration/_regtest.js
  4. 43
      test/integration/addresses.js
  5. 18
      test/integration/bip32.js
  6. 17
      test/integration/cltv.js
  7. 7
      test/integration/csv.js
  8. 39
      test/integration/stealth.js
  9. 26
      test/integration/transactions.js
  10. 35
      test/transaction_builder.js

24
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
}

10
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 () {

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

43
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')

18
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('../../')
var baddress = bitcoin.address
var bcrypto = bitcoin.crypto
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), bitcoin.networks.bitcoin.pubKeyHash)
let bip39 = require('bip39')
let bitcoin = require('../../')
// 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 () {

17
test/integration/cltv.js

@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, before */
var assert = require('assert')
var bitcoin = require('../../')
@ -10,7 +10,12 @@ var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxp
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', regtest)
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
var hashType = bitcoin.Transaction.SIGHASH_ALL
// force update MTP
before(function (done) {
regtestUtils.mine(11, done)
})
let hashType = bitcoin.Transaction.SIGHASH_ALL
function cltvCheckSigOutput (aQ, bQ, lockTime) {
return bitcoin.script.compile([
@ -38,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) {

7
test/integration/csv.js

@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, before */
let assert = require('assert')
let bitcoin = require('../../')
@ -10,6 +10,11 @@ let alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxp
let bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', regtest)
describe('bitcoinjs-lib (transactions w/ CSV)', function () {
// force update MTP
before(function (done) {
regtestUtils.mine(11, done)
})
let hashType = bitcoin.Transaction.SIGHASH_ALL
// IF MTP (from when confirmed) > seconds, aQ can redeem

39
test/integration/stealth.js

@ -1,13 +1,20 @@
/* global describe, it */
var assert = require('assert')
var bigi = require('bigi')
var bitcoin = require('../../')
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
var G = secp256k1.G
var n = secp256k1.n
let assert = require('assert')
let bigi = require('bigi')
let bitcoin = require('../../')
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))
})
})

26
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)

35
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')
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')
var fixtures = require('./fixtures/transaction_builder')
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')
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')
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]

Loading…
Cancel
Save