Skip to content

Commit b59ea6a

Browse files
committed
fix: wagmi as optional provider
1 parent c2e4e6d commit b59ea6a

File tree

1 file changed

+30
-5
lines changed
  • packages/auth-browser/src/lib/chains

1 file changed

+30
-5
lines changed

packages/auth-browser/src/lib/chains/eth.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ interface signAndSaveAuthParams {
9494
expiration: string;
9595
uri?: string;
9696
nonce: string;
97+
provider: AuthProvider;
9798
}
9899

99100
interface IABI {
@@ -124,6 +125,7 @@ interface SignMessageParams {
124125
body: string;
125126
web3: Web3Provider;
126127
account: string;
128+
provider: AuthProvider;
127129
}
128130

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

622-
let web3: Web3Provider;
623-
let account: string;
624+
let web3: Web3Provider | undefined;
625+
let account: string | undefined;
624626

625627
if (provider === AuthProvider.Wagmi) {
626628
({ web3, account } = await connectWeb3WithWagmi({
627629
chainId: selectedChain.chainId,
628630
walletConnectProjectId,
629631
}));
630-
} else {
632+
} else if (provider === AuthProvider.LitConnectModal) {
631633
({ web3, account } = await connectWeb3WithLitConnectModal({
632634
chainId: selectedChain.chainId,
633635
walletConnectProjectId,
634636
}));
637+
} else {
638+
throw new Error('Invalid provider');
639+
}
640+
641+
if (!web3) {
642+
throw new Error('Web3Provider is undefined');
643+
}
644+
if (!account) {
645+
throw new Error('Account is undefined');
635646
}
636647

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

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

673684
// -- (case) if able to switch chain id
674685
try {
@@ -733,6 +744,7 @@ export const checkAndSignEVMAuthMessage = async ({
733744
expiration: expirationString,
734745
uri,
735746
nonce,
747+
provider,
736748
});
737749

738750
authSigOrError = {
@@ -779,6 +791,7 @@ export const checkAndSignEVMAuthMessage = async ({
779791
expiration: expirationString,
780792
uri,
781793
nonce,
794+
provider,
782795
});
783796
log('7. authSig:', authSig);
784797

@@ -795,6 +808,7 @@ export const checkAndSignEVMAuthMessage = async ({
795808
expiration: expirationString,
796809
uri,
797810
nonce,
811+
provider,
798812
});
799813
}
800814
log('8. mustResign:', mustResign);
@@ -819,6 +833,7 @@ export const checkAndSignEVMAuthMessage = async ({
819833
expiration: expirationString,
820834
uri,
821835
nonce,
836+
provider,
822837
});
823838
}
824839

@@ -837,6 +852,7 @@ const _signAndGetAuth = async ({
837852
expiration,
838853
uri,
839854
nonce,
855+
provider,
840856
}: signAndSaveAuthParams): Promise<AuthSig> => {
841857
await signAndSaveAuthMessage({
842858
web3,
@@ -846,6 +862,7 @@ const _signAndGetAuth = async ({
846862
expiration,
847863
uri,
848864
nonce,
865+
provider,
849866
});
850867

851868
const authSigOrError = getStorageItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE);
@@ -885,6 +902,7 @@ export const signAndSaveAuthMessage = async ({
885902
expiration,
886903
uri,
887904
nonce,
905+
provider,
888906
}: signAndSaveAuthParams): Promise<AuthSig> => {
889907
// check if it's nodejs
890908
if (isNode()) {
@@ -925,6 +943,7 @@ export const signAndSaveAuthMessage = async ({
925943
body,
926944
web3,
927945
account: formattedAccount,
946+
provider,
928947
});
929948

930949
// -- 3. prepare auth message
@@ -970,6 +989,7 @@ export const signMessage = async ({
970989
body,
971990
web3,
972991
account,
992+
provider,
973993
}: SignMessageParams): Promise<SignedMessage> => {
974994
// check if it's nodejs
975995
if (isNode()) {
@@ -983,7 +1003,12 @@ export const signMessage = async ({
9831003
// -- validate
9841004
if (!web3 || !account) {
9851005
log(`web3: ${web3} OR ${account} not found. Connecting web3..`);
986-
const res = await connectWeb3({ chainId: 1 });
1006+
let res;
1007+
if (provider === AuthProvider.Wagmi) {
1008+
res = await connectWeb3WithWagmi({ chainId: 1 });
1009+
} else {
1010+
res = await connectWeb3WithLitConnectModal({ chainId: 1 });
1011+
}
9871012
web3 = res.web3;
9881013
account = res.account;
9891014
}

0 commit comments

Comments
 (0)