-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypted.ts
37 lines (35 loc) · 1.04 KB
/
encrypted.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { JsonRpcProvider, Wallet } from "ethers"
import { create } from "zustand"
import { persist } from "zustand/middleware"
type Store = {
encrypted: string | null
provider: JsonRpcProvider | null
address: string
createEncrypted: (password: string) => Promise<string | null>
updateEncrypted: (data: string | null) => void
}
export const useEncryptedStore = create<Store>()(
persist(
(set, get) => ({
encrypted: null,
provider: null,
address: "No address found",
createEncrypted: async (password: string) => {
if (get().encrypted) return null
const provider = new JsonRpcProvider(process.env.NEXT_PUBLIC_RPC_URL!)
const wallet = Wallet.createRandom(provider)
const encrypted = await wallet.encrypt(password)
set({
encrypted: encrypted,
provider: provider,
address: wallet.address,
})
return encrypted
},
updateEncrypted: (data) => set({ encrypted: data }),
}),
{
name: "channel-4-encrypted",
}
)
)