Skip to content

Commit 56cb99f

Browse files
authored
Wallet private key export (#153)
1 parent 4250403 commit 56cb99f

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Button } from '@/components/ui/button';
2+
import { usePrivy } from '@privy-io/react-auth';
3+
4+
export const ExportWallet = () => {
5+
const { ready, authenticated, user, exportWallet } = usePrivy();
6+
const isAuthenticated = ready && authenticated;
7+
// check that your user has an embedded wallet
8+
const hasEmbeddedWallet = !!user?.linkedAccounts.find(
9+
(account) => account.type === 'wallet' && account.walletClientType === 'privy' && account.chainType === 'ethereum',
10+
);
11+
12+
return (
13+
<>
14+
<h1 className="text-2xl font-bold">Export Wallet</h1>
15+
<Button onClick={exportWallet} disabled={!isAuthenticated || !hasEmbeddedWallet}>
16+
Export my wallet
17+
</Button>
18+
</>
19+
);
20+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
2+
import { privateKeyToAccount } from 'viem/accounts';
3+
4+
export const createEthereumAccount = () => {
5+
const privateKey = `0x${bytesToHex(randomBytes(32))}` as `0x${string}`;
6+
const account = privateKeyToAccount(privateKey);
7+
return {
8+
privateKey,
9+
address: account.address,
10+
};
11+
};

apps/events/src/routeTree.gen.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createFileRoute } from '@tanstack/react-router'
1515
import { Route as rootRoute } from './routes/__root'
1616
import { Route as IndexImport } from './routes/index'
1717
import { Route as SpaceSpaceIdImport } from './routes/space/$spaceId'
18+
import { Route as SettingsExportWalletImport } from './routes/settings/export-wallet'
1819

1920
// Create Virtual Routes
2021

@@ -40,6 +41,12 @@ const SpaceSpaceIdRoute = SpaceSpaceIdImport.update({
4041
getParentRoute: () => rootRoute,
4142
} as any)
4243

44+
const SettingsExportWalletRoute = SettingsExportWalletImport.update({
45+
id: '/settings/export-wallet',
46+
path: '/settings/export-wallet',
47+
getParentRoute: () => rootRoute,
48+
} as any)
49+
4350
// Populate the FileRoutesByPath interface
4451

4552
declare module '@tanstack/react-router' {
@@ -58,6 +65,13 @@ declare module '@tanstack/react-router' {
5865
preLoaderRoute: typeof LoginLazyImport
5966
parentRoute: typeof rootRoute
6067
}
68+
'/settings/export-wallet': {
69+
id: '/settings/export-wallet'
70+
path: '/settings/export-wallet'
71+
fullPath: '/settings/export-wallet'
72+
preLoaderRoute: typeof SettingsExportWalletImport
73+
parentRoute: typeof rootRoute
74+
}
6175
'/space/$spaceId': {
6276
id: '/space/$spaceId'
6377
path: '/space/$spaceId'
@@ -73,40 +87,50 @@ declare module '@tanstack/react-router' {
7387
export interface FileRoutesByFullPath {
7488
'/': typeof IndexRoute
7589
'/login': typeof LoginLazyRoute
90+
'/settings/export-wallet': typeof SettingsExportWalletRoute
7691
'/space/$spaceId': typeof SpaceSpaceIdRoute
7792
}
7893

7994
export interface FileRoutesByTo {
8095
'/': typeof IndexRoute
8196
'/login': typeof LoginLazyRoute
97+
'/settings/export-wallet': typeof SettingsExportWalletRoute
8298
'/space/$spaceId': typeof SpaceSpaceIdRoute
8399
}
84100

85101
export interface FileRoutesById {
86102
__root__: typeof rootRoute
87103
'/': typeof IndexRoute
88104
'/login': typeof LoginLazyRoute
105+
'/settings/export-wallet': typeof SettingsExportWalletRoute
89106
'/space/$spaceId': typeof SpaceSpaceIdRoute
90107
}
91108

92109
export interface FileRouteTypes {
93110
fileRoutesByFullPath: FileRoutesByFullPath
94-
fullPaths: '/' | '/login' | '/space/$spaceId'
111+
fullPaths: '/' | '/login' | '/settings/export-wallet' | '/space/$spaceId'
95112
fileRoutesByTo: FileRoutesByTo
96-
to: '/' | '/login' | '/space/$spaceId'
97-
id: '__root__' | '/' | '/login' | '/space/$spaceId'
113+
to: '/' | '/login' | '/settings/export-wallet' | '/space/$spaceId'
114+
id:
115+
| '__root__'
116+
| '/'
117+
| '/login'
118+
| '/settings/export-wallet'
119+
| '/space/$spaceId'
98120
fileRoutesById: FileRoutesById
99121
}
100122

101123
export interface RootRouteChildren {
102124
IndexRoute: typeof IndexRoute
103125
LoginLazyRoute: typeof LoginLazyRoute
126+
SettingsExportWalletRoute: typeof SettingsExportWalletRoute
104127
SpaceSpaceIdRoute: typeof SpaceSpaceIdRoute
105128
}
106129

107130
const rootRouteChildren: RootRouteChildren = {
108131
IndexRoute: IndexRoute,
109132
LoginLazyRoute: LoginLazyRoute,
133+
SettingsExportWalletRoute: SettingsExportWalletRoute,
110134
SpaceSpaceIdRoute: SpaceSpaceIdRoute,
111135
}
112136

@@ -122,6 +146,7 @@ export const routeTree = rootRoute
122146
"children": [
123147
"/",
124148
"/login",
149+
"/settings/export-wallet",
125150
"/space/$spaceId"
126151
]
127152
},
@@ -131,6 +156,9 @@ export const routeTree = rootRoute
131156
"/login": {
132157
"filePath": "login.lazy.tsx"
133158
},
159+
"/settings/export-wallet": {
160+
"filePath": "settings/export-wallet.tsx"
161+
},
134162
"/space/$spaceId": {
135163
"filePath": "space/$spaceId.tsx"
136164
}

apps/events/src/routes/__root.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export const Route = createRootRoute({
3030
</Link>
3131
<nav className="ml-auto flex gap-4 sm:gap-6">
3232
{authenticated ? (
33-
<div className="flex items-center gap-2">
33+
<div className="flex items-center gap-4">
34+
<Link className="text-xs" to="/settings/export-wallet">
35+
Export Wallet
36+
</Link>
3437
<span className="text-xs text-gray-500 dark:text-gray-400">
3538
{identity?.accountId.substring(0, 6)}
3639
</span>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ExportWallet } from '@/components/export-wallet';
2+
import { createFileRoute } from '@tanstack/react-router';
3+
4+
export const Route = createFileRoute('/settings/export-wallet')({
5+
component: RouteComponent,
6+
});
7+
8+
function RouteComponent() {
9+
return (
10+
<div className="flex flex-col gap-4 max-w-screen-sm mx-auto py-8">
11+
<ExportWallet />
12+
</div>
13+
);
14+
}

0 commit comments

Comments
 (0)