Check actual sighash flags instead of psbtInput one
This commit is contained in:
parent
09fcb1c6ee
commit
36a966cfcd
2 changed files with 37 additions and 48 deletions
43
src/psbt.js
43
src/psbt.js
|
@ -815,32 +815,27 @@ function checkTxEmpty(tx) {
|
|||
function checkInputsForPartialSig(inputs, action) {
|
||||
inputs.forEach(input => {
|
||||
let throws = false;
|
||||
if ((input.partialSig || []).length > 0) {
|
||||
if (input.sighashType !== undefined) {
|
||||
const whitelist = [];
|
||||
const isAnyoneCanPay =
|
||||
input.sighashType & transaction_1.Transaction.SIGHASH_ANYONECANPAY;
|
||||
if (isAnyoneCanPay) whitelist.push('addInput');
|
||||
if (!isAnyoneCanPay && action === 'addInput') {
|
||||
throws = true;
|
||||
}
|
||||
const hashType = input.sighashType & 0x1f;
|
||||
switch (hashType) {
|
||||
case transaction_1.Transaction.SIGHASH_ALL:
|
||||
break;
|
||||
case transaction_1.Transaction.SIGHASH_SINGLE:
|
||||
case transaction_1.Transaction.SIGHASH_NONE:
|
||||
whitelist.push('addOutput');
|
||||
whitelist.push('setSequence');
|
||||
break;
|
||||
}
|
||||
if (whitelist.indexOf(action) === -1) {
|
||||
throws = true;
|
||||
}
|
||||
} else {
|
||||
if ((input.partialSig || []).length === 0) return;
|
||||
input.partialSig.forEach(pSig => {
|
||||
const { hashType } = bscript.signature.decode(pSig.signature);
|
||||
const whitelist = [];
|
||||
const isAnyoneCanPay =
|
||||
hashType & transaction_1.Transaction.SIGHASH_ANYONECANPAY;
|
||||
if (isAnyoneCanPay) whitelist.push('addInput');
|
||||
const hashMod = hashType & 0x1f;
|
||||
switch (hashMod) {
|
||||
case transaction_1.Transaction.SIGHASH_ALL:
|
||||
break;
|
||||
case transaction_1.Transaction.SIGHASH_SINGLE:
|
||||
case transaction_1.Transaction.SIGHASH_NONE:
|
||||
whitelist.push('addOutput');
|
||||
whitelist.push('setSequence');
|
||||
break;
|
||||
}
|
||||
if (whitelist.indexOf(action) === -1) {
|
||||
throws = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (throws) {
|
||||
throw new Error('Can not modify transaction, signatures exist.');
|
||||
}
|
||||
|
|
|
@ -1028,32 +1028,26 @@ function checkTxEmpty(tx: Transaction): void {
|
|||
function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void {
|
||||
inputs.forEach(input => {
|
||||
let throws = false;
|
||||
if ((input.partialSig || []).length > 0) {
|
||||
if (input.sighashType !== undefined) {
|
||||
const whitelist: string[] = [];
|
||||
const isAnyoneCanPay =
|
||||
input.sighashType & Transaction.SIGHASH_ANYONECANPAY;
|
||||
if (isAnyoneCanPay) whitelist.push('addInput');
|
||||
if (!isAnyoneCanPay && action === 'addInput') {
|
||||
throws = true;
|
||||
}
|
||||
const hashType = input.sighashType & 0x1f;
|
||||
switch (hashType) {
|
||||
case Transaction.SIGHASH_ALL:
|
||||
break;
|
||||
case Transaction.SIGHASH_SINGLE:
|
||||
case Transaction.SIGHASH_NONE:
|
||||
whitelist.push('addOutput');
|
||||
whitelist.push('setSequence');
|
||||
break;
|
||||
}
|
||||
if (whitelist.indexOf(action) === -1) {
|
||||
throws = true;
|
||||
}
|
||||
} else {
|
||||
if ((input.partialSig || []).length === 0) return;
|
||||
input.partialSig!.forEach(pSig => {
|
||||
const { hashType } = bscript.signature.decode(pSig.signature);
|
||||
const whitelist: string[] = [];
|
||||
const isAnyoneCanPay = hashType & Transaction.SIGHASH_ANYONECANPAY;
|
||||
if (isAnyoneCanPay) whitelist.push('addInput');
|
||||
const hashMod = hashType & 0x1f;
|
||||
switch (hashMod) {
|
||||
case Transaction.SIGHASH_ALL:
|
||||
break;
|
||||
case Transaction.SIGHASH_SINGLE:
|
||||
case Transaction.SIGHASH_NONE:
|
||||
whitelist.push('addOutput');
|
||||
whitelist.push('setSequence');
|
||||
break;
|
||||
}
|
||||
if (whitelist.indexOf(action) === -1) {
|
||||
throws = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (throws) {
|
||||
throw new Error('Can not modify transaction, signatures exist.');
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue