Skip to content

Commit 5762970

Browse files
committed
fix(network): optional bech32 for bitgo compatibility
Issue: BG-47820
1 parent 083d2c7 commit 5762970

File tree

10 files changed

+37
-17
lines changed

10 files changed

+37
-17
lines changed

package-lock.json

Lines changed: 9 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"mocha:ts": "mocha --recursive --require test/ts-node-register",
3434
"nobuild:coverage-report": "nyc report --reporter=lcov",
3535
"nobuild:coverage-html": "nyc report --reporter=html",
36-
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 90 --functions 90 --lines 90 mocha && npm run clean:jstests",
36+
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 89 --functions 90 --lines 90 mocha && npm run clean:jstests",
3737
"nobuild:integration": "npm run mocha:ts -- --timeout 50000 'test/integration/*.ts'",
3838
"nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
3939
"prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
@@ -77,7 +77,7 @@
7777
"bn.js": "^4.11.8",
7878
"bs58": "^4.0.0",
7979
"dhttp": "^3.0.0",
80-
"ecpair": "^2.0.1",
80+
"ecpair": "git+https://github.com/brandonblack/ecpair#2fd5696110aa8bba136c5a5240019cd1ced16431",
8181
"hoodwink": "^2.0.0",
8282
"minimaldata": "^1.0.2",
8383
"mocha": "^10.0.0",
@@ -87,7 +87,7 @@
8787
"randombytes": "^2.1.0",
8888
"regtest-client": "0.2.0",
8989
"rimraf": "^2.6.3",
90-
"tiny-secp256k1": "^2.2.0",
90+
"tiny-secp256k1": "^2.2.1",
9191
"ts-node": "^8.3.0",
9292
"tslint": "^6.1.3",
9393
"typescript": "^4.4.4"

src/address.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function _toFutureSegwitAddress(output, network) {
3434
if (output[1] !== data.length)
3535
throw new TypeError('Invalid script for segwit address');
3636
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
37+
if (!network.bech32) {
38+
throw new TypeError("Network doesn't support native segwit");
39+
}
3740
return toBech32(data, version, network.bech32);
3841
}
3942
function fromBase58Check(address) {

src/networks.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface Network {
22
messagePrefix: string;
3-
bech32: string;
3+
bech32?: string;
44
bip32: Bip32;
55
pubKeyHash: number;
66
scriptHash: number;

src/payments/p2wpkh.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ function p2wpkh(a, opts) {
4343
});
4444
const network = a.network || networks_1.bitcoin;
4545
const o = { name: 'p2wpkh', network };
46+
if (!network.bech32) {
47+
throw new TypeError("Network doesn't support native segwit");
48+
}
4649
lazy.prop(o, 'address', () => {
4750
if (!o.hash) return;
4851
const words = bech32_1.bech32.toWords(o.hash);

src/payments/p2wsh.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function p2wsh(a, opts) {
7272
if (!network) {
7373
network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
7474
}
75+
if (!network.bech32) {
76+
throw new TypeError("Network doesn't support native segwit");
77+
}
7578
const o = { network };
7679
lazy.prop(o, 'address', () => {
7780
if (!o.hash) return;

ts_src/address.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
5151

5252
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
5353

54+
if (!network.bech32) {
55+
throw new TypeError("Network doesn't support native segwit");
56+
}
57+
5458
return toBech32(data, version, network.bech32);
5559
}
5660

ts_src/networks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
33
export interface Network {
44
messagePrefix: string;
5-
bech32: string;
5+
bech32?: string;
66
bip32: Bip32;
77
pubKeyHash: number;
88
scriptHash: number;

ts_src/payments/p2wpkh.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@ export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
4545
const network = a.network || BITCOIN_NETWORK;
4646
const o: Payment = { name: 'p2wpkh', network };
4747

48+
if (!network.bech32) {
49+
throw new TypeError("Network doesn't support native segwit");
50+
}
51+
4852
lazy.prop(o, 'address', () => {
4953
if (!o.hash) return;
5054

5155
const words = bech32.toWords(o.hash);
5256
words.unshift(0x00);
53-
return bech32.encode(network.bech32, words);
57+
return bech32.encode(network.bech32!, words);
5458
});
5559
lazy.prop(o, 'hash', () => {
5660
if (a.output) return a.output.slice(2, 22);

ts_src/payments/p2wsh.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
7777
network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
7878
}
7979

80+
if (!network.bech32) {
81+
throw new TypeError("Network doesn't support native segwit");
82+
}
83+
8084
const o: Payment = { network };
8185

8286
lazy.prop(o, 'address', () => {
8387
if (!o.hash) return;
8488
const words = bech32.toWords(o.hash);
8589
words.unshift(0x00);
86-
return bech32.encode(network!.bech32, words);
90+
return bech32.encode(network!.bech32!, words);
8791
});
8892
lazy.prop(o, 'hash', () => {
8993
if (a.output) return a.output.slice(2);

0 commit comments

Comments
 (0)