import { init_esm_shims, __name } from './CWKWODSW.js';
import { AccAddress } from '@initia/initia.js';
import { StrKey } from '@stellar/stellar-sdk';
import bs58 from 'bs58';
import { normalizeHex } from '@layerzerolabs/common-chain-model';
import { hexZeroPad, hexToBytes, bytesToHexPrefixed, isBase58, isHexString } from '@layerzerolabs/common-encoding-utils';
import { addressToHex, bigintToAddress, parseTonAddress } from '@layerzerolabs/common-ton';
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";
  AddressEncoding2["STELLAR"] = "stellar";
  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);
  if (chainName === ChainName.STELLAR) return stellarAddressParser(chainName);
  if (chainName === ChainName.STARKNET) return starknetAddressParser(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 = {}));
function assertNative(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);
  }
}
__name(assertNative, "assertNative");
var getAddress = /* @__PURE__ */ __name((nativeAddress) => {
  return typeof nativeAddress === "object" ? nativeAddress.nativeAddress : nativeAddress;
}, "getAddress");
var tonAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) {
        return false;
      }
      try {
        parseTonAddress(nativeAddress);
        return true;
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      const numericalValue = addressParser.normalizedToBigInt(address);
      return {
        // FIXME this is wrong
        nativeAddress: bigintToAddress(numericalValue).toString(),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return normalizeHex(addressToHex(address));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToUnpaddedHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToUnpaddedHex"),
    tonToNormalized: /* @__PURE__ */ __name((tonAddress) => {
      return addressParser(chainName).parseNative(addressToHex(tonAddress));
    }, "tonToNormalized"),
    encoding: "ton"
  };
}, "tonAddressParser");
var initiaAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) {
        return false;
      }
      try {
        return isHexString(nativeAddress) || /^(init)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,66})$/.test(nativeAddress);
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) {
        return void 0;
      }
      return {
        nativeAddress: AccAddress.fromHex(address),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      if (isHexString(address)) {
        return normalizeHex(address);
      }
      return normalizeHex(AccAddress.toHex(nativeAddress.toString()));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToUnpaddedHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToUnpaddedHex"),
    encoding: "initia"
  };
}, "initiaAddressParser");
var base58AddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return false;
      return isBase58(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return {
        nativeAddress: bs58.encode(hexToBytes(address)),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      const hex = bytesToHexPrefixed(bs58.decode(address));
      return normalizeHex(hex);
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return bs58.decode(address);
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToUnpaddedHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToUnpaddedHex"),
    encoding: "base58"
  };
}, "base58AddressParser");
var hexAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return false;
      return isHexString(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName);
      return {
        // we could have EIP-55 here, but omitted for now
        nativeAddress: hexZeroPad(address, nativeByteLength),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName);
      return hexZeroPad(address, nativeByteLength);
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return normalizeHex(address);
    }, "nativeToNormalized"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return hexToBytes(address);
    }, "nativeToBytes"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return hexZeroPad(address, 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (chainName === ChainName.TRON && nativeAddress) {
        nativeAddress = nativeAddress.startsWith("41") ? `0x${nativeAddress.slice(2)}` : nativeAddress;
      }
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToUnpaddedHex: /* @__PURE__ */ __name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToUnpaddedHex"),
    encoding: "hex"
  };
}, "hexAddressParser");
var stellarAddressParser = /* @__PURE__ */ __name((chainName) => {
  const isValidStellarAddress = /* @__PURE__ */ __name((address) => {
    if (!address) return false;
    return StrKey.isValidEd25519PublicKey(address) || StrKey.isValidContract(address) || StrKey.isValidMed25519PublicKey(address) || StrKey.isValidLiquidityPool(address) || StrKey.isValidClaimableBalance(address) || StrKey.isValidSignedPayload(address);
  }, "isValidStellarAddress");
  const stellarTextEncoder = new TextEncoder();
  const stellarAsciiToNormalized = /* @__PURE__ */ __name((address) => {
    const bytes = stellarTextEncoder.encode(address);
    return normalizeHex(bytesToHexPrefixed(bytes));
  }, "stellarAsciiToNormalized");
  const stellarTextDecoder = new TextDecoder();
  const normalizedToStellarAscii = /* @__PURE__ */ __name((normalizedHex) => {
    const bytes = hexToBytes(normalizedHex);
    return stellarTextDecoder.decode(bytes);
  }, "normalizedToStellarAscii");
  return {
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return false;
      return isValidStellarAddress(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      const stellarAddress = normalizedToStellarAscii(address);
      return {
        nativeAddress: stellarAddress,
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return normalizedToStellarAscii(address);
    }, "normalizedToNativeString"),
    nativeToNormalized: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return stellarAsciiToNormalized(address);
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      let rawBytes;
      if (StrKey.isValidEd25519PublicKey(address)) {
        rawBytes = StrKey.decodeEd25519PublicKey(address);
      } else if (StrKey.isValidContract(address)) {
        rawBytes = StrKey.decodeContract(address);
      } else if (StrKey.isValidLiquidityPool(address)) {
        rawBytes = StrKey.decodeLiquidityPool(address);
      } else if (StrKey.isValidClaimableBalance(address)) {
        const fullBytes = StrKey.decodeClaimableBalance(address);
        rawBytes = fullBytes.subarray(1);
      } else if (StrKey.isValidMed25519PublicKey(address)) {
        throw new Error("nativeToBytes32 is not supported for Stellar muxed addresses (M...)");
      } else if (StrKey.isValidSignedPayload(address)) {
        throw new Error("nativeToBytes32 is not supported for Stellar signed payload addresses (P...)");
      } else {
        throw new Error(`Unknown Stellar address type: ${address}`);
      }
      return bytesToHexPrefixed(rawBytes);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const address = getAddress(nativeAddress);
      assertNative(address, chainName);
      return stellarTextEncoder.encode(address);
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      const hex = addressParser(chainName).nativeToBytes32Hex(nativeAddress);
      return hexToBytes(hex);
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToUnpaddedHex: /* @__PURE__ */ __name((nativeAddress) => {
      assertNative(nativeAddress, chainName);
      const normalized = addressParser(chainName).nativeToNormalized(nativeAddress);
      return normalized;
    }, "parseNativeToUnpaddedHex"),
    encoding: "stellar"
  };
}, "stellarAddressParser");
var starknetAddressParser = /* @__PURE__ */ __name((chainName) => {
  return {
    ...hexAddressParser(chainName),
    validateNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return false;
      return isHexString(nativeAddress);
    }, "validateNative"),
    parseNative: /* @__PURE__ */ __name((nativeAddress) => {
      if (nativeAddress === void 0) return void 0;
      nativeAddress = `0x${BigInt(nativeAddress).toString(16)}`;
      assertNative(nativeAddress, chainName);
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    normalizedToNative: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return {
        nativeAddress: address,
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ __name((address) => {
      if (address === void 0) return void 0;
      return address;
    }, "normalizedToNativeString")
  };
}, "starknetAddressParser");

export { AddressEncoding, addressParser, base58AddressParser, hexAddressParser, initiaAddressParser, starknetAddressParser, stellarAddressParser, tonAddressParser };
//# sourceMappingURL=3457WEGJ.js.map
//# sourceMappingURL=3457WEGJ.js.map