@@ -69,6 +69,14 @@ class Psbt {
69
69
__NON_WITNESS_UTXO_BUF_CACHE : [ ] ,
70
70
__TX_IN_CACHE : { } ,
71
71
__TX : this . data . globalMap . unsignedTx . tx ,
72
+ // Old TransactionBuilder behavior was to not confirm input values
73
+ // before signing. Even though we highly encourage people to get
74
+ // the full parent transaction to verify values, the ability to
75
+ // sign non-segwit inputs without the full transaction was often
76
+ // requested. So the only way to activate is to use @ts -ignore.
77
+ // We will disable exporting the Psbt when unsafe sign is active.
78
+ // because it is not BIP174 compliant.
79
+ __UNSAFE_SIGN_NONSEGWIT : false ,
72
80
} ;
73
81
if ( this . data . inputs . length === 0 ) this . setVersion ( 2 ) ;
74
82
// Make data hidden when enumerating
@@ -313,6 +321,7 @@ class Psbt {
313
321
inputIndex ,
314
322
Object . assign ( { } , input , { sighashType : sig . hashType } ) ,
315
323
this . __CACHE ,
324
+ true ,
316
325
)
317
326
: { hash : hashCache , script : scriptCache } ;
318
327
sighashCache = sig . hashType ;
@@ -513,12 +522,15 @@ class Psbt {
513
522
} ) ;
514
523
}
515
524
toBuffer ( ) {
525
+ checkCache ( this . __CACHE ) ;
516
526
return this . data . toBuffer ( ) ;
517
527
}
518
528
toHex ( ) {
529
+ checkCache ( this . __CACHE ) ;
519
530
return this . data . toHex ( ) ;
520
531
}
521
532
toBase64 ( ) {
533
+ checkCache ( this . __CACHE ) ;
522
534
return this . data . toBase64 ( ) ;
523
535
}
524
536
updateGlobal ( updateData ) {
@@ -626,6 +638,11 @@ function canFinalize(input, script, scriptType) {
626
638
return false ;
627
639
}
628
640
}
641
+ function checkCache ( cache ) {
642
+ if ( cache . __UNSAFE_SIGN_NONSEGWIT !== false ) {
643
+ throw new Error ( 'Not BIP174 compliant, can not export' ) ;
644
+ }
645
+ }
629
646
function hasSigs ( neededSigs , partialSig , pubkeys ) {
630
647
if ( ! partialSig ) return false ;
631
648
let sigs ;
@@ -857,6 +874,7 @@ function getHashAndSighashType(
857
874
inputIndex ,
858
875
input ,
859
876
cache ,
877
+ false ,
860
878
sighashTypes ,
861
879
) ;
862
880
checkScriptForPubkey ( pubkey , script , 'sign' ) ;
@@ -865,7 +883,7 @@ function getHashAndSighashType(
865
883
sighashType,
866
884
} ;
867
885
}
868
- function getHashForSig ( inputIndex , input , cache , sighashTypes ) {
886
+ function getHashForSig ( inputIndex , input , cache , forValidate , sighashTypes ) {
869
887
const unsignedTx = cache . __TX ;
870
888
const sighashType =
871
889
input . sighashType || transaction_1 . Transaction . SIGHASH_ALL ;
@@ -925,11 +943,24 @@ function getHashForSig(inputIndex, input, cache, sighashTypes) {
925
943
) ;
926
944
} else {
927
945
// non-segwit
928
- if ( input . nonWitnessUtxo === undefined )
946
+ if (
947
+ input . nonWitnessUtxo === undefined &&
948
+ cache . __UNSAFE_SIGN_NONSEGWIT === false
949
+ )
929
950
throw new Error (
930
951
`Input #${ inputIndex } has witnessUtxo but non-segwit script: ` +
931
952
`${ meaningfulScript . toString ( 'hex' ) } ` ,
932
953
) ;
954
+ if ( ! forValidate && cache . __UNSAFE_SIGN_NONSEGWIT !== false )
955
+ console . warn (
956
+ 'Warning: Signing non-segwit inputs without the full parent transaction ' +
957
+ 'means there is a chance that a miner could feed you incorrect information ' +
958
+ 'to trick you into paying large fees. This behavior is the same as the old ' +
959
+ 'TransactionBuilder class when signing non-segwit scripts. You are not ' +
960
+ 'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
961
+ 'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
962
+ '*********************' ,
963
+ ) ;
933
964
hash = unsignedTx . hashForSignature (
934
965
inputIndex ,
935
966
meaningfulScript ,
0 commit comments