From b35236fa797ee5315479aa8f5cba37a3aca493e1 Mon Sep 17 00:00:00 2001 From: ahonn Date: Thu, 18 Jul 2024 17:03:21 +1000 Subject: [PATCH] feat: support spore leap transaction --- src/services/transaction.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/services/transaction.ts b/src/services/transaction.ts index d0016c24..199b6bb6 100644 --- a/src/services/transaction.ts +++ b/src/services/transaction.ts @@ -317,21 +317,39 @@ export default class TransactionProcessor ]); // using for spv proof, we need to remove the witness data from the transaction const hexWithoutWitness = transactionToHex(BitcoinTransaction.fromHex(hex), false); - let signedTx = await appendCkbTxWitnesses({ + const signedTx = await appendCkbTxWitnesses({ ckbRawTx, btcTxBytes: hexWithoutWitness, rgbppApiSpvProof, })!; - // append the spore cobuild witness to the transaction if needed + return signedTx; + } + + /** + * Append the spore cobuild witness to the transaction if needed + * @param signedTx - the signed CKB transaction + */ + private async appendSporeCobuildWitnessIfNeed(signedTx: CKBRawTransaction, needPaymasterCell: boolean) { const sporeTypeDep = getSporeTypeDep(this.isMainnet); const hasSporeTypeDep = signedTx.cellDeps.some((cellDep) => { return serializeCellDep(cellDep) === serializeCellDep(sporeTypeDep); }); if (hasSporeTypeDep) { - signedTx = await this.appendSporeCobuildWitness(signedTx); - } + // if paymaster cell is needed, we need to exchange the last two witnesses + // make sure the spore cobuild witness is the last witness + if (needPaymasterCell) { + const witnesses = signedTx.witnesses; + signedTx.witnesses = [ + ...witnesses.slice(0, witnesses.length - 2), + witnesses[witnesses.length - 1], + witnesses[witnesses.length - 2], + ]; + } + const tx = await this.appendSporeCobuildWitness(signedTx); + return tx; + } return signedTx; } @@ -354,7 +372,7 @@ export default class TransactionProcessor if (sporeLiveCells.length > 0) { signedTx.witnesses[signedTx.witnesses.length - 1] = generateSporeTransferCoBuild( sporeLiveCells, - signedTx.outputs, + signedTx.outputs.slice(0, 1), ); } return signedTx; @@ -446,6 +464,9 @@ export default class TransactionProcessor } this.cradle.logger.debug(`[TransactionProcessor] Transaction signed: ${JSON.stringify(signedTx)}`); + // append the spore cobuild witness to the transaction if needed + signedTx = await this.appendSporeCobuildWitnessIfNeed(signedTx, ckbVirtualResult.needPaymasterCell); + const txHash = await this.cradle.ckb.sendTransaction(signedTx); job.returnvalue = txHash; this.cradle.logger.info(`[TransactionProcessor] Transaction sent: ${txHash}`);