|
| 1 | +import { Block, Transaction, utils } from "@ckb-lumos/base"; |
| 2 | +import { ScriptConfig } from "./types"; |
| 3 | + |
| 4 | +// https://github.com/nervosnetwork/ckb-sdk-rust/blob/94ce4379454cdaf046f64b346e18e73e029f0ae6/src/constants.rs#L19C1-L24C62 |
| 5 | +// the index of a transaction in a block |
| 6 | +type TransactionIndex = number; |
| 7 | +// the index of an output in a transaction |
| 8 | +type OutputIndex = number; |
| 9 | +export const SIGHASH_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 1]; |
| 10 | +export const MULTISIG_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 4]; |
| 11 | +export const DAO_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [0, 2]; |
| 12 | +export const SIGHASH_GROUP_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [1, 0]; |
| 13 | +// prettier-ignore |
| 14 | +export const MULTISIG_GROUP_OUTPUT_LOC: [TransactionIndex, OutputIndex] = [1, 1]; |
| 15 | + |
| 16 | +/** |
| 17 | + * Generate {@link ScriptConfig} for the genesis block, |
| 18 | + * use this function when you are on a testnet, |
| 19 | + * or you cannot determine which network you are on |
| 20 | + * @example |
| 21 | + * const rpc = new RPC('http://localhost:8114') |
| 22 | + * const genesisBlock = await rpc.getBlockByNumber('0x0') |
| 23 | + * const scriptConfig = generateGenesisScriptConfigs(genesisBlock) |
| 24 | + * @param genesisBlock |
| 25 | + */ |
| 26 | +export function generateGenesisScriptConfigs( |
| 27 | + genesisBlock: Block |
| 28 | +): Record< |
| 29 | + "SECP256K1_BLAKE160" | "SECP256K1_BLAKE160_MULTISIG" | "DAO", |
| 30 | + ScriptConfig |
| 31 | +> { |
| 32 | + if (!genesisBlock || Number(genesisBlock.header.number) !== 0) { |
| 33 | + throw new Error("The block must be a genesis block"); |
| 34 | + } |
| 35 | + |
| 36 | + const transactions = genesisBlock.transactions; |
| 37 | + |
| 38 | + return { |
| 39 | + SECP256K1_BLAKE160: { |
| 40 | + ...createScriptConfig({ |
| 41 | + transaction: transactions[SIGHASH_OUTPUT_LOC[0]], |
| 42 | + outputIndex: SIGHASH_OUTPUT_LOC[1], |
| 43 | + depGroupTransaction: transactions[SIGHASH_GROUP_OUTPUT_LOC[0]], |
| 44 | + depGroupOutputIndex: SIGHASH_GROUP_OUTPUT_LOC[1], |
| 45 | + }), |
| 46 | + SHORT_ID: 0, |
| 47 | + }, |
| 48 | + SECP256K1_BLAKE160_MULTISIG: { |
| 49 | + ...createScriptConfig({ |
| 50 | + transaction: transactions[MULTISIG_OUTPUT_LOC[0]], |
| 51 | + outputIndex: MULTISIG_OUTPUT_LOC[1], |
| 52 | + depGroupTransaction: transactions[MULTISIG_GROUP_OUTPUT_LOC[0]], |
| 53 | + depGroupOutputIndex: MULTISIG_GROUP_OUTPUT_LOC[1], |
| 54 | + }), |
| 55 | + SHORT_ID: 1, |
| 56 | + }, |
| 57 | + DAO: createScriptConfig({ |
| 58 | + transaction: transactions[DAO_OUTPUT_LOC[0]], |
| 59 | + outputIndex: DAO_OUTPUT_LOC[1], |
| 60 | + }), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +type ScriptConfigOptions = LocByCode | LocByDepGroup; |
| 65 | + |
| 66 | +type LocByCode = { |
| 67 | + transaction: Transaction; |
| 68 | + outputIndex: number; |
| 69 | +}; |
| 70 | +type LocByDepGroup = { |
| 71 | + transaction: Transaction; |
| 72 | + outputIndex: number; |
| 73 | + depGroupTransaction: Transaction; |
| 74 | + depGroupOutputIndex: number; |
| 75 | +}; |
| 76 | + |
| 77 | +function createScriptConfig(config: ScriptConfigOptions): ScriptConfig { |
| 78 | + const { transaction, outputIndex } = config; |
| 79 | + |
| 80 | + const codeHash = utils.computeScriptHash( |
| 81 | + mustGenesisBlock(transaction.outputs[outputIndex]?.type) |
| 82 | + ); |
| 83 | + |
| 84 | + if ("depGroupTransaction" in config) { |
| 85 | + const { depGroupOutputIndex, depGroupTransaction } = config; |
| 86 | + |
| 87 | + return { |
| 88 | + HASH_TYPE: "type", |
| 89 | + CODE_HASH: codeHash, |
| 90 | + |
| 91 | + DEP_TYPE: "depGroup", |
| 92 | + TX_HASH: mustGenesisBlock(depGroupTransaction.hash), |
| 93 | + INDEX: toHexNumber(depGroupOutputIndex), |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + HASH_TYPE: "type", |
| 99 | + CODE_HASH: codeHash, |
| 100 | + |
| 101 | + DEP_TYPE: "code", |
| 102 | + INDEX: toHexNumber(outputIndex), |
| 103 | + TX_HASH: mustGenesisBlock(transaction.hash), |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +function mustGenesisBlock<T>(x: T): NonNullable<T> { |
| 108 | + if (x == null) { |
| 109 | + throw new Error("The block must be a genesis block"); |
| 110 | + } |
| 111 | + return x; |
| 112 | +} |
| 113 | + |
| 114 | +function toHexNumber(number: number): string { |
| 115 | + return `0x${number.toString(16)}`; |
| 116 | +} |
2 commit comments
vercel[bot] commentedon Aug 16, 2023
Successfully deployed to the following URLs:
lumos-website – ./
lumos-website-magickbase.vercel.app
lumos-website-git-develop-magickbase.vercel.app
lumos-website.vercel.app
github-actions[bot] commentedon Aug 16, 2023
🚀 New canary release:
0.0.0-canary-1cb43fe-20230816060512