Skip to content

Commit

Permalink
Merge pull request #2068 from bitcoinjs/only-warn-once
Browse files Browse the repository at this point in the history
Only warn once for forward segwit warnings
  • Loading branch information
junderw authored Dec 27, 2024
2 parents 2a2d82e + 0902017 commit 151173f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/cjs/address.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const FUTURE_SEGWIT_VERSION_WARNING =
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
'then decide when it is safe to use which version of segwit.';
const WARNING_STATES = [false, false];
/**
* Converts an output buffer to a future segwit address.
* @param output - The output buffer.
Expand All @@ -95,7 +96,10 @@ function _toFutureSegwitAddress(output, network) {
throw new TypeError('Invalid version for segwit address');
if (output[1] !== data.length)
throw new TypeError('Invalid script for segwit address');
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[0] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[0] = true;
}
return toBech32(data, version, network.bech32);
}
/**
Expand Down Expand Up @@ -241,7 +245,10 @@ function toOutputScript(address, network) {
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[1] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[1] = true;
}
return bscript.compile([
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
decodeBech32.data,
Expand Down
11 changes: 9 additions & 2 deletions src/esm/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const FUTURE_SEGWIT_VERSION_WARNING =
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
'then decide when it is safe to use which version of segwit.';
const WARNING_STATES = [false, false];
/**
* Converts an output buffer to a future segwit address.
* @param output - The output buffer.
Expand All @@ -38,7 +39,10 @@ function _toFutureSegwitAddress(output, network) {
throw new TypeError('Invalid version for segwit address');
if (output[1] !== data.length)
throw new TypeError('Invalid script for segwit address');
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[0] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[0] = true;
}
return toBech32(data, version, network.bech32);
}
/**
Expand Down Expand Up @@ -181,7 +185,10 @@ export function toOutputScript(address, network) {
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[1] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[1] = true;
}
return bscript.compile([
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
decodeBech32.data,
Expand Down
11 changes: 9 additions & 2 deletions ts_src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const FUTURE_SEGWIT_VERSION_WARNING: string =
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
'then decide when it is safe to use which version of segwit.';
const WARNING_STATES: boolean[] = [false, false];

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

console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[0] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[0] = true;
}

return toBech32(data, version, network.bech32);
}
Expand Down Expand Up @@ -247,7 +251,10 @@ export function toOutputScript(address: string, network?: Network): Uint8Array {
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
if (WARNING_STATES[1] === false) {
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
WARNING_STATES[1] = true;
}

return bscript.compile([
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
Expand Down

0 comments on commit 151173f

Please sign in to comment.