|
| 1 | +import type { OmniAddress, Bytes32 } from '@layerzerolabs/devtools' |
| 2 | + |
| 3 | +/** |
| 4 | + * Converts a hexadecimal address string to a 32-byte Uint8Array format used by Aptos |
| 5 | + * |
| 6 | + * @param address - The hex address string to convert, optionally starting with '0x'. Can be null/undefined. |
| 7 | + * @returns A 32-byte Uint8Array with the address right-aligned (padded with zeros on the left) |
| 8 | + * |
| 9 | + * If the input is null/undefined, returns an empty 32-byte array. |
| 10 | + * Otherwise, removes '0x' prefix if present, converts hex string to bytes, |
| 11 | + * and right-aligns the result in a 32-byte array. |
| 12 | + */ |
| 13 | +export function hexAddrToAptosBytesAddr(address: string | null | undefined): Uint8Array { |
| 14 | + const bytes = address ? Buffer.from(address.replace('0x', ''), 'hex') : new Uint8Array(0) |
| 15 | + const bytes32 = new Uint8Array(32) |
| 16 | + bytes32.set(bytes, 32 - bytes.length) |
| 17 | + return bytes32 |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Converts a Uint8Array to a hex string with 0x prefix |
| 22 | + * |
| 23 | + * @param bytes - The bytes to convert |
| 24 | + * @returns Hex string with 0x prefix |
| 25 | + */ |
| 26 | +export function bytesToHex(bytes: Uint8Array): string { |
| 27 | + return '0x' + Buffer.from(bytes).toString('hex') |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Normalizes an address to bytes32 format (64 hex characters with 0x prefix) |
| 32 | + * |
| 33 | + * Aptos uses 32-byte addresses. This function ensures addresses from |
| 34 | + * other chains (like EVM with 20 bytes) are properly padded to 32 bytes. |
| 35 | + * |
| 36 | + * @param address - The address to normalize |
| 37 | + * @returns Normalized bytes32 address |
| 38 | + */ |
| 39 | +export function normalizeAddressToBytes32(address: OmniAddress | null | undefined): Bytes32 { |
| 40 | + if (!address) { |
| 41 | + return '0x' + '0'.repeat(64) |
| 42 | + } |
| 43 | + |
| 44 | + // Remove 0x prefix if present |
| 45 | + const hex = address.replace('0x', '') |
| 46 | + |
| 47 | + // Pad to 64 characters (32 bytes) |
| 48 | + const padded = hex.padStart(64, '0') |
| 49 | + |
| 50 | + return `0x${padded}` |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Checks if an address is an empty/zero address |
| 55 | + * |
| 56 | + * @param address - The address to check |
| 57 | + * @returns true if the address is null, undefined, or all zeros |
| 58 | + */ |
| 59 | +export function isEmptyAddress(address: OmniAddress | null | undefined): boolean { |
| 60 | + if (!address) { |
| 61 | + return true |
| 62 | + } |
| 63 | + |
| 64 | + const normalized = normalizeAddressToBytes32(address) |
| 65 | + return normalized === '0x' + '0'.repeat(64) |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Compares two addresses for equality, handling different lengths |
| 70 | + * |
| 71 | + * Both addresses are normalized to bytes32 before comparison. |
| 72 | + * |
| 73 | + * @param a - First address |
| 74 | + * @param b - Second address |
| 75 | + * @returns true if addresses are equal |
| 76 | + */ |
| 77 | +export function areAddressesEqual(a: OmniAddress | null | undefined, b: OmniAddress | null | undefined): boolean { |
| 78 | + return normalizeAddressToBytes32(a) === normalizeAddressToBytes32(b) |
| 79 | +} |
0 commit comments