diff --git a/examples/angular/src/app/pages/wallet-selector/wallet-selector.component.ts b/examples/angular/src/app/pages/wallet-selector/wallet-selector.component.ts index 74be6a265..090ef6410 100644 --- a/examples/angular/src/app/pages/wallet-selector/wallet-selector.component.ts +++ b/examples/angular/src/app/pages/wallet-selector/wallet-selector.component.ts @@ -42,8 +42,6 @@ declare global { // Get a project ID at https://cloud.walletconnect.com const projectId = "30147604c5f01d0bc4482ab0665b5697"; -// NOTE: This is the NEAR wallet playground used in dev mode. -// TODO: Replace with the NEAR chain after the protocol upgrade. const near: Chain = { id: 398, name: "NEAR Protocol Testnet", @@ -53,13 +51,13 @@ const near: Chain = { symbol: "NEAR", }, rpcUrls: { - default: { http: ["https://near-wallet-relayer.testnet.aurora.dev"] }, - public: { http: ["https://near-wallet-relayer.testnet.aurora.dev"] }, + default: { http: ["https://eth-rpc.testnet.near.org"] }, + public: { http: ["https://eth-rpc.testnet.near.org"] }, }, blockExplorers: { default: { name: "NEAR Explorer", - url: "https://testnet.nearblocks.io", + url: "https://eth-explorer-testnet.near.org", }, }, testnet: true, diff --git a/examples/react/contexts/WalletSelectorContext.tsx b/examples/react/contexts/WalletSelectorContext.tsx index 5fadd74e9..55d0fdcac 100644 --- a/examples/react/contexts/WalletSelectorContext.tsx +++ b/examples/react/contexts/WalletSelectorContext.tsx @@ -73,8 +73,6 @@ const WalletSelectorContext = // Get a project ID at https://cloud.walletconnect.com const projectId = "30147604c5f01d0bc4482ab0665b5697"; -// NOTE: This is the NEAR wallet playground used in dev mode. -// TODO: Replace with the NEAR chain after the protocol upgrade. const near: Chain = { id: 398, name: "NEAR Protocol Testnet", @@ -84,13 +82,13 @@ const near: Chain = { symbol: "NEAR", }, rpcUrls: { - default: { http: ["https://near-wallet-relayer.testnet.aurora.dev"] }, - public: { http: ["https://near-wallet-relayer.testnet.aurora.dev"] }, + default: { http: ["https://eth-rpc.testnet.near.org"] }, + public: { http: ["https://eth-rpc.testnet.near.org"] }, }, blockExplorers: { default: { name: "NEAR Explorer", - url: "https://testnet.nearblocks.io", + url: "https://eth-explorer-testnet.near.org", }, }, testnet: true, @@ -137,6 +135,7 @@ export const WalletSelectorContextProvider: React.FC<{ if (!selector) { return; } + // Watch the connected Ethereum account and connect to the `ethereum-wallets` module automatically. watchAccount(wagmiConfig, { onChange: (data: GetAccountReturnType) => { if (!data.address || selector.store.getState().selectedWalletId) { diff --git a/packages/ethereum-wallets/README.md b/packages/ethereum-wallets/README.md index 93e3b33ba..c0623e612 100644 --- a/packages/ethereum-wallets/README.md +++ b/packages/ethereum-wallets/README.md @@ -5,19 +5,19 @@ The package adds support for Ethereum wallets by creating Ethereum-compatible tr Ethereum wallet support NEP: https://github.com/near/NEPs/issues/518 -Any Ethereum wallet can be connected via Web3Modal: the dApp can chose which wallets to support and a multichain dApp can switch networks using the same wallet connection. +Any Ethereum wallet can be connected via Web3Modal: the App can chose which wallets to support and a multichain App can switch networks using the same wallet connection. SignIn requires switching to NEAR network to ensure that the wallet is compatible, if the user switches to other networks he will be prompted to switch back to NEAR before making a transaction. Sign out prompts to remove the FunctionCall access key if there is one, this action is non blocking and the user can sign out without executing the transaction. -A NEAR dApp can connect to multiple Ethereum wallet addresses. If the user switches to a new address from the Ethereum wallet, the NEAR wallet will be disconnected so that it can reconnect with the signIn flow. If the dApp doesn't require a FunctionCall access key or the Ethereum wallet address already signed in, then the address connects automatically when changed. +A NEAR App can connect to multiple Ethereum wallet addresses. If the user switches to a new address from the Ethereum wallet, the NEAR wallet will be disconnected so that it can reconnect with the signIn flow. If the App doesn't require a FunctionCall access key or the Ethereum wallet address already signed in, then the address connects automatically when changed. NEP-518 doesn't support multiple actions within the same transaction, so when multiple actions are requested, they are split into separate transactions and executed 1 by 1. NEP-518 rpc relayer uses a FunctionCall access key to execute transactions on behalf of the user by calling `rlp_execute`. If this key is not yet added, the wallet will be onboarded before the first transaction is made. -`signMessage` and `verifyOwner` are not implemented because Ethereum wallets are not compatible with these standards, instead a dApp can use `personal_sign` or `eth_signTypedData_v4` to authenticate the wallet by interacting with it directly. +`signMessage` and `verifyOwner` are not implemented because Ethereum wallets are not compatible with these standards, instead Apps can use `personal_sign` or `eth_signTypedData_v4` to authenticate the wallet by interacting with it directly. ## Installation and Usage @@ -29,42 +29,113 @@ yarn add near-api-js @web3modal/wagmi wagmi viem @tanstack/react-query @near-wal npm install near-api-js @web3modal/wagmi wagmi viem @tanstack/react-query @near-wallet-selector/ethereum-wallets ``` -Then use it in your dApp: +Then use it in your App: Visit https://docs.walletconnect.com for the latest configuration of Web3Modal. +Tested versions from `/examples`: +```json +"dependencies": { + "@web3modal/wagmi": "5.0.6", + "@tanstack/react-query": "5.24.8", + "viem": "2.16.2", + "wagmi": "2.10.9", +} +``` + ```ts import type { Config } from "@wagmi/core"; +import type { Chain } from "@wagmi/core/chains"; import { reconnect, http, createConfig } from "@wagmi/core"; import { walletConnect, injected } from "@wagmi/connectors"; import { setupWalletSelector } from "@near-wallet-selector/core"; +import { setupModal } from "@near-wallet-selector/modal-ui"; import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets"; +// Mainnet +const near: Chain = { + id: 397, + name: "NEAR Protocol", + nativeCurrency: { + decimals: 18, + name: "NEAR", + symbol: "NEAR", + }, + rpcUrls: { + default: { http: ["https://eth-rpc.mainnet.near.org"] }, + public: { http: ["https://eth-rpc.mainnet.near.org"] }, + }, + blockExplorers: { + default: { + name: "NEAR Explorer", + url: "https://eth-explorer.near.org", + }, + }, +} + +// Testnet +/* +const near: Chain = { + id: 398, + name: "NEAR Protocol Testnet", + nativeCurrency: { + decimals: 18, + name: "NEAR", + symbol: "NEAR", + }, + rpcUrls: { + default: { http: ["https://eth-rpc.testnet.near.org"] }, + public: { http: ["https://eth-rpc.testnet.near.org"] }, + }, + blockExplorers: { + default: { + name: "NEAR Explorer", + url: "https://eth-explorer-testnet.near.org", + }, + }, + testnet: true, +}; +*/ + +// Get a project ID at https://cloud.walletconnect.com +const projectId = "" + const wagmiConfig: Config = createConfig({ chains: [near], transports: { [near.id]: http(), }, connectors: [ - walletConnect({ projectId, metadata, showQrModal: false }), + walletConnect({ + projectId, + metadata: { + name: "NEAR Guest Book", + description: "A guest book with comments stored on the NEAR blockchain", + url: "https://near.github.io/wallet-selector", + icons: ["https://near.github.io/wallet-selector/favicon.ico"], + }, + showQrModal: false + }), injected({ shimDisconnect: true }), ], }); -reconnect(wagmiConfig); const web3Modal = createWeb3Modal({ - wagmiConfig: config, - // Get a project ID at https://cloud.walletconnect.com + wagmiConfig, projectId, }); -const _selector = await setupWalletSelector({ - network: "mainnet", - debug: true, - modules: [ - setupEthereumWallets({ wagmiConfig, web3Modal }), - ], -}); +export const WalletSelectorContextProvider = () => { + const init = useCallback(async () => { + const _selector = await setupWalletSelector({ + network: "mainnet", + modules: [ + setupEthereumWallets({ wagmiConfig, web3Modal }), + ], + }); + const _modal = setupModal(_selector, { contractId: "" }) + }, []); +} ``` ## Wallet Connect Configuration @@ -77,8 +148,9 @@ Project ID is required, please obtain it from [walletconnect.com](https://wallet - `web3Modal` (`Web3Modal?`): Web3Modal object for connecting an Ethereum wallet. If not provided this module will connect to the default injected wallet (MetaMask browser extension, embedded browser wallets...). - `chainId` (`number?`): Chain ID of the NEAR web3 rpc to connect to. Defaults to `397` (`mainnet`) or `398` (`testnet`) depending on the `setupWalletSelector` network configuration. - `iconUrl` (`string?`): Image URL for the icon shown in the modal. This can also be a relative path or base64 encoded image. Defaults to `./assets/ethereum-wallets-icon.png`. -- `wagmiCore` (`typeof import("@wagmi/core")?`): Optional, @wagmi/core functions can be overidden by the dapp to interract with the wallet. -- `alwaysOnboardDuringSignIn` (`boolean?`): A dapp without SignIn access key will not onboard the relayer by default, this option does the relayer onboarding during login. +- `wagmiCore` (`typeof import("@wagmi/core")?`): Optional, @wagmi/core functions can be overidden by the App to interract with the wallet. +- `alwaysOnboardDuringSignIn` (`boolean?`): Apps without SignIn access key will not onboard the relayer by default, this option does the relayer onboarding during login. +- `skipSignInAccessKey` (`boolean?`): Allows connecting Ethereum wallets without adding a Limited Access Key which would require owning NEAR to execute the transaction (for rainbowbridge.app and welcome.near.org). - `nearNodeUrl` (`string?`): NEAR node url to query the NEAR transaction status and onboarding access key. Developent options (before the NEAR protocol upgrade to support 0x accounts natively): @@ -88,33 +160,22 @@ Developent options (before the NEAR protocol upgrade to support 0x accounts nati ## Log in with Ethereum flow -Dapps can connect to Ethereum wallets directly (using `w3m-button` for example) by watching the connected Ethereum account and connecting to the `ethereum-wallets` module automatically without opening the NEAR modal. +Apps can connect to Ethereum wallets directly without opening the NEAR modal (using a dedicated button). ```js -import { watchAccount } from "@wagmi/core"; -useEffect(() => { - if (!selector) { - return - } - watchAccount(wagmiConfig, { - onChange: (data) => { - if (!data.address || selector.store.getState().selectedWalletId) { - return - } - selector.wallet("ethereum-wallets").then((wallet) => - wallet.signIn({ - contractId: CONTRACT_ID, - }) - ) - }, - }) -}, [selector]) +const loginWithEthereum = () => { + selector.wallet("ethereum-wallets").then((wallet) => + wallet.signIn({ + contractId: CONTRACT_ID, + }) + ) +} ``` ## Use without Web3Modal -Web3Modal and Log in with Ethereum flows are the preferred UX for connecting to any Ethereum wallet. -But this module is also available to use without Web3Modal: it will connect to the default injected wallet (Metamask browser extension, embedded browser wallets...). +Web3Modal is the preferred UX for connecting to any Ethereum wallet. +But `ethereum-wallets` is also available to use without Web3Modal: it will connect to the default injected wallet (Metamask browser extension, embedded browser wallets...). ## License diff --git a/packages/ethereum-wallets/src/lib/index.ts b/packages/ethereum-wallets/src/lib/index.ts index 92efa1fec..0d9acfe75 100644 --- a/packages/ethereum-wallets/src/lib/index.ts +++ b/packages/ethereum-wallets/src/lib/index.ts @@ -65,6 +65,7 @@ export interface EthereumWalletsParams { devModeAccount?: string; deprecated?: boolean; nearNodeUrl?: string; + skipSignInAccessKey?: boolean; } interface EthereumWalletsState { @@ -105,6 +106,7 @@ const EthereumWallets: WalletBehaviourFactory< devMode, devModeAccount = "eth-wallet.testnet", nearNodeUrl, + skipSignInAccessKey, }, }) => { if (!wagmiCore) { @@ -848,7 +850,7 @@ const EthereumWallets: WalletBehaviourFactory< // Login with FunctionCall access key, reuse keypair or create a new one. const accountId = devMode ? address + "." + devModeAccount : address; let publicKey; - if (contractId) { + if (contractId && !skipSignInAccessKey) { const keyPair = await _state.keystore.getKey( options.network.networkId, accountId @@ -995,7 +997,7 @@ export function setupEthereumWallets( iconUrl: params.iconUrl ?? icon, deprecated: params.deprecated ?? false, available: true, - downloadUrl: "", + downloadUrl: "https://explorer.walletconnect.com", }, init: (config) => { return EthereumWallets({