UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

90 lines (89 loc) 3.43 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var sui_types_exports = {}; __export(sui_types_exports, { SUI_ADDRESS_LENGTH: () => SUI_ADDRESS_LENGTH, isValidSuiAddress: () => isValidSuiAddress, isValidSuiObjectId: () => isValidSuiObjectId, isValidTransactionDigest: () => isValidTransactionDigest, normalizeStructTag: () => normalizeStructTag, normalizeSuiAddress: () => normalizeSuiAddress, normalizeSuiObjectId: () => normalizeSuiObjectId, parseStructTag: () => parseStructTag }); module.exports = __toCommonJS(sui_types_exports); var import_bcs = require("@mysten/bcs"); const TX_DIGEST_LENGTH = 32; function isValidTransactionDigest(value) { try { const buffer = (0, import_bcs.fromB58)(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, module2] = type.split("::"); const rest = type.slice(address.length + module2.length + 4); const name = rest.includes("<") ? rest.slice(0, rest.indexOf("<")) : rest; const typeParams = rest.includes("<") ? (0, import_bcs.splitGenericParameters)(rest.slice(rest.indexOf("<") + 1, rest.lastIndexOf(">"))).map( (typeParam) => parseTypeTag(typeParam.trim()) ) : []; return { address: normalizeSuiAddress(address), module: module2, name, typeParams }; } function normalizeStructTag(type) { const { address, module: module2, 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}::${module2}::${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; } //# sourceMappingURL=sui-types.js.map