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(ckb|examples): Support BTC Signet and update examples #227

Merged
merged 16 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- develop
- feat/btc-signet

concurrency: ${{ github.workflow }}-${{ github.ref }}

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ devbox.json
devbox.lock
.envrc

# examples logs
examples/rgbpp/logs
9 changes: 7 additions & 2 deletions apps/service/.env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# testnet for CKB and BTC Testnet and mainnet for CKB and BTC Mainnet, the default value is testnet
NETWORK=testnet

# The Bitcoin Testnet type including Testnet3 and Signet, default value is Testnet3
# Testnet3: https://mempool.space/testnet
# Signet: https://mempool.space/signet
BTC_TESTNET_TYPE=Testnet3

# CKB node url which should be matched with NETWORK
CKB_RPC_URL=https://testnet.ckb.dev

# The BTC assets api url which should be matched with NETWORK
BTC_SERVICE_URL=https://btc-assets-api.testnet.mibao.pro

# The BTC assets api token which should be matched with IS_MAINNET
# The BTC assets api token which should be matched with NETWORK and BTC_TESTNET_TYPE
# To get an access token, please refer to https://github.com/ckb-cell/rgbpp-sdk/tree/develop/packages/service#get-an-access-token
BTC_SERVICE_TOKEN=

# The BTC assets api origin which should be matched with IS_MAINNET
# The BTC assets api origin which should be matched with NETWORK and BTC_TESTNET_TYPE
BTC_SERVICE_ORIGIN=https://btc-assets-api.testnet.mibao.pro
9 changes: 7 additions & 2 deletions apps/service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ Update the configuration values:
# testnet for CKB and BTC Testnet and mainnet for CKB and BTC Mainnet, the default value is testnet
NETWORK=testnet

# The Bitcoin Testnet type including Testnet3 and Signet, default value is Testnet3
# Testnet3: https://mempool.space/testnet
# Signet: https://mempool.space/signet
BTC_TESTNET_TYPE=Testnet3

# CKB node url which should be matched with NETWORK
CKB_RPC_URL=https://testnet.ckb.dev

# The BTC assets api url which should be matched with NETWORK
BTC_SERVICE_URL=https://btc-assets-api.testnet.mibao.pro

# The BTC assets api token which should be matched with IS_MAINNET
# The BTC assets api token which should be matched with NETWORK and BTC_TESTNET_TYPE
# To get an access token, please refer to https://github.com/ckb-cell/rgbpp-sdk/tree/develop/packages/service#get-an-access-token
BTC_SERVICE_TOKEN=

