Skip to content

Commit

Permalink
feat: wagmi as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
anshss committed Jan 2, 2025
1 parent c2e4e6d commit 6711032
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
35 changes: 30 additions & 5 deletions packages/auth-browser/src/lib/chains/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ interface signAndSaveAuthParams {
expiration: string;
uri?: string;
nonce: string;
provider: AuthProvider;
}

interface IABI {
Expand Down Expand Up @@ -124,6 +125,7 @@ interface SignMessageParams {
body: string;
web3: Web3Provider;
account: string;
provider: AuthProvider;
}

interface SignedMessage {
Expand Down Expand Up @@ -619,19 +621,28 @@ export const checkAndSignEVMAuthMessage = async ({
const selectedChain = LIT_CHAINS[chain];
const expirationString = expiration ?? getDefaultExpiration();

let web3: Web3Provider;
let account: string;
let web3: Web3Provider | undefined;
let account: string | undefined;

if (provider === AuthProvider.Wagmi) {
({ web3, account } = await connectWeb3WithWagmi({
chainId: selectedChain.chainId,
walletConnectProjectId,
}));
} else {
} else if (provider === AuthProvider.LitConnectModal) {
({ web3, account } = await connectWeb3WithLitConnectModal({
chainId: selectedChain.chainId,
walletConnectProjectId,
}));
} else {
throw new Error('Invalid provider');
}

if (!web3) {
throw new Error('Web3Provider is undefined');
}
if (!account) {
throw new Error('Account is undefined');
}

log(`got web3 and account: ${account}`);
Expand Down Expand Up @@ -668,7 +679,7 @@ export const checkAndSignEVMAuthMessage = async ({

// -- 4. case: (current chain id is NOT equal to selected chain) AND is set to switch chain
if (currentChainIdOrError.result !== selectedChainId && switchChain) {
const provider = web3.provider as any;
const provider = web3?.provider as any;

// -- (case) if able to switch chain id
try {
Expand Down Expand Up @@ -733,6 +744,7 @@ export const checkAndSignEVMAuthMessage = async ({
expiration: expirationString,
uri,
nonce,
provider,
});

authSigOrError = {
Expand Down Expand Up @@ -779,6 +791,7 @@ export const checkAndSignEVMAuthMessage = async ({
expiration: expirationString,
uri,
nonce,
provider,
});
log('7. authSig:', authSig);

Expand All @@ -795,6 +808,7 @@ export const checkAndSignEVMAuthMessage = async ({
expiration: expirationString,
uri,
nonce,
provider,
});
}
log('8. mustResign:', mustResign);
Expand All @@ -819,6 +833,7 @@ export const checkAndSignEVMAuthMessage = async ({
expiration: expirationString,
uri,
nonce,
provider,
});
}

Expand All @@ -837,6 +852,7 @@ const _signAndGetAuth = async ({
expiration,
uri,
nonce,
provider,
}: signAndSaveAuthParams): Promise<AuthSig> => {
await signAndSaveAuthMessage({
web3,
Expand All @@ -846,6 +862,7 @@ const _signAndGetAuth = async ({
expiration,
uri,
nonce,
provider,
});

const authSigOrError = getStorageItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE);
Expand Down Expand Up @@ -885,6 +902,7 @@ export const signAndSaveAuthMessage = async ({
expiration,
uri,
nonce,
provider,
}: signAndSaveAuthParams): Promise<AuthSig> => {
// check if it's nodejs
if (isNode()) {
Expand Down Expand Up @@ -925,6 +943,7 @@ export const signAndSaveAuthMessage = async ({
body,
web3,
account: formattedAccount,
provider,
});

// -- 3. prepare auth message
Expand Down Expand Up @@ -970,6 +989,7 @@ export const signMessage = async ({
body,
web3,
account,
provider,
}: SignMessageParams): Promise<SignedMessage> => {
// check if it's nodejs
if (isNode()) {
Expand All @@ -983,7 +1003,12 @@ export const signMessage = async ({
// -- validate
if (!web3 || !account) {
log(`web3: ${web3} OR ${account} not found. Connecting web3..`);
const res = await connectWeb3({ chainId: 1 });
let res;
if (provider === AuthProvider.Wagmi) {
res = await connectWeb3WithWagmi({ chainId: 1 });
} else {
res = await connectWeb3WithLitConnectModal({ chainId: 1 });
}
web3 = res.web3;
account = res.account;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export interface CosmosAuthSig extends AuthSig {
export type CosmosWalletType = 'keplr' | 'leap';

export enum AuthProvider {
Wagmi = "wagmi",
LitConnectModal = "litConnectModal",
Wagmi = 'wagmi',
LitConnectModal = 'litConnectModal',
}

export interface AuthCallbackParams extends LitActionSdkParams {
Expand Down

0 comments on commit 6711032

Please sign in to comment.