import { Buffer } from 'buffer'; import { normalizeStructTag, normalizeSuiAddress } from '@mysten/sui.js/utils'; /** * Check if the given addresses are same address * @param address1 address 1 * @param address2 address 2 * @returns if two addresses are equal */ export function isSameAddress(address1: string, address2: string) { return normalizeSuiAddress(address1) === normalizeSuiAddress(address2); } /** * Check if the given structs are same * @param struct1 struct 1 * @param struct2 struct 2 * @returns if two struct are equal */ export function isSuiStructEqual(struct1: string, struct2: string) { return normalizeStructTag(struct1) === normalizeStructTag(struct2); } /** * Check if is coin object type * @param struct struct * @returns if is coin object type */ export function isCoinObjectType(struct: string) { const normalized = normalizeStructTag(struct); const coinPrefix = normalizeStructTag('0x2::coin::Coin'); return normalized.startsWith(coinPrefix); } /** * Convert a string to Buffer object * @param s string * @returns */ export function stringToBuffer(s: string): Buffer { return Buffer.from(s, 'utf-8'); } /** * Convert buffer to hex string * @param buffer buffer * @returns hex string */ export function Uint8ArrayToHex(buffer: Uint8Array): string { return `0x${Array.prototype.map.call(buffer, (x) => `0${x.toString(16)}`.slice(-2)).join('')}`; } /** * Convert hex string to buffer * @param hex hex string * @returns buffer */ export function HexToUint8Array(hex: string): Uint8Array { return Uint8Array.from(Buffer.from(hex.startsWith('0x') ? hex.slice(2) : hex, 'hex')); }