-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from AreaLayer/connection-provider
[DEV] [Feature] Wallet Connection
- Loading branch information
Showing
8 changed files
with
362 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
import {Box} from '@gluestack-ui/themed'; | ||
|
||
export const Splash = () => { | ||
return <Box bg="$primary400" flex={1} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, {useContext, createContext, useEffect, useState} from 'react'; | ||
import storage from '../utils/storage'; | ||
import {STORAGE_KEYS} from '../utils/storage/storageKeys'; | ||
import {decryptAesGcm} from '../utils/encryption'; | ||
|
||
interface Props { | ||
children: React.ReactNode; | ||
} | ||
interface ContextProps { | ||
loading: boolean; | ||
isWalletConnected: boolean; | ||
validatePin: (pin: string) => boolean; | ||
seed: string | undefined; | ||
} | ||
const ConnectionContext = createContext<ContextProps>({ | ||
loading: false, | ||
isWalletConnected: false, | ||
validatePin: () => false, | ||
seed: undefined, | ||
}); | ||
|
||
export const ConnectionProvider = ({children}: Props) => { | ||
const [loading, setLoading] = useState(false); | ||
const [encryptedSeed, setEncryptedSeed] = useState(); | ||
const [seed, setSeed] = useState<string | undefined>(); | ||
|
||
useEffect(() => { | ||
const loadWallet = async () => { | ||
setLoading(true); | ||
try { | ||
const data = await storage.load({key: STORAGE_KEYS.seedCipher}); | ||
setEncryptedSeed(data); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
loadWallet(); | ||
}, []); | ||
|
||
const validatePin = (pin: string) => { | ||
if (encryptedSeed) { | ||
const decryptedSeed = decryptAesGcm(encryptedSeed, pin); | ||
if (decryptedSeed) { | ||
setSeed(decryptedSeed); | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
return ( | ||
<ConnectionContext.Provider | ||
value={{loading, isWalletConnected: !!encryptedSeed, validatePin, seed}}> | ||
{children} | ||
</ConnectionContext.Provider> | ||
); | ||
}; | ||
|
||
export const useConnectionContext = () => { | ||
const context = useContext(ConnectionContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
'useConnectionContext must be used within ConnectionProvider', | ||
); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.