@enclavemoney/enclave-wallet-sdk
Version:
A simple enclave wallet SDK for React applications
265 lines (264 loc) • 10 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProviderName = exports.getProviderIcon = exports.ProtocolProvider = exports.availableNetworks = exports.handleNumericKeyDown = exports.validateNumericInput = exports.debounce = exports.formatAmount = exports.NETWORK_LOGO_URLS = exports.getNetworkName = exports.NETWORK_NAME_TO_CHAIN_ID = void 0;
// Add this mapping at the top (after TOKEN_NETWORKS)
exports.NETWORK_NAME_TO_CHAIN_ID = {
Arbitrum: 42161,
Optimism: 10,
Base: 8453,
Solana: 792703809,
Bitcoin: 8253038,
Sonic: 146,
Ethereum: 1,
Unichain: 130,
Avalanche: 43114,
Polygon: 137,
BNB: 56,
};
function getNetworkName(chainId) {
var _a;
var chainIdNumber = typeof chainId === "string" ? parseInt(chainId) : chainId;
var networkName = (_a = Object.entries(exports.NETWORK_NAME_TO_CHAIN_ID).find(function (_a) {
var _ = _a[0], id = _a[1];
return id === chainIdNumber;
})) === null || _a === void 0 ? void 0 : _a[0];
return networkName || chainId.toString();
}
exports.getNetworkName = getNetworkName;
exports.NETWORK_LOGO_URLS = {
42161: "https://storage.googleapis.com/enclave_assets/arb-logo.svg",
10: "https://storage.googleapis.com/enclave_assets/optimism-logo.svg",
8453: "https://storage.googleapis.com/enclave_assets/base.svg",
792703809: "https://storage.googleapis.com/enclave_assets/sol.png",
8253038: "https://assets.coingecko.com/coins/images/1/standard/bitcoin.png?1696501400",
56: "https://assets.coingecko.com/coins/images/825/standard/bnb-icon2_2x.png?1696501970",
137: "https://assets.coingecko.com/coins/images/4713/standard/polygon.png?1698233745",
43114: "https://assets.coingecko.com/coins/images/12559/standard/Avalanche_Circle_RedWhite_Trans.png?1696512369",
130: "https://icons.llamao.fi/icons/chains/rsz_unichain.jpg",
146: "https://assets.coingecko.com/coins/images/38108/standard/200x200_Sonic_Logo.png?1734679256",
1: "https://media.socket.tech/tokens/all/ETH",
};
function formatAmount(amount) {
var _a;
var num = parseFloat(amount);
var decimalPlaces = ((_a = amount.split(".")[1]) === null || _a === void 0 ? void 0 : _a.length) || 0;
return decimalPlaces > 6 ? num.toFixed(6) : amount;
}
exports.formatAmount = formatAmount;
// Simple debounce implementation
var debounce = function (func, wait) {
var timeout;
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
clearTimeout(timeout);
timeout = setTimeout(function () { return func.apply(void 0, args); }, wait);
};
};
exports.debounce = debounce;
/**
* Validates and sanitizes numeric input to prevent invalid formats
* @param value The input value to validate
* @param maxDecimals Maximum number of decimal places allowed (optional)
* @returns The sanitized value or empty string if invalid
*/
function validateNumericInput(value, maxDecimals) {
if (!value)
return "";
// Remove any non-digit, non-decimal point characters
// This prevents things like "1+1", "1--", etc.
var sanitized = value.replace(/[^0-9.]/g, "");
// Handle multiple decimal points - keep only the first one
var parts = sanitized.split(".");
if (parts.length > 2) {
sanitized = parts[0] + "." + parts.slice(1).join("");
}
// Prevent starting with decimal point for better UX
if (sanitized.startsWith(".")) {
sanitized = "0" + sanitized;
}
// Prevent multiple leading zeros (but allow single zero before decimal)
if (sanitized.length > 1 &&
sanitized.startsWith("0") &&
!sanitized.startsWith("0.")) {
sanitized = sanitized.replace(/^0+/, "0");
}
// Enforce maximum decimal places if specified
if (maxDecimals !== undefined && sanitized.includes(".")) {
var _a = sanitized.split("."), integerPart = _a[0], decimalPart = _a[1];
if (decimalPart.length > maxDecimals) {
sanitized = integerPart + "." + decimalPart.slice(0, maxDecimals);
}
}
// Final validation - ensure it's a valid number format
if (sanitized && (isNaN(Number(sanitized)) || sanitized.includes(".."))) {
return "";
}
return sanitized;
}
exports.validateNumericInput = validateNumericInput;
/**
* Handles keydown events to prevent invalid characters from being typed
* @param e The keyboard event
* @param currentValue The current input value
* @param maxDecimals Maximum number of decimal places allowed (optional)
*/
function handleNumericKeyDown(e, currentValue, maxDecimals) {
// Allow basic navigation and editing keys
var allowedKeys = [
"Backspace",
"Delete",
"Tab",
"Escape",
"Enter",
"ArrowLeft",
"ArrowRight",
"ArrowUp",
"ArrowDown",
"Home",
"End",
"PageUp",
"PageDown",
];
// Allow Ctrl/Cmd combinations (copy, paste, select all, etc.)
if (e.ctrlKey || e.metaKey) {
return;
}
// Allow allowed keys
if (allowedKeys.includes(e.key)) {
return;
}
// Allow digits, but check decimal limit if applicable
if (/^[0-9]$/.test(e.key)) {
// If maxDecimals is specified and we're after a decimal point, check limit
if (maxDecimals !== undefined && currentValue.includes(".")) {
var decimalPart = currentValue.split(".")[1];
if (decimalPart && decimalPart.length >= maxDecimals) {
// Check if cursor is in the decimal part
var target = e.target;
var cursorPosition = target.selectionStart || 0;
var decimalIndex = currentValue.indexOf(".");
// If cursor is after decimal and we've reached the limit, prevent input
if (cursorPosition > decimalIndex &&
decimalPart.length >= maxDecimals) {
e.preventDefault();
return;
}
}
}
return;
}
// Allow decimal point only if there isn't one already
if (e.key === "." && !currentValue.includes(".")) {
return;
}
// Prevent all other characters including +, -, e, E, etc.
e.preventDefault();
}
exports.handleNumericKeyDown = handleNumericKeyDown;
exports.availableNetworks = [
{
name: "Solana",
icon: "https://storage.googleapis.com/enclave_assets/sol.png",
},
{
name: "Arbitrum",
icon: "https://storage.googleapis.com/enclave_assets/arb-logo.svg",
},
{
name: "Optimism",
icon: "https://storage.googleapis.com/enclave_assets/optimism-logo.svg",
},
{
name: "Base",
icon: "https://storage.googleapis.com/enclave_assets/base.svg",
},
{
name: "BNB",
icon: "https://assets.coingecko.com/coins/images/825/standard/bnb-icon2_2x.png?1696501970",
},
{
name: "Polygon",
icon: "https://assets.coingecko.com/coins/images/4713/standard/polygon.png?1698233745",
},
{
name: "Avalanche",
icon: "https://assets.coingecko.com/coins/images/12559/standard/Avalanche_Circle_RedWhite_Trans.png?1696512369",
},
{
name: "Unichain",
icon: "https://icons.llamao.fi/icons/chains/rsz_unichain.jpg",
},
{
name: "Sonic",
icon: "https://assets.coingecko.com/coins/images/38108/standard/200x200_Sonic_Logo.png?1734679256",
},
{
name: "Ethereum",
icon: "https://media.socket.tech/tokens/all/ETH",
},
];
var ProtocolProvider;
(function (ProtocolProvider) {
ProtocolProvider["LIFI"] = "LIFI";
ProtocolProvider["RELAY"] = "RELAY";
ProtocolProvider["BUNGEE"] = "BUNGEE";
ProtocolProvider["GARDEN"] = "GARDEN";
ProtocolProvider["ONEINCH"] = "1INCH";
ProtocolProvider["INCOMING"] = "INCOMING";
ProtocolProvider["CCTP_V1"] = "CCTP_V1";
ProtocolProvider["CCTP_V2_FAST"] = "CCTP_V2_FAST";
ProtocolProvider["CCTP_V2_STANDARD"] = "CCTP_V2_SLOW";
})(ProtocolProvider = exports.ProtocolProvider || (exports.ProtocolProvider = {}));
/**
* Gets the appropriate icon URL for a protocol provider
* @param provider The protocol provider
* @returns The icon URL for the provider
*/
function getProviderIcon(provider) {
switch (provider) {
case ProtocolProvider.LIFI:
return "https://storage.googleapis.com/enclave_assets/lifi.png";
case ProtocolProvider.BUNGEE:
return "https://storage.googleapis.com/enclave_assets/bungee.png";
case ProtocolProvider.RELAY:
return "https://storage.googleapis.com/enclave_assets/relay.png";
case ProtocolProvider.CCTP_V1:
case ProtocolProvider.CCTP_V2_FAST:
case ProtocolProvider.CCTP_V2_STANDARD:
return "https://storage.googleapis.com/enclave_assets/circle.png";
default:
return null;
}
}
exports.getProviderIcon = getProviderIcon;
/**
* Gets the human-readable name for a protocol provider
* @param provider The protocol provider
* @returns The display name for the provider
*/
function getProviderName(provider) {
switch (provider) {
case ProtocolProvider.BUNGEE:
return "Bungee";
case ProtocolProvider.RELAY:
return "Relay";
case ProtocolProvider.CCTP_V1:
return "Circle CCTP V1";
case ProtocolProvider.CCTP_V2_FAST:
return "Circle CCTP V2 Fast";
case ProtocolProvider.CCTP_V2_STANDARD:
return "Circle CCTP V2 Standard";
case ProtocolProvider.LIFI:
return "LiFi";
case ProtocolProvider.GARDEN:
return "Garden";
case ProtocolProvider.ONEINCH:
return "1inch";
default:
return provider || "Unknown Provider";
}
}
exports.getProviderName = getProviderName;