Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement getRgbppSpvProof for OfflineBtcAssetsDataSource #298

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/nervous-doors-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rgbpp-sdk/service': minor
---

Implement `getRgbppSpvProof` for `OfflineBtcAssetsDataSource`, enabling support for offline unlocking of BTC time lock cells.

- Add an example demonstrating how to unlock BTC time lock cells offline.
57 changes: 57 additions & 0 deletions examples/rgbpp/xudt/offline/5-unlock-btc-time-cell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { buildBtcTimeCellsSpentTx, signBtcTimeCellSpentTx } from 'rgbpp';
import { sendCkbTx, getBtcTimeLockScript, btcTxIdAndAfterFromBtcTimeLockArgs } from 'rgbpp/ckb';
import { BTC_TESTNET_TYPE, CKB_PRIVATE_KEY, btcService, ckbAddress, collector, isMainnet } from '../../env';
import { OfflineBtcAssetsDataSource, SpvProofEntry } from 'rgbpp/service';

const unlockBtcTimeCell = async ({ btcTimeCellArgs }: { btcTimeCellArgs: string }) => {
const btcTimeCells = await collector.getCells({
lock: {
...getBtcTimeLockScript(isMainnet, BTC_TESTNET_TYPE),
args: btcTimeCellArgs,
},
isDataMustBeEmpty: false,
});
if (!btcTimeCells || btcTimeCells.length === 0) {
throw new Error('No btc time cell found');
}

const spvProofs: SpvProofEntry[] = [];
for (const btcTimeCell of btcTimeCells) {
const { btcTxId, after } = btcTxIdAndAfterFromBtcTimeLockArgs(btcTimeCell.output.lock.args);
spvProofs.push({
txid: btcTxId,
confirmations: after,
proof: await btcService.getRgbppSpvProof(btcTxId, after),
});
}

const offlineBtcAssetsDataSource = new OfflineBtcAssetsDataSource({
txs: [],
utxos: [],
rgbppSpvProofs: spvProofs,
});

const ckbRawTx: CKBComponents.RawTransaction = await buildBtcTimeCellsSpentTx({
btcTimeCells,
btcAssetsApi: offlineBtcAssetsDataSource,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

const signedTx = await signBtcTimeCellSpentTx({
secp256k1PrivateKey: CKB_PRIVATE_KEY,
collector,
masterCkbAddress: ckbAddress,
ckbRawTx,
isMainnet,
});

const txHash = await sendCkbTx({ collector, signedTx });
console.info(`BTC time cell has been spent and CKB tx hash is ${txHash}`);
};

// The btcTimeCellArgs is from the outputs[0].lock.args(BTC Time lock args) of the 3-btc-leap-ckb.ts CKB transaction
unlockBtcTimeCell({
btcTimeCellArgs:
'0x7d00000010000000590000005d000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8011400000021e782eeb1c9893b341ed71c2dfe6fa496a6435c0600000045241651e435d786d0ba8a1280d4bb3283eca10db728e2ba0a3978c136f5bb19',
});
2 changes: 2 additions & 0 deletions packages/service/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum ErrorCodes {
ASSETS_API_RESPONSE_DECODE_ERROR,

OFFLINE_DATA_SOURCE_METHOD_NOT_AVAILABLE,
OFFLINE_DATA_SOURCE_SPV_PROOF_NOT_FOUND,
}

export const ErrorMessages = {
Expand All @@ -22,6 +23,7 @@ export const ErrorMessages = {
[ErrorCodes.ASSETS_API_RESPONSE_DECODE_ERROR]: 'Failed to decode the response of BtcAssetsAPI',

[ErrorCodes.OFFLINE_DATA_SOURCE_METHOD_NOT_AVAILABLE]: 'Method not available for offline data source',
[ErrorCodes.OFFLINE_DATA_SOURCE_SPV_PROOF_NOT_FOUND]: 'SPV proof not found for the given txid and confirmations',
};

export class BtcAssetsApiError extends Error {
Expand Down
37 changes: 33 additions & 4 deletions packages/service/src/service/offline-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ import {
RgbppApiRetryCkbTransactionPayload,
OfflineBtcUtxo,
BtcApiRecommendedFeeRates,
RgbppApiSpvProof,
} from '../types';

export interface OfflineBtcData {
txs: BtcApiTransaction[];
utxos: OfflineBtcUtxo[];
rgbppSpvProofs: SpvProofEntry[];
}

export interface SpvProofEntry {
txid: string;
confirmations: number;
proof: RgbppApiSpvProof;
}

/*
Expand All @@ -35,6 +43,8 @@ export class OfflineBtcAssetsDataSource extends BtcAssetsApi {
private txs: Record<string, BtcApiTransaction>;
// address -> utxos
private utxos: Record<string, OfflineBtcUtxo[]>;
// txid:confirmations -> spv proof
private rgbppSpvProofs: Record<string, RgbppApiSpvProof>;

private defaultFee = 1;

Expand All @@ -56,6 +66,18 @@ export class OfflineBtcAssetsDataSource extends BtcAssetsApi {
},
{} as Record<string, OfflineBtcUtxo[]>,
);

this.rgbppSpvProofs = offlineData.rgbppSpvProofs.reduce(
(acc, proof) => {
acc[this.spvKey(proof.txid, proof.confirmations)] = proof.proof;
return acc;
},
{} as Record<string, RgbppApiSpvProof>,
);
}

private spvKey(txid: string, confirmations: number) {
return `${txid}:${confirmations}`;
}

getBtcTransaction(txId: string): Promise<BtcApiTransaction> {
Expand Down Expand Up @@ -86,6 +108,17 @@ export class OfflineBtcAssetsDataSource extends BtcAssetsApi {
});
}

getRgbppSpvProof(btcTxId: string, confirmations: number) {
const proof = this.rgbppSpvProofs[this.spvKey(btcTxId, confirmations)];
if (!proof) {
throw new OfflineBtcAssetsDataSourceError(
ErrorCodes.OFFLINE_DATA_SOURCE_SPV_PROOF_NOT_FOUND,
`SPV proof not found for txid ${btcTxId} with ${confirmations} confirmations`,
);
}
return Promise.resolve(proof);
}

/*
* The following methods are not available in offline mode.
*/
Expand Down Expand Up @@ -160,10 +193,6 @@ export class OfflineBtcAssetsDataSource extends BtcAssetsApi {
return Promise.reject(new OfflineBtcAssetsDataSourceError(ErrorCodes.OFFLINE_DATA_SOURCE_METHOD_NOT_AVAILABLE));
}

getRgbppSpvProof(btcTxId: string, confirmations: number) {
return Promise.reject(new OfflineBtcAssetsDataSourceError(ErrorCodes.OFFLINE_DATA_SOURCE_METHOD_NOT_AVAILABLE));
}

sendRgbppCkbTransaction(payload: RgbppApiSendCkbTransactionPayload) {
return Promise.reject(new OfflineBtcAssetsDataSourceError(ErrorCodes.OFFLINE_DATA_SOURCE_METHOD_NOT_AVAILABLE));
}
Expand Down
Loading