Skip to content

Commit

Permalink
Merge pull request #4163 from BitGo/BTC-726-add-rp-update
Browse files Browse the repository at this point in the history
feat(wp): add sighash to replay protection update
  • Loading branch information
davidkaplanbitgo authored Dec 19, 2023
2 parents bad75fd + 3cf9002 commit ac8575a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions modules/utxo-lib/src/bitgo/wallet/Unspent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export function updateReplayProtectionUnspentToPsbt(
} else if (!isZcash && !input.nonWitnessUtxo) {
psbt.updateInput(inputIndex, { nonWitnessUtxo: (u as UnspentWithPrevTx<bigint>).prevTx });
}

const sighashType = getDefaultSigHash(psbt.network);
if (psbt.data.inputs[inputIndex].sighashType === undefined) {
psbt.updateInput(inputIndex, { sighashType });
}
}

function addUnspentToPsbt(
Expand Down
9 changes: 6 additions & 3 deletions modules/utxo-lib/test/bitgo/psbt/Psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
withUnsafeNonSegwit,
getTransactionAmountsFromPsbt,
WalletUnspent,
getDefaultSigHash,
} from '../../../src/bitgo';
import {
createOutputScript2of3,
Expand Down Expand Up @@ -432,8 +433,8 @@ describe('isTransactionWithKeyPathSpendInput', function () {
describe('Parse PSBT', function () {
it('p2shP2pk parsing', function () {
const signer = rootWalletKeys['user'];
const psbt = createPsbtForNetwork({ network });
const unspent = mockReplayProtectionUnspent(network, BigInt(1e8), { key: signer });
const psbt = createPsbtForNetwork({ network: networks.bitcoincash });
const unspent = mockReplayProtectionUnspent(networks.bitcoincash, BigInt(1e8), { key: signer });
const { redeemScript } = createOutputScriptP2shP2pk(signer.publicKey);
assert(redeemScript);
addReplayProtectionUnspentToPsbt(psbt, unspent, redeemScript);
Expand All @@ -454,10 +455,12 @@ describe('Parse PSBT', function () {

assert.strictEqual(parsed.scriptType, 'p2shP2pk');
assert.strictEqual(parsed.signatures?.length, 1);
assert.strictEqual(parsed.signatures[0].length, 72);
assert.strictEqual(parsed.publicKeys.length, 1);
assert.ok(parsed.publicKeys[0].length === 33);
assert.ok(parsed.pubScript.equals(redeemScript));

const sighash: number = parsed.signatures[0][parsed.signatures[0].length - 1];
assert.strictEqual(sighash, getDefaultSigHash(psbt.network));
});

it('fail to parse finalized psbt', function () {
Expand Down

0 comments on commit ac8575a

Please sign in to comment.