Skip to content

Commit 4e02ba7

Browse files
authored
Merge pull request #1754 from bitcoinjs/fix/addwarning
Add warning to future segwit version address generation/parsing
2 parents ac411e2 + 11202eb commit 4e02ba7

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitcoinjs-lib",
3-
"version": "6.0.0",
3+
"version": "6.0.1",
44
"description": "Client-side Bitcoin JavaScript library",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/address.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const FUTURE_SEGWIT_MIN_SIZE = 2;
1313
const FUTURE_SEGWIT_MAX_VERSION = 16;
1414
const FUTURE_SEGWIT_MIN_VERSION = 1;
1515
const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
16+
const FUTURE_SEGWIT_VERSION_WARNING =
17+
'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
18+
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
19+
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
20+
'then decide when it is safe to use which version of segwit.';
1621
function _toFutureSegwitAddress(output, network) {
1722
const data = output.slice(2);
1823
if (
@@ -28,6 +33,7 @@ function _toFutureSegwitAddress(output, network) {
2833
throw new TypeError('Invalid version for segwit address');
2934
if (output[1] !== data.length)
3035
throw new TypeError('Invalid script for segwit address');
36+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
3137
return toBech32(data, version, network.bech32);
3238
}
3339
function fromBase58Check(address) {
@@ -128,11 +134,13 @@ function toOutputScript(address, network) {
128134
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
129135
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
130136
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
131-
)
137+
) {
138+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
132139
return bscript.compile([
133140
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
134141
decodeBech32.data,
135142
]);
143+
}
136144
}
137145
}
138146
throw new Error(address + ' has no matching Script');

ts_src/address.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const FUTURE_SEGWIT_MIN_SIZE: number = 2;
2323
const FUTURE_SEGWIT_MAX_VERSION: number = 16;
2424
const FUTURE_SEGWIT_MIN_VERSION: number = 1;
2525
const FUTURE_SEGWIT_VERSION_DIFF: number = 0x50;
26+
const FUTURE_SEGWIT_VERSION_WARNING: string =
27+
'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
28+
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
29+
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
30+
'then decide when it is safe to use which version of segwit.';
2631

2732
function _toFutureSegwitAddress(output: Buffer, network: Network): string {
2833
const data = output.slice(2);
@@ -44,6 +49,8 @@ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
4449
if (output[1] !== data.length)
4550
throw new TypeError('Invalid script for segwit address');
4651

52+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
53+
4754
return toBech32(data, version, network.bech32);
4855
}
4956

@@ -163,11 +170,14 @@ export function toOutputScript(address: string, network?: Network): Buffer {
163170
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
164171
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
165172
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
166-
)
173+
) {
174+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
175+
167176
return bscript.compile([
168177
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
169178
decodeBech32.data,
170179
]);
180+
}
171181
}
172182
}
173183

0 commit comments

Comments
 (0)