Skip to content

Replace litConnectModal with Wagmi #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c235094
replace litConnectModal with wagmi
anshss Dec 28, 2024
94c091a
Merge branch 'master' into anshs
Ansonhkg Dec 30, 2024
429b95c
fix: linting errs
anshss Jan 2, 2025
c2e4e6d
refactor: litConnectModal as deafult
anshss Jan 2, 2025
456ad3b
chore: update version to `7.0.4`
Ansonhkg Jan 2, 2025
6711032
feat: wagmi as optional
anshss Jan 2, 2025
9fd958c
fmt
Ansonhkg Jan 2, 2025
1525da4
Merge pull request #753 from LIT-Protocol/v7.0.4-Publish
Ansonhkg Jan 2, 2025
412264b
fmt
Ansonhkg Jan 2, 2025
382d38d
Merge branch 'master' into anshs
Ansonhkg Jan 2, 2025
39af557
docs: improve package descriptions in READMEs
devin-ai-integration[bot] Jan 2, 2025
b3f6537
docs: update core package READMEs with improved descriptions and exam…
devin-ai-integration[bot] Jan 2, 2025
dd4a818
docs: update auth package READMEs with improved descriptions
devin-ai-integration[bot] Jan 2, 2025
14b4c41
docs: update PKP package READMEs with improved documentation
devin-ai-integration[bot] Jan 2, 2025
873aacf
docs: improve package descriptions and standardize README formatting
devin-ai-integration[bot] Jan 2, 2025
19054af
docs: improve root README.md organization and clarity
devin-ai-integration[bot] Jan 2, 2025
5884685
docs: fix formatting issues in README.md
devin-ai-integration[bot] Jan 2, 2025
7893a7c
doc: clean up devin's work
Ansonhkg Jan 2, 2025
763bd14
fmt
Ansonhkg Jan 2, 2025
fee3b14
doc: cleanup Devin's work
Ansonhkg Jan 2, 2025
862199b
fix logger test
susumutomita Jan 3, 2025
30f3217
fix wrapped keys test issue760
susumutomita Jan 4, 2025
9f0bcde
docs: address reviewer feedback on README
devin-ai-integration[bot] Jan 6, 2025
45efa33
chore: fix formatting
devin-ai-integration[bot] Jan 6, 2025
6cd6f07
Update packages/pkp-base/README.md
Ansonhkg Jan 6, 2025
90ee710
chore: apply prettier formatting
devin-ai-integration[bot] Jan 6, 2025
35137ce
feat: fix feedback devin missed
FedericoAmura Jan 6, 2025
0801de0
feat: fix feedback devin missed
FedericoAmura Jan 6, 2025
b1ed607
Merge pull request #761 from susumutomita/fix/wrapped-keys-test-issue760
Ansonhkg Jan 15, 2025
71a8f2d
Merge pull request #759 from susumutomita/bugfix/logger-tests-issue758
Ansonhkg Jan 15, 2025
6fb9791
Merge pull request #754 from LIT-Protocol/devin/1735845077-update-rea…
Ansonhkg Jan 15, 2025
e1fd8bd
Merge branch 'master' into anshs
Ansonhkg Jan 15, 2025
f1d068d
fix(ci): update Rust version to 1.83.0 for wasm compatibility
Ansonhkg Jan 15, 2025
d4e1a64
Merge pull request #766 from LIT-Protocol/feature/lit-4129-js-sdk-fix…
Ansonhkg Jan 15, 2025
309512b
Merge branch 'master' into anshs
Ansonhkg Jan 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"@openagenda/verror": "^3.1.4",
"@simplewebauthn/browser": "^7.2.0",
"@simplewebauthn/typescript-types": "^7.0.0",
"@tanstack/react-query": "^5.62.11",
"@wagmi/core": "^2.16.3",
"@walletconnect/ethereum-provider": "2.9.2",
"@walletconnect/jsonrpc-utils": "1.0.8",
"@walletconnect/types": "2.9.2",
Expand All @@ -64,12 +66,15 @@
"micromodal": "^0.4.10",
"multiformats": "^9.7.1",
"pako": "^2.1.0",
"react": "^19.0.0",
"siwe": "^2.3.2",
"siwe-recap": "0.0.2-alpha.0",
"tslib": "^2.7.0",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"uint8arrays": "^4.0.3"
"uint8arrays": "^4.0.3",
"viem": "^2.21.57",
"wagmi": "^2.14.6"
},
"devDependencies": {
"@nx/devkit": "17.3.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/auth-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
"tweetnacl-util": "^0.13.3",
"util": "^0.12.4",
"web-vitals": "^3.0.4",
"@lit-protocol/contracts": "^0.0.74"
"@lit-protocol/contracts": "^0.0.74",
"wagmi": "^2.14.6",
"viem": "^2.0.0",
"@tanstack/react-query": "^5.0.0",
"react": "^18.0.0"
},
"tags": [
"browser"
Expand Down
170 changes: 75 additions & 95 deletions packages/auth-browser/src/lib/chains/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { hexlify } from '@ethersproject/bytes';
import { Web3Provider, JsonRpcSigner } from '@ethersproject/providers';
import { toUtf8Bytes } from '@ethersproject/strings';

// import WalletConnectProvider from '@walletconnect/ethereum-provider';
import { verifyMessage } from '@ethersproject/wallet';
import {
EthereumProvider,
default as WalletConnectProvider,
} from '@walletconnect/ethereum-provider';
import { ethers } from 'ethers';
import { getAddress } from 'ethers/lib/utils';
import { SiweMessage } from 'siwe';
Expand Down Expand Up @@ -43,9 +38,14 @@ import {
validateSessionSig,
} from '@lit-protocol/misc';
import { getStorageItem } from '@lit-protocol/misc-browser';
import { AuthSig, AuthCallbackParams } from '@lit-protocol/types';
import { AuthSig, AuthCallbackParams, LITEVMChain } from '@lit-protocol/types';

import LitConnectModal from '../connect-modal/modal';
import {
createConfig,
} from 'wagmi';
import { connect as wagmiConnect, disconnect as wagmiDisconnect, getWalletClient, Config } from '@wagmi/core';
import { injected, walletConnect } from '@wagmi/connectors'
import { http } from 'viem';

const deprecated = depd('lit-js-sdk:auth-browser:index');

Expand Down Expand Up @@ -74,8 +74,6 @@ interface ConnectWeb3Result {
account: string | any;
}

type RPCUrls = Record<string, string>;

interface signAndSaveAuthParams {
web3: Web3Provider;
account: string;
Expand Down Expand Up @@ -131,8 +129,7 @@ export type WALLET_ERROR_VALUES =

/** ---------- Local Helpers ---------- */

let litWCProvider: WalletConnectProvider | undefined;

let wagmiConfig: Config;
/**
*
* Convert chain hex id to chain name
Expand Down Expand Up @@ -291,35 +288,6 @@ export const getMustResign = (authSig: AuthSig, resources: any): boolean => {
return mustResign;
};

/**
*
* Get RPC Urls in the correct format
* need to make it look like this:
---
rpc: {
1: "https://mainnet.mycustomnode.com",
3: "https://ropsten.mycustomnode.com",
100: "https://dai.poa.network",
// ...
},
---
*
* @returns
*/
export const getRPCUrls = (): RPCUrls => {
const rpcUrls: RPCUrls = {};

const keys: string[] = Object.keys(LIT_CHAINS);

for (const chainName of keys) {
const chainId = LIT_CHAINS[chainName].chainId;
const rpcUrl = LIT_CHAINS[chainName].rpcUrls[0];
rpcUrls[chainId.toString()] = rpcUrl;
}

return rpcUrls;
};

/** ---------- Exports ---------- */
/**
* @deprecated
Expand Down Expand Up @@ -353,6 +321,49 @@ export const decodeCallResult = deprecated.function(
'decodeCallResult will be removed.'
);

const getWagmiProvider = async (chainId: number, walletConnectProjectId?: string) => {
const chain = Object.values(LIT_CHAINS).find(c => c.chainId === chainId);
if (!chain) {
throw new Error(`Chain ID ${chainId} not supported`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we throw a ParamsMissingError or another LitError? 🙏🏻
Same on all other throw Error lines

}

const litChainToWagmiChain = (litChain: LITEVMChain) => ({
id: litChain.chainId,
name: litChain.name,
network: litChain.name.toLowerCase(),
nativeCurrency: {
name: litChain.name,
symbol: litChain.symbol,
decimals: litChain.decimals,
},
rpcUrls: {
default: { http: litChain.rpcUrls },
public: { http: litChain.rpcUrls },
},
});

const litChain = litChainToWagmiChain(chain);

const config = createConfig({
chains: [litChain],
transports: {
[litChain.id]: http(litChain.rpcUrls.default.http[0])
},
connectors: [
injected(),
...(walletConnectProjectId
? [
walletConnect({
projectId: walletConnectProjectId
})
]
: []),
]
});

return config;
};

/**
* @browserOnly
* Connect to web 3
Expand All @@ -371,58 +382,33 @@ export const connectWeb3 = async ({
return { web3: null, account: null };
}

const rpcUrls: RPCUrls = getRPCUrls();

let providerOptions = {};

if (walletConnectProjectId) {
const wcProvider = await EthereumProvider.init({
projectId: walletConnectProjectId,
chains: [chainId],
showQrModal: true,
optionalMethods: ['eth_sign'],
rpcMap: rpcUrls,
});

providerOptions = {
walletconnect: {
provider: wcProvider,
},
};

if (isBrowser()) {
litWCProvider = wcProvider;
}
}

log('getting provider via lit connect modal');
log('getting provider via wagmi');

const dialog = new LitConnectModal({ providerOptions });

const provider = await dialog.getWalletProvider();
const config = await getWagmiProvider(chainId, walletConnectProjectId);
wagmiConfig = config;

const result = await wagmiConnect(config, {
connector: config.connectors[0],
chainId,
});


log('got provider');
if (!result) {
throw new Error('Failed to connect wallet');
}

// @ts-ignore
const web3 = new Web3Provider(provider);

// trigger metamask popup
try {
deprecated(
'@deprecated soon to be removed. - trying to enable provider. this will trigger the metamask popup.'
);
// @ts-ignore
await provider.enable();
} catch (e) {
log(
"error enabling provider but swallowed it because it's not important. most wallets use a different function now to enable the wallet so you can ignore this error, because those other methods will be tried.",
e
);
const walletClient = await getWalletClient(config);

if (!walletClient) {
throw new Error('No wallet client found');
}

// @ts-ignore - Create Web3Provider from wallet client
const web3 = new Web3Provider(walletClient);

log('listing accounts');
const accounts = await web3.listAccounts();

log('accounts', accounts);
const account = ethers.utils.getAddress(accounts[0]);

Expand All @@ -437,22 +423,16 @@ export const connectWeb3 = async ({
*
* @return { void }
*/
export const disconnectWeb3 = (): void => {
export const disconnectWeb3 = async (): Promise<void> => {
if (isNode()) {
log('disconnectWeb3 is not supported in nodejs.');
return;
}

// @ts-ignore
if (isBrowser() && litWCProvider) {
try {
litWCProvider.disconnect();
} catch (err) {
log(
'Attempted to disconnect global WalletConnectProvider for lit-connect-modal',
err
);
}
try {
await wagmiDisconnect(wagmiConfig);
} catch (err) {
log('Error disconnecting wallet:', err);
}

const storage = LOCAL_STORAGE_KEYS;
Expand Down
Loading
Loading