Skip to content

Commit 7d8dd86

Browse files
committed
testing: use NODE_ENV instead of mocha constants
1 parent 0cfc88d commit 7d8dd86

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"coverage-report": "nyc report --reporter=lcov",
1818
"coverage-html": "nyc report --reporter=html",
1919
"coverage": "nyc --check-coverage --branches 90 --functions 90 mocha",
20-
"integration": "mocha --timeout 50000 test/integration/",
20+
"integration": "NODE_ENV=TESTING-BITCOINJS mocha --timeout 50000 test/integration/",
2121
"standard": "standard",
2222
"test": "npm run standard && npm run coverage",
2323
"unit": "mocha"

test/integration/addresses.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@ const LITECOIN = {
1414
wif: 0xb0
1515
}
1616

17-
// deterministic RNG for testing only
18-
function rng (c) {
19-
if (describe === undefined || it === undefined) {
20-
console.error('DO NOT USE THIS rng FUNCTION OUTSIDE OF AUTOMATED TESTING!')
21-
const randomBytes = require('randombytes')
22-
return randomBytes(c)
23-
}
17+
// deterministic random number generator for TESTING ONLY
18+
// WARNING: DO NOT USE THIS - IT IS NOT RANDOM - it produces the same private key every time for the purposes of testing.
19+
function unsafeDeterministicRng (c) {
20+
if (process.env.NODE_ENV !== 'TESTING-BITCOINJS') throw new Error('DO NOT USE THIS FUNCTION - IT IS NOT RANDOM - IT IS FOR TESTING ONLY - IT PRODUCES THE SAME PRIVATE KEY EVERY TIME')
2421
return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')
2522
}
2623

2724
describe('bitcoinjs-lib (addresses)', function () {
2825
it('can generate a random address', function () {
29-
// in production: const keyPair = bitcoin.ECPair.makeRandom({})
30-
const keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
26+
// const keyPair = bitcoin.ECPair.makeRandom()
27+
const keyPair = bitcoin.ECPair.makeRandom({ rng: unsafeDeterministicRng })
3128
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })
3229

3330
assert.strictEqual(address, '1F5VhMHukdnUES9kfXqzPzMeF1GPHKiF64')
@@ -118,8 +115,8 @@ describe('bitcoinjs-lib (addresses)', function () {
118115
// other networks
119116
it('can generate a Testnet address', function () {
120117
const testnet = bitcoin.networks.testnet
121-
// in production: const keyPair = bitcoin.ECPair.makeRandom({ network: testnet })
122-
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: rng })
118+
// const keyPair = bitcoin.ECPair.makeRandom({ network: testnet })
119+
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: unsafeDeterministicRng })
123120
const wif = keyPair.toWIF()
124121
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: testnet })
125122

@@ -128,8 +125,8 @@ describe('bitcoinjs-lib (addresses)', function () {
128125
})
129126

130127
it('can generate a Litecoin address', function () {
131-
// in production: const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN })
132-
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: rng })
128+
// const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN })
129+
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: unsafeDeterministicRng })
133130
const wif = keyPair.toWIF()
134131
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: LITECOIN })
135132

0 commit comments

Comments
 (0)