# The BTC assets api origin which should be matched with IS_MAINNET
# The BTC assets api origin which should be matched with NETWORK and BTC_TESTNET_TYPE
BTC_SERVICE_ORIGIN=https://btc-assets-api.testnet.mibao.pro
```

Expand Down
9 changes: 7 additions & 2 deletions apps/service/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import JsonRpcModule from './json-rpc/json-rpc.module';
import { RgbppModule } from './rgbpp/rgbpp.module';
import { AppService } from './app.service';
import { envSchema } from './env';
import { Collector } from 'rgbpp/ckb';
import { BTCTestnetType, Collector } from 'rgbpp/ckb';
import { BtcAssetsApi } from 'rgbpp';

@Global()
Expand All @@ -25,6 +25,11 @@ import { BtcAssetsApi } from 'rgbpp';
useFactory: (configService: ConfigService): boolean => configService.get('NETWORK') === 'mainnet',
inject: [ConfigService],
},
{
provide: 'BTC_TESTNET_TYPE',
useFactory: (configService: ConfigService): BTCTestnetType => configService.get('BTC_TESTNET_TYPE'),
inject: [ConfigService],
},
{
provide: 'COLLECTOR',
useFactory: (configService: ConfigService) => {
Expand All @@ -47,6 +52,6 @@ import { BtcAssetsApi } from 'rgbpp';
inject: [ConfigService],
},
],
exports: ['IS_MAINNET', 'COLLECTOR', 'BTC_ASSETS_API'],
exports: ['IS_MAINNET', 'COLLECTOR', 'BTC_ASSETS_API', 'BTC_TESTNET_TYPE'],
})
export class AppModule {}
1 change: 1 addition & 0 deletions apps/service/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const envSchema = z.object({
BTC_SERVICE_URL: z.string(),
BTC_SERVICE_TOKEN: z.string(),
BTC_SERVICE_ORIGIN: z.string(),
BTC_TESTNET_TYPE: z.string().default('Testnet3'),
});
4 changes: 3 additions & 1 deletion apps/service/src/rgbpp/rgbpp.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject } from '@nestjs/common';
import { RpcHandler, RpcMethodHandler } from 'src/json-rpc/json-rpc.decorators';
import { DataSource, NetworkType } from 'rgbpp/btc';
import { Collector, Hex, toCamelcase } from 'rgbpp/ckb';
import { BTCTestnetType, Collector, Hex, toCamelcase } from 'rgbpp/ckb';
import { RgbppTransferReq, RgbppCkbBtcTransaction, RgbppCkbTxBtcTxId, RgbppStateReq, RgbppCkbTxHashReq } from './types';
import { toSnakeCase } from 'src/utils/snake';
import { buildRgbppTransferTx } from 'rgbpp';
Expand All @@ -11,6 +11,7 @@ import { BtcAssetsApi } from 'rgbpp/service';
export class RgbppService {
constructor(
@Inject('IS_MAINNET') private isMainnet: boolean,
@Inject('BTC_TESTNET_TYPE') private btcTestnetType: BTCTestnetType,
@Inject('COLLECTOR') private collector: Collector,
@Inject('BTC_ASSETS_API') private btcAssetsApi: BtcAssetsApi,
) {}
Expand All @@ -32,6 +33,7 @@ export class RgbppService {
fromAddress: fromBtcAddress,
toAddress: toBtcAddress,
dataSource: btcDataSource,
testnetType: this.btcTestnetType,
},
isMainnet: this.isMainnet,
});
Expand Down
16 changes: 11 additions & 5 deletions examples/rgbpp/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ CKB_INDEXER_URL=https://testnet.ckb.dev/indexer

# BTC Variables

# The Bitcoin Testnet type including Testnet3 and Signet, default value is Testnet3
# Testnet3: https://mempool.space/testnet
# Signet: https://mempool.space/signet
BTC_TESTNET_TYPE=Testnet3

# The BTC private key whose format is 32bytes hex string without 0x prefix
# The Native Segwit P2WPKH address will be generated with the BTC private key
# Read more about P2WPKH in BIP141: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
BTC_PRIVATE_KEY=private-key

# The BTC address type to use, available options: P2WPKH or P2TR
# The Native Segwit P2WPKH address will be generated with the BTC private key as default
# Read more about P2WPKH in BIP141: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
BTC_ADDRESS_TYPE=P2WPKH

# The BTC assets api url which should be matched with IS_MAINNET
# The BTC assets api url which should be matched with IS_MAINNET and BTC_TESTNET_TYPE
VITE_BTC_SERVICE_URL=https://btc-assets-api.testnet.mibao.pro

# The BTC assets api token which should be matched with IS_MAINNET
# The BTC assets api token which should be matched with IS_MAINNET and BTC_TESTNET_TYPE
# To get an access token, please refer to https://github.com/ckb-cell/rgbpp-sdk/tree/develop/packages/service#get-an-access-token
VITE_BTC_SERVICE_TOKEN=

# The BTC assets api origin which should be matched with IS_MAINNET
# The BTC assets api origin which should be matched with IS_MAINNET and BTC_TESTNET_TYPE
VITE_BTC_SERVICE_ORIGIN=https://btc-test.app

29 changes: 18 additions & 11 deletions examples/rgbpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
- Spore directory: The examples for RGB++ Spore creation, transfer and leap

> [!TIP]
> All the parameters of the examples should be repalced with your own, including BTC private key, CKB private key, BTC Service origin, BTC Service token, BTC UTXO, xUDT type args, Spore type args, etc. Please confirm whether the parameters are correct according to the code comments
> All the parameters of the examples should be repalced with your own, including BTC private key, CKB private key, BTC Service origin, BTC Service token, BTC UTXO, xUDT type args, Spore type args, etc.

> Please confirm whether the parameters are correct according to the code comments

## How to Start

Expand Down Expand Up @@ -40,12 +42,17 @@ CKB_INDEXER_URL=https://testnet.ckb.dev/indexer

# BTC Variables

# The Bitcoin Testnet type including Testnet3 and Signet, default value is Testnet3
# Testnet3: https://mempool.space/testnet
# Signet: https://mempool.space/signet
BTC_TESTNET_TYPE=Testnet3

# The BTC private key whose format is 32bytes hex string without 0x prefix
# The Native Segwit P2WPKH address will be generated with the BTC private key
# Read more about P2WPKH in BIP141: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
BTC_PRIVATE_KEY=private-key

# The BTC address type to use, available options: P2WPKH or P2TR
# The Native Segwit P2WPKH address will be generated with the BTC private key as default
# Read more about P2WPKH in BIP141: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
BTC_ADDRESS_TYPE=P2WPKH

# The BTC assets api url which should be matched with IS_MAINNET
Expand All @@ -66,7 +73,7 @@ VITE_BTC_SERVICE_ORIGIN=https://btc-test.app
#### 1. Prepare Launch

> [!TIP]
> Please make sure the CKB private key in the .env is correct
> Please make sure the CKB private key in the .env is correct.

```shell
# Create a CKB empty rgbpp lock cell to launch RGB++ xUDT assets later
Expand All @@ -75,7 +82,7 @@ npx ts-node xudt/launch/1-prepare-launch.ts
#### 2. Launch RGB++ xUDT on BTC

