diff --git a/src/ecpair.js b/src/ecpair.js index 9467b1b..d0653be 100644 --- a/src/ecpair.js +++ b/src/ecpair.js @@ -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') @@ -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) { diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 4bf6785..4d73c2a 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -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) }) diff --git a/test/ecpair.js b/test/ecpair.js index 89deae2..5e2ea59 100644 --- a/test/ecpair.js +++ b/test/ecpair.js @@ -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) }) }) @@ -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') })) }) @@ -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) }) @@ -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]) }) @@ -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) }) }) }) @@ -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) @@ -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) diff --git a/test/integration/_regtest.js b/test/integration/_regtest.js index 46a0393..346fa41 100644 --- a/test/integration/_regtest.js +++ b/test/integration/_regtest.js @@ -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 () { diff --git a/test/integration/addresses.js b/test/integration/addresses.js index 5cd6a46..9451c69 100644 --- a/test/integration/addresses.js +++ b/test/integration/addresses.js @@ -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 () { @@ -57,9 +57,8 @@ 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') @@ -67,9 +66,8 @@ describe('bitcoinjs-lib (addresses)', function () { 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) diff --git a/test/integration/cltv.js b/test/integration/cltv.js index 52b9f00..b7c6c2c 100644 --- a/test/integration/cltv.js +++ b/test/integration/cltv.js @@ -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 ]) } diff --git a/test/integration/csv.js b/test/integration/csv.js index 6aa3087..adb28b5 100644 --- a/test/integration/csv.js +++ b/test/integration/csv.js @@ -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 ]) } diff --git a/test/integration/stealth.js b/test/integration/stealth.js index 91508e2..90acb7b 100644 --- a/test/integration/stealth.js +++ b/test/integration/stealth.js @@ -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) @@ -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') @@ -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 @@ -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()) }) @@ -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 @@ -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 diff --git a/test/integration/transactions.js b/test/integration/transactions.js index 70d68a6..7f78ee8 100644 --- a/test/integration/transactions.js +++ b/test/integration/transactions.js @@ -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 () { @@ -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)) @@ -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.publicKey) var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash) var redeemScriptHash = bitcoin.crypto.hash160(redeemScript) @@ -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)) @@ -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) @@ -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) }) }) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 9c9cc47..c5c7c1e 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -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) {