-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
809 additions
and
292 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,22 @@ | ||
import { useInterval } from 'usehooks-ts'; | ||
import { RESCAN_INTERVAL } from '@webzjs/demo-wallet/src/App/Constants.tsx'; | ||
import { useWebZjsActions } from '@hooks/useWebzjsActions.ts'; | ||
import Layout from '@components/Layout/Layout.tsx'; | ||
import { Outlet } from 'react-router-dom'; | ||
|
||
function App() { | ||
const { triggerRescan } = useWebZjsActions(); | ||
|
||
// rescan the wallet periodically | ||
useInterval(() => { | ||
triggerRescan(); | ||
}, RESCAN_INTERVAL); | ||
|
||
return ( | ||
<Layout> | ||
<Outlet /> | ||
</Layout> | ||
); | ||
} | ||
|
||
export default App; |
2 changes: 1 addition & 1 deletion
2
packages/web-wallet/src/components/ProtectedRoute/ProtectedRoute.tsx
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,4 @@ | ||
export const MAINNET_LIGHTWALLETD_PROXY = 'https://zcash-mainnet.chainsafe.dev'; | ||
export const ZATOSHI_PER_ZEC = 1e8; | ||
export const RESCAN_INTERVAL = 20000; | ||
export const NU5_ACTIVATION = 1687104; |
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,193 @@ | ||
import React, { createContext, useReducer, useEffect } from 'react'; | ||
import type { ReactNode } from 'react'; | ||
import type { MetaMaskInpageProvider } from '@metamask/providers'; | ||
import { get, set } from 'idb-keyval'; | ||
|
||
import initWebzWallet, { initThreadPool, WebWallet } from '@webzjs/webz-wallet'; | ||
import initWebzKeys from '@webzjs/webz-keys'; | ||
|
||
import type { Snap } from '../types'; | ||
import { getSnapsProvider } from '../utils'; | ||
import { MAINNET_LIGHTWALLETD_PROXY } from '../config/constants.ts'; | ||
|
||
interface Summary { | ||
chain_tip_height: number; | ||
fully_scanned_height: number; | ||
next_sapling_subtree_index: bigint; | ||
next_orchard_subtree_index: bigint; | ||
account_balances: [number, number][]; | ||
} | ||
|
||
interface State { | ||
webWallet: WebWallet | null; | ||
provider: MetaMaskInpageProvider | null; | ||
installedSnap: Snap | null; | ||
error: Error | null; | ||
summary: Summary | null; | ||
chainHeight: bigint | null; | ||
activeAccount: number | null; | ||
syncInProgress: boolean; | ||
loading: boolean; | ||
} | ||
|
||
type Action = | ||
| { type: 'set-web-wallet'; payload: WebWallet } | ||
| { type: 'set-provider'; payload: MetaMaskInpageProvider | null } | ||
| { type: 'set-error'; payload: Error | null } | ||
| { type: 'set-summary'; payload: Summary } | ||
| { type: 'set-chain-height'; payload: bigint } | ||
| { type: 'set-active-account'; payload: number } | ||
| { type: 'set-sync-in-progress'; payload: boolean } | ||
| { type: 'set-loading'; payload: boolean }; | ||
|
||
const initialState: State = { | ||
webWallet: null, | ||
provider: null, | ||
installedSnap: null, | ||
error: null, | ||
summary: null, | ||
chainHeight: null, | ||
activeAccount: null, | ||
syncInProgress: false, | ||
loading: true, | ||
}; | ||
|
||
function reducer(state: State, action: Action): State { | ||
switch (action.type) { | ||
case 'set-web-wallet': | ||
return { ...state, webWallet: action.payload }; | ||
case 'set-provider': | ||
return { ...state, provider: action.payload }; | ||
case 'set-error': | ||
return { ...state, error: action.payload }; | ||
case 'set-summary': | ||
return { ...state, summary: action.payload }; | ||
case 'set-chain-height': | ||
return { ...state, chainHeight: action.payload }; | ||
case 'set-active-account': | ||
return { ...state, activeAccount: action.payload }; | ||
case 'set-sync-in-progress': | ||
return { ...state, syncInProgress: action.payload }; | ||
case 'set-loading': | ||
return { ...state, loading: action.payload }; | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
|
||
interface WebZjsContextType { | ||
state: State; | ||
dispatch: React.Dispatch<Action>; | ||
} | ||
|
||
const WebZjsContext = createContext<WebZjsContextType>({ | ||
state: initialState, | ||
dispatch: () => {}, | ||
}); | ||
|
||
export const WebZjsProvider = ({ children }: { children: ReactNode }) => { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
|
||
// Initialize provider and web wallet | ||
useEffect(() => { | ||
initAll(); | ||
}, []); | ||
|
||
async function initAll() { | ||
try { | ||
await initWebzWallet(); | ||
await initWebzKeys(); | ||
try { | ||
await initThreadPool(10); | ||
} catch (err) { | ||
console.error(err); | ||
throw Error('Unable to initialize Thread Pool'); | ||
} | ||
const provider = await getSnapsProvider(); | ||
dispatch({ type: 'set-provider', payload: provider }); | ||
|
||
const bytes = await get('wallet'); | ||
let wallet; | ||
|
||
if (bytes) { | ||
console.info('Saved wallet detected. Restoring wallet from storage'); | ||
wallet = new WebWallet('main', MAINNET_LIGHTWALLETD_PROXY, 1, bytes); | ||
} else { | ||
console.info('No saved wallet detected. Creating new wallet'); | ||
wallet = new WebWallet('main', MAINNET_LIGHTWALLETD_PROXY, 1); | ||
} | ||
|
||
dispatch({ type: 'set-web-wallet', payload: wallet }); | ||
|
||
const summary = await wallet.get_wallet_summary(); | ||
if (summary) { | ||
dispatch({ type: 'set-summary', payload: summary }); | ||
// Set an active account from summary if available | ||
if (summary.account_balances.length > 0) { | ||
dispatch({ | ||
type: 'set-active-account', | ||
payload: summary.account_balances[0][0], | ||
}); | ||
} | ||
} | ||
|
||
const chainHeight = await wallet.get_latest_block(); | ||
if (chainHeight) { | ||
dispatch({ type: 'set-chain-height', payload: chainHeight }); | ||
} | ||
|
||
dispatch({ type: 'set-loading', payload: false }); | ||
} catch (err: never) { | ||
console.error('Initialization error:', err); | ||
dispatch({ type: 'set-error', payload: err.toString() }); | ||
dispatch({ type: 'set-loading', payload: false }); | ||
} | ||
} | ||
|
||
// Clear error after 10 seconds if any | ||
useEffect(() => { | ||
if (state.error) { | ||
const timeout = setTimeout(() => { | ||
dispatch({ type: 'set-error', payload: null }); | ||
}, 10000); | ||
|
||
return () => clearTimeout(timeout); | ||
} | ||
}, [state.error]); | ||
|
||
// Persist changes to IndexedDB whenever relevant parts of state change | ||
useEffect(() => { | ||
if (!state.webWallet) return; | ||
|
||
async function flushDb() { | ||
console.info('Serializing wallet and dumping to IndexedDB store'); | ||
|
||
if (state.webWallet instanceof WebWallet) { | ||
const bytes = await state.webWallet.db_to_bytes(); | ||
await set('wallet', bytes); | ||
console.info('Wallet saved to storage'); | ||
} | ||
} | ||
|
||
// Flush changes on these triggers: | ||
if (!state.loading && !state.syncInProgress && state.webWallet) { | ||
flushDb().catch(console.error); | ||
} | ||
}, [state.webWallet, state.syncInProgress, state.loading]); | ||
|
||
return ( | ||
<WebZjsContext.Provider value={{ state, dispatch }}> | ||
{children} | ||
</WebZjsContext.Provider> | ||
); | ||
}; | ||
|
||
export function useWebZjsContext(): WebZjsContextType { | ||
const context = React.useContext(WebZjsContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('useWebZjsContext must be used within a WebZjsProvider'); | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from './MetamaskContext'; | ||
export * from './useMetaMask'; | ||
export * from './useRequest'; | ||
export * from './useRequestSnap'; | ||
export * from './useInvokeSnap'; | ||
export * from './snaps/useMetaMask.ts'; | ||
export * from './snaps/useRequest.ts'; | ||
export * from './snaps/useRequestSnap.ts'; | ||
export * from './snaps/useInvokeSnap.ts'; | ||
export * from './useWebzjsActions.ts'; |
4 changes: 2 additions & 2 deletions
4
...ges/web-wallet/src/hooks/useInvokeSnap.ts → ...b-wallet/src/hooks/snaps/useInvokeSnap.ts
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
10 changes: 5 additions & 5 deletions
10
packages/web-wallet/src/hooks/useMetaMask.ts → ...web-wallet/src/hooks/snaps/useMetaMask.ts
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
2 changes: 1 addition & 1 deletion
2
packages/web-wallet/src/hooks/useRequest.ts → .../web-wallet/src/hooks/snaps/useRequest.ts
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
8 changes: 4 additions & 4 deletions
8
...es/web-wallet/src/hooks/useRequestSnap.ts → ...-wallet/src/hooks/snaps/useRequestSnap.ts
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.