Skip to content

Commit

Permalink
Merge pull request #27 from ckb-cell/fix/lock-args-txid
Browse files Browse the repository at this point in the history
fix(rpbpp-sdk/ckb): Replace lock args placeholder with the real BTC tx id
  • Loading branch information
duanyytop authored Mar 18, 2024
2 parents 1ce5dc3 + fe28a39 commit d8311cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
20 changes: 15 additions & 5 deletions packages/ckb/src/rgbpp/ckb-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import {
serializeWitnessArgs,
} from '@nervosnetwork/ckb-sdk-utils';
import {
AppendBtcTxIdToLockArgsParams,
UpdateCkbTxWithRealBtcTxIdParams,
AppendPaymasterCellAndSignTxParams,
AppendWitnessesParams,
Hex,
SendCkbTxParams,
} from '../types';
import { RGBPP_WITNESS_PLACEHOLDER, SECP256K1_WITNESS_LOCK_SIZE, getRgbppLockScript } from '../constants';
import { append0x, calculateTransactionFee, isRgbppLockOrBtcTimeLock, remove0x, u8ToHex } from '../utils';
import {
append0x,
calculateTransactionFee,
isRgbppLockOrBtcTimeLock,
replaceLockArgsWithRealBtcTxId,
u8ToHex,
} from '../utils';
import { InputsCapacityNotEnoughError } from '../error';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses';
import { buildSpvClientCellDep } from '../spv';
Expand Down Expand Up @@ -150,19 +156,23 @@ export const sendCkbTx = async ({ collector, signedTx }: SendCkbTxParams) => {
};

/**
* Append BTC transaction id to the rgbpp lock args and BTC time lock args
* Replace the RGBPP_TX_ID_PLACEHOLDER with the real btc tx id of the rgbpp lock args and BTC time lock args
* @param ckbRawTx CKB raw transaction
* @param btcTxId The BTC transaction id
* @param isMainnet
*/
export const appendBtcTxIdToLockArgs = ({ ckbRawTx, btcTxId, isMainnet }: AppendBtcTxIdToLockArgsParams) => {
export const updateCkbTxWithRealBtcTxId = ({
ckbRawTx,
btcTxId,
isMainnet,
}: UpdateCkbTxWithRealBtcTxIdParams): CKBComponents.RawTransaction => {
const outputs = ckbRawTx.outputs
.filter((output) => isRgbppLockOrBtcTimeLock(output.lock, isMainnet))
.map((output) => ({
...output,
lock: {
...output.lock,
args: `${output.lock.args}${remove0x(btcTxId)}`,
args: replaceLockArgsWithRealBtcTxId(output.lock.args, btcTxId),
},
}));
const newRawTx: CKBComponents.RawTransaction = {
Expand Down
2 changes: 1 addition & 1 deletion packages/ckb/src/types/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export interface CkbJumpBtcVirtualTxParams {
witnessLockPlaceholderSize?: number;
}

export interface AppendBtcTxIdToLockArgsParams {
export interface UpdateCkbTxWithRealBtcTxIdParams {
// CKB raw transaction
ckbRawTx: CKBComponents.RawTransaction;
// The BTC transaction id
Expand Down
17 changes: 16 additions & 1 deletion packages/ckb/src/utils/rgbpp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
calculateCommitment,
genBtcTimeLockScript,
lockScriptFromBtcTimeLockArgs,
replaceLockArgsWithRealBtcTxId,
} from './rgbpp';
import { RgbppCkbVirtualTx } from '../types';
import { calculateUdtCellCapacity } from './ckb-tx';
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('rgbpp tests', () => {
};
const lock = genBtcTimeLockScript(toLock, false);
expect(lock.args).toBe(
'0x5500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a50020000000c0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f06000000',
'0x5500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a50020000000c0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f060000000000000000000000000000000000000000000000000000000000000000000000',
);
});

Expand Down Expand Up @@ -111,4 +112,18 @@ describe('rgbpp tests', () => {
const lockArgs = buildPreLockArgs('02000000');
expect('020000000000000000000000000000000000000000000000000000000000000000000000').toBe(lockArgs);
});

it('replaceRealBtcTxId', async () => {
const rgbppLockArgs = '0x020000000000000000000000000000000000000000000000000000000000000000000000';
const realBtcTxId = '0x06ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3f008906c849399';
const lockArgs = replaceLockArgsWithRealBtcTxId(rgbppLockArgs, realBtcTxId);
expect('0x0200000006ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3f008906c849399').toBe(lockArgs);

const btcTimeLockArgs =
'0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000e616d1460d634668b8ad81971c3a53e705f51e60060000000000000000000000000000000000000000000000000000000000000000000000';
const args = replaceLockArgsWithRealBtcTxId(btcTimeLockArgs, realBtcTxId);
expect(
'0x490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce80114000000e616d1460d634668b8ad81971c3a53e705f51e600600000006ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3f008906c849399',
).toBe(args);
});
});
14 changes: 13 additions & 1 deletion packages/ckb/src/utils/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const genRgbppLockScript = (rgbppLockArgs: Hex, isMainnet: boolean) => {
};

export const genBtcTimeLockScript = (toLock: CKBComponents.Script, isMainnet: boolean) => {
const lockArgs = `${append0x(serializeScript(toLock))}${u32ToLe(BTC_JUMP_CONFIRMATION_BLOCKS)}`;
const lockArgs = `${append0x(serializeScript(toLock))}${u32ToLe(BTC_JUMP_CONFIRMATION_BLOCKS)}${RGBPP_TX_ID_PLACEHOLDER}`;
return {
...getRgbppLockScript(isMainnet),
args: lockArgs,
Expand Down Expand Up @@ -95,3 +95,15 @@ export const compareInputs = (a: IndexerCell, b: IndexerCell) => {
}
return 0;
};

// RGBPP lock args: out_index | bitcoin_tx_id
// BTC time lock args: lock_script | after | bitcoin_tx_id
const RGBPP_MIN_LOCK_ARGS_SIZE = 36 * 2;
const BTC_TX_ID_SIZE = 32 * 2;
export const replaceLockArgsWithRealBtcTxId = (lockArgs: Hex, txId: Hex): Hex => {
const argsLength = remove0x(lockArgs).length;
if (argsLength < RGBPP_MIN_LOCK_ARGS_SIZE) {
throw new Error('Rgbpp lock args or BTC time lock args length is invalid');
}
return `0x${remove0x(lockArgs).substring(0, argsLength - BTC_TX_ID_SIZE)}${remove0x(txId)}`;
};

1 comment on commit d8311cc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[{"name":"@rgbpp-sdk/btc","version":"0.0.0-snap-20240318031112"},{"name":"@rgbpp-sdk/ckb","version":"0.0.0-snap-20240318031112"}]

Please sign in to comment.