Skip to content

Commit 7352283

Browse files
committed
delegation experiment
1 parent 4250403 commit 7352283

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Button } from '@/components/ui/button';
2+
import { createEthereumAccount } from '@/lib/create-ethereum-account';
3+
import { useDelegatedActions, usePrivy, useWallets } from '@privy-io/react-auth';
4+
5+
export const Delegate = () => {
6+
const { user } = usePrivy();
7+
const { ready, wallets } = useWallets();
8+
const { delegateWallet, revokeWallets } = useDelegatedActions();
9+
10+
// find the embedded wallet to delegate from the array of the user's wallets
11+
const walletToDelegate = wallets.find((wallet) => wallet.walletClientType === 'privy');
12+
13+
// // check if the wallet to delegate by inspecting the user's linked accounts
14+
// const isAlreadyDelegated = !!user?.linkedAccounts.find(
15+
// (account): account is WalletWithMetadata => account.type === 'wallet' && account.delegated,
16+
// );
17+
18+
const onDelegate = async () => {
19+
const { address, privateKey } = createEthereumAccount();
20+
if (!walletToDelegate || !ready) return;
21+
await delegateWallet({ address: walletToDelegate.address, chainType: 'ethereum' });
22+
alert(`Delegated to ${address} with private key ${privateKey}`);
23+
};
24+
25+
return (
26+
<>
27+
<h1 className="text-2xl font-bold">Delegate Access</h1>
28+
<h2 className="text-lg font-bold">Accounts</h2>
29+
<ul>
30+
{user?.linkedAccounts.map((account) => {
31+
if (account.type === 'wallet') {
32+
return <li key={account.address}>{account.address}</li>;
33+
}
34+
})}
35+
</ul>
36+
<Button disabled={!ready || !walletToDelegate} onClick={onDelegate} type="button">
37+
Delegate access
38+
</Button>
39+
</>
40+
);
41+
};
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: 26 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 SettingsDelegateImport } from './routes/settings/delegate'
1819

1920
// Create Virtual Routes
2021

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

44+
const SettingsDelegateRoute = SettingsDelegateImport.update({
45+
id: '/settings/delegate',
46+
path: '/settings/delegate',
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/delegate': {
69+
id: '/settings/delegate'
70+
path: '/settings/delegate'
71+
fullPath: '/settings/delegate'
72+
preLoaderRoute: typeof SettingsDelegateImport
73+
parentRoute: typeof rootRoute
74+
}
6175
'/space/$spaceId': {
6276
id: '/space/$spaceId'
6377
path: '/space/$spaceId'
@@ -73,40 +87,45 @@ declare module '@tanstack/react-router' {
7387
export interface FileRoutesByFullPath {
7488
'/': typeof IndexRoute
7589
'/login': typeof LoginLazyRoute
90+
'/settings/delegate': typeof SettingsDelegateRoute
7691
'/space/$spaceId': typeof SpaceSpaceIdRoute
7792
}
7893

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

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

92109
export interface FileRouteTypes {
93110
fileRoutesByFullPath: FileRoutesByFullPath
94-
fullPaths: '/' | '/login' | '/space/$spaceId'
111+
fullPaths: '/' | '/login' | '/settings/delegate' | '/space/$spaceId'
95112
fileRoutesByTo: FileRoutesByTo
96-
to: '/' | '/login' | '/space/$spaceId'
97-
id: '__root__' | '/' | '/login' | '/space/$spaceId'
113+
to: '/' | '/login' | '/settings/delegate' | '/space/$spaceId'
114+
id: '__root__' | '/' | '/login' | '/settings/delegate' | '/space/$spaceId'
98115
fileRoutesById: FileRoutesById
99116
}
100117

101118
export interface RootRouteChildren {
102119
IndexRoute: typeof IndexRoute
103120
LoginLazyRoute: typeof LoginLazyRoute
121+
SettingsDelegateRoute: typeof SettingsDelegateRoute
104122
SpaceSpaceIdRoute: typeof SpaceSpaceIdRoute
105123
}
106124

107125
const rootRouteChildren: RootRouteChildren = {
108126
IndexRoute: IndexRoute,
109127
LoginLazyRoute: LoginLazyRoute,
128+
SettingsDelegateRoute: SettingsDelegateRoute,
110129
SpaceSpaceIdRoute: SpaceSpaceIdRoute,
111130
}
112131

@@ -122,6 +141,7 @@ export const routeTree = rootRoute
122141
"children": [
123142
"/",
124143
"/login",
144+
"/settings/delegate",
125145
"/space/$spaceId"
126146
]
127147
},
@@ -131,6 +151,9 @@ export const routeTree = rootRoute
131151
"/login": {
132152
"filePath": "login.lazy.tsx"
133153
},
154+
"/settings/delegate": {
155+
"filePath": "settings/delegate.tsx"
156+
},
134157
"/space/$spaceId": {
135158
"filePath": "space/$spaceId.tsx"
136159
}

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/delegate">
35+
Delegate
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 { Delegate } from '@/components/delegate';
2+
import { createFileRoute } from '@tanstack/react-router';
3+
4+
export const Route = createFileRoute('/settings/delegate')({
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+
<Delegate />
12+
</div>
13+
);
14+
}

0 commit comments

Comments
 (0)