UNPKG

@yubing744/rooch-sdk

Version:
252 lines (251 loc) 8.71 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var tx_exports = {}; __export(tx_exports, { addressToListTuple: () => addressToListTuple, addressToSCS: () => addressToSCS, addressToSeqNumber: () => addressToSeqNumber, encodeArg: () => encodeArg, encodeFunctionCall: () => encodeFunctionCall, encodeMoveCallData: () => encodeMoveCallData, encodeMoveCallDataWithETH: () => encodeMoveCallDataWithETH, encodeStructTypeTag: () => encodeStructTypeTag, encodeStructTypeTags: () => encodeStructTypeTags, structTagToSCS: () => structTagToSCS, typeTagToSCS: () => typeTagToSCS }); module.exports = __toCommonJS(tx_exports); var import_bytes = require("@ethersproject/bytes"); var import_hex = require("./hex"); var import_constants = require("../constants"); var rooch_types = __toESM(require("../types/bcs")); var import_bcs = require("../types/bcs"); var import_encode = require("./encode"); function encodeFunctionCall(functionId, tyArgs, args) { const funcId = (0, import_encode.parseFunctionId)(functionId); const functionCall = new rooch_types.FunctionCall( new rooch_types.FunctionId( new rooch_types.ModuleId( addressToSCS(funcId.address), new rooch_types.Identifier(funcId.module) ), new rooch_types.Identifier(funcId.functionName) ), tyArgs.map((t) => typeTagToSCS(t)), bytesArrayToSeqSeq(args) ); return new rooch_types.MoveActionVariantFunction(functionCall); } function typeTagToSCS(ty) { if (ty === "Bool") { return new rooch_types.TypeTagVariantbool(); } if (ty === "U8") { return new rooch_types.TypeTagVariantu8(); } if (ty === "U16") { return new rooch_types.TypeTagVariantu16(); } if (ty === "U32") { return new rooch_types.TypeTagVariantu32(); } if (ty === "U64") { return new rooch_types.TypeTagVariantu64(); } if (ty === "U128") { return new rooch_types.TypeTagVariantu128(); } if (ty === "U256") { return new rooch_types.TypeTagVariantu256(); } if (ty === "Address") { return new rooch_types.TypeTagVariantaddress(); } if (ty === "Signer") { return new rooch_types.TypeTagVariantsigner(); } if (ty.Vector) { return new rooch_types.TypeTagVariantvector(typeTagToSCS(ty.Vector)); } if (ty.Struct) { return new rooch_types.TypeTagVariantstruct( structTagToSCS(ty.Struct) ); } throw new Error(`invalid type tag: ${ty}`); } function structTagToSCS(data) { return new rooch_types.StructTag( addressToSCS(data.address), new rooch_types.Identifier(data.module), new rooch_types.Identifier(data.name), data.type_params ? data.type_params.map((t) => typeTagToSCS(t)) : [] ); } function addressToSCS(addr) { const bytes = (0, import_hex.fromHexString)(addr, 16 * 2); const data = []; for (let i = 0; i < bytes.length; i++) { data.push([bytes[i]]); } return new rooch_types.AccountAddress(data); } function encodeStructTypeTags(typeArgsString) { return typeArgsString.map((str) => encodeStructTypeTag(str)); } function encodeStructTypeTag(str) { const arr = str.split("<"); const arr1 = arr[0].split("::"); const address = arr1[0]; const module2 = arr1[1]; const name = arr1[2]; const params = arr[1] ? arr[1].replace(">", "").split(",") : []; const type_params = []; if (params.length > 0) { params.forEach((param) => { type_params.push(encodeStructTypeTag(param.trim())); }); } const result = { Struct: { address, module: module2, name, type_params } }; return result; } function bytesToSeq(byteArray) { return Array.from(byteArray); } function stringToSeq(str) { const seq = new Array(); for (let i = 0; i < str.length; i++) { seq.push(str.charCodeAt(i)); } return seq; } function bytesArrayToSeqSeq(input) { return input.map((byteArray) => bytesToSeq(byteArray)); } function addressToListTuple(ethAddress) { const cleanedEthAddress = ethAddress.startsWith("0x") ? ethAddress.slice(2) : ethAddress; if (cleanedEthAddress.length !== import_constants.ROOCH_ADDRESS_LENGTH) { throw new Error("Invalid Rooch address"); } const listTuple = []; for (let i = 0; i < cleanedEthAddress.length; i += 2) { const byte = parseInt(cleanedEthAddress.slice(i, i + 2), 16); listTuple.push([byte]); } return listTuple; } function addressToSeqNumber(ethAddress) { const cleanedEthAddress = ethAddress.startsWith("0x") ? ethAddress.slice(2) : ethAddress; const seqNumber = []; for (let i = 0; i < cleanedEthAddress.length; i += 2) { const byte = parseInt(cleanedEthAddress.slice(i, i + 2), 16); seqNumber.push(byte); } return seqNumber; } function serializeValue(value, type, se) { if (type === "Bool") { se.serializeBool(value); } else if (type === "U8") { se.serializeU8(value); } else if (type === "U16") { se.serializeU16(value); } else if (type === "U32") { se.serializeU32(value); } else if (type === "U64") { se.serializeU64(value); } else if (type === "U128") { se.serializeU128(value); } else if (type === "U256") { const u256 = new import_bcs.U256(value); u256.serialize(se); } else if (type === "Address") { const list = addressToListTuple((0, import_encode.normalizeRoochAddress)(value)); const accountAddress = new rooch_types.AccountAddress(list); accountAddress.serialize(se); } else if (type === "Ascii") { const bytes = stringToSeq(value); const moveAsciiString = new rooch_types.MoveAsciiString(bytes); moveAsciiString.serialize(se); } else if (type === "String") { const bytes = stringToSeq(value); const moveString = new rooch_types.MoveString(bytes); moveString.serialize(se); } else if (type.Vector) { const vectorValues = value; se.serializeLen(vectorValues.length); for (let item of vectorValues) { serializeValue(item, type.Vector, se); } } else if (type.Struct) { const serializable = value; serializable.serialize(se); } else if (type === "ObjectID") { const list = addressToListTuple((0, import_encode.normalizeRoochAddress)(value)); const accountAddress = new rooch_types.AccountAddress(list); accountAddress.serialize(se); } else if (type === "Object") { const objectId = (0, import_encode.structTagToObjectID)(value); const list = addressToListTuple((0, import_encode.normalizeRoochAddress)(objectId)); const accountAddress = new rooch_types.AccountAddress(list); accountAddress.serialize(se); } else if (type === "Raw") { const vectorValues = value; se.serializeLen(vectorValues.length); for (let item of vectorValues) { se.serializeU8(item); } } } function encodeArg(arg) { const se = new import_bcs.BcsSerializer(); serializeValue(arg.value, arg.type, se); return se.getBytes(); } const encodeMoveCallData = (funcId, tyArgs, args) => { const bcsArgs = args?.map((arg) => encodeArg(arg)); const scriptFunction = encodeFunctionCall(funcId, tyArgs, bcsArgs); const payloadInHex = (() => { const se = new import_bcs.BcsSerializer(); scriptFunction.serialize(se); return se.getBytes(); })(); return payloadInHex; }; const encodeMoveCallDataWithETH = (funcId, tyArgs, args) => { return (0, import_bytes.hexlify)(encodeMoveCallData(funcId, tyArgs, args)); }; //# sourceMappingURL=tx.js.map