Skip to content
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

feat: use last stored account #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 48 additions & 20 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import { getIpfsGatewayUrl } from './environment';
import { MetaMaskProvider } from './context/MetamaskContext';
import { SNAP_URL } from './urls';
import { connectWc } from './utils/walletConnect';
import { connectWc, getWcClient } from './utils/walletConnect';
import useConnectedWallet from './hooks/useConnectedWallet';

Check failure on line 22 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Import 'getWcClient' is declared but its value is never read. Allowed unused names must match /^_/
import useWcPreloader from './hooks/useWcPreloader';
import WcPreloader from './common/WcPreloader';
import useAccountSelector from './hooks/useAccountSelector';

Check failure on line 26 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

All imports in import declaration are unused
const { WalletSelector, walletSelectorOptions } = Components;

export const availableWalletOptions = [
Expand All @@ -33,29 +33,48 @@
walletSelectorOptions[reefExt.REEF_WALLET_CONNECT_IDENT]
];

const connectWcFromRes = (res:any,setSelExtensionName:any,ident:any,setWcPreloader:any)=>{
const sessionData = {

Check failure on line 37 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Missing return type on function

Check failure on line 37 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type

Check failure on line 37 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type

Check failure on line 37 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type

Check failure on line 37 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type
topic: res.session.topic,
relay: res.session.relay,
expiry: res.session.expiry,
namespaces: res.session.namespaces,
acknowledged: res.session.acknowledged,
pairingTopic: res.session.pairingTopic,
};

localStorage.setItem("wcSessionData", JSON.stringify(sessionData));
reefExt.injectWcAsExtension(res, {
name: reefExt.REEF_WALLET_CONNECT_IDENT,
version: "1.0.0"
});

setSelExtensionName(ident);

// Display preloader
setWcPreloader({
value: true,
message: "Wait while we are establishing a connection"
});
}

export const connectWalletConnect = async(ident:string,setSelExtensionName:any,setWcPreloader:any)=>{
setWcPreloader({

Check warning on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Missing return type on function

Check failure on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Missing return type on function

Check warning on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Argument 'setSelExtensionName' should be typed with a non-any type

Check failure on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type

Check warning on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Argument 'setWcPreloader' should be typed with a non-any type

Check failure on line 62 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Unexpected any. Specify a different type
value:true,
message:"initializing mobile app connection"
});
setSelExtensionName(undefined); //force setting this to different value from the ident initially or else it doesn't call useInitReefState hook

const response:reefExt.WcConnection | undefined = await connectWc(setWcPreloader)
console.log('connectWalletConnect',response);
if (response) {
reefExt.injectWcAsExtension(response, { name: reefExt.REEF_WALLET_CONNECT_IDENT, version: "1.0.0" });
setSelExtensionName(ident);
// display preloader
setWcPreloader({
value:true,
message:"wait while we are establishing a connection"
});
} else {
// if proposal expired, recursively call
Uik.notify.danger("Connection QR expired, reloading")
await connectWalletConnect(ident,setSelExtensionName,setWcPreloader);
}
}
const response: reefExt.WcConnection | undefined = await connectWc(setWcPreloader);

if (response) {
connectWcFromRes(response,setSelExtensionName,ident,setWcPreloader);
} else {
// If the proposal expired, recursively call
Uik.notify.danger("Connection QR expired, reloading");
await connectWalletConnect(ident, setSelExtensionName, setWcPreloader);
}
}

const App = (): JSX.Element => {
const {selExtensionName,setSelExtensionName} = useConnectedWallet();
Expand All @@ -73,15 +92,27 @@
setSelectedSigner(undefined);
},[selExtensionName])

//checking for previous wc session
useEffect(()=>{
const handleWcPreviousSession = async()=>{
if(selExtensionName==reefExt.REEF_WALLET_CONNECT_IDENT){
connectWalletConnect(reefExt.REEF_WALLET_CONNECT_IDENT,setSelExtensionName,setWcPreloader)
}
}

handleWcPreviousSession();

},[])

Check warning on line 105 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

React Hook useEffect has missing dependencies: 'selExtensionName', 'setSelExtensionName', and 'setWcPreloader'. Either include them or remove the dependency array

useEffect(()=>{
setAccounts(signers);
setSelectedSigner(selectedReefSigner);

// if account connected , hide preloader & set account address
if(signers?.length && signers?.indexOf(selectedReefSigner!)==-1){

Check warning on line 112 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

Forbidden non-null assertion
reefState.setSelectedAddress(signers[0].address)
}
},[selectedReefSigner,signers])

Check warning on line 115 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

React Hook useEffect has a missing dependency: 'reefState'. Either include it or remove the dependency array

const history = useHistory();
const [isBalanceHidden, setBalanceHidden] = useState(getStoredPref());
Expand All @@ -105,7 +136,7 @@
if (selExtensionName === reefExt.REEF_SNAP_IDENT && error?.code === 2) {
history.push(SNAP_URL);
}
}, [extension, error]);

Check warning on line 139 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

React Hook useEffect has missing dependencies: 'history' and 'selExtensionName'. Either include them or remove the dependency array

const[errorToast,setErrorToast] = useState<{
message:String;
Expand Down Expand Up @@ -165,12 +196,11 @@
message:""
})
}
},[signers])

Check warning on line 199 in src/App.tsx

View workflow job for this annotation

GitHub Actions / unit-lint

React Hook useEffect has missing dependencies: 'setWcPreloader' and 'wcPreloader.value'. Either include them or remove the dependency array



const onExtensionSelected = async(ident: string) => {
console.log('onExtensionSelected', ident);
try {
if (ident === reefExt.REEF_WALLET_CONNECT_IDENT) {
await connectWalletConnect(ident,setSelExtensionName,setWcPreloader);
Expand All @@ -182,8 +212,6 @@
}
}

const {isAccountSelectorOpen} = useAccountSelector()

// @ts-ignore
return (
<>
Expand Down
13 changes: 12 additions & 1 deletion src/utils/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ const appMetadata: CoreTypes.Metadata = {
icons: [window.location.origin + '/favicon.ico'],
};

export const getWcClient = async()=>{
return await reefExt.initWcClient(appMetadata);
}

export const connectWc = async (setWcPreloader:any): Promise<reefExt.WcConnection | undefined> => {
try {
const client = await reefExt.initWcClient(appMetadata);
const client = await getWcClient();

const wcSessionDataString = localStorage.getItem("wcSessionData");

if(wcSessionDataString){
const session= JSON.parse(wcSessionDataString);
return {client,session}
}

const { uri, approval } = await client.connect({
requiredNamespaces: reefExt.getWcRequiredNamespaces(),
Expand Down
Loading