|
1 | 1 | import { Cell, CellDep, HexNumber, HexString, Input, Script, WitnessArgs, utils } from '@ckb-lumos/base'; |
2 | 2 | import { bytesToJsonString } from './util'; |
3 | 3 | 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'; |
6 | 5 | import { TagName } from './tag'; |
7 | 6 | import { calcEventId, EventToBind, parseSignedEvent } from './event'; |
8 | 7 | import { minimalCellCapacity } from '@ckb-lumos/helpers'; |
| 8 | +import { RPC } from '@ckb-lumos/rpc'; |
9 | 9 |
|
10 | 10 | export class NostrBinding { |
11 | 11 | readonly prefix: 'ckt' | 'ckb'; |
12 | | - readonly scriptConfig: ScriptConfig; |
| 12 | + readonly scriptConfig: NostrScriptConfig; |
| 13 | + rpc: RPC; |
13 | 14 |
|
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 | + ) { |
15 | 20 | this.prefix = prefix; |
16 | 21 | this.scriptConfig = scriptConfig; |
| 22 | + this.rpc = new RPC(rpcUrl); |
17 | 23 | } |
18 | 24 |
|
19 | 25 | isBindingType(type: Script | undefined) { |
@@ -86,15 +92,38 @@ export class NostrBinding { |
86 | 92 | return typeId; |
87 | 93 | } |
88 | 94 |
|
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, |
95 | 125 | }, |
96 | | - depType: this.scriptConfig.DEP_TYPE, |
97 | | - }); |
| 126 | + ]; |
98 | 127 | return cellDeps; |
99 | 128 | } |
100 | 129 | } |
0 commit comments