Skip to content

Commit 797d833

Browse files
Merge pull request #80 from BitGo/BTC-720-disable-warn-sometimes
feat: add flag to disable unsafe sign warning
2 parents bf24f1d + b5e2e3e commit 797d833

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/psbt.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Psbt {
7878
// We will disable exporting the Psbt when unsafe sign is active.
7979
// because it is not BIP174 compliant.
8080
__UNSAFE_SIGN_NONSEGWIT: false,
81+
__WARN_UNSAFE_SIGN_NONSEGWIT: true,
8182
__TX_FROM_BUFFER: buf =>
8283
this.constructor.transactionFromBuffer(buf, this.opts.network),
8384
};
@@ -1016,15 +1017,17 @@ function getHashForSig(inputIndex, input, cache, forValidate, sighashTypes) {
10161017
`${meaningfulScript.toString('hex')}`,
10171018
);
10181019
if (!forValidate && cache.__UNSAFE_SIGN_NONSEGWIT !== false)
1019-
console.warn(
1020-
'Warning: Signing non-segwit inputs without the full parent transaction ' +
1021-
'means there is a chance that a miner could feed you incorrect information ' +
1022-
"to trick you into paying large fees. This behavior is the same as Psbt's predecesor " +
1023-
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
1024-
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
1025-
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
1026-
'*********************',
1027-
);
1020+
if (cache.__WARN_UNSAFE_SIGN_NONSEGWIT) {
1021+
console.warn(
1022+
'Warning: Signing non-segwit inputs without the full parent transaction ' +
1023+
'means there is a chance that a miner could feed you incorrect information ' +
1024+
"to trick you into paying large fees. This behavior is the same as Psbt's predecesor " +
1025+
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
1026+
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
1027+
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
1028+
'*********************',
1029+
);
1030+
}
10281031
hash = unsignedTx.hashForSignature(
10291032
inputIndex,
10301033
meaningfulScript,

ts_src/psbt.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export class Psbt {
152152
// We will disable exporting the Psbt when unsafe sign is active.
153153
// because it is not BIP174 compliant.
154154
__UNSAFE_SIGN_NONSEGWIT: false,
155+
__WARN_UNSAFE_SIGN_NONSEGWIT: true,
155156
__TX_FROM_BUFFER: buf =>
156157
(this.constructor as typeof Psbt).transactionFromBuffer(
157158
buf,
@@ -775,6 +776,7 @@ interface PsbtCache {
775776
__FEE?: bigint;
776777
__EXTRACTED_TX?: Transaction<bigint>;
777778
__UNSAFE_SIGN_NONSEGWIT: boolean;
779+
__WARN_UNSAFE_SIGN_NONSEGWIT: boolean;
778780
__TX_FROM_BUFFER: (buf: Buffer) => Transaction<bigint>;
779781
}
780782

@@ -1363,15 +1365,17 @@ function getHashForSig(
13631365
`${meaningfulScript.toString('hex')}`,
13641366
);
13651367
if (!forValidate && cache.__UNSAFE_SIGN_NONSEGWIT !== false)
1366-
console.warn(
1367-
'Warning: Signing non-segwit inputs without the full parent transaction ' +
1368-
'means there is a chance that a miner could feed you incorrect information ' +
1369-
"to trick you into paying large fees. This behavior is the same as Psbt's predecesor " +
1370-
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
1371-
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
1372-
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
1373-
'*********************',
1374-
);
1368+
if (cache.__WARN_UNSAFE_SIGN_NONSEGWIT) {
1369+
console.warn(
1370+
'Warning: Signing non-segwit inputs without the full parent transaction ' +
1371+
'means there is a chance that a miner could feed you incorrect information ' +
1372+
"to trick you into paying large fees. This behavior is the same as Psbt's predecesor " +
1373+
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
1374+
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
1375+
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
1376+
'*********************',
1377+
);
1378+
}
13751379
hash = unsignedTx.hashForSignature(
13761380
inputIndex,
13771381
meaningfulScript,

0 commit comments

Comments
 (0)