Skip to content

Commit

Permalink
fix: testnet network init problem
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu committed Sep 3, 2024
1 parent 123b847 commit 380e13d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 70 deletions.
67 changes: 36 additions & 31 deletions packages/demo/app/lib/ccc.client.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
import { ccc, CellDepInfoLike, KnownScript, Script } from "@ckb-ccc/ccc";
import offCKBConfig, { Network } from "offckb.config";

export const DEVNET_SCRIPTS: Record<
string,
Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfoLike[] }
> = {
[KnownScript.Secp256k1Blake160]:
offCKBConfig.systemScripts.secp256k1_blake160_sighash_all!.script,
[KnownScript.Secp256k1Multisig]:
offCKBConfig.systemScripts.secp256k1_blake160_multisig_all!.script,
[KnownScript.AnyoneCanPay]: offCKBConfig.systemScripts.anyone_can_pay!.script,
[KnownScript.OmniLock]: offCKBConfig.systemScripts.omnilock!.script,
[KnownScript.TypeId]: {
codeHash:
"0x00000000000000000000000000000000000000000000000000545950455f4944",
hashType: "type",
cellDeps: [],
},
[KnownScript.XUdt]: offCKBConfig.systemScripts.xudt!.script,
[KnownScript.NostrLock]: offCKBConfig.myScripts["nostr-lock"]!,
};

export function buildCccClient(network: Network) {
const client =
network === "mainnet"
? new ccc.ClientPublicMainnet()
: network === "testnet"
? new ccc.ClientPublicTestnet()
: new ccc.ClientPublicTestnet(
offCKBConfig.rpcUrl,
undefined,
DEVNET_SCRIPTS,
);
if (network === "devnet") {
const DEVNET_SCRIPTS: Record<
string,
Pick<Script, "codeHash" | "hashType"> & { cellDeps: CellDepInfoLike[] }
> = {
[KnownScript.Secp256k1Blake160]:
offCKBConfig.systemScripts.secp256k1_blake160_sighash_all!.script,
[KnownScript.Secp256k1Multisig]:
offCKBConfig.systemScripts.secp256k1_blake160_multisig_all!.script,
[KnownScript.AnyoneCanPay]:
offCKBConfig.systemScripts.anyone_can_pay!.script,
[KnownScript.OmniLock]: offCKBConfig.systemScripts.omnilock!.script,
[KnownScript.TypeId]: {
codeHash:
"0x00000000000000000000000000000000000000000000000000545950455f4944",
hashType: "type",
cellDeps: [],
},
[KnownScript.XUdt]: offCKBConfig.systemScripts.xudt!.script,
[KnownScript.NostrLock]: offCKBConfig.myScripts["nostr-lock"]!,
};
return new ccc.ClientPublicTestnet(
offCKBConfig.rpcUrl,
undefined,
DEVNET_SCRIPTS,
);
}

if (network === "testnet") {
return new ccc.ClientPublicTestnet();
}

if (network === "mainnet") {
return new ccc.ClientPublicMainnet();
}

return client;
throw new Error("invalid network");
}
77 changes: 38 additions & 39 deletions packages/demo/app/lib/sdk.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,48 @@ import {
} from "@nostr-binding/sdk";
import offCKBConfig, { readEnvNetwork } from "offckb.config";

export const DEVNET_CONFIGS: SDKConfig = {
prefix: offCKBConfig.addressPrefix,
rpcUrl: offCKBConfig.rpcUrl,
NOSTR_LOCK: {
CODE_HASH: offCKBConfig.myScripts["nostr-lock"]!.codeHash,
HASH_TYPE: offCKBConfig.myScripts["nostr-lock"]!.hashType,
TX_HASH:
offCKBConfig.myScripts["nostr-lock"]!.cellDeps[0].cellDep.outPoint.txHash,
INDEX:
"0x" +
offCKBConfig.myScripts[
"nostr-lock"
]!.cellDeps[0].cellDep.outPoint.index.toString(16),
DEP_TYPE:
offCKBConfig.myScripts["nostr-lock"]!.cellDeps[0].cellDep.depType ===
"code"
? "code"
: "depGroup",
},
NOSTR_BINDING: {
CODE_HASH: offCKBConfig.myScripts["nostr-binding"]!.codeHash,
HASH_TYPE: offCKBConfig.myScripts["nostr-binding"]!.hashType,
TX_HASH:
offCKBConfig.myScripts["nostr-binding"]!.cellDeps[0].cellDep.outPoint
.txHash,
INDEX:
"0x" +
offCKBConfig.myScripts[
"nostr-binding"
]!.cellDeps[0].cellDep.outPoint.index.toString(16),
DEP_TYPE:
offCKBConfig.myScripts["nostr-binding"]!.cellDeps[0].cellDep.depType ===
"code"
? "code"
: "depGroup",
},
};

const network = readEnvNetwork();

export const sdk = new NostrBindingSDK(
network === "devnet"
? DEVNET_CONFIGS
? ({
prefix: offCKBConfig.addressPrefix,
rpcUrl: offCKBConfig.rpcUrl,
NOSTR_LOCK: {
CODE_HASH: offCKBConfig.myScripts["nostr-lock"]!.codeHash,
HASH_TYPE: offCKBConfig.myScripts["nostr-lock"]!.hashType,
TX_HASH:
offCKBConfig.myScripts["nostr-lock"]!.cellDeps[0].cellDep.outPoint
.txHash,
INDEX:
"0x" +
offCKBConfig.myScripts[
"nostr-lock"
]!.cellDeps[0].cellDep.outPoint.index.toString(16),
DEP_TYPE:
offCKBConfig.myScripts["nostr-lock"]!.cellDeps[0].cellDep
.depType === "code"
? "code"
: "depGroup",
},
NOSTR_BINDING: {
CODE_HASH: offCKBConfig.myScripts["nostr-binding"]!.codeHash,
HASH_TYPE: offCKBConfig.myScripts["nostr-binding"]!.hashType,
TX_HASH:
offCKBConfig.myScripts["nostr-binding"]!.cellDeps[0].cellDep
.outPoint.txHash,
INDEX:
"0x" +
offCKBConfig.myScripts[
"nostr-binding"
]!.cellDeps[0].cellDep.outPoint.index.toString(16),
DEP_TYPE:
offCKBConfig.myScripts["nostr-binding"]!.cellDeps[0].cellDep
.depType === "code"
? "code"
: "depGroup",
},
} as SDKConfig)
: network === "testnet"
? TESTNET_CONFIGS
: MAINNET_CONFIGS,
Expand Down

0 comments on commit 380e13d

Please sign in to comment.