|
| 1 | +import {Args, Command, Flags} from '@oclif/core' |
| 2 | +import {getCellDepsFromSearchKeys, getCLIConfig} from '../../libs/config.js' |
| 3 | +import {ccc, Cell, CellOutputLike, fixedPointFrom, HasherCkb, numToBytes} from '@ckb-ccc/core' |
| 4 | +import {ClientCollectableSearchKeyLike, cccA} from '@ckb-ccc/core/advanced' |
| 5 | +// TODO: Switch to codec in CCC. |
| 6 | +import {Byte32, Bytes, BytesVec, Script} from '@ckb-lumos/base/lib/blockchain.js' |
| 7 | +import axios from 'axios' |
| 8 | +import {blockchain} from '@ckb-lumos/base' |
| 9 | +import {encodeHex, encodeU832Array} from '../../libs/utils.js' |
| 10 | +import {array, option, table, vector} from '@ckb-lumos/codec/lib/molecule/layout.js' |
| 11 | +import {BIish, Uint8} from '@ckb-lumos/codec/lib/number/uint.js' |
| 12 | +import {decodeHex} from '../../libs/utils.js' |
| 13 | + |
| 14 | +export default class DevModifyData extends Command { |
| 15 | + static override args = { |
| 16 | + symbol: Args.string({ |
| 17 | + description: 'Symbol of UDT to unpause. ', |
| 18 | + required: true, |
| 19 | + }), |
| 20 | + lock_hash: Args.string({description: 'Lock hash in hex. Variable length'}), |
| 21 | + } |
| 22 | + |
| 23 | + static strict = false |
| 24 | + |
| 25 | + static override description = |
| 26 | + 'Add a lock_hash to pause list. If UDTPausable.enumerate_paused() returns a list of only one UDTPausableData in the vec which means there is no external pausable data cell, a new pausable data cell will be created but manual reference in the contract code is required. NOTE: You cannot use holdSend for more than one pause/pause action in the same transaction, and results of pause/unpause would only take effect after the transaction is confirmed.' |
| 27 | + |
| 28 | + static override examples = ['<%= config.bin %> <%= command.id %>'] |
| 29 | + |
| 30 | + static override flags = { |
| 31 | + fromTransactionJSON: Flags.file({ |
| 32 | + description: |
| 33 | + '(NOT IMPLEMENTED YET!) Assemble transaction on the basis of a previous action; use together with holdSend to make multiple transfers within the same transaction.', |
| 34 | + exists: true, |
| 35 | + }), |
| 36 | + holdSendToJSON: Flags.file({ |
| 37 | + description: |
| 38 | + '(NOT IMPLEMENTED YET!) Hold the transaction and send it later. Will output the transaction JSON. Use together with fromTransactionJson to make multiple transfers within the same transaction.', |
| 39 | + exists: false, |
| 40 | + }), |
| 41 | + holdSend: Flags.boolean({ |
| 42 | + description: |
| 43 | + '(NOT IMPLEMENTED YET!) Hold the transaction and send it later. When neither holdSend nor holdSendToJSON is set, the transaction will be sent immediately.', |
| 44 | + }), |
| 45 | + } |
| 46 | + |
| 47 | + public async run(): Promise<void> { |
| 48 | + const {args, argv, flags} = await this.parse(DevModifyData) |
| 49 | + |
| 50 | + const CLIConfig = await getCLIConfig(this.config.configDir) |
| 51 | + const client = new ccc.ClientPublicTestnet({url: process.env.CKB_RPC_URL}) |
| 52 | + |
| 53 | + const signer = new ccc.SignerCkbPrivateKey( |
| 54 | + client, |
| 55 | + flags.privateKey ?? |
| 56 | + CLIConfig.accountRegistry[flags.fromAccount ?? '']?.privateKey ?? |
| 57 | + process.env.MAIN_WALLET_PRIVATE_KEY!, |
| 58 | + ) |
| 59 | + |
| 60 | + const udtConfig = CLIConfig.UDTRegistry[args.symbol] |
| 61 | + |
| 62 | + const unpauseHasher = new HasherCkb() |
| 63 | + const unpausePathHex = unpauseHasher.update(Buffer.from('UDTPausable.unpause')).digest().slice(0, 18) |
| 64 | + |
| 65 | + const codeCellDep = (await getCellDepsFromSearchKeys(client, udtConfig.cellDepSearchKeys))[0] |
| 66 | + |
| 67 | + const u832Codec = array(Uint8, 32) |
| 68 | + const u832VecCodec = vector(u832Codec) |
| 69 | + |
| 70 | + const udtPausableDataCodec = table( |
| 71 | + { |
| 72 | + pause_list: u832VecCodec, |
| 73 | + next_type_script: option(Script), |
| 74 | + }, |
| 75 | + ['pause_list', 'next_type_script'], |
| 76 | + ) |
| 77 | + |
| 78 | + const {script: ownerLock} = await signer.getRecommendedAddressObj() |
| 79 | + |
| 80 | + let lockHashU832Array = [] |
| 81 | + for (const lockHash of argv.slice(1)) { |
| 82 | + lockHashU832Array.push(numToBytes(String(lockHash), 32).reverse()) |
| 83 | + } |
| 84 | + this.debug(`lockHashU832Array: ${lockHashU832Array}`) |
| 85 | + const lockHashU832ArrayEncoded = encodeU832Array(lockHashU832Array) |
| 86 | + this.debug(`lockHashArrayEncoded: ${lockHashU832ArrayEncoded}`) |
| 87 | + const lockHashU832ArrayEncodedHex = encodeHex(lockHashU832ArrayEncoded) |
| 88 | + const inputTypeScript = await ccc.Script.fromKnownScript( |
| 89 | + client, |
| 90 | + ccc.KnownScript.TypeId, |
| 91 | + '0x4804b04f37f22f77b3cb621e6fbc471330893c98c56868f0d74b91cc996fe0cb', |
| 92 | + ) |
| 93 | + |
| 94 | + const inputTypeIDCell = (await client.findCellsByType(inputTypeScript).next()).value as Cell |
| 95 | + |
| 96 | + const inputTypeIDCellInput = new ccc.CellInput(inputTypeIDCell.outPoint, BigInt(0)) |
| 97 | + |
| 98 | + // TODO: Placeholder for holdSend and holdSendToJSON. |
| 99 | + const modifyTx = ccc.Transaction.from({ |
| 100 | + version: 0, |
| 101 | + cellDeps: [], |
| 102 | + headerDeps: [], |
| 103 | + inputs: [inputTypeIDCellInput], |
| 104 | + outputs: [], |
| 105 | + outputsData: [], |
| 106 | + witnesses: [], |
| 107 | + }) |
| 108 | + |
| 109 | + modifyTx.addOutput( |
| 110 | + { |
| 111 | + lock: ownerLock, |
| 112 | + type: inputTypeScript, |
| 113 | + }, |
| 114 | + '0x500000000c0000005000000002000000d19228c64920eb8c3d79557d8ae59ee7a14b9d7de45ccf8bafacf82c91fc359ea320a09489791af2e5e1fe84927eda84f71afcbd2c7a65cb419464fe46e75085', |
| 115 | + ) |
| 116 | + |
| 117 | + try { |
| 118 | + await modifyTx.completeInputsByCapacity(signer) |
| 119 | + await modifyTx.completeFeeBy(signer) |
| 120 | + const modifyTxHash = await signer.sendTransaction(modifyTx) |
| 121 | + this.log(`Modified. Tx hash: ${modifyTxHash}`) |
| 122 | + } catch (error) { |
| 123 | + // ISSUE: [Prettify responses from SSRI calls #21](https://github.com/Alive24/ckb_ssri_cli/issues/21) |
| 124 | + console.error('Request failed', error) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments