@ckb-ccc/core
Version:
Core of CCC - CKBer's Codebase
174 lines (173 loc) • 5.71 kB
JavaScript
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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScriptVec = exports.ScriptOpt = exports.Script = exports.HashTypeCodec = void 0;
exports.hashTypeFrom = hashTypeFrom;
exports.hashTypeToBytes = hashTypeToBytes;
exports.hashTypeFromBytes = hashTypeFromBytes;
const index_js_1 = require("../bytes/index.js");
const index_js_2 = require("../hex/index.js");
const index_js_3 = require("../molecule/index.js");
const script_advanced_js_1 = require("./script.advanced.js");
exports.HashTypeCodec = index_js_3.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"
* ```
*/
function hashTypeFrom(val) {
const hashType = (() => {
if (typeof val === "number") {
return script_advanced_js_1.NUM_TO_HASH_TYPE[val];
}
if (typeof val === "bigint") {
return script_advanced_js_1.NUM_TO_HASH_TYPE[Number(val)];
}
if (!script_advanced_js_1.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]
* ```
*/
function hashTypeToBytes(hashType) {
return (0, index_js_1.bytesFrom)([script_advanced_js_1.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"
* ```
*/
function hashTypeFromBytes(bytes) {
return script_advanced_js_1.NUM_TO_HASH_TYPE[(0, index_js_1.bytesFrom)(bytes)[0]];
}
/**
* @public
*/
let Script = Script_1 = class Script extends index_js_3.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 + (0, index_js_1.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((0, index_js_2.hexFrom)(script.codeHash), hashTypeFrom(script.hashType), (0, index_js_2.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, (0, index_js_2.hexFrom)(args));
}
};
exports.Script = Script;
exports.Script = Script = Script_1 = __decorate([
index_js_3.mol.codec(index_js_3.mol.table({
codeHash: index_js_3.mol.Byte32,
hashType: exports.HashTypeCodec,
args: index_js_3.mol.Bytes,
}))
], Script);
exports.ScriptOpt = index_js_3.mol.option(Script);
exports.ScriptVec = index_js_3.mol.vector(Script);
;