Skip to content

Commit e5781d9

Browse files
committed
testing: extra warnings
1 parent 7d8dd86 commit e5781d9

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

test/integration/addresses.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ const assert = require('assert')
33
const bitcoin = require('../../')
44
const dhttp = require('dhttp/200')
55

6+
// WARNING: DO NOT USE THIS - IT IS NOT RANDOM
7+
// WARNING: It produces the same 'number' every time for the purposes of testing.
8+
function unsafeDeterministicRng (c) {
9+
if (process.env.NODE_ENV !== 'TESTING-BITCOINJS') {
10+
throw new Error('DO NOT USE THIS FUNCTION - IT IS NOT RANDOM - IT IS FOR TESTING ONLY - IT PRODUCES THE SAME NUMBER EVERY TIME')
11+
}
12+
13+
// deterministic result for TESTING ONLY
14+
return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')
15+
}
16+
617
const LITECOIN = {
718
messagePrefix: '\x19Litecoin Signed Message:\n',
819
bip32: {
@@ -14,16 +25,11 @@ const LITECOIN = {
1425
wif: 0xb0
1526
}
1627

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')
21-
return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz')
22-
}
23-
2428
describe('bitcoinjs-lib (addresses)', function () {
2529
it('can generate a random address', function () {
2630
// const keyPair = bitcoin.ECPair.makeRandom()
31+
32+
// WARNING: uses unsafeDeterministicRng function for testing, see warning at top of file
2733
const keyPair = bitcoin.ECPair.makeRandom({ rng: unsafeDeterministicRng })
2834
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })
2935

@@ -116,6 +122,8 @@ describe('bitcoinjs-lib (addresses)', function () {
116122
it('can generate a Testnet address', function () {
117123
const testnet = bitcoin.networks.testnet
118124
// const keyPair = bitcoin.ECPair.makeRandom({ network: testnet })
125+
126+
// WARNING: uses unsafeDeterministicRng function for testing, see warning at top of file
119127
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet, rng: unsafeDeterministicRng })
120128
const wif = keyPair.toWIF()
121129
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: testnet })
@@ -126,6 +134,8 @@ describe('bitcoinjs-lib (addresses)', function () {
126134

127135
it('can generate a Litecoin address', function () {
128136
// const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN })
137+
138+
// WARNING: uses unsafeDeterministicRng function for testing, see warning at top of file
129139
const keyPair = bitcoin.ECPair.makeRandom({ network: LITECOIN, rng: unsafeDeterministicRng })
130140
const wif = keyPair.toWIF()
131141
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: LITECOIN })

0 commit comments

Comments
 (0)