> [!TIP]
> Please make sure the `1-prepare-launch.ts` has been run and the corresponding CKB transaction has been committed
> Please make sure the `1-prepare-launch.ts` has been run and the corresponding CKB transaction has been committed.

```shell
npx ts-node xudt/launch/2-launch-rgbpp.ts
Expand All @@ -86,8 +93,8 @@ When the command is executed successfully, the **RGB++ Asset type script args**
#### 3. Distribute RGB++ xUDT on BTC

> [!TIP]
> Please make sure the `2-launch-rgbpp.ts` has been run and the corresponding BTC and CKB transactions have been committed
> The **RGB++ Asset type script args** in the above should be set to the `xudtTypeArgs`
> Please make sure the `2-launch-rgbpp.ts` has been run and the corresponding BTC and CKB transactions have been committed.
> The **RGB++ Asset type script args** in the above should be set to the `xudtTypeArgs`.

```shell
npx ts-node xudt/launch/3-distribute-rgbpp.ts
Expand Down Expand Up @@ -132,8 +139,8 @@ npx ts-node xudt/4-unlock-btc-time.ts
#### 1. Create RGB++ Cluster Cell

> [!TIP]
> Please make sure all the variables in the .env are correct
> The BTC UTXO of `1-prepare-cluster.ts` and `2-create-cluster.ts` should be same
> Please make sure all the variables in the .env are correct.
> The BTC UTXO of `1-prepare-cluster.ts` and `2-create-cluster.ts` should be same.

```shell
# Create a CKB empty rgbpp lock cell to create cluster later
Expand All @@ -148,8 +155,8 @@ When the commands are executed successfully, the **clusterId** and **cluster rgb
#### 2. Create RGB++ Spores with Cluster on BTC

> [!TIP]
> Please make sure the `2-create-cluster.ts` has been run and the corresponding BTC and CKB transactions have been committed
> The **clusterId** in the above should be set to the `clusterId` and the **cluster rgbpp lock args** should be set to the `clusterRgbppLockArgs`
> Please make sure the `2-create-cluster.ts` has been run and the corresponding BTC and CKB transactions have been committed.
> The **clusterId** in the above should be set to the `clusterId` and the **cluster rgbpp lock args** should be set to the `clusterRgbppLockArgs`.

```shell
npx ts-node spore/launch/3-create-spores.ts
Expand Down
3 changes: 2 additions & 1 deletion examples/rgbpp/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@nervosnetwork/ckb-sdk-utils';
import { NetworkType, AddressType, DataSource } from 'rgbpp/btc';
import { BtcAssetsApi } from 'rgbpp/service';
import { Collector } from 'rgbpp/ckb';
import { BTCTestnetType, Collector } from 'rgbpp/ckb';
import { createBtcAccount } from './shared/btc-account';

