Skip to content

Commit 72bcde7

Browse files
authored
feat(sdk): support mainnet scripts (#38)
* feat(sdk): support mainnet scripts * chore(sdk): update readme about cellDeps * fix: nostr-binding mainnet tx_hash * fix: pass rpcUrl to binding and lock
1 parent e52e61e commit 72bcde7

File tree

9 files changed

+131
-42
lines changed

9 files changed

+131
-42
lines changed

packages/demo/app/conmponents/connect-nostr.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function ConnectNostr() {
4444
setNostrPubkey(pubkey.toBech32());
4545

4646
if (!ckbSigner) {
47-
const ckbSigner = buildNostrCKBSigner(pubkey, signer);
47+
const ckbSigner = await buildNostrCKBSigner(pubkey, signer);
4848
setCKBSigner(ckbSigner);
4949

5050
const ckbAddress = ckbSigner.ckbAddress;
@@ -53,7 +53,7 @@ export function ConnectNostr() {
5353
}
5454
};
5555

56-
const buildNostrCKBSigner = (
56+
const buildNostrCKBSigner = async (
5757
publicKey: PublicKey,
5858
nostrSigner: NostrSigner,
5959
) => {
@@ -105,6 +105,7 @@ export function ConnectNostr() {
105105
const lockScript = sdk.lock.buildScript("0x" + publicKey.toHex());
106106
const ckbAddress = helpers.encodeToAddress(lockScript);
107107

108+
const cellDeps = await sdk.lock.buildCellDeps();
108109
const ckbSigner: CKBSigner = {
109110
ckbAddress,
110111
originAddress: publicKey.toBech32(),
@@ -113,7 +114,7 @@ export function ConnectNostr() {
113114
signTransaction,
114115
signPreparedTransaction,
115116
prepareTransaction,
116-
cellDeps: sdk.lock.buildCellDeps(),
117+
cellDeps,
117118
};
118119
return ckbSigner;
119120
};

packages/demo/app/conmponents/unlock-button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ export function UnlockButton({ setResult, assetEvent }: UnlockButtonProp) {
4242
bindingType,
4343
);
4444

45+
const cellDep = await sdk.binding.buildCellDeps();
4546
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) =>
46-
cellDeps.push(...sdk.binding.buildCellDeps()),
47+
cellDeps.push(...cellDep),
4748
);
4849
const tx = createTransactionFromSkeleton(txSkeleton);
4950
const { transaction, lockIndexes } = await ckbSigner.prepareTransaction(tx);

packages/demo/app/lib/ckb.client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function buildUnlockCKBTransaction(
4141
const capacity = helpers.minimalCellCapacity(output);
4242
output.cellOutput.capacity = BI.from(capacity).toHexString();
4343

44-
const txCellDeps = sdk.lock.buildCellDeps();
44+
const txCellDeps = await sdk.lock.buildCellDeps();
4545

4646
txSkeleton = txSkeleton.update("inputs", (inputs) =>
4747
inputs.push(...collectedInputs),
@@ -114,8 +114,11 @@ export async function buildMintTransaction(
114114
txSkeleton = txSkeleton.update("inputs", (inputs) =>
115115
inputs.push(...collectedInputs),
116116
);
117+
118+
const bindingDep = await sdk.binding.buildCellDeps();
119+
const lockDep = await sdk.lock.buildCellDeps();
117120
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) =>
118-
cellDeps.concat(sdk.binding.buildCellDeps(), sdk.lock.buildCellDeps()),
121+
cellDeps.concat(lockDep, bindingDep),
119122
);
120123
return { txSkeleton, mintEvent };
121124
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import {
2-
NostrBindingConfig,
2+
MAINNET_CONFIGS,
33
NostrBindingSDK,
4+
SDKConfig,
45
TESTNET_CONFIGS,
56
} from "@nostr-binding/sdk";
67
import offCKBConfig, { readEnvNetwork } from "offckb.config";
78

8-
export const DEVNET_CONFIGS: NostrBindingConfig = {
9+
export const DEVNET_CONFIGS: SDKConfig = {
910
prefix: offCKBConfig.lumosConfig.PREFIX as "ckt" | "ckb",
11+
rpcUrl: offCKBConfig.indexer.ckbIndexerUrl,
1012
NOSTR_LOCK: offCKBConfig.lumosConfig.SCRIPTS["NOSTR_LOCK"]!,
1113
NOSTR_BINDING: offCKBConfig.lumosConfig.SCRIPTS["NOSTR_BINDING"]!,
1214
};
1315

1416
const network = readEnvNetwork();
1517

1618
export const sdk = new NostrBindingSDK(
17-
network === "devnet" ? DEVNET_CONFIGS : TESTNET_CONFIGS,
19+
network === "devnet"
20+
? DEVNET_CONFIGS
21+
: network === "testnet"
22+
? TESTNET_CONFIGS
23+
: MAINNET_CONFIGS,
1824
);

packages/sdk/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ const {transaction, lockIndexes} = await sdk.lock.prepareTx(transaction: Transac
3838
// and then directly generate sigHashAll from the giving transaction, sign it and return
3939
// signed transaction. You need to call prepareTx before this function.
4040
const signedTx = await sdk.lock.signPreparedTx(transaction, lockIndexes, signer);
41+
42+
43+
//**** Get Nostr Scripts CellDeps ****//
44+
const lockCellDeps = await sdk.lock.buildCellDeps();
45+
const bindingCellDeps = await sdk.binding.buildCellDeps();
4146
```

packages/sdk/src/binding.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { Cell, CellDep, HexNumber, HexString, Input, Script, WitnessArgs, utils } from '@ckb-lumos/base';
22
import { bytesToJsonString } from './util';
33
import { bytes } from '@ckb-lumos/codec';
4-
import { TESTNET_CONFIGS } from './config';
5-
import { ScriptConfig } from '@ckb-lumos/config-manager';
4+
import { NostrScriptConfig, TESTNET_CONFIGS } from './config';
65
import { TagName } from './tag';
76
import { calcEventId, EventToBind, parseSignedEvent } from './event';
87
import { minimalCellCapacity } from '@ckb-lumos/helpers';
8+
import { RPC } from '@ckb-lumos/rpc';
99

1010
export class NostrBinding {
1111
readonly prefix: 'ckt' | 'ckb';
12-
readonly scriptConfig: ScriptConfig;
12+
readonly scriptConfig: NostrScriptConfig;
13+
rpc: RPC;
1314

14-
constructor(scriptConfig = TESTNET_CONFIGS.NOSTR_BINDING, prefix: 'ckt' | 'ckb' = 'ckt') {
15+
constructor(
16+
scriptConfig = TESTNET_CONFIGS.NOSTR_LOCK,
17+
prefix: 'ckt' | 'ckb' = 'ckt',
18+
rpcUrl = TESTNET_CONFIGS.rpcUrl,
19+
) {
1520
this.prefix = prefix;
1621
this.scriptConfig = scriptConfig;
22+
this.rpc = new RPC(rpcUrl);
1723
}
1824

1925
isBindingType(type: Script | undefined) {
@@ -86,15 +92,38 @@ export class NostrBinding {
8692
return typeId;
8793
}
8894

89-
buildCellDeps() {
90-
const cellDeps: CellDep[] = [];
91-
cellDeps.push({
92-
outPoint: {
93-
txHash: this.scriptConfig.TX_HASH,
94-
index: this.scriptConfig.INDEX,
95+
async buildCellDeps() {
96+
if (this.scriptConfig.HASH_TYPE === 'type' && this.scriptConfig.TYPE_SCRIPT) {
97+
// fetch newest info for type script
98+
const cells = await this.rpc.getCells(
99+
{ script: this.scriptConfig.TYPE_SCRIPT, scriptType: 'type' },
100+
'desc',
101+
BigInt(1),
102+
);
103+
if (cells.objects.length === 0) throw new Error('cells not found');
104+
105+
const cell = cells.objects[0];
106+
const cellDeps: CellDep[] = [
107+
{
108+
outPoint: {
109+
txHash: cell.outPoint.txHash,
110+
index: cell.outPoint.index,
111+
},
112+
depType: this.scriptConfig.DEP_TYPE,
113+
},
114+
];
115+
return cellDeps;
116+
}
117+
118+
const cellDeps: CellDep[] = [
119+
{
120+
outPoint: {
121+
txHash: this.scriptConfig.TX_HASH,
122+
index: this.scriptConfig.INDEX,
123+
},
124+
depType: this.scriptConfig.DEP_TYPE,
95125
},
96-
depType: this.scriptConfig.DEP_TYPE,
97-
});
126+
];
98127
return cellDeps;
99128
}
100129
}

packages/sdk/src/config.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,71 @@
1+
import { Script } from '@ckb-lumos/base';
12
import { ScriptConfig } from '@ckb-lumos/config-manager';
23

3-
export interface NostrBindingConfig {
4+
export interface NostrScriptConfig extends ScriptConfig {
5+
TYPE_SCRIPT?: Script;
6+
}
7+
8+
export interface SDKConfig {
49
prefix: 'ckt' | 'ckb';
510
rpcUrl: string;
6-
NOSTR_BINDING: ScriptConfig;
7-
NOSTR_LOCK: ScriptConfig;
11+
NOSTR_BINDING: NostrScriptConfig;
12+
NOSTR_LOCK: NostrScriptConfig;
813
}
914

10-
export const TESTNET_CONFIGS: NostrBindingConfig = {
15+
export const TESTNET_CONFIGS: SDKConfig = {
1116
prefix: 'ckt',
12-
rpcUrl: ' https://testnet.ckbapp.dev/rpc',
17+
rpcUrl: 'https://testnet.ckbapp.dev/rpc',
1318
NOSTR_LOCK: {
1419
CODE_HASH: '0x6ae5ee0cb887b2df5a9a18137315b9bdc55be8d52637b2de0624092d5f0c91d5',
1520
HASH_TYPE: 'type',
1621
TX_HASH: '0xa2a434dcdbe280b9ed75bb7d6c7d68186a842456aba0fc506657dc5ed7c01d68',
1722
INDEX: '0x0',
1823
DEP_TYPE: 'code',
24+
TYPE_SCRIPT: {
25+
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
26+
hashType: 'type',
27+
args: '0x8dc56c6f35f0c535e23ded1629b1f20535477a1b43e59f14617d11e32c50e0aa',
28+
},
1929
},
2030
NOSTR_BINDING: {
2131
CODE_HASH: '0x4105801324b70b3a1508ded8958aba66a6faf68cab26f863b4902b50dfb8b9ab',
2232
HASH_TYPE: 'type',
2333
TX_HASH: '0x0e3949fa8afbbdf6d4abdda0d12ac1206c8d05dd51ec490b7341586291db85a6',
2434
INDEX: '0x0',
2535
DEP_TYPE: 'code',
36+
TYPE_SCRIPT: {
37+
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
38+
hashType: 'type',
39+
args: '0x8f8ef331361e061eccf629b30586969340ba6c9fac051bacf2f811369af49f51',
40+
},
2641
},
2742
};
2843

29-
/**
30-
// todo: update
31-
export const MAINNET_CONFIGS: NostrBindingConfig = {
44+
export const MAINNET_CONFIGS: SDKConfig = {
3245
prefix: 'ckt',
46+
rpcUrl: 'https://mainnet.ckbapp.dev/rpc',
3347
NOSTR_LOCK: {
34-
CODE_HASH: '0x0a82c98ccd9fe5d1d8e07789664b3bd5f6c9e79885846b2e718f5954edb4776a',
48+
CODE_HASH: '0x641a89ad2f77721b803cd50d01351c1f308444072d5fa20088567196c0574c68',
3549
HASH_TYPE: 'type',
36-
TX_HASH: '0xac9071e7a25564c1bfab7884885547e1d9fd5505b5653adcaf2bf77f926fd6e3',
50+
TX_HASH: '0x1911208b136957d5f7c1708a8835edfe8ae1d02700d5cb2c3a6aacf4d5906306',
3751
INDEX: '0x0',
3852
DEP_TYPE: 'code',
53+
TYPE_SCRIPT: {
54+
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
55+
hashType: 'type',
56+
args: '0xfad8cb75eb0bb01718e2336002064568bc05887af107f74ed5dd501829e192f8',
57+
},
3958
},
4059
NOSTR_BINDING: {
41-
CODE_HASH: '0x09f2f1ceef2d59a368dc0b2be38494c8e11b0ec459f62cf7db74a645fbeabeee',
60+
CODE_HASH: '0xb56ea08c4b10b454ed3389bb0e504ecfc57dcfe3089a5030654525a2def2108e',
4261
HASH_TYPE: 'type',
43-
TX_HASH: '0x6be8a9b73326a9694f12d6670c74535463b3a508038cd5ddaa4ba7450fcad305',
62+
TX_HASH: '0xa3a63292ce23600faa508f2b2762d1a41d520eb4dd582a2dc544424ce06c3870',
4463
INDEX: '0x0',
4564
DEP_TYPE: 'code',
65+
TYPE_SCRIPT: {
66+
codeHash: '0x00000000000000000000000000000000000000000000000000545950455f4944',
67+
hashType: 'type',
68+
args: '0x1f2c9ac4a2a340fb13ff7e9b8511a35e448e4db54e42c44c14f358b4de2dc197',
69+
},
4670
},
4771
};
48-
*/

packages/sdk/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NostrBinding } from './binding';
22
import { NostrLock } from './lock';
3-
import { NostrBindingConfig } from './config';
3+
import { SDKConfig } from './config';
44

55
export * from './lock';
66
export * from './binding';
@@ -13,10 +13,10 @@ export class NostrBindingSDK {
1313
binding: NostrBinding;
1414
lock: NostrLock;
1515

16-
constructor(config?: NostrBindingConfig) {
16+
constructor(config?: SDKConfig) {
1717
if (config) {
18-
this.binding = new NostrBinding(config.NOSTR_BINDING, config.prefix);
19-
this.lock = new NostrLock(config.NOSTR_LOCK, config.prefix);
18+
this.binding = new NostrBinding(config.NOSTR_BINDING, config.prefix, config.rpcUrl);
19+
this.lock = new NostrLock(config.NOSTR_LOCK, config.prefix, config.rpcUrl);
2020
} else {
2121
this.binding = new NostrBinding();
2222
this.lock = new NostrLock();

packages/sdk/src/lock.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ScriptConfig } from '@ckb-lumos/config-manager';
2-
import { TESTNET_CONFIGS } from './config';
1+
import { NostrScriptConfig, TESTNET_CONFIGS } from './config';
32
import { TagName } from './tag';
43
import { bytesToJsonString, getTimestampNowSecs, jsonStringToBytes } from './util';
54
import { CellDep, HexString, utils, WitnessArgs, Script, blockchain, Transaction } from '@ckb-lumos/base';
@@ -17,7 +16,7 @@ export class NostrLock {
1716
readonly dummyCkbSigHashAll = '0x' + '00'.repeat(32);
1817

1918
readonly prefix: 'ckt' | 'ckb';
20-
readonly scriptConfig: ScriptConfig;
19+
readonly scriptConfig: NostrScriptConfig;
2120
rpc: RPC;
2221

2322
constructor(
@@ -108,7 +107,29 @@ export class NostrLock {
108107
return script.args.slice(4);
109108
}
110109

111-
buildCellDeps() {
110+
async buildCellDeps() {
111+
if (this.scriptConfig.HASH_TYPE === 'type' && this.scriptConfig.TYPE_SCRIPT) {
112+
// fetch newest info for type script
113+
const cells = await this.rpc.getCells(
114+
{ script: this.scriptConfig.TYPE_SCRIPT, scriptType: 'type' },
115+
'desc',
116+
BigInt(1),
117+
);
118+
if (cells.objects.length === 0) throw new Error('cells not found');
119+
120+
const cell = cells.objects[0];
121+
const cellDeps: CellDep[] = [
122+
{
123+
outPoint: {
124+
txHash: cell.outPoint.txHash,
125+
index: cell.outPoint.index,
126+
},
127+
depType: this.scriptConfig.DEP_TYPE,
128+
},
129+
];
130+
return cellDeps;
131+
}
132+
112133
const cellDeps: CellDep[] = [
113134
{
114135
outPoint: {

0 commit comments

Comments
 (0)