Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sighash all calc for PSBTs #375

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions transactions/bitcoin/enhancedPsbt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { base64, hex } from '@scure/base';
import * as btc from '@scure/btc-signer';

import { UTXO } from '../../types';
import { InputToSign } from '../psbt';
import { TransactionContext } from './context';
import { ExtendedDummyUtxo, ExtendedUtxo } from './extendedUtxo';
import {
EnhancedInput,
InputMetadata,
Expand All @@ -13,8 +15,6 @@ import {
TransactionScriptOutput,
} from './types';
import { extractOutputInscriptionsAndSatributes, getTransactionTotals, mapInputToEnhancedInput } from './utils';
import { UTXO } from '../../types';
import { ExtendedDummyUtxo, ExtendedUtxo } from './extendedUtxo';

export class EnhancedPsbt {
private readonly _context!: TransactionContext;
Expand All @@ -38,7 +38,7 @@ export class EnhancedPsbt {
if (inputsToSign) {
this._inputsToSignMap = {};
let hasSigHashNone = false;
let isSigHashAll = true;
let isSigHashAll = false;

for (const input of inputsToSign) {
for (const inputIndex of input.signingIndexes) {
Expand All @@ -48,18 +48,17 @@ export class EnhancedPsbt {

this._inputsToSignMap[inputIndex].push({ address: input.address, sigHash: input.sigHash });

if (!input.sigHash) {
if (!input.sigHash || (input.sigHash & btc.SigHash.SINGLE) === btc.SigHash.SINGLE) {
continue;
}

if ((input.sigHash & btc.SigHash.SINGLE) === btc.SigHash.SINGLE) {
isSigHashAll = false;
if (input.sigHash === btc.SigHash.DEFAULT || (input.sigHash & btc.SigHash.ALL) === btc.SigHash.ALL) {
isSigHashAll = true;
continue;
}

if ((input.sigHash & btc.SigHash.NONE) === btc.SigHash.NONE) {
hasSigHashNone = true;
isSigHashAll = false;
}
}
}
Expand Down Expand Up @@ -114,7 +113,7 @@ export class EnhancedPsbt {
private async _extractInputMetadata(transaction: btc.Transaction): Promise<InputMetadata> {
const inputs: { extendedUtxo: ExtendedUtxo | ExtendedDummyUtxo; sigHash?: btc.SigHash }[] = [];

let isSigHashAll = this._isSigHashAll ?? true;
let isSigHashAll = this._isSigHashAll ?? false;
let hasSigHashNone = this._hasSigHashNone ?? false;

let inputTotal = 0;
Expand Down Expand Up @@ -142,12 +141,17 @@ export class EnhancedPsbt {

inputTotal += inputExtendedUtxo.utxo.value || 0;

// sig hash single value is 3 while sighash none is 2 and sighash all is 1, so we need to ensure we
// sighash single value is 3 while sighash none is 2 and sighash all is 1, so we need to ensure we
// don't have a single before we do the bitwise or for all and none
const isSigHashSingle = (sigHash && sigHash & btc.SigHash.SINGLE) === btc.SigHash.SINGLE;

isSigHashAll =
isSigHashAll && !isSigHashSingle && (sigHash === undefined || (sigHash & btc.SigHash.ALL) === btc.SigHash.ALL);
isSigHashAll ||
(!isSigHashSingle &&
(sigHash === undefined ||
sigHash === btc.SigHash.DEFAULT ||
(sigHash & btc.SigHash.ALL) === btc.SigHash.ALL));

hasSigHashNone =
hasSigHashNone || (!isSigHashSingle && (sigHash && sigHash & btc.SigHash.NONE) === btc.SigHash.NONE);
}
Expand Down
Loading