-
Notifications
You must be signed in to change notification settings - Fork 11
/
chain.ts
49 lines (44 loc) · 1.67 KB
/
chain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { RPC, config, utils } from '@ckb-lumos/lumos'
// Special cells in genesis transactions: (transaction-index, output-index)
const SpecialCellLocation: {
[name: string]: [number, number]
} = {
SIGHASH: [0, 1],
MULTISIG: [0, 4],
DAO: [0, 2],
SIGHASH_GROUP: [1, 0],
MULTISIG_GROUP: [1, 1],
}
export async function getGenesisScriptsConfig(rpcUrl: string): Promise<Record<string, config.ScriptConfig>> {
const rpc = new RPC(rpcUrl, { fetch: (request, init) => globalThis.fetch(request, { ...init, keepalive: true }) })
const genesisBlock = await rpc.getBlockByNumber('0x0')
const generateGenesisScriptConfig = (transactionIndex: number, outputIndex: number) => {
const cellTypeScript = genesisBlock.transactions[transactionIndex].outputs[outputIndex].type
return {
CODE_HASH: cellTypeScript ? utils.computeScriptHash(cellTypeScript) : '',
TX_HASH: genesisBlock.transactions[transactionIndex].hash!,
INDEX: '0x' + outputIndex.toString(16),
}
}
return {
DAO: {
HASH_TYPE: 'type',
...generateGenesisScriptConfig(...SpecialCellLocation['DAO']),
DEP_TYPE: 'code',
},
SECP256K1_BLAKE160: {
HASH_TYPE: 'type',
...generateGenesisScriptConfig(...SpecialCellLocation['SIGHASH_GROUP']),
CODE_HASH: generateGenesisScriptConfig(...SpecialCellLocation['SIGHASH']).CODE_HASH,
DEP_TYPE: 'depGroup',
SHORT_ID: 0,
},
SECP256K1_BLAKE160_MULTISIG: {
HASH_TYPE: 'type',
...generateGenesisScriptConfig(...SpecialCellLocation['MULTISIG_GROUP']),
CODE_HASH: generateGenesisScriptConfig(...SpecialCellLocation['MULTISIG']).CODE_HASH,
DEP_TYPE: 'depGroup',
SHORT_ID: 1,
},
}
}