UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

72 lines (71 loc) 2.43 kB
import { fromBase58, splitGenericParameters } from "@mysten/bcs"; import { isValidNamedPackage } from "./move-registry.js"; const TX_DIGEST_LENGTH = 32; function isValidTransactionDigest(value) { try { const buffer = fromBase58(value); return buffer.length === TX_DIGEST_LENGTH; } catch (e) { return false; } } const SUI_ADDRESS_LENGTH = 32; function isValidSuiAddress(value) { return isHex(value) && getHexByteLength(value) === SUI_ADDRESS_LENGTH; } function isValidSuiObjectId(value) { return isValidSuiAddress(value); } function parseTypeTag(type) { if (!type.includes("::")) return type; return parseStructTag(type); } function parseStructTag(type) { const [address, module] = type.split("::"); const isMvrPackage = isValidNamedPackage(address); const rest = type.slice(address.length + module.length + 4); const name = rest.includes("<") ? rest.slice(0, rest.indexOf("<")) : rest; const typeParams = rest.includes("<") ? splitGenericParameters(rest.slice(rest.indexOf("<") + 1, rest.lastIndexOf(">"))).map( (typeParam) => parseTypeTag(typeParam.trim()) ) : []; return { address: isMvrPackage ? address : normalizeSuiAddress(address), module, name, typeParams }; } function normalizeStructTag(type) { const { address, module, name, typeParams } = typeof type === "string" ? parseStructTag(type) : type; const formattedTypeParams = typeParams?.length > 0 ? `<${typeParams.map( (typeParam) => typeof typeParam === "string" ? typeParam : normalizeStructTag(typeParam) ).join(",")}>` : ""; return `${address}::${module}::${name}${formattedTypeParams}`; } function normalizeSuiAddress(value, forceAdd0x = false) { let address = value.toLowerCase(); if (!forceAdd0x && address.startsWith("0x")) { address = address.slice(2); } return `0x${address.padStart(SUI_ADDRESS_LENGTH * 2, "0")}`; } function normalizeSuiObjectId(value, forceAdd0x = false) { return normalizeSuiAddress(value, forceAdd0x); } function isHex(value) { return /^(0x|0X)?[a-fA-F0-9]+$/.test(value) && value.length % 2 === 0; } function getHexByteLength(value) { return /^(0x|0X)/.test(value) ? (value.length - 2) / 2 : value.length / 2; } export { SUI_ADDRESS_LENGTH, isValidSuiAddress, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiObjectId, parseStructTag }; //# sourceMappingURL=sui-types.js.map