UNPKG

@ckb-ccc/core

Version:

Core of CCC - CKBer's Codebase

168 lines (167 loc) 5.15 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var Script_1; import { bytesFrom } from "../bytes/index.js"; import { hexFrom } from "../hex/index.js"; import { mol } from "../molecule/index.js"; import { HASH_TYPES, HASH_TYPE_TO_NUM, NUM_TO_HASH_TYPE, } from "./script.advanced.js"; export const HashTypeCodec = mol.Codec.from({ byteLength: 1, encode: hashTypeToBytes, decode: hashTypeFromBytes, }); /** * Converts a HashTypeLike value to a HashType. * @public * * @param val - The value to convert, which can be a string, number, or bigint. * @returns The corresponding HashType. * * @throws Will throw an error if the input value is not a valid hash type. * * @example * ```typescript * const hashType = hashTypeFrom(1); // Outputs "data" * const hashType = hashTypeFrom("type"); // Outputs "type" * ``` */ export function hashTypeFrom(val) { const hashType = (() => { if (typeof val === "number") { return NUM_TO_HASH_TYPE[val]; } if (typeof val === "bigint") { return NUM_TO_HASH_TYPE[Number(val)]; } if (!HASH_TYPES.includes(val)) { return; } return val; })(); if (hashType === undefined) { throw new Error(`Invalid hash type ${val}`); } return hashType; } /** * Converts a HashTypeLike value to its corresponding byte representation. * @public * * @param hashType - The hash type value to convert. * @returns A Uint8Array containing the byte representation of the hash type. * * @example * ```typescript * const hashTypeBytes = hashTypeToBytes("type"); // Outputs Uint8Array [0] * ``` */ export function hashTypeToBytes(hashType) { return bytesFrom([HASH_TYPE_TO_NUM[hashTypeFrom(hashType)]]); } /** * Converts a byte-like value to a HashType. * @public * * @param bytes - The byte-like value to convert. * @returns The corresponding HashType. * * @throws Will throw an error if the input bytes do not correspond to a valid hash type. * * @example * ```typescript * const hashType = hashTypeFromBytes(new Uint8Array([0])); // Outputs "type" * ``` */ export function hashTypeFromBytes(bytes) { return NUM_TO_HASH_TYPE[bytesFrom(bytes)[0]]; } /** * @public */ let Script = Script_1 = class Script extends mol.Entity.Base() { /** * Creates an instance of Script. * * @param codeHash - The code hash of the script. * @param hashType - The hash type of the script. * @param args - The arguments for the script. */ constructor(codeHash, hashType, args) { super(); this.codeHash = codeHash; this.hashType = hashType; this.args = args; } get occupiedSize() { return 33 + bytesFrom(this.args).length; } /** * Clone a script. * * @returns A cloned Script instance. * * @example * ```typescript * const script1 = script0.clone(); * ``` */ clone() { return new Script_1(this.codeHash, this.hashType, this.args); } /** * Creates a Script instance from a ScriptLike object. * * @param script - A ScriptLike object or an instance of Script. * @returns A Script instance. * * @example * ```typescript * const script = Script.from({ * codeHash: "0x1234...", * hashType: "type", * args: "0xabcd..." * }); * ``` */ static from(script) { if (script instanceof Script_1) { return script; } return new Script_1(hexFrom(script.codeHash), hashTypeFrom(script.hashType), hexFrom(script.args)); } /** * Creates a Script instance from client and known script. * * @param knownScript - A KnownScript enum. * @param args - Args for the script. * @param client - A ScriptLike object or an instance of Script. * @returns A promise that resolves to the script instance. * * @example * ```typescript * const script = await Script.fromKnownScript( * client, * KnownScript.XUdt, * args: "0xabcd..." * ); * ``` */ static async fromKnownScript(client, knownScript, args) { const script = await client.getKnownScript(knownScript); return new Script_1(script.codeHash, script.hashType, hexFrom(args)); } }; Script = Script_1 = __decorate([ mol.codec(mol.table({ codeHash: mol.Byte32, hashType: HashTypeCodec, args: mol.Bytes, })) ], Script); export { Script }; export const ScriptOpt = mol.option(Script); export const ScriptVec = mol.vector(Script);