Skip to content

Commit

Permalink
Merge pull request #4147 from BitGo/BTC-726-bch-stuff
Browse files Browse the repository at this point in the history
feat(utxo-lib): add correct sighashes to non-HD signing
  • Loading branch information
davidkaplanbitgo authored Dec 12, 2023
2 parents 9dde240 + 4ef66d9 commit b836496
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions modules/utxo-lib/src/bitgo/UtxoPsbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { decodeProprietaryKey, encodeProprietaryKey } from 'bip174/src/lib/propr
import {
taproot,
HDSigner,
Signer,
Psbt,
PsbtTransaction,
Transaction,
Expand Down Expand Up @@ -72,8 +73,11 @@ type SignatureParams = {
sighashTypes: number[];
};

function defaultSighashTypes(network: Network): number[] {
const sighashTypes = [Transaction.SIGHASH_DEFAULT, Transaction.SIGHASH_ALL];
function defaultSighashTypes(): number[] {
return [Transaction.SIGHASH_DEFAULT, Transaction.SIGHASH_ALL];
}

function addForkIdToSighashesIfNeeded(network: Network, sighashTypes: number[]): number[] {
switch (getMainnet(network)) {
case networks.bitcoincash:
case networks.bitcoinsv:
Expand All @@ -87,7 +91,10 @@ function defaultSighashTypes(network: Network): number[] {

function toSignatureParams(network: Network, v?: Partial<SignatureParams> | number[]): SignatureParams {
if (Array.isArray(v)) return toSignatureParams(network, { sighashTypes: v });
return { deterministic: false, sighashTypes: defaultSighashTypes(network), ...v };
const defaultSignatureParams = { deterministic: false, sighashTypes: defaultSighashTypes() };
const ret = { ...defaultSignatureParams, ...v };
ret.sighashTypes = addForkIdToSighashesIfNeeded(network, ret.sighashTypes);
return ret;
}

/**
Expand Down Expand Up @@ -821,6 +828,11 @@ export class UtxoPsbt<Tx extends UtxoTransaction<bigint> = UtxoTransaction<bigin
return this;
}

signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this {
const { sighashTypes: sighashForNetwork } = toSignatureParams(this.network, sighashTypes);
return super.signInput(inputIndex, keyPair, sighashForNetwork);
}

signInputHD(
inputIndex: number,
hdKeyPair: HDTaprootSigner | HDTaprootMusig2Signer,
Expand Down

0 comments on commit b836496

Please sign in to comment.