Skip to content

Commit

Permalink
transition ECPair to properties over getters
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 30, 2018
1 parent 070215a commit 1307a37
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 52 deletions.
17 changes: 7 additions & 10 deletions src/ecpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ function ECPair (d, Q, options) {
if (Q) this.__Q = ecc.pointCompress(Q, this.compressed)
}

ECPair.prototype.getNetwork = function () {
return this.network
}

ECPair.prototype.getPrivateKey = function () {
return this.__d
}
Object.defineProperty(ECPair.prototype, 'privateKey', {
enumerable: false,
get: function () { return this.__d }
})

ECPair.prototype.getPublicKey = function () {
Object.defineProperty(ECPair.prototype, 'publicKey', { get: function () {
if (!this.__Q) this.__Q = ecc.pointFromScalar(this.__d, this.compressed)
return this.__Q
}
}})

ECPair.prototype.toWIF = function () {
if (!this.__d) throw new Error('Missing private key')
Expand All @@ -48,7 +45,7 @@ ECPair.prototype.sign = function (hash) {
}

ECPair.prototype.verify = function (hash, signature) {
return ecc.verify(hash, this.getPublicKey(), signature)
return ecc.verify(hash, this.publicKey, signature)
}

function fromPrivateKey (buffer, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/bitcoin.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Bitcoin-core', function () {
var keyPair = bitcoin.ECPair.fromWIF(string, network)

it('fromWIF imports ' + string, function () {
assert.strictEqual(keyPair.getPrivateKey().toString('hex'), hex)
assert.strictEqual(keyPair.privateKey.toString('hex'), hex)
assert.strictEqual(keyPair.compressed, params.isCompressed)
})

Expand Down
18 changes: 10 additions & 8 deletions test/ecpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ECPair', function () {
compressed: f.compressed
})

assert.strictEqual(keyPair.getPublicKey().toString('hex'), f.Q)
assert.strictEqual(keyPair.publicKey.toString('hex'), f.Q)
})
})

Expand Down Expand Up @@ -85,7 +85,9 @@ describe('ECPair', function () {

it('calls pointFromScalar lazily', hoodwink(function () {
assert.strictEqual(keyPair.__Q, null)
keyPair.getPublicKey()

// .publicKey forces the memoization
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
assert.strictEqual(keyPair.__Q.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
}))
})
Expand All @@ -96,7 +98,7 @@ describe('ECPair', function () {
let network = NETWORKS[f.network]
let keyPair = ECPair.fromWIF(f.WIF, network)

assert.strictEqual(keyPair.getPrivateKey().toString('hex'), f.d)
assert.strictEqual(keyPair.privateKey.toString('hex'), f.d)
assert.strictEqual(keyPair.compressed, f.compressed)
assert.strictEqual(keyPair.network, network)
})
Expand All @@ -106,7 +108,7 @@ describe('ECPair', function () {
it('imports ' + f.WIF + ' (via list of networks)', function () {
let keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)

assert.strictEqual(keyPair.getPrivateKey().toString('hex'), f.d)
assert.strictEqual(keyPair.privateKey.toString('hex'), f.d)
assert.strictEqual(keyPair.compressed, f.compressed)
assert.strictEqual(keyPair.network, NETWORKS[f.network])
})
Expand Down Expand Up @@ -202,13 +204,13 @@ describe('ECPair', function () {
}))
})

