From a796732b3c8ad7b566f6e502c46bbb439ea69a33 Mon Sep 17 00:00:00 2001 From: Brian Richter Date: Wed, 21 May 2025 10:15:31 -0700 Subject: [PATCH] test: add commit-reveal integration --- .../src/transactions/reveal-transaction.ts | 9 +-- .../commit-reveal.integration.test.ts | 78 +++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 packages/ordinalsplus/tests/integration/commit-reveal.integration.test.ts diff --git a/packages/ordinalsplus/src/transactions/reveal-transaction.ts b/packages/ordinalsplus/src/transactions/reveal-transaction.ts index ed3387d..738daf5 100644 --- a/packages/ordinalsplus/src/transactions/reveal-transaction.ts +++ b/packages/ordinalsplus/src/transactions/reveal-transaction.ts @@ -152,7 +152,6 @@ export async function createRevealTransaction(params: RevealTransactionParams): 'Missing commit address, script, or inscription data in prepared inscription for reveal' ); } - const commitScript = preparedInscription.commitAddress.script; const commitAddress = preparedInscription.commitAddress.address; const inscriptionObject = preparedInscription.inscription; const pubKey = preparedInscription.revealPublicKey; @@ -166,7 +165,6 @@ export async function createRevealTransaction(params: RevealTransactionParams): ORDINAL_CUSTOM_SCRIPTS ); - console.log(`[DEBUG-REVEAL-FIX] Using PRE-CALCULATED commit script for witness: ${Buffer.from(commitScript).toString('hex')}`); console.log(`[DEBUG-REVEAL-FIX] Expected Commit Address from prep: ${commitAddress}`); console.log(`[DEBUG-REVEAL-FIX] Reveal Payment Script (for input): ${revealPayment.script ? Buffer.from(revealPayment.script).toString('hex') : 'undefined'}`); // Log the entire revealPayment object to inspect its structure for tapleaf info @@ -177,9 +175,9 @@ export async function createRevealTransaction(params: RevealTransactionParams): ...revealPayment, txid: selectedUTXO.txid, index: selectedUTXO.vout, - witnessUtxo: { - script: commitScript, - amount: inputAmount + witnessUtxo: { + script: revealPayment.script, + amount: inputAmount }, // Removed incorrect redeemScript property. // We need to inspect revealPayment object structure to find where @@ -195,7 +193,6 @@ export async function createRevealTransaction(params: RevealTransactionParams): // DEBUG-COMMIT-REVEAL logging console.log(`[DEBUG-COMMIT-REVEAL] Reveal Address used for output: ${outputAddress}`); - console.log(`[DEBUG-COMMIT-REVEAL] Commit Script used for witness: ${Buffer.from(commitScript).toString('hex')}`); console.log(`[DEBUG-COMMIT-REVEAL] Using Reveal Public Key for signing: ${Buffer.from(pubKey).toString('hex')}`); // Add progress event for adding input diff --git a/packages/ordinalsplus/tests/integration/commit-reveal.integration.test.ts b/packages/ordinalsplus/tests/integration/commit-reveal.integration.test.ts new file mode 100644 index 0000000..25c7839 --- /dev/null +++ b/packages/ordinalsplus/tests/integration/commit-reveal.integration.test.ts @@ -0,0 +1,78 @@ +import { describe, test, expect } from 'bun:test'; +import * as btc from '@scure/btc-signer'; +import { KeyPairGenerator } from '../../src/key-management/key-pair-generator'; +import { createTextInscription } from '../../src/inscription'; +import { prepareCommitTransaction } from '../../src/transactions/commit-transaction'; +import { signTransaction } from '../../src/transactions/transaction-signing'; +import { createRevealTransaction } from '../../src/transactions/reveal-transaction'; +import { Utxo } from '../../src/types'; + +// Integration test that builds a commit and reveal transaction pair +// ensuring both transactions are fully signed and ready for broadcast. + +describe('Commit & Reveal Integration', () => { + test('should create broadcastable commit and reveal transactions', async () => { + // Generate funding key pair for P2WPKH input + const fundingKey = KeyPairGenerator.generateSecp256k1KeyPair({ includeAddress: true, network: 'testnet' }); + const fundingAddress = fundingKey.address!; + const fundingPayment = btc.p2wpkh(fundingKey.publicKeyCompressed, btc.TEST_NETWORK); + + // Mock UTXO used to fund the commit transaction + const utxo: Utxo = { + txid: 'aa'.repeat(32), + vout: 0, + value: 20_000, + scriptPubKey: Buffer.from(fundingPayment.script).toString('hex'), + script: { type: 'p2wpkh', address: fundingAddress } + }; + + // Prepare inscription and commit transaction + const inscription = createTextInscription('Integration commit-reveal', 'testnet'); + const commitRes = await prepareCommitTransaction({ + inscription, + utxos: [utxo], + changeAddress: fundingAddress, + feeRate: 1, + network: 'testnet' + }); + + // Sign the commit transaction so it would be valid for broadcast + const signedCommit = await signTransaction(commitRes.commitPsbt, { + privateKey: fundingKey.privateKey, + transactionType: 'COMMIT' + }); + + expect(signedCommit.tx.isFinal).toBe(true); + expect(signedCommit.hex.length).toBeGreaterThan(0); + + // Build UTXO from commit output for the reveal transaction + const commitOutput = signedCommit.tx.getOutput(0); + const commitUtxo: Utxo = { + txid: signedCommit.tx.id, + vout: 0, + value: Number(commitOutput.amount), + script: { type: 'p2tr', address: inscription.commitAddress.address } + }; + + // Create and sign the reveal transaction using the inscription key + // Mock network calls used by system health check + (globalThis as any).fetch = async () => ({ ok: true }); + const revealRes = await createRevealTransaction({ + selectedUTXO: commitUtxo, + preparedInscription: inscription, + feeRate: 1, + network: btc.TEST_NETWORK, + privateKey: inscription.revealPrivateKey!, + commitTransactionId: signedCommit.tx.id + }); + + expect(revealRes.tx.isFinal).toBe(true); + expect(revealRes.hex.length).toBeGreaterThan(0); + + // Verify reveal spends the exact script produced in the commit output + const commitScriptHex = Buffer.from(commitOutput.script).toString('hex'); + const revealInput = revealRes.tx.getInput(0); + const revealScriptHex = Buffer.from(revealInput.witnessUtxo!.script).toString('hex'); + expect(revealScriptHex).toBe(commitScriptHex); + }); +});