Check actual sighash flags instead of psbtInput one

This commit is contained in:
junderw 2019-07-09 11:06:39 +09:00
parent 09fcb1c6ee
commit 36a966cfcd
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 37 additions and 48 deletions

View file

@ -815,32 +815,27 @@ function checkTxEmpty(tx) {
function checkInputsForPartialSig(inputs, action) { function checkInputsForPartialSig(inputs, action) {
inputs.forEach(input => { inputs.forEach(input => {
let throws = false; let throws = false;
if ((input.partialSig || []).length > 0) { if ((input.partialSig || []).length === 0) return;
if (input.sighashType !== undefined) { input.partialSig.forEach(pSig => {
const whitelist = []; const { hashType } = bscript.signature.decode(pSig.signature);
const isAnyoneCanPay = const whitelist = [];
input.sighashType & transaction_1.Transaction.SIGHASH_ANYONECANPAY; const isAnyoneCanPay =
if (isAnyoneCanPay) whitelist.push('addInput'); hashType & transaction_1.Transaction.SIGHASH_ANYONECANPAY;
if (!isAnyoneCanPay && action === 'addInput') { if (isAnyoneCanPay) whitelist.push('addInput');
throws = true; const hashMod = hashType & 0x1f;
} switch (hashMod) {
const hashType = input.sighashType & 0x1f; case transaction_1.Transaction.SIGHASH_ALL:
switch (hashType) { break;
case transaction_1.Transaction.SIGHASH_ALL: case transaction_1.Transaction.SIGHASH_SINGLE:
break; case transaction_1.Transaction.SIGHASH_NONE:
case transaction_1.Transaction.SIGHASH_SINGLE: whitelist.push('addOutput');
case transaction_1.Transaction.SIGHASH_NONE: whitelist.push('setSequence');
whitelist.push('addOutput'); break;
whitelist.push('setSequence'); }
break; if (whitelist.indexOf(action) === -1) {
}
if (whitelist.indexOf(action) === -1) {
throws = true;
}
} else {
throws = true; throws = true;
} }
} });
if (throws) { if (throws) {
throw new Error('Can not modify transaction, signatures exist.'); throw new Error('Can not modify transaction, signatures exist.');
} }

View file

@ -1028,32 +1028,26 @@ function checkTxEmpty(tx: Transaction): void {
function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void { function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void {
inputs.forEach(input => { inputs.forEach(input => {
let throws = false; let throws = false;
if ((input.partialSig || []).length > 0) { if ((input.partialSig || []).length === 0) return;
if (input.sighashType !== undefined) { input.partialSig!.forEach(pSig => {
const whitelist: string[] = []; const { hashType } = bscript.signature.decode(pSig.signature);
const isAnyoneCanPay = const whitelist: string[] = [];
input.sighashType & Transaction.SIGHASH_ANYONECANPAY; const isAnyoneCanPay = hashType & Transaction.SIGHASH_ANYONECANPAY;
if (isAnyoneCanPay) whitelist.push('addInput'); if (isAnyoneCanPay) whitelist.push('addInput');
if (!isAnyoneCanPay && action === 'addInput') { const hashMod = hashType & 0x1f;
throws = true; switch (hashMod) {
} case Transaction.SIGHASH_ALL:
const hashType = input.sighashType & 0x1f; break;
switch (hashType) { case Transaction.SIGHASH_SINGLE:
case Transaction.SIGHASH_ALL: case Transaction.SIGHASH_NONE:
break; whitelist.push('addOutput');
case Transaction.SIGHASH_SINGLE: whitelist.push('setSequence');
case Transaction.SIGHASH_NONE: break;
whitelist.push('addOutput'); }
whitelist.push('setSequence'); if (whitelist.indexOf(action) === -1) {
break;
}
if (whitelist.indexOf(action) === -1) {
throws = true;
}
} else {
throws = true; throws = true;
} }
} });
if (throws) { if (throws) {
throw new Error('Can not modify transaction, signatures exist.'); throw new Error('Can not modify transaction, signatures exist.');
} }