-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(solid): combine into one file
- Loading branch information
Showing
5 changed files
with
201 additions
and
208 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
packages/use-wallet-solid/src/__tests__/WalletProvider.test.tsx
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,151 @@ | ||
import { useStore } from '@tanstack/solid-store' | ||
import algosdk from 'algosdk' | ||
import { JSX, createContext, createMemo, createSignal, onMount, useContext } from 'solid-js' | ||
import type { | ||
NetworkId, | ||
WalletAccount, | ||
WalletId, | ||
WalletManager, | ||
WalletMetadata, | ||
WalletState | ||
} from '@txnlab/use-wallet' | ||
|
||
export * from '@txnlab/use-wallet' | ||
export { WalletProvider } from './WalletProvider' | ||
export { useWallet } from './useWallet' | ||
|
||
interface WalletProviderProps { | ||
manager: WalletManager | ||
children: JSX.Element | ||
} | ||
|
||
const WalletContext = createContext<() => WalletManager>() | ||
|
||
export const WalletProvider = (props: WalletProviderProps) => { | ||
const store = () => props.manager | ||
|
||
onMount(async () => { | ||
try { | ||
await props.manager.resumeSessions() | ||
} catch (error) { | ||
console.error('Error resuming sessions:', error) | ||
} | ||
}) | ||
|
||
return <WalletContext.Provider value={store}>{props.children}</WalletContext.Provider> | ||
} | ||
|
||
export const useWalletManager = (): WalletManager => { | ||
const manager = useContext(WalletContext) | ||
if (!manager) { | ||
throw new Error('useWalletManager must be used within a WalletProvider') | ||
} | ||
return manager() | ||
} | ||
|
||
export interface Wallet { | ||
id: () => string | ||
metadata: () => WalletMetadata | ||
accounts: () => WalletAccount[] | ||
activeAccount: () => WalletAccount | null | ||
isConnected: () => boolean | ||
isActive: () => boolean | ||
connect: (args?: Record<string, any>) => Promise<WalletAccount[]> | ||
disconnect: () => Promise<void> | ||
setActive: () => void | ||
setActiveAccount: (address: string) => void | ||
} | ||
|
||
export function useWallet() { | ||
const manager = createMemo(() => useWalletManager()) | ||
|
||
const [algodClient, setAlgodClient] = createSignal<algosdk.Algodv2>(manager().algodClient) | ||
|
||
const walletStore = useStore(manager().store, (state) => state.wallets) | ||
|
||
const walletState = (walletId: WalletId): WalletState | null => walletStore()[walletId] || null | ||
|
||
const activeWalletId = useStore(manager().store, (state) => state.activeWallet) | ||
|
||
const activeWallet = () => manager().getWallet(activeWalletId() as WalletId) || null | ||
|
||
const activeWalletState = () => walletState(activeWalletId() as WalletId) | ||
|
||
const activeWalletAccounts = () => activeWalletState()?.accounts ?? null | ||
|
||
const activeWalletAddresses = () => | ||
activeWalletAccounts()?.map((account) => account.address) ?? null | ||
|
||
const activeAccount = () => activeWalletState()?.activeAccount ?? null | ||
|
||
const activeAddress = () => activeAccount()?.address ?? null | ||
|
||
const isWalletActive = (walletId: WalletId) => walletId === activeWalletId() | ||
const isWalletConnected = (walletId: WalletId) => | ||
!!walletState(walletId)?.accounts.length || false | ||
|
||
const activeNetwork = useStore(manager().store, (state) => state.activeNetwork) | ||
|
||
const setActiveNetwork = async (networkId: NetworkId): Promise<void> => { | ||
if (activeNetwork() === networkId) { | ||
return | ||
} | ||
// Disconnect any connected wallets | ||
await manager().disconnect() | ||
|
||
console.info(`[Solid] Creating Algodv2 client for ${networkId}...`) | ||
|
||
const { token, baseServer, port, headers } = manager().networkConfig[networkId] | ||
const newClient = new algosdk.Algodv2(token, baseServer, port, headers) | ||
setAlgodClient(newClient) | ||
|
||
manager().algodClient = newClient | ||
|
||
manager().store.setState((state) => ({ | ||
...state, | ||
activeNetwork: networkId | ||
})) | ||
|
||
console.info(`[Solid] ✅ Active network set to ${networkId}.`) | ||
} | ||
|
||
const signTransactions = <T extends algosdk.Transaction[] | Uint8Array[]>( | ||
txnGroup: T | T[], | ||
indexesToSign?: number[] | ||
): Promise<Uint8Array[]> => { | ||
const wallet = activeWallet() | ||
if (!wallet) { | ||
throw new Error('No active wallet') | ||
} | ||
return wallet.signTransactions(txnGroup, indexesToSign) | ||
} | ||
|
||
const transactionSigner = ( | ||
txnGroup: algosdk.Transaction[], | ||
indexesToSign: number[] | ||
): Promise<Uint8Array[]> => { | ||
const wallet = activeWallet() | ||
if (!wallet) { | ||
throw new Error('No active wallet') | ||
} | ||
return wallet.transactionSigner(txnGroup, indexesToSign) | ||
} | ||
|
||
return { | ||
activeWalletId, | ||
walletStore, | ||
algodClient, | ||
activeNetwork, | ||
activeWallet, | ||
activeWalletAccounts, | ||
activeWalletAddresses, | ||
activeWalletState, | ||
activeAccount, | ||
activeAddress, | ||
isWalletActive, | ||
isWalletConnected, | ||
setActiveNetwork, | ||
setAlgodClient, | ||
signTransactions, | ||
transactionSigner, | ||
wallets: manager().wallets | ||
} | ||
} |
Oops, something went wrong.