Skip to content

Commit

Permalink
feat(transaction): append spore cobuild witness when process spore tr…
Browse files Browse the repository at this point in the history
…ansfer tx
  • Loading branch information
ahonn committed Apr 24, 2024
1 parent 61f61de commit 5e28e36
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"@fastify/swagger-ui": "^3.0.0",
"@immobiliarelabs/fastify-sentry": "^8.0.1",
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@rgbpp-sdk/btc": "0.0.0-snap-20240423032314",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240423032314",
"@rgbpp-sdk/service": "0.0.0-snap-20240423032314",
"@rgbpp-sdk/btc": "0.0.0-snap-20240423144119",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240423144119",
"@rgbpp-sdk/service": "0.0.0-snap-20240423144119",
"@sentry/node": "^7.102.1",
"@sentry/profiling-node": "^7.102.1",
"awilix": "^10.0.1",
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion src/services/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
RGBPPLock,
RGBPP_TX_ID_PLACEHOLDER,
appendCkbTxWitnesses,
generateSporeTransferCoBuild,
getBtcTimeLockScript,
getRgbppLockScript,
getSecp256k1CellDep,
getSporeTypeDep,
isClusterSporeTypeSupported,
updateCkbTxWithRealBtcTxId,
} from '@rgbpp-sdk/ckb';
import {
Expand All @@ -30,6 +33,7 @@ import { BI } from '@ckb-lumos/lumos';
import { CKBRpcError, CKBRPCErrorCodes } from './ckb';
import { cloneDeep } from 'lodash';
import { JwtPayload } from '../plugins/jwt';
import { serializeCellDep } from '@nervosnetwork/ckb-sdk-utils';

export interface ITransactionRequest {
txid: string;
Expand Down Expand Up @@ -314,11 +318,42 @@ export default class TransactionManager implements ITransactionManager {
]);
// using for spv proof, we need to remove the witness data from the transaction
const hexWithoutWitness = transactionToHex(BitcoinTransaction.fromHex(hex), false);
const signedTx = await appendCkbTxWitnesses({
let signedTx = await appendCkbTxWitnesses({
ckbRawTx,
btcTxBytes: hexWithoutWitness,
rgbppApiSpvProof,
})!;

// append the spore cobuild witness to the transaction if the cellDeps contains spore type dep
const sporeTypeDep = getSporeTypeDep(this.isMainnet);
const hasSporeTypeDep = signedTx.cellDeps.some((cellDep) => {
return serializeCellDep(cellDep) === serializeCellDep(sporeTypeDep);
});
if (hasSporeTypeDep) {
signedTx = await this.appendSporeCobuildWitness(signedTx);
}

return signedTx;
}

/**
* Append the spore cobuild witness to the transaction if the input contains spore cell
* (support spore transfer only for now, will support more in the future)
* @param signedTx - the signed CKB transaction
*/
private async appendSporeCobuildWitness(signedTx: CKBRawTransaction) {
const inputs = await Promise.all(
signedTx.inputs.map(async (input) => {
return this.cradle.ckb.rpc.getLiveCell(input.previousOutput!, false);
}),
);
const sporeLiveCell = inputs.find(({ cell }) => {
return cell?.output.type && isClusterSporeTypeSupported(cell?.output.type, this.isMainnet);
});
if (sporeLiveCell?.cell) {
const [output] = signedTx.outputs;
signedTx.witnesses[signedTx.witnesses.length - 1] = generateSporeTransferCoBuild(sporeLiveCell.cell, output);
}
return signedTx;
}

Expand Down

0 comments on commit 5e28e36

Please sign in to comment.