import { init_esm_shims, __name } from './YXE7PREU.js';
import { AccAddress } from '@initia/initia.js';
import bs58 from 'bs58';
import { normalizeHex } from '@layerzerolabs/common-chain-model';
import { addressToHex, bigintToAddress, parseTonAddress } from '@layerzerolabs/common-ton';
import { hexZeroPad, hexToBytes, bytesToHexPrefixed, isBase58, isHexString } from '@layerzerolabs/common-utils';
import { ChainName } from '@layerzerolabs/layerzero-definitions';
import { StaticChainConfigs } from '@layerzerolabs/static-chain-info';

// src/addressParser.ts
init_esm_shims();
var AddressEncoding = /* @__PURE__ */ (function(AddressEncoding2) {
  AddressEncoding2["HEX"] = "hex";
  AddressEncoding2["BASE58"] = "base58";
  AddressEncoding2["TON"] = "ton";
  AddressEncoding2["INITIA"] = "initia";
  return AddressEncoding2;
})({});
function addressParser(chainName) {
  if (chainName === ChainName.TON) return tonAddressParser(chainName);
  if (chainName === ChainName.SOLANA) return base58AddressParser(chainName);
  if (chainName === ChainName.INITIA) return initiaAddressParser(chainName);
  return hexAddressParser(chainName);
}
__name(addressParser, "addressParser");
(function(addressParser2) {
  addressParser2.normalizedToBytes32Hex = (address) => {
    return hexZeroPad(address, 32);
  };
  addressParser2.normalizedToUnpaddedString = (address) => {
    return address.toString();
  };
  addressParser2.normalizedToBytes32 = (address) => {
    return hexToBytes(addressParser2.normalizedToBytes32Hex(address));
  };
  addressParser2.normalizedToBigInt = (address) => {
    return BigInt(address);
  };
  addressParser2.parseNative = (nativeAddress) => {
    return addressParser2(nativeAddress.chainName).parseNative(nativeAddress.nativeAddress);
  };
})(addressParser || (addressParser = {}));
var validateNative = /* @__PURE__ */ __name((nativeAddress, expectedChainName) => {
  if (!addressParser(expectedChainName).validateNative(nativeAddress)) {
    throw new Error("Cannot convert: " + nativeAddress + " - The native address does not match the expected format for the chain: " + expectedChainName);
  }
  return true;
}, "validateNative");
var getAddress = /* @__PURE__ */ __name((nativeAddress) => {
  return typeof nativeAddress === "object" ? nativeAddress.nativeAddress : nativeAddress;
}, "getAddress");
var tonAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      try {
        parseTonAddress(nativeAddress);
        return true;
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      const numericalValue = addressParser.normalizedToBigInt(address);
      return {
        // FIXME this is wrong
        nativeAddress: bigintToAddress(numericalValue).toString(),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return normalizeHex(addressToHex(address));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    tonToNormalized: /* @__PURE__ */ __name((tonAddress) => {
      return addressParser(chainName).parseNative(addressToHex(tonAddress));
    }, "tonToNormalized"),
    encoding: "ton"
  };
}, "tonAddressParser");
var initiaAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      try {
        return isHexString(nativeAddress) || /^(init)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,66})$/.test(nativeAddress);
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      return {
        nativeAddress: AccAddress.fromHex(address),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      if (isHexString(address)) {
        return normalizeHex(address);
      }
      return normalizeHex(AccAddress.toHex(nativeAddress.toString()));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "initia"
  };
}, "initiaAddressParser");
var base58AddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      return isBase58(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      return {
        nativeAddress: bs58.encode(hexToBytes(address)),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      const hex = bytesToHexPrefixed(bs58.decode(address));
      return normalizeHex(hex);
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return bs58.decode(address);
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "base58"
  };
}, "base58AddressParser");
var hexAddressParser = /* @__PURE__ */ __name((chainName) => {
  const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName);
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      return isHexString(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      return {
        // we could have EIP-55 here, but omitted for now
        nativeAddress: hexZeroPad(address, nativeByteLength),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      return hexZeroPad(address, nativeByteLength);
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return normalizeHex(address);
    }, "nativeToNormalized"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return hexToBytes(address);
    }, "nativeToBytes"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return hexZeroPad(address, 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (chainName === ChainName.TRON) {
        nativeAddress = nativeAddress.startsWith("41") ? `0x${nativeAddress.slice(2)}` : nativeAddress;
      }
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "hex"
  };
}, "hexAddressParser");

export { AddressEncoding, addressParser, base58AddressParser, hexAddressParser, initiaAddressParser, tonAddressParser };
//# sourceMappingURL=MR2I35UU.js.map
//# sourceMappingURL=MR2I35UU.js.map