dotenv.config({ path: __dirname + '/.env' });
Expand Down Expand Up @@ -39,6 +39,7 @@ export const ckbAddress = scriptToAddress(secp256k1Lock, isMainnet);
*/

export const BTC_PRIVATE_KEY = process.env.BTC_PRIVATE_KEY!;
export const BTC_TESTNET_TYPE = process.env.BTC_TESTNET_TYPE! as BTCTestnetType;
export const BTC_SERVICE_URL = process.env.VITE_BTC_SERVICE_URL!;
export const BTC_SERVICE_TOKEN = process.env.VITE_BTC_SERVICE_TOKEN!;
export const BTC_SERVICE_ORIGIN = process.env.VITE_BTC_SERVICE_ORIGIN!;
Expand Down
2 changes: 1 addition & 1 deletion examples/rgbpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"rgbpp": "^0.3.0"
"rgbpp": "workspace:*"
},
"devDependencies": {
"@types/node": "^20.11.28",
Expand Down
6 changes: 5 additions & 1 deletion examples/rgbpp/spore/4-transfer-spore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { buildRgbppLockArgs } from 'rgbpp/ckb';
import { genTransferSporeCkbVirtualTx, sendRgbppUtxos } from 'rgbpp';
import { getSporeTypeScript, Hex } from 'rgbpp/ckb';
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils';
import { isMainnet, collector, btcDataSource, btcService, btcAccount } from '../env';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../env';
import { saveCkbVirtualTxResult } from '../shared/utils';
import { signAndSendPsbt } from '../shared/btc-account';

Expand All @@ -23,6 +23,7 @@ const transferSpore = async ({ sporeRgbppLockArgs, toBtcAddress, sporeTypeArgs }
sporeRgbppLockArgs,
sporeTypeBytes,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

// Save ckbVirtualTxResult
Expand Down Expand Up @@ -68,6 +69,9 @@ const transferSpore = async ({ sporeRgbppLockArgs, toBtcAddress, sporeTypeArgs }
};

// Please use your real BTC UTXO information on the BTC Testnet
// BTC Testnet3: https://mempool.space/testnet
// BTC Signet: https://mempool.space/signet

// rgbppLockArgs: outIndexU32 + btcTxId
transferSpore({
sporeRgbppLockArgs: buildRgbppLockArgs(2, 'd5868dbde4be5e49876b496449df10150c356843afb6f94b08f8d81f394bb350'),
Expand Down
6 changes: 5 additions & 1 deletion examples/rgbpp/spore/5-leap-spore-to-ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { buildRgbppLockArgs } from 'rgbpp/ckb';
import { genLeapSporeFromBtcToCkbVirtualTx, sendRgbppUtxos } from 'rgbpp';
import { getSporeTypeScript, Hex } from 'rgbpp/ckb';
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils';
import { isMainnet, collector, btcDataSource, btcService, btcAccount } from '../env';
import { isMainnet, collector, btcDataSource, btcService, btcAccount, BTC_TESTNET_TYPE } from '../env';
import { saveCkbVirtualTxResult } from '../shared/utils';
import { signAndSendPsbt } from '../shared/btc-account';

Expand All @@ -24,6 +24,7 @@ const leapSporeFromBtcToCkb = async ({ sporeRgbppLockArgs, toCkbAddress, sporeTy
sporeTypeBytes,
toCkbAddress,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

// Save ckbVirtualTxResult
Expand Down Expand Up @@ -69,6 +70,9 @@ const leapSporeFromBtcToCkb = async ({ sporeRgbppLockArgs, toCkbAddress, sporeTy
};

// Please use your real BTC UTXO information on the BTC Testnet
// BTC Testnet3: https://mempool.space/testnet
// BTC Signet: https://mempool.space/signet

// rgbppLockArgs: outIndexU32 + btcTxId
leapSporeFromBtcToCkb({
sporeRgbppLockArgs: buildRgbppLockArgs(3, 'd8a31796fbd42c546f6b22014b9b82b16586ce1df81b0e7ca9a552cdc492a0af'),
Expand Down
5 changes: 3 additions & 2 deletions examples/rgbpp/spore/6-unlock-btc-time-cell.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { buildSporeBtcTimeCellsSpentTx, signBtcTimeCellSpentTx } from 'rgbpp';
import { CKB_PRIVATE_KEY, btcService, ckbAddress, collector, isMainnet } from '../env';
import { BTC_TESTNET_TYPE, CKB_PRIVATE_KEY, btcService, ckbAddress, collector, isMainnet } from '../env';
import { sendCkbTx, getBtcTimeLockScript } from 'rgbpp/ckb';

// Warning: Wait at least 6 BTC confirmation blocks to spend the BTC time cells after 5-leap-spore-to-ckb.ts
const unlockSporeBtcTimeCell = async ({ btcTimeCellArgs }: { btcTimeCellArgs: string }) => {
const btcTimeCells = await collector.getCells({
lock: {
...getBtcTimeLockScript(false),
...getBtcTimeLockScript(isMainnet, BTC_TESTNET_TYPE),
args: btcTimeCellArgs,
},
isDataMustBeEmpty: false,
Expand All @@ -20,6 +20,7 @@ const unlockSporeBtcTimeCell = async ({ btcTimeCellArgs }: { btcTimeCellArgs: st
btcTimeCells,
btcAssetsApi: btcService,
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

const signedTx = await signBtcTimeCellSpentTx({
Expand Down
5 changes: 4 additions & 1 deletion examples/rgbpp/spore/7-leap-spore-to-btc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serializeScript } from '@nervosnetwork/ckb-sdk-utils';
import { genLeapSporeFromCkbToBtcRawTx } from 'rgbpp';
import { isMainnet, collector, ckbAddress, CKB_PRIVATE_KEY } from '../env';
import { isMainnet, collector, ckbAddress, CKB_PRIVATE_KEY, BTC_TESTNET_TYPE } from '../env';
import { buildRgbppLockArgs, getSecp256k1CellDep, getSporeTypeScript } from 'rgbpp/ckb';

const leapSporeFromCkbToBtc = async ({
Expand All @@ -25,6 +25,7 @@ const leapSporeFromCkbToBtc = async ({
toRgbppLockArgs,
sporeTypeBytes: serializeScript(sporeType),
isMainnet,
btcTestnetType: BTC_TESTNET_TYPE,
});

const emptyWitness = { lock: '', inputType: '', outputType: '' };
Expand All @@ -41,6 +42,8 @@ const leapSporeFromCkbToBtc = async ({
};

// Please use your real BTC UTXO information on the BTC Testnet
// BTC Testnet3: https://mempool.space/testnet
// BTC Signet: https://mempool.space/signet
leapSporeFromCkbToBtc({
outIndex: 1,
btcTxId: '448897515cf07b4ca0cd38af9806399ede55775b4c760b274ed2322121ed185f',
Expand Down
4 changes: 2 additions & 2 deletions examples/rgbpp/spore/launch/1-prepare-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
genRgbppLockScript,
getSecp256k1CellDep,
} from 'rgbpp/ckb';
import { ckbAddress, isMainnet, collector, CKB_PRIVATE_KEY } from '../../env';
import { ckbAddress, isMainnet, collector, CKB_PRIVATE_KEY, BTC_TESTNET_TYPE } from '../../env';
import { CLUSTER_DATA } from './0-cluster-info';

const prepareClusterCell = async ({ outIndex, btcTxId }: { outIndex: number; btcTxId: string }) => {
Expand All @@ -33,7 +33,7 @@ const prepareClusterCell = async ({ outIndex, btcTxId }: { outIndex: number; btc

const outputs: CKBComponents.CellOutput[] = [
{
lock: genRgbppLockScript(buildRgbppLockArgs(outIndex, btcTxId), isMainnet),
lock: genRgbppLockScript(buildRgbppLockArgs(outIndex, btcTxId), isMainnet, BTC_TESTNET_TYPE),
capacity: append0x(clusterCellCapacity.toString(16)),
},
];
Expand Down
Loading
Loading