diff --git a/packages/constants/src/lib/constants/constants.ts b/packages/constants/src/lib/constants/constants.ts index abcb18ea8..072504217 100644 --- a/packages/constants/src/lib/constants/constants.ts +++ b/packages/constants/src/lib/constants/constants.ts @@ -466,21 +466,33 @@ export const LIT_CHAINS: LITChain = { type: null, vmType: 'EVM', }, + + /** + * Chainlist entry for the Chronicle Testnet. + * https://chainlist.org/chain/175177 + */ chronicleTestnet: { contractAddress: null, chainId: 175177, name: 'Chronicle - Lit Protocol Testnet', - symbol: 'testLPX', + symbol: 'tstLPX', decimals: 18, - rpcUrls: ['https://lit-protocol.calderachain.xyz/replica-http'], + rpcUrls: [ + 'https://lit-protocol.calderachain.xyz/replica-http', + 'https://chain-rpc.litprotocol.com/http', + ], blockExplorerUrls: ['https://chain.litprotocol.com/'], type: null, vmType: 'EVM', }, + + /** + * @deprecated Will be removed in version 7.x. - Use `chronicleVesuviusTestnet` instead. + */ datilDevnet: { contractAddress: null, chainId: 2311, - name: 'Vesuvius - Lit Protocol Devnet', + name: 'Chronicle Vesuvius - Lit Protocol Testnet', symbol: 'tstLit', decimals: 18, rpcUrls: ['https://vesuvius-rpc.litprotocol.com/'], @@ -488,10 +500,15 @@ export const LIT_CHAINS: LITChain = { type: null, vmType: 'EVM', }, - datilTestnet: { + + /** + * Chainlist entry for the Chronicle Vesuvius Testnet. + * https://chainlist.org/chain/2311 + */ + chronicleVesuviusTestnet: { contractAddress: null, chainId: 2311, - name: 'Vesuvius - Lit Protocol Testnet', + name: 'Chronicle Vesuvius - Lit Protocol Testnet', symbol: 'tstLit', decimals: 18, rpcUrls: ['https://vesuvius-rpc.litprotocol.com/'], @@ -503,9 +520,12 @@ export const LIT_CHAINS: LITChain = { contractAddress: null, chainId: 175177, name: 'Chronicle - Lit Protocol Testnet', - symbol: 'testLPX', + symbol: 'tstLit', decimals: 18, - rpcUrls: ['https://lit-protocol.calderachain.xyz/replica-http'], + rpcUrls: [ + 'https://lit-protocol.calderachain.xyz/replica-http', + 'https://chain-rpc.litprotocol.com/http', + ], blockExplorerUrls: ['https://chain.litprotocol.com/'], type: null, vmType: 'EVM', @@ -625,27 +645,171 @@ export const LIT_CHAINS: LITChain = { export const LIT_CHAIN_RPC_URL = LIT_CHAINS['chronicleTestnet'].rpcUrls[0]; /** - * Enum representing the available LIT RPC endpoints. + * Object containing information to submit to Metamask + */ +export const metamaskChainInfo = { + /** + * Information about the "chronicle" chain. + */ + chronicle: { + chainId: LIT_CHAINS['chronicleTestnet'].chainId, + chainName: LIT_CHAINS['chronicleTestnet'].name, + nativeCurrency: { + name: LIT_CHAINS['chronicleTestnet'].symbol, + symbol: LIT_CHAINS['chronicleTestnet'].symbol, + decimals: LIT_CHAINS['chronicleTestnet'].decimals, + }, + rpcUrls: LIT_CHAINS['chronicleTestnet'].rpcUrls, + blockExplorerUrls: LIT_CHAINS['chronicleTestnet'].blockExplorerUrls, + iconUrls: ['future'], + }, + /** + * Information about the "chronicleVesuvius" chain. + */ + chronicleVesuvius: { + chainId: LIT_CHAINS['chronicleVesuviusTestnet'].chainId, + chainName: LIT_CHAINS['chronicleVesuviusTestnet'].name, + nativeCurrency: { + name: LIT_CHAINS['chronicleVesuviusTestnet'].symbol, + symbol: LIT_CHAINS['chronicleVesuviusTestnet'].symbol, + decimals: LIT_CHAINS['chronicleVesuviusTestnet'].decimals, + }, + rpcUrls: LIT_CHAINS['chronicleVesuviusTestnet'].rpcUrls, + blockExplorerUrls: LIT_CHAINS['chronicleVesuviusTestnet'].blockExplorerUrls, + iconUrls: ['future'], + }, +}; + +/** + * Constants representing the available LIT RPC endpoints. */ -export enum LIT_RPC { +export const LIT_RPC = { /** * Local Anvil RPC endpoint. */ - LOCAL_ANVIL = 'http://127.0.0.1:8545', + LOCAL_ANVIL: 'http://127.0.0.1:8545', /** * Chronicle RPC endpoint - Used for Cayenne, Manzano, Habanero */ - CHRONICLE = 'https://chain-rpc.litprotocol.com/http', + CHRONICLE: 'https://chain-rpc.litprotocol.com/http', /** - * Vesuvius RPC endpoint - used for >= Datil-dev, Datil-test + * Chronicle Vesuvius RPC endpoint - used for >= Datil-dev, Datil-test + * @deprecated Will be removed in version 7.x. - Use CHRONICLE_VESUVIUS instead */ - VESUVIUS = 'https://vesuvius-rpc.litprotocol.com', -} + VESUVIUS: 'https://vesuvius-rpc.litprotocol.com', + + /** + * Chronicle Vesuvius RPC endpoint - used for >= Datil-dev, Datil-test + */ + CHRONICLE_VESUVIUS: 'https://vesuvius-rpc.litprotocol.com', +} as const; export const LIT_EVM_CHAINS = LIT_CHAINS; +/** + * Represents the Lit Network constants. + */ +export const LIT_NETWORK = { + Cayenne: 'cayenne', + Manzano: 'manzano', + Habanero: 'habanero', + DatilDev: 'datil-dev', + DatilTest: 'datil-test', + Custom: 'custom', + Localhost: 'localhost', +} as const; + +/** + * The type representing the keys of the LIT_NETWORK object. + */ +export type LIT_NETWORK_TYPES = keyof typeof LIT_NETWORK; + +/** + * The type representing the values of the LIT_NETWORK object. + */ +export type LIT_NETWORK_VALUES = (typeof LIT_NETWORK)[keyof typeof LIT_NETWORK]; + +/** + * RPC URL by Network + * + * A mapping of network names to their corresponding RPC URLs. + */ +export const RPC_URL_BY_NETWORK: { [key in LIT_NETWORK_VALUES]: string } = { + cayenne: LIT_RPC.CHRONICLE, + manzano: LIT_RPC.CHRONICLE, + habanero: LIT_RPC.CHRONICLE, + 'datil-dev': LIT_RPC.CHRONICLE_VESUVIUS, + 'datil-test': LIT_RPC.CHRONICLE_VESUVIUS, + custom: LIT_RPC.LOCAL_ANVIL, + localhost: LIT_RPC.LOCAL_ANVIL, +}; + +/** + * Mapping of network names to their corresponding relayer URLs. + */ +export const RELAYER_URL_BY_NETWORK: { [key in LIT_NETWORK_VALUES]: string } = { + cayenne: 'https://relayer-server-staging-cayenne.getlit.dev', + manzano: 'https://manzano-relayer.getlit.dev', + habanero: 'https://habanero-relayer.getlit.dev', + 'datil-dev': 'https://datil-dev-relayer.getlit.dev', + 'datil-test': 'https://datil-test-relayer.getlit.dev', + custom: 'http://localhost:3000', + localhost: 'http://localhost:3000', +}; + +/** + * URL mappings for general worker URLs by network. + */ +export const GENERAL_WORKER_URL_BY_NETWORK: { + [key in LIT_NETWORK_VALUES]: string; +} = { + cayenne: 'https://apis.getlit.dev/cayenne/contracts', + manzano: 'https://apis.getlit.dev/manzano/contracts', + habanero: 'https://apis.getlit.dev/habanero/contracts', + 'datil-dev': 'https://apis.getlit.dev/datil-dev/contracts', + 'datil-test': 'https://apis.getlit.dev/datil-test/contracts', + + // just use cayenne abis for custom and localhost + custom: 'https://apis.getlit.dev/cayenne/contracts', + localhost: 'https://apis.getlit.dev/cayenne/contracts', +}; + +/** + * URL constants for the staging worker by network. + */ +export const GENERAL_STAGING_WORKER_URL_BY_NETWORK: { + [key in LIT_NETWORK_VALUES]: string; +} = { + cayenne: 'https://staging.apis.getlit.dev/cayenne/contracts', + manzano: 'https://staging.apis.getlit.dev/manzano/contracts', + habanero: 'https://staging.apis.getlit.dev/habanero/contracts', + 'datil-dev': 'https://staging.apis.getlit.dev/datil-dev/contracts', + 'datil-test': 'https://staging.apis.getlit.dev/datil-test/contracts', + + // just use cayenne abis for custom and localhost + custom: 'https://apis.getlit.dev/cayenne/contracts', + localhost: 'https://apis.getlit.dev/cayenne/contracts', +}; + +/** + * Mapping of network values to corresponding Metamask chain info. + */ +export const METAMASK_CHAIN_INFO_BY_NETWORK: Record< + LIT_NETWORK_VALUES, + | typeof metamaskChainInfo.chronicle + | typeof metamaskChainInfo.chronicleVesuvius +> = { + cayenne: metamaskChainInfo.chronicle, + manzano: metamaskChainInfo.chronicle, + habanero: metamaskChainInfo.chronicle, + 'datil-dev': metamaskChainInfo.chronicleVesuvius, + 'datil-test': metamaskChainInfo.chronicleVesuvius, + custom: metamaskChainInfo.chronicleVesuvius, + localhost: metamaskChainInfo.chronicleVesuvius, +}; + /** * Solana Chains supported by the LIT protocol. Use the chain name as a key in this object. * @constant @@ -839,11 +1003,30 @@ export const TELEM_API_URL = 'https://lit-general-worker.getlit.dev'; // ========== RLI Delegation ========== export const SIWE_DELEGATION_URI = 'lit:capability:delegation'; +/** + * @deprecated Will be removed in version 7.x. - Use RELAYER_URL_BY_NETWORK.Cayenne instead + */ export const RELAY_URL_CAYENNE = 'https://relayer-server-staging-cayenne.getlit.dev'; + +/** + * @deprecated Will be removed in version 7.x. - Use RELAYER_URL_BY_NETWORK.Habanero instead + */ export const RELAY_URL_HABANERO = 'https://habanero-relayer.getlit.dev'; + +/** + * @deprecated Will be removed in version 7.x. - Use RELAYER_URL_BY_NETWORK.Manzano instead + */ export const RELAY_URL_MANZANO = 'https://manzano-relayer.getlit.dev'; + +/** + * @deprecated Will be removed in version 7.x. - Use RELAYER_URL_BY_NETWORK.DatilDev instead + */ export const RELAY_URL_DATIL_DEV = 'https://datil-dev-relayer.getlit.dev'; + +/** + * @deprecated Will be removed in version 7.x. - Use RELAYER_URL_BY_NETWORK.DatilTest instead + */ export const RELAY_URL_DATIL_TEST = 'https://datil-test-relayer.getlit.dev'; // ========== Lit Actions ========== diff --git a/packages/contracts-sdk/src/lib/contracts-sdk.ts b/packages/contracts-sdk/src/lib/contracts-sdk.ts index 38ebee49c..cd8157481 100644 --- a/packages/contracts-sdk/src/lib/contracts-sdk.ts +++ b/packages/contracts-sdk/src/lib/contracts-sdk.ts @@ -1,13 +1,4 @@ /* eslint-disable import/order */ -import { - BigNumber, - BigNumberish, - BytesLike, - ContractReceipt, - ethers, -} from 'ethers'; -import { hexToDec, decToHex, intToIP } from './hex2dec'; -import bs58 from 'bs58'; import { isBrowser, isNode } from '@lit-protocol/misc'; import { CreateCustomAuthMethodRequest, @@ -21,6 +12,9 @@ import { MintWithAuthParams, MintWithAuthResponse, } from '@lit-protocol/types'; +import bs58 from 'bs58'; +import { BytesLike, ContractReceipt, ethers } from 'ethers'; +import { decToHex, hexToDec, intToIP } from './hex2dec'; // ----- autogen:import-data:start ----- // Generated at 2023-11-07T01:50:52.460Z @@ -52,28 +46,26 @@ import * as stakingContract from '../abis/Staking.sol/Staking'; import * as stakingBalancesContract from '../abis/StakingBalances.sol/StakingBalances'; // ----- autogen:imports:end ----- -import { TokenInfo, derivedAddresses } from './addresses'; -import { IPubkeyRouter } from '../abis/PKPNFT.sol/PKPNFT'; +import { + AuthMethodScope, + AuthMethodType, + METAMASK_CHAIN_INFO_BY_NETWORK, + GENERAL_WORKER_URL_BY_NETWORK, + LIT_NETWORK_VALUES, + RPC_URL_BY_NETWORK, +} from '@lit-protocol/constants'; +import { LogManager, Logger } from '@lit-protocol/logger'; import { computeAddress } from 'ethers/lib/utils'; +import { IPubkeyRouter } from '../abis/PKPNFT.sol/PKPNFT'; +import { minStakingAbi } from '../abis/minAbi/minStakingAbi'; +import { TokenInfo, derivedAddresses } from './addresses'; import { getAuthIdByAuthMethod, stringToArrayify } from './auth-utils'; -import { Logger, LogManager } from '@lit-protocol/logger'; -import { - calculateUTCMidnightExpiration, - convertRequestsPerDayToPerSecond, - requestsToKilosecond, -} from './utils'; import { CIDParser, IPFSHash, getBytes32FromMultihash, } from './helpers/getBytes32FromMultihash'; -import { - AuthMethodScope, - AuthMethodType, - LIT_CHAINS, - LIT_RPC, -} from '@lit-protocol/constants'; -import { minStakingAbi } from '../abis/minAbi/minStakingAbi'; +import { calculateUTCMidnightExpiration, requestsToKilosecond } from './utils'; // const DEFAULT_RPC = 'https://lit-protocol.calderachain.xyz/replica-http'; // const DEFAULT_READ_RPC = 'https://lit-protocol.calderachain.xyz/replica-http'; @@ -214,10 +206,7 @@ export class LitContracts { this.network = args?.network || 'cayenne'; // if rpc is not specified, use the default rpc if (!this.rpc) { - this.rpc = - args?.network === 'datil-dev' || args?.network === 'datil-test' - ? LIT_RPC.VESUVIUS - : LIT_RPC.CHRONICLE; + this.rpc = RPC_URL_BY_NETWORK[this.network]; } if (!this.rpcs) { @@ -279,45 +268,21 @@ export class LitContracts { return '0x' + decimal.toString(16); } - const chronicleChainInfo = { - chainId: _decimalToHex(LIT_CHAINS['chronicleTestnet'].chainId), - chainName: LIT_CHAINS['chronicleTestnet'].name, - nativeCurrency: { - name: LIT_CHAINS['chronicleTestnet'].symbol, - symbol: LIT_CHAINS['chronicleTestnet'].symbol, - decimals: LIT_CHAINS['chronicleTestnet'].decimals, - }, - rpcUrls: LIT_CHAINS['chronicleTestnet'].rpcUrls, - blockExplorerUrls: LIT_CHAINS['chronicleTestnet'].blockExplorerUrls, - iconUrls: ['future'], - }; + const chainInfo = METAMASK_CHAIN_INFO_BY_NETWORK[this.network]; - const vesuviusChainInfo = { - chainId: _decimalToHex(LIT_CHAINS['datilDevnet'].chainId), - chainName: LIT_CHAINS['datilDevnet'].name, - nativeCurrency: { - name: LIT_CHAINS['datilDevnet'].symbol, - symbol: LIT_CHAINS['datilDevnet'].symbol, - decimals: LIT_CHAINS['datilDevnet'].decimals, - }, - rpcUrls: LIT_CHAINS['datilDevnet'].rpcUrls, - blockExplorerUrls: LIT_CHAINS['datilDevnet'].blockExplorerUrls, - iconUrls: ['future'], + const metamaskChainInfo = { + ...chainInfo, + chainId: _decimalToHex(chainInfo.chainId), }; - const chainInfo = - this.network === 'datil-dev' || this.network === 'datil-test' - ? vesuviusChainInfo - : chronicleChainInfo; - try { await web3Provider.send('wallet_switchEthereumChain', [ - { chainId: chainInfo.chainId }, + { chainId: metamaskChainInfo.chainId }, ]); } catch (e) { await web3Provider.request({ method: 'wallet_addEthereumChain', - params: [chainInfo], + params: [metamaskChainInfo], }); } @@ -615,10 +580,7 @@ export class LitContracts { rpcUrl?: string ) { let provider: ethers.providers.JsonRpcProvider; - rpcUrl = - rpcUrl ?? (network === 'datil-dev' || network === 'datil-test') - ? LIT_RPC.VESUVIUS - : LIT_RPC.CHRONICLE; + rpcUrl = RPC_URL_BY_NETWORK[network]; if (context && 'provider' in context!) { provider = context.provider; } else { @@ -980,20 +942,9 @@ export class LitContracts { }; private static async _resolveContractContext( - network: LIT_NETWORKS_KEYS + network: LIT_NETWORK_VALUES // context?: LitContractContext | LitContractResolverContext ) { - let data; - const CAYENNE_API = - 'https://lit-general-worker.getlit.dev/contract-addresses'; - const MANZANO_API = - 'https://lit-general-worker.getlit.dev/manzano-contract-addresses'; - const HABANERO_API = - 'https://lit-general-worker.getlit.dev/habanero-contract-addresses'; - const DATIL_DEV_API = - 'https://lit-general-worker.getlit.dev/datil-dev/contracts'; - const DATIL_TEST_API = 'https://apis.getlit.dev/datil-test/contracts'; - const fetchData = async (url: string) => { try { return await fetch(url).then((res) => res.json()); @@ -1002,33 +953,15 @@ export class LitContracts { } }; - switch (network) { - case 'cayenne': - data = await fetchData(CAYENNE_API); - break; - case 'manzano': - data = await fetchData(MANZANO_API); - break; - case 'habanero': - data = await fetchData(HABANERO_API); - break; - case 'datil-dev': - data = await fetchData(DATIL_DEV_API); - break; - case 'datil-test': - data = await fetchData(DATIL_TEST_API); - break; - case 'custom': - case 'localhost': - // just use cayenne abis for custom and localhost - data = await fetchData(CAYENNE_API); - break; - default: - throw new Error( - `[_resolveContractContext] Unsupported network: ${network}` - ); + // -- check if it's supported network + if (!GENERAL_WORKER_URL_BY_NETWORK[network]) { + throw new Error( + `[_resolveContractContext] Unsupported network: ${network}` + ); } + const data = await fetchData(GENERAL_WORKER_URL_BY_NETWORK[network]); + if (!data) { throw new Error('[_resolveContractContext] No data found'); } diff --git a/packages/lit-auth-client/src/lib/lit-auth-client.ts b/packages/lit-auth-client/src/lib/lit-auth-client.ts index 03444a686..496e034af 100644 --- a/packages/lit-auth-client/src/lib/lit-auth-client.ts +++ b/packages/lit-auth-client/src/lib/lit-auth-client.ts @@ -2,26 +2,22 @@ import { ethers } from 'ethers'; import { AuthMethodType, - LIT_RPC, ProviderType, - RELAY_URL_CAYENNE, - RELAY_URL_DATIL_DEV, - RELAY_URL_DATIL_TEST, - RELAY_URL_HABANERO, - RELAY_URL_MANZANO, + RELAYER_URL_BY_NETWORK, + RPC_URL_BY_NETWORK, } from '@lit-protocol/constants'; import { LitNodeClient } from '@lit-protocol/lit-node-client'; -import { bootstrapLogManager, getLoggerbyId, log } from '@lit-protocol/misc'; +import { bootstrapLogManager, log } from '@lit-protocol/misc'; import { + AuthMethod, EthWalletProviderOptions, IRelay, LitAuthClientOptions, + MintRequestBody, OAuthProviderOptions, - StytchOtpProviderOptions, ProviderOptions, + StytchOtpProviderOptions, WebAuthnProviderOptions, - AuthMethod, - MintRequestBody, } from '@lit-protocol/types'; import AppleProvider from './providers/AppleProvider'; @@ -114,25 +110,7 @@ export class LitAuthClient { ); } - let url; - - switch (this.litNodeClient.config.litNetwork) { - case 'cayenne': - url = RELAY_URL_CAYENNE; - break; - case 'habanero': - url = RELAY_URL_HABANERO; - break; - case 'manzano': - url = RELAY_URL_MANZANO; - break; - case 'datil-dev': - url = RELAY_URL_DATIL_DEV; - break; - case 'datil-test': - url = RELAY_URL_DATIL_TEST; - break; - } + const url = RELAYER_URL_BY_NETWORK[this.litNodeClient.config.litNetwork]; this.relay = new LitRelay({ relayUrl: url, @@ -157,10 +135,12 @@ export class LitAuthClient { // Set RPC URL this.rpcUrl = options?.rpcUrl || - this.litNodeClient.config.litNetwork === 'datil-dev' || - this.litNodeClient.config.litNetwork === 'datil-test' - ? LIT_RPC.VESUVIUS - : LIT_RPC.CHRONICLE; + RPC_URL_BY_NETWORK[this.litNodeClient.config.litNetwork]; + + if (!this.rpcUrl) { + throw new Error('No RPC URL provided'); + } + log('rpc url: ', this.rpcUrl); log('relay config: ', options.litRelayConfig); log('relay instance: ', this.relay); diff --git a/packages/misc/src/lib/misc.ts b/packages/misc/src/lib/misc.ts index 8bd7649a5..75e1de937 100644 --- a/packages/misc/src/lib/misc.ts +++ b/packages/misc/src/lib/misc.ts @@ -3,14 +3,12 @@ import { ILitError, LIT_AUTH_SIG_CHAIN_KEYS, LIT_CHAINS, - LIT_ENDPOINT, LIT_ERROR, + LIT_NETWORK, + LIT_NETWORK_TYPES, + LIT_NETWORK_VALUES, LitNetwork, - RELAY_URL_CAYENNE, - RELAY_URL_DATIL_DEV, - RELAY_URL_DATIL_TEST, - RELAY_URL_HABANERO, - RELAY_URL_MANZANO, + RELAYER_URL_BY_NETWORK, } from '@lit-protocol/constants'; import { @@ -667,29 +665,20 @@ export const defaultMintClaimCallback: MintCallback< network: string = 'cayenne' ): Promise => { try { - let relayUrl: string = ''; - - switch (network) { - case LitNetwork.Cayenne: - relayUrl = RELAY_URL_CAYENNE + '/auth/claim'; - break; - case LitNetwork.Habanero: - relayUrl = RELAY_URL_HABANERO + 'auth/claim'; - break; - case LitNetwork.Manzano: - relayUrl = RELAY_URL_MANZANO + 'auth/claim'; - break; - case LitNetwork.DatilDev: - relayUrl = RELAY_URL_DATIL_DEV + '/auth/claim'; - break; - break; - case LitNetwork.DatilTest: - relayUrl = RELAY_URL_DATIL_TEST + '/auth/claim'; - break; + const AUTH_CLAIM_PATH = '/auth/claim'; + + const relayUrl: string = + RELAYER_URL_BY_NETWORK[network as LIT_NETWORK_VALUES]; + + if (!params.relayUrl && !relayUrl) { + throw new Error( + 'No relayUrl provided and no default relayUrl found for network' + ); } - const url = params.relayUrl ? params.relayUrl : relayUrl; - const response = await fetch(url, { + const relayUrlWithPath = relayUrl + AUTH_CLAIM_PATH; + + const response = await fetch(relayUrlWithPath, { method: 'POST', body: JSON.stringify(params), headers: {