Skip to content

Commit d354913

Browse files
jkbktlfelicio
andauthored
add new wallet flow (#696)
Co-authored-by: Felicio <[email protected]>
1 parent b10aa44 commit d354913

File tree

26 files changed

+905
-198
lines changed

26 files changed

+905
-198
lines changed

.changeset/chilly-poets-beg.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@status-im/wallet': patch
3+
'wallet': patch
4+
---
5+
6+
add new wallet flow

.vscode/extensions.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"mikestead.dotenv",
88
"bradlc.vscode-tailwindcss",
99
"vitest.explorer",
10-
"github.vscode-github-actions"
10+
"github.vscode-github-actions",
11+
"eamodio.gitlens",
12+
"github.vscode-pull-request-github"
1113
]
1214
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use client'
22

33
import { Navbar as NavbarBase } from '@status-im/wallet/components'
4-
import { usePathname } from 'next/navigation'
54

65
const Navbar = () => {
7-
const pathname = usePathname()
8-
9-
return <NavbarBase pathname={pathname} />
6+
return <NavbarBase />
107
}
118

129
export { Navbar }

apps/wallet/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@cardano-sdk/core": "^0.45.4",
3131
"@cardano-sdk/crypto": "^0.2.3",
3232
"@cardano-sdk/key-management": "^0.27.5",
33+
"@hookform/resolvers": "^3.1.1",
3334
"@radix-ui/react-dialog": "^1.1.1",
3435
"@status-im/colors": "workspace:*",
3536
"@status-im/components": "workspace:*",

apps/wallet/src/data/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ const apiRouter = router({
573573
// )
574574

575575
const { id } = await keyStore.importKey(
576-
Buffer.from(input.privateKey),
576+
new Uint8Array(Buffer.from(input.privateKey)),
577577
input.name,
578578
input.password,
579579
walletCore.CoinType.ethereum,

apps/wallet/src/hooks/use-create-wallet.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { useMutation } from '@tanstack/react-query'
1+
import { useMutation, useQueryClient } from '@tanstack/react-query'
22

33
import { useAPI } from '../providers/api-client'
44

55
export const useCreateWallet = () => {
66
const api = useAPI()
7+
const queryClient = useQueryClient()
78

89
const { mutate, mutateAsync, ...result } = useMutation({
910
mutationKey: ['create-wallet'],
@@ -15,6 +16,9 @@ export const useCreateWallet = () => {
1516

1617
return mnemonic
1718
},
19+
onSuccess: () => {
20+
queryClient.invalidateQueries({ queryKey: ['wallets'] })
21+
},
1822
})
1923

2024
return {

apps/wallet/src/hooks/use-import-wallet.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { useMutation } from '@tanstack/react-query'
1+
import { useMutation, useQueryClient } from '@tanstack/react-query'
22

33
import { useAPI } from '../providers/api-client'
44

55
export const useImportWallet = () => {
66
const api = useAPI()
7+
const queryClient = useQueryClient()
78

89
const { mutate, mutateAsync, ...result } = useMutation({
910
mutationKey: ['import-wallet'],
@@ -20,6 +21,9 @@ export const useImportWallet = () => {
2021
name: 'Imported Wallet',
2122
})
2223
},
24+
onSuccess: () => {
25+
queryClient.invalidateQueries({ queryKey: ['wallets'] })
26+
},
2327
})
2428

2529
return {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect, useState } from 'react'
2+
3+
export const usePinExtension = () => {
4+
const [isPinExtension, setIsPinExtension] = useState<boolean>(false)
5+
6+
const handleClose = async () => {
7+
setIsPinExtension(false)
8+
await chrome.storage.local.set({ pinExtension: true })
9+
}
10+
11+
useEffect(() => {
12+
async function checkSettings() {
13+
const storage = await chrome.storage.local.get(['pinExtension'])
14+
if (storage.pinExtension) {
15+
setIsPinExtension(false)
16+
return
17+
}
18+
19+
const settings = await chrome.action.getUserSettings()
20+
setIsPinExtension(!settings.isOnToolbar)
21+
}
22+
23+
checkSettings()
24+
}, [])
25+
26+
return { isPinExtension, handleClose }
27+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { createContext, useContext, useEffect, useState } from 'react'
2+
3+
import { useQuery } from '@tanstack/react-query'
4+
5+
import { apiClient } from './api-client'
6+
7+
import type { KeyStore } from '@trustwallet/wallet-core'
8+
9+
type Wallet = KeyStore.Wallet
10+
11+
type WalletContext = {
12+
currentWallet: Wallet | null
13+
wallets: Wallet[]
14+
isLoading: boolean
15+
hasWallets: boolean
16+
setCurrentWallet: (id: Wallet['id']) => void
17+
}
18+
19+
const WalletContext = createContext<WalletContext | undefined>(undefined)
20+
21+
export function useWallet() {
22+
const context = useContext(WalletContext)
23+
if (!context) {
24+
throw new Error('useWallet must be used within WalletProvider')
25+
}
26+
return context
27+
}
28+
29+
export function WalletProvider({ children }: { children: React.ReactNode }) {
30+
const [selectedWalletId, setSelectedWalletId] = useState<string | null>(null)
31+
32+
const { data: wallets = [], isLoading } = useQuery({
33+
queryKey: ['wallets'],
34+
queryFn: () => apiClient.wallet.all.query(),
35+
staleTime: 5 * 60 * 1000, // 5 minutes
36+
})
37+
38+
const hasWallets = wallets.length > 0
39+
40+
const currentWallet = useMemo(() => {
41+
if (!hasWallets) return null
42+
43+
if (selectedWalletId) {
44+
const selectedWallet = wallets.find(
45+
wallet => wallet.id === selectedWalletId,
46+
)
47+
if (selectedWallet) return selectedWallet
48+
}
49+
50+
return wallets[0] || null
51+
}, [hasWallets, selectedWalletId, wallets])
52+
53+
useEffect(() => {
54+
if (hasWallets && !selectedWalletId && wallets[0]) {
55+
setSelectedWalletId(wallets[0].id)
56+
}
57+
}, [hasWallets, selectedWalletId, wallets])
58+
59+
const setCurrentWallet = (id: string) => {
60+
setSelectedWalletId(id)
61+
}
62+
63+
const contextValue: WalletContext = {
64+
currentWallet,
65+
wallets,
66+
isLoading,
67+
hasWallets,
68+
setCurrentWallet,
69+
}
70+
71+
return (
72+
<WalletContext.Provider value={contextValue}>
73+
{children}
74+
</WalletContext.Provider>
75+
)
76+
}
66 KB
Loading

0 commit comments

Comments
 (0)