Skip to content

Commit 151173f

Browse files
authored
Merge pull request #2068 from bitcoinjs/only-warn-once
Only warn once for forward segwit warnings
2 parents 2a2d82e + 0902017 commit 151173f

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/cjs/address.cjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const FUTURE_SEGWIT_VERSION_WARNING =
7373
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
7474
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
7575
'then decide when it is safe to use which version of segwit.';
76+
const WARNING_STATES = [false, false];
7677
/**
7778
* Converts an output buffer to a future segwit address.
7879
* @param output - The output buffer.
@@ -95,7 +96,10 @@ function _toFutureSegwitAddress(output, network) {
9596
throw new TypeError('Invalid version for segwit address');
9697
if (output[1] !== data.length)
9798
throw new TypeError('Invalid script for segwit address');
98-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
99+
if (WARNING_STATES[0] === false) {
100+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
101+
WARNING_STATES[0] = true;
102+
}
99103
return toBech32(data, version, network.bech32);
100104
}
101105
/**
@@ -241,7 +245,10 @@ function toOutputScript(address, network) {
241245
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
242246
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
243247
) {
244-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
248+
if (WARNING_STATES[1] === false) {
249+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
250+
WARNING_STATES[1] = true;
251+
}
245252
return bscript.compile([
246253
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
247254
decodeBech32.data,

src/esm/address.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const FUTURE_SEGWIT_VERSION_WARNING =
1616
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
1717
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
1818
'then decide when it is safe to use which version of segwit.';
19+
const WARNING_STATES = [false, false];
1920
/**
2021
* Converts an output buffer to a future segwit address.
2122
* @param output - The output buffer.
@@ -38,7 +39,10 @@ function _toFutureSegwitAddress(output, network) {
3839
throw new TypeError('Invalid version for segwit address');
3940
if (output[1] !== data.length)
4041
throw new TypeError('Invalid script for segwit address');
41-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
42+
if (WARNING_STATES[0] === false) {
43+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
44+
WARNING_STATES[0] = true;
45+
}
4246
return toBech32(data, version, network.bech32);
4347
}
4448
/**
@@ -181,7 +185,10 @@ export function toOutputScript(address, network) {
181185
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
182186
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
183187
) {
184-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
188+
if (WARNING_STATES[1] === false) {
189+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
190+
WARNING_STATES[1] = true;
191+
}
185192
return bscript.compile([
186193
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
187194
decodeBech32.data,

ts_src/address.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const FUTURE_SEGWIT_VERSION_WARNING: string =
4545
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
4646
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
4747
'then decide when it is safe to use which version of segwit.';
48+
const WARNING_STATES: boolean[] = [false, false];
4849

4950
/**
5051
* Converts an output buffer to a future segwit address.
@@ -73,7 +74,10 @@ function _toFutureSegwitAddress(output: Uint8Array, network: Network): string {
7374
if (output[1] !== data.length)
7475
throw new TypeError('Invalid script for segwit address');
7576

76-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
77+
if (WARNING_STATES[0] === false) {
78+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
79+
WARNING_STATES[0] = true;
80+
}
7781

7882
return toBech32(data, version, network.bech32);
7983
}
@@ -247,7 +251,10 @@ export function toOutputScript(address: string, network?: Network): Uint8Array {
247251
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
248252
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
249253
) {
250-
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
254+
if (WARNING_STATES[1] === false) {
255+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
256+
WARNING_STATES[1] = true;
257+
}
251258

252259
return bscript.compile([
253260
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,

0 commit comments

Comments
 (0)