describe('getNetwork', function () {
describe('.network', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + f.network + ' for ' + f.WIF, function () {
let network = NETWORKS[f.network]
let keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)

assert.strictEqual(keyPair.getNetwork(), network)
assert.strictEqual(keyPair.network, network)
})
})
})
Expand All @@ -226,7 +228,7 @@ describe('ECPair', function () {
it('wraps tinysecp.sign', hoodwink(function () {
this.mock(tinysecp, 'sign', function (h, d) {
assert.strictEqual(h, hash)
assert.strictEqual(d, keyPair.getPrivateKey())
assert.strictEqual(d, keyPair.privateKey)
return signature
}, 1)

Expand All @@ -246,7 +248,7 @@ describe('ECPair', function () {
it('wraps tinysecp.verify', hoodwink(function () {
this.mock(tinysecp, 'verify', function (h, q, s) {
assert.strictEqual(h, hash)
assert.strictEqual(q, keyPair.getPublicKey())
assert.strictEqual(q, keyPair.publicKey)
assert.strictEqual(s, signature)
return true
}, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/_regtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKey()), network.pubKeyHash)
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
}

function randomAddress () {
Expand Down
8 changes: 3 additions & 5 deletions test/integration/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKey()), network.pubKeyHash)
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
}

describe('bitcoinjs-lib (addresses)', function () {
Expand Down Expand Up @@ -57,19 +57,17 @@ describe('bitcoinjs-lib (addresses)', function () {

it('can generate a SegWit address', function () {
var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
var pubKey = keyPair.getPublicKey()

var scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
var scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.publicKey))
var address = bitcoin.address.fromOutputScript(scriptPubKey)

assert.strictEqual(address, 'bc1qt97wqg464zrhnx23upykca5annqvwkwujjglky')
})

it('can generate a SegWit address (via P2SH)', function () {
var keyPair = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
var pubKey = keyPair.getPublicKey()

var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keyPair.publicKey))
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey)

Expand Down
4 changes: 2 additions & 2 deletions test/integration/cltv.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
bitcoin.opcodes.OP_DROP,

bitcoin.opcodes.OP_ELSE,
bQ.getPublicKey(),
bQ.publicKey,
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,

aQ.getPublicKey(),
aQ.publicKey,
bitcoin.opcodes.OP_CHECKSIG
])
}
Expand Down
5 changes: 4 additions & 1 deletion test/integration/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('bitcoinjs-lib (crypto)', function () {
assert(publicKey.verify(m, scriptSignature.signature), 'Invalid m')

// store the required information
input.signature = scriptSignature.signature
input.signature = {
r: bigi.fromBuffer(scriptSignature.signature.slice(0, 32)),
s: bigi.fromBuffer(scriptSignature.signature.slice(32, 64))
}
input.z = bigi.fromBuffer(m)
})

Expand Down
4 changes: 2 additions & 2 deletions test/integration/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe('bitcoinjs-lib (transactions w/ CSV)', function () {
bitcoin.opcodes.OP_DROP,

bitcoin.opcodes.OP_ELSE,
bQ.getPublicKey(),
bQ.publicKey,
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,

aQ.getPublicKey(),
aQ.publicKey,
bitcoin.opcodes.OP_CHECKSIG
])
}
Expand Down
28 changes: 14 additions & 14 deletions test/integration/stealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let ecc = require('tiny-secp256k1')
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKey()), bitcoin.networks.bitcoin.pubKeyHash)
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), bitcoin.networks.bitcoin.pubKeyHash)
}

