|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | + |
| 3 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 4 | + |
| 5 | +import { signAndVerifyPsbt } from '../../../../src/transaction/fixedScript/sign'; |
| 6 | + |
| 7 | +function describeSignAndVerifyPsbt(acidTest: utxolib.testutil.AcidTest) { |
| 8 | + describe(`${acidTest.name}`, function () { |
| 9 | + it('should sign unsigned psbt to halfsigned', function () { |
| 10 | + // Create unsigned PSBT |
| 11 | + const psbt = acidTest.createPsbt(); |
| 12 | + |
| 13 | + // Set musig2 nonces for taproot inputs before signing |
| 14 | + const sessionId = Buffer.alloc(32); |
| 15 | + psbt.setAllInputsMusig2NonceHD(acidTest.rootWalletKeys.user, { sessionId }); |
| 16 | + psbt.setAllInputsMusig2NonceHD(acidTest.rootWalletKeys.bitgo, { deterministic: true }); |
| 17 | + |
| 18 | + // Sign with user key |
| 19 | + const result = signAndVerifyPsbt(psbt, acidTest.rootWalletKeys.user, { |
| 20 | + isLastSignature: false, |
| 21 | + }); |
| 22 | + |
| 23 | + // Result should be a PSBT (not finalized) |
| 24 | + assert(result instanceof utxolib.bitgo.UtxoPsbt, 'should return UtxoPsbt when not last signature'); |
| 25 | + |
| 26 | + // Verify that all wallet inputs have been signed by user key |
| 27 | + result.data.inputs.forEach((input, inputIndex) => { |
| 28 | + const { scriptType } = utxolib.bitgo.parsePsbtInput(input); |
| 29 | + |
| 30 | + // Skip replay protection inputs (p2shP2pk) |
| 31 | + if (scriptType === 'p2shP2pk') { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Verify user signature is present |
| 36 | + const isValid = result.validateSignaturesOfInputHD(inputIndex, acidTest.rootWalletKeys.user); |
| 37 | + assert(isValid, `input ${inputIndex} should have valid user signature`); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +describe('signAndVerifyPsbt', function () { |
| 44 | + // Create test suite with includeP2trMusig2ScriptPath set to false |
| 45 | + // p2trMusig2 script path inputs are signed by user and backup keys, |
| 46 | + // which is not the typical signing pattern and makes testing more complex |
| 47 | + utxolib.testutil.AcidTest.suite({ includeP2trMusig2ScriptPath: false }) |
| 48 | + .filter((test) => test.signStage === 'unsigned') |
| 49 | + .forEach((test) => { |
| 50 | + describeSignAndVerifyPsbt(test); |
| 51 | + }); |
| 52 | +}); |
0 commit comments