@roochnetwork/rooch-sdk
Version:
135 lines (134 loc) • 4.35 kB
JavaScript
import { splitGenericParameters } from "@mysten/bcs";
import { concatBytes, isBytes, isHex, sha3_256, stringToBytes, toHEX } from "../utils/index.js";
import { canonicalRoochAddress, normalizeRoochAddress, RoochAddress } from "../address/index.js";
const VECTOR_REGEX = /^vector<(.+)>$/;
const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;
class Serializer {
static structTagToObjectID(input) {
return `0x${toHEX(sha3_256(typeof input === "string" ? input : Serializer.structTagToCanonicalString(input)))}`;
}
//TODO: rooch Address bug
static accountNamedObjectID(address2, structTag) {
let addressBytes;
if (typeof address2 === "string") {
const finalAddress = isHex(address2) ? normalizeRoochAddress(address2) : address2;
addressBytes = new RoochAddress(finalAddress).toBytes();
} else if (isBytes(address2)) {
addressBytes = address2;
} else {
addressBytes = address2.toBytes();
}
const tagBytes = stringToBytes(
"utf8",
typeof structTag === "string" ? structTag : Serializer.structTagToCanonicalString(structTag)
);
return `0x${toHEX(sha3_256(concatBytes(addressBytes, tagBytes)))}`;
}
static structTagToCanonicalString(input) {
let result = `${canonicalRoochAddress(input.address)}::${input.module}::${input.name}`;
if (input.typeParams && input.typeParams.length > 0) {
const typeParams = input.typeParams.map(Serializer.typeTagToString).join(",");
result += `<${typeParams}>`;
}
return result;
}
static typeTagToString(input) {
if (typeof input === "string") {
return input;
}
if ("Vector" in input) {
return `vector<${Serializer.typeTagToString(input.Vector)}>`;
}
if ("Struct" in input) {
return Serializer.structTagToCanonicalString(input.Struct);
}
throw new Error("Invalid TypeTag");
}
static typeTagParseFromStr(str, normalizeAddress = false) {
if (str === "address") {
return { address: null };
} else if (str === "bool") {
return { bool: null };
} else if (str === "u8") {
return { u8: null };
} else if (str === "u16") {
return { u16: null };
} else if (str === "u32") {
return { u32: null };
} else if (str === "u64") {
return { u64: null };
} else if (str === "u128") {
return { u128: null };
} else if (str === "u256") {
return { u256: null };
} else if (str === "signer") {
return { signer: null };
}
const vectorMatch = str.match(VECTOR_REGEX);
if (vectorMatch) {
return {
vector: Serializer.typeTagParseFromStr(vectorMatch[1], normalizeAddress)
};
}
const structMatch = str.match(STRUCT_REGEX);
if (structMatch) {
const address2 = normalizeAddress ? normalizeRoochAddress(structMatch[1]) : structMatch[1];
return {
struct: {
address: address2,
module: structMatch[2],
name: structMatch[3],
typeParams: structMatch[5] === void 0 ? [] : Serializer.parseStructTypeArgs(structMatch[5], normalizeAddress)
}
};
}
throw new Error(`Encountered unexpected token when parsing type args for ${str}`);
}
static parseStructTypeArgs(str, normalizeAddress = false) {
return splitGenericParameters(str).map(
(tok) => Serializer.typeTagParseFromStr(tok, normalizeAddress)
);
}
static tagToString(tag) {
if ("bool" in tag) {
return "bool";
}
if ("u8" in tag) {
return "u8";
}
if ("u16" in tag) {
return "u16";
}
if ("u32" in tag) {
return "u32";
}
if ("u64" in tag) {
return "u64";
}
if ("u128" in tag) {
return "u128";
}
if ("u256" in tag) {
return "u256";
}
if ("address" in tag) {
return "address";
}
if ("signer" in tag) {
return "signer";
}
if ("vector" in tag) {
return `vector<${Serializer.tagToString(tag.vector)}>`;
}
if ("struct" in tag) {
const struct = tag.struct;
const typeParams = struct.typeParams.map(Serializer.tagToString).join(", ");
return `${struct.address}::${struct.module}::${struct.name}${typeParams ? `<${typeParams}>` : ""}`;
}
throw new Error("Invalid TypeTag");
}
}
export {
Serializer
};
//# sourceMappingURL=serializer.js.map