// vG = (dG \+ sha256(e * dG)G)
Expand Down Expand Up @@ -78,12 +78,12 @@ describe('bitcoinjs-lib (crypto)', function () {
var nonce = bitcoin.ECPair.fromWIF('KxVqB96pxbw1pokzQrZkQbLfVBjjHFfp2mFfEp8wuEyGenLFJhM9') // private to sender

// ... recipient reveals public key (recipient.Q) to sender
var forSender = stealthSend(nonce.getPrivateKey(), recipient.getPublicKey())
var forSender = stealthSend(nonce.privateKey, recipient.publicKey)
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.getPrivateKey(), nonce.getPublicKey())
var forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
assert.equal(getAddress(forRecipient), '1CcZWwCpACJL3AxqoDbwEt4JgDFuTHUspE')
assert.equal(forRecipient.toWIF(), 'L1yjUN3oYyCXV3LcsBrmxCNTa62bZKWCybxVJMvqjMmmfDE8yk7n')

Expand All @@ -96,11 +96,11 @@ describe('bitcoinjs-lib (crypto)', function () {
var nonce = bitcoin.ECPair.makeRandom() // private to sender

// ... recipient reveals public key (recipient.Q) to sender
var forSender = stealthSend(nonce.getPrivateKey(), recipient.getPublicKey())
var forSender = stealthSend(nonce.privateKey, recipient.publicKey)
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)

// ... sender reveals nonce public key (nonce.Q) to recipient
var forRecipient = stealthReceive(recipient.getPrivateKey(), nonce.getPublicKey())
var forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
assert.doesNotThrow(function () { forRecipient.toWIF() })

// sender and recipient, both derived same address
Expand All @@ -112,15 +112,15 @@ describe('bitcoinjs-lib (crypto)', function () {
var nonce = bitcoin.ECPair.makeRandom() // private to sender

// ... recipient reveals public key (recipient.Q) to sender
var forSender = stealthSend(nonce.getPrivateKey(), recipient.getPublicKey())
var forSender = stealthSend(nonce.privateKey, recipient.publicKey)
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)

// ... sender reveals nonce public key (nonce.Q) to recipient
var forRecipient = stealthReceive(recipient.getPrivateKey(), nonce.getPublicKey())
var forRecipient = stealthReceive(recipient.privateKey, nonce.publicKey)
assert.doesNotThrow(function () { forRecipient.toWIF() })

// ... recipient accidentally leaks forRecipient.d on the blockchain
var leaked = stealthRecoverLeaked(forRecipient.getPrivateKey(), nonce.getPrivateKey(), recipient.getPublicKey())
var leaked = stealthRecoverLeaked(forRecipient.privateKey, nonce.privateKey, recipient.publicKey)
assert.equal(leaked.toWIF(), recipient.toWIF())
})

Expand All @@ -131,15 +131,15 @@ describe('bitcoinjs-lib (crypto)', function () {
var nonce = bitcoin.ECPair.fromWIF('KxVqB96pxbw1pokzQrZkQbLfVBjjHFfp2mFfEp8wuEyGenLFJhM9') // private to sender

// ... recipient reveals public key(s) (recipient.Q, scan.Q) to sender
var forSender = stealthDualSend(nonce.getPrivateKey(), recipient.getPublicKey(), scan.getPublicKey())
var forSender = stealthDualSend(nonce.privateKey, recipient.publicKey, scan.publicKey)
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)

// ... sender reveals nonce public key (nonce.Q) to scanner
var forScanner = stealthDualScan(scan.getPrivateKey(), recipient.getPublicKey(), nonce.getPublicKey())
var forScanner = stealthDualScan(scan.privateKey, recipient.publicKey, nonce.publicKey)
assert.throws(function () { forScanner.toWIF() }, /Error: Missing private key/)

// ... scanner reveals relevant transaction + nonce public key (nonce.Q) to recipient
var forRecipient = stealthDualReceive(scan.getPrivateKey(), recipient.getPrivateKey(), nonce.getPublicKey())
var forRecipient = stealthDualReceive(scan.privateKey, recipient.privateKey, nonce.publicKey)
assert.doesNotThrow(function () { forRecipient.toWIF() })

// scanner, sender and recipient, all derived same address
Expand All @@ -153,15 +153,15 @@ describe('bitcoinjs-lib (crypto)', function () {
var nonce = bitcoin.ECPair.makeRandom() // private to sender

// ... recipient reveals public key(s) (recipient.Q, scan.Q) to sender
var forSender = stealthDualSend(nonce.getPrivateKey(), recipient.getPublicKey(), scan.getPublicKey())
var forSender = stealthDualSend(nonce.privateKey, recipient.publicKey, scan.publicKey)
assert.throws(function () { forSender.toWIF() }, /Error: Missing private key/)

// ... sender reveals nonce public key (nonce.Q) to scanner
var forScanner = stealthDualScan(scan.getPrivateKey(), recipient.getPublicKey(), nonce.getPublicKey())
var forScanner = stealthDualScan(scan.privateKey, recipient.publicKey, nonce.publicKey)
assert.throws(function () { forScanner.toWIF() }, /Error: Missing private key/)

// ... scanner reveals relevant transaction + nonce public key (nonce.Q) to recipient
var forRecipient = stealthDualReceive(scan.getPrivateKey(), recipient.getPrivateKey(), nonce.getPublicKey())
var forRecipient = stealthDualReceive(scan.privateKey, recipient.privateKey, nonce.publicKey)
assert.doesNotThrow(function () { forRecipient.toWIF() })

// scanner, sender and recipient, all derived same address
Expand Down
13 changes: 6 additions & 7 deletions test/integration/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
network = network || bitcoin.networks.bitcoin
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKey()), network.pubKeyHash)
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
}

function rng () {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('bitcoinjs-lib (transactions)', function () {
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe',
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx9rcrL7'
].map(function (wif) { return bitcoin.ECPair.fromWIF(wif, regtest) })
var pubKeys = keyPairs.map(function (x) { return x.getPublicKey() })
var pubKeys = keyPairs.map(function (x) { return x.publicKey })

var redeemScript = bitcoin.script.multisig.output.encode(2, pubKeys)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
Expand Down Expand Up @@ -150,8 +150,7 @@ describe('bitcoinjs-lib (transactions)', function () {
this.timeout(30000)

var keyPair = bitcoin.ECPair.fromWIF('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA', regtest)
var pubKey = keyPair.getPublicKey()
var pubKeyHash = bitcoin.crypto.hash160(pubKey)
var pubKeyHash = bitcoin.crypto.hash160(keyPair.pubKey)

var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript)
Expand Down Expand Up @@ -191,7 +190,7 @@ describe('bitcoinjs-lib (transactions)', function () {
'cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87KcLPVfXz',
'cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87L7FgDCKE'
].map(function (wif) { return bitcoin.ECPair.fromWIF(wif, regtest) })
var pubKeys = keyPairs.map(function (x) { return x.getPublicKey() })
var pubKeys = keyPairs.map(function (x) { return x.publicKey })

var witnessScript = bitcoin.script.multisig.output.encode(3, pubKeys)
var redeemScript = bitcoin.script.witnessScriptHash.output.encode(bitcoin.crypto.sha256(witnessScript))
Expand Down Expand Up @@ -230,7 +229,7 @@ describe('bitcoinjs-lib (transactions)', function () {
'032b4c06c06c3ec0b7fa29519dfa5aae193ee2cc35ca127f29f14ec605d62fb63d',
'0216c92abe433106491bdeb4a261226f20f5a4ac86220cc6e37655aac6bf3c1f2a',
'039e05da8b8ea4f9868ecebb25998c7701542986233f4401799551fbecf316b18f'
].map(function (q) { return bitcoin.ECPair.fromPublicKeyBuffer(Buffer.from(q, 'hex')) })
].map(function (q) { return bitcoin.ECPair.fromPublicKey(Buffer.from(q, 'hex')) })

var tx = bitcoin.Transaction.fromHex(txHex)

Expand All @@ -241,7 +240,7 @@ describe('bitcoinjs-lib (transactions)', function () {
var ss = bitcoin.script.signature.decode(scriptSig.signature)
var hash = tx.hashForSignature(i, prevOutScript, ss.hashType)

assert.strictEqual(scriptSig.pubKey.toString('hex'), keyPair.getPublicKey().toString('hex'))
assert.strictEqual(scriptSig.pubKey.toString('hex'), keyPair.publicKey.toString('hex'))
assert.strictEqual(keyPair.verify(hash, ss.signature), true)
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let fixtures = require('./fixtures/transaction_builder')

// TODO: remove
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.getPublicKey()), NETWORKS.bitcoin.pubKeyHash)
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), NETWORKS.bitcoin.pubKeyHash)
}

function construct (f, dontSign) {
Expand Down

0 comments on commit 1307a37

Please sign in to comment.