Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only warn once for forward segwit warnings #2068

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading