'use strict';

var J6P5K7DC_cjs = require('./J6P5K7DC.cjs');
var initia_js = require('@initia/initia.js');
var bs58 = require('bs58');
var commonChainModel = require('@layerzerolabs/common-chain-model');
var commonTon = require('@layerzerolabs/common-ton');
var commonUtils = require('@layerzerolabs/common-utils');
var layerzeroDefinitions = require('@layerzerolabs/layerzero-definitions');
var staticChainInfo = require('@layerzerolabs/static-chain-info');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var bs58__default = /*#__PURE__*/_interopDefault(bs58);

// src/addressParser.ts
J6P5K7DC_cjs.init_cjs_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 === layerzeroDefinitions.ChainName.TON) return tonAddressParser(chainName);
  if (chainName === layerzeroDefinitions.ChainName.SOLANA) return base58AddressParser(chainName);
  if (chainName === layerzeroDefinitions.ChainName.INITIA) return initiaAddressParser(chainName);
  return hexAddressParser(chainName);
}
J6P5K7DC_cjs.__name(addressParser, "addressParser");
(function(addressParser2) {
  addressParser2.normalizedToBytes32Hex = (address) => {
    return commonUtils.hexZeroPad(address, 32);
  };
  addressParser2.normalizedToBytes32 = (address) => {
    return commonUtils.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__ */ J6P5K7DC_cjs.__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__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
  return typeof nativeAddress === "object" ? nativeAddress.nativeAddress : nativeAddress;
}, "getAddress");
var tonAddressParser = /* @__PURE__ */ J6P5K7DC_cjs.__name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      try {
        commonTon.parseTonAddress(nativeAddress);
        return true;
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      const numericalValue = addressParser.normalizedToBigInt(address);
      return {
        // FIXME this is wrong
        nativeAddress: commonTon.bigintToAddress(numericalValue).toString(),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    normalizedToUnpaddedString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return String(address);
    }, "normalizedToUnpaddedString"),
    nativeToNormalized: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return commonChainModel.normalizeHex(commonTon.addressToHex(address));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    tonToNormalized: /* @__PURE__ */ J6P5K7DC_cjs.__name((tonAddress) => {
      return addressParser(chainName).parseNative(commonTon.addressToHex(tonAddress));
    }, "tonToNormalized"),
    encoding: "ton"
  };
}, "tonAddressParser");
var initiaAddressParser = /* @__PURE__ */ J6P5K7DC_cjs.__name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      try {
        return commonUtils.isHexString(nativeAddress) || /^(init)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,66})$/.test(nativeAddress);
      } catch {
        return false;
      }
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return {
        nativeAddress: initia_js.AccAddress.fromHex(address),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    normalizedToUnpaddedString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return String(address);
    }, "normalizedToUnpaddedString"),
    nativeToNormalized: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      if (commonUtils.isHexString(address)) {
        return commonChainModel.normalizeHex(address);
      }
      return commonChainModel.normalizeHex(initia_js.AccAddress.toHex(nativeAddress.toString()));
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "initia"
  };
}, "initiaAddressParser");
var base58AddressParser = /* @__PURE__ */ J6P5K7DC_cjs.__name((chainName) => {
  return {
    validateNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.isBase58(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return {
        nativeAddress: bs58__default.default.encode(commonUtils.hexToBytes(address)),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return addressParser(chainName).normalizedToNative(address).nativeAddress;
    }, "normalizedToNativeString"),
    normalizedToUnpaddedString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return String(address);
    }, "normalizedToUnpaddedString"),
    nativeToNormalized: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      const hex = commonUtils.bytesToHexPrefixed(bs58__default.default.decode(address));
      return commonChainModel.normalizeHex(hex);
    }, "nativeToNormalized"),
    nativeToBytes32Hex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexZeroPad(addressParser(chainName).nativeToNormalized(nativeAddress), 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return bs58__default.default.decode(address);
    }, "nativeToBytes"),
    nativeToBytes32: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      if (!validateNative(nativeAddress, chainName)) {
        throw new Error("Invalid native address: " + nativeAddress);
      }
      return addressParser(chainName).nativeToNormalized(nativeAddress);
    }, "parseNative"),
    parseNativeToHex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "base58"
  };
}, "base58AddressParser");
var hexAddressParser = /* @__PURE__ */ J6P5K7DC_cjs.__name((chainName) => {
  const nativeByteLength = staticChainInfo.StaticChainConfigs.getAddressSizeInBytes(chainName);
  return {
    validateNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.isHexString(nativeAddress);
    }, "validateNative"),
    normalizedToNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return {
        // we could have EIP-55 here, but omitted for now
        nativeAddress: commonUtils.hexZeroPad(address, nativeByteLength),
        chainName
      };
    }, "normalizedToNative"),
    normalizedToNativeString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return commonUtils.hexZeroPad(address, nativeByteLength);
    }, "normalizedToNativeString"),
    normalizedToUnpaddedString: /* @__PURE__ */ J6P5K7DC_cjs.__name((address) => {
      return String(address);
    }, "normalizedToUnpaddedString"),
    nativeToNormalized: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return commonChainModel.normalizeHex(address);
    }, "nativeToNormalized"),
    nativeToBytes: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return commonUtils.hexToBytes(address);
    }, "nativeToBytes"),
    nativeToBytes32Hex: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      const address = getAddress(nativeAddress);
      validateNative(address, chainName);
      return commonUtils.hexZeroPad(address, 32);
    }, "nativeToBytes32Hex"),
    nativeToBytes32: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return commonUtils.hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
    }, "nativeToBytes32"),
    parseNative: /* @__PURE__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      if (chainName === layerzeroDefinitions.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__ */ J6P5K7DC_cjs.__name((nativeAddress) => {
      return addressParser(chainName).parseNative(nativeAddress);
    }, "parseNativeToHex"),
    encoding: "hex"
  };
}, "hexAddressParser");

exports.AddressEncoding = AddressEncoding;
exports.addressParser = addressParser;
exports.base58AddressParser = base58AddressParser;
exports.hexAddressParser = hexAddressParser;
exports.initiaAddressParser = initiaAddressParser;
exports.tonAddressParser = tonAddressParser;
//# sourceMappingURL=6W3DK5BA.cjs.map
//# sourceMappingURL=6W3DK5BA.cjs.map