Skip to content

Commit d15a1c5

Browse files
committed
chore: update privy dependency
1 parent 28eab81 commit d15a1c5

34 files changed

+376
-597
lines changed

packages/demo/backend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
"attribution": "tsx scripts/attribution.ts"
3838
},
3939
"dependencies": {
40-
"@eth-optimism/utils-app": "^0.0.6",
4140
"@eth-optimism/actions-sdk": "workspace:*",
41+
"@eth-optimism/utils-app": "^0.0.6",
4242
"@eth-optimism/viem": "^0.4.13",
4343
"@hono/node-server": "^1.14.0",
44-
"@privy-io/server-auth": "^1.31.1",
44+
"@privy-io/node": "^0.3.0",
4545
"commander": "^13.1.0",
4646
"dotenv": "^16.4.5",
4747
"envalid": "^8.1.0",

packages/demo/backend/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ActionsApp extends App {
6060
return null
6161
},
6262
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
63-
allowHeaders: ['Content-Type', 'Authorization'],
63+
allowHeaders: ['Content-Type', 'Authorization', 'privy-id-token'],
6464
}),
6565
)
6666

packages/demo/backend/src/config/actions.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createActions } from '@eth-optimism/actions-sdk'
22
import type { NodeActionsConfig } from '@eth-optimism/actions-sdk/node'
3-
import { PrivyClient } from '@privy-io/server-auth'
3+
import { type AuthorizationContext, PrivyClient } from '@privy-io/node'
44

55
import { BASE_SEPOLIA, UNICHAIN } from './chains.js'
66
import { env } from './env.js'
@@ -16,6 +16,7 @@ export function createActionsConfig(): NodeActionsConfig<'privy'> {
1616
type: 'privy',
1717
config: {
1818
privyClient: getPrivyClient(),
19+
authorizationContext: getAuthorizationContext(),
1920
},
2021
},
2122
},
@@ -51,9 +52,14 @@ export function getActions() {
5152
}
5253

5354
export function getPrivyClient() {
54-
const privy = new PrivyClient(env.PRIVY_APP_ID, env.PRIVY_APP_SECRET)
55-
if (env.SESSION_SIGNER_PK) {
56-
privy.walletApi.updateAuthorizationKey(env.SESSION_SIGNER_PK)
55+
return new PrivyClient({
56+
appId: env.PRIVY_APP_ID,
57+
appSecret: env.PRIVY_APP_SECRET,
58+
})
59+
}
60+
61+
export function getAuthorizationContext(): AuthorizationContext {
62+
return {
63+
authorization_private_keys: [env.SESSION_SIGNER_PK],
5764
}
58-
return privy
5965
}

packages/demo/backend/src/controllers/lend.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ export async function openPosition(c: Context) {
6969
body: { amount, tokenAddress, marketId },
7070
} = validation.data
7171
const auth = c.get('auth') as AuthContext | undefined
72-
if (!auth || !auth.userId) {
72+
if (!auth || !auth.idToken) {
7373
return c.json({ error: 'Unauthorized' }, 401)
7474
}
7575

7676
const result = await lendService.openPosition({
77-
userId: auth.userId,
77+
idToken: auth.idToken,
7878
amount,
7979
tokenAddress: tokenAddress as Address,
8080
marketId: {
8181
address: marketId.address as Address,
8282
chainId: marketId.chainId as SupportedChainId,
8383
},
84-
isUserWallet: Boolean(auth?.userId),
8584
})
8685

8786
return c.json({ result: serializeBigInt(result) })
@@ -113,19 +112,18 @@ export async function closePosition(c: Context) {
113112
body: { amount, tokenAddress, marketId },
114113
} = validation.data
115114
const auth = c.get('auth') as AuthContext | undefined
116-
if (!auth || !auth.userId) {
115+
if (!auth || !auth.idToken) {
117116
return c.json({ error: 'Unauthorized' }, 401)
118117
}
119118

120119
const result = await lendService.closePosition({
121-
userId: auth.userId,
120+
idToken: auth.idToken,
122121
amount,
123122
tokenAddress: tokenAddress as Address,
124123
marketId: {
125124
address: marketId.address as Address,
126125
chainId: marketId.chainId as SupportedChainId,
127126
},
128-
isUserWallet: Boolean(auth?.userId),
129127
})
130128

131129
return c.json({ result: serializeBigInt(result) })

packages/demo/backend/src/controllers/wallet.ts

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { type Address } from 'viem'
44
import { z } from 'zod'
55

66
import type { AuthContext } from '@/middleware/auth.js'
7-
import type {
8-
CreateWalletResponse,
9-
GetWalletResponse,
10-
} from '@/types/service.js'
7+
import type { GetWalletResponse } from '@/types/service.js'
118

129
import { validateRequest } from '../helpers/validation.js'
1310
import * as walletService from '../services/wallet.js'
@@ -23,63 +20,31 @@ const LendPositionRequestSchema = z.object({
2320
})
2421

2522
export class WalletController {
26-
/**
27-
* POST - Create a new wallet for a user
28-
*/
29-
async createWallet(c: Context) {
30-
try {
31-
const auth = c.get('auth') as AuthContext | undefined
32-
33-
if (!auth || !auth.userId) {
34-
return c.json({ error: 'Unauthorized' }, 401)
35-
}
36-
37-
const { privyAddress, smartWalletAddress } =
38-
await walletService.createWallet()
39-
40-
return c.json({
41-
privyAddress,
42-
smartWalletAddress,
43-
userId: auth.userId,
44-
} satisfies CreateWalletResponse)
45-
} catch (error) {
46-
console.error(error)
47-
return c.json(
48-
{
49-
error: 'Failed to create wallet',
50-
message: error instanceof Error ? error.message : 'Unknown error',
51-
},
52-
500,
53-
)
54-
}
55-
}
56-
5723
/**
5824
* GET - Retrieve wallet information by user ID
5925
*/
6026
async getWallet(c: Context) {
6127
try {
6228
const auth = c.get('auth') as AuthContext | undefined
6329

64-
if (!auth || !auth.userId) {
30+
if (!auth || !auth.idToken) {
6531
return c.json({ error: 'Unauthorized' }, 401)
6632
}
6733

68-
const wallet = await walletService.getWallet(auth.userId)
34+
const wallet = await walletService.getWallet(auth.idToken)
6935

7036
if (!wallet) {
7137
return c.json(
7238
{
7339
error: 'Wallet not found',
74-
message: `No wallet found for user ${auth.userId}`,
40+
message: `No wallet found for user`,
7541
},
7642
404,
7743
)
7844
}
7945

8046
return c.json({
8147
address: wallet.address,
82-
userId: auth.userId,
8348
} satisfies GetWalletResponse)
8449
} catch (error) {
8550
console.error(error)
@@ -100,11 +65,11 @@ export class WalletController {
10065
try {
10166
const auth = c.get('auth') as AuthContext | undefined
10267

103-
if (!auth || !auth.userId) {
68+
if (!auth || !auth.idToken) {
10469
return c.json({ error: 'Unauthorized' }, 401)
10570
}
10671

107-
const wallet = await walletService.getWallet(auth.userId)
72+
const wallet = await walletService.getWallet(auth.idToken)
10873
if (!wallet) {
10974
throw new Error('Wallet not found')
11075
}
@@ -138,11 +103,11 @@ export class WalletController {
138103

139104
const auth = c.get('auth') as AuthContext | undefined
140105

141-
if (!auth || !auth.userId) {
106+
if (!auth || !auth.idToken) {
142107
return c.json({ error: 'Unauthorized' }, 401)
143108
}
144109

145-
const wallet = await walletService.getWallet(auth.userId)
110+
const wallet = await walletService.getWallet(auth.idToken)
146111
if (!wallet) {
147112
throw new Error('Wallet not found')
148113
}
@@ -156,11 +121,11 @@ export class WalletController {
156121
async fundWallet(c: Context) {
157122
try {
158123
const auth = c.get('auth') as AuthContext | undefined
159-
if (!auth || !auth.userId) {
124+
if (!auth || !auth.idToken) {
160125
return c.json({ error: 'Unauthorized' }, 401)
161126
}
162127

163-
const wallet = await walletService.getWallet(auth.userId)
128+
const wallet = await walletService.getWallet(auth.idToken)
164129
if (!wallet) {
165130
throw new Error('Wallet not found')
166131
}

packages/demo/backend/src/middleware/auth.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ import type { Context, Next } from 'hono'
33
import { getPrivyClient } from '@/config/actions.js'
44

55
export interface AuthContext {
6-
userId: string
6+
idToken: string
77
}
88

99
export async function authMiddleware(c: Context, next: Next) {
1010
const authHeader = c.req.header('Authorization')
11+
const idToken = c.req.header('privy-id-token')
1112

1213
if (!authHeader?.startsWith('Bearer ')) {
1314
return c.json({ error: 'Unauthorized' }, 401)
1415
}
1516

17+
if (!idToken) {
18+
return c.json({ error: 'Unauthorized' }, 401)
19+
}
20+
1621
const accessToken = parseAuthorizationHeader(authHeader)
1722

1823
try {
1924
const privy = getPrivyClient()
20-
const verifiedPrivy = await privy.verifyAuthToken(accessToken)
21-
const userId = verifiedPrivy.userId
25+
await privy.utils().auth().verifyAuthToken(accessToken)
2226
const authContext: AuthContext = {
23-
userId,
27+
idToken,
2428
}
2529
c.set('auth', authContext)
2630
} catch (err) {

packages/demo/backend/src/mocks/MockPrivyClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PrivyClient } from '@privy-io/server-auth'
1+
import type { PrivyClient } from '@privy-io/node'
22
import type { Address } from 'viem'
33

44
import { getRandomAddress } from '@/utils/testUtils.js'

packages/demo/backend/src/router.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ router.get(
4545
walletController.getLendPosition,
4646
)
4747
// Parameterized routes
48-
router.post('/wallet', authMiddleware, walletController.createWallet)
4948
router.get('/wallet', authMiddleware, walletController.getWallet)
5049
router.post('/wallet/fund', authMiddleware, walletController.fundWallet)
5150

packages/demo/backend/src/services/lend.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,13 @@ describe('Lend Service', () => {
126126

127127
await expect(
128128
lendService.closePosition({
129-
userId: 'invalid-wallet',
129+
idToken: 'invalid-wallet',
130130
amount: 500,
131131
tokenAddress: '0x078d782b760474a361dda0af3839290b0ef57ad6',
132132
marketId: {
133133
address: '0x38f4f3B6533de0023b9DCd04b02F93d36ad1F9f9',
134134
chainId: 130,
135135
},
136-
isUserWallet: false,
137136
}),
138137
).rejects.toThrow('Wallet not found')
139138
})

packages/demo/backend/src/services/lend.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
LendMarket,
33
LendMarketId,
4-
LendMarketPosition,
54
LendTransactionReceipt,
65
SupportedChainId,
76
} from '@eth-optimism/actions-sdk'
@@ -50,30 +49,16 @@ export async function getMarket(marketId: LendMarketId): Promise<LendMarket> {
5049
return await actions.lend.getMarket(marketId)
5150
}
5251

53-
export async function getPosition(
54-
marketId: LendMarketId,
55-
walletId: string,
56-
): Promise<LendMarketPosition> {
57-
// Try to get wallet as authenticated user first (for Privy user IDs like did:privy:...)
58-
const wallet = await getWallet(walletId)
59-
60-
if (!wallet) {
61-
throw new Error(`Wallet not found for user ID: ${walletId}`)
62-
}
63-
64-
return wallet.lend!.getPosition({ marketId })
65-
}
66-
6752
async function executePosition(
6853
params: PositionParams,
6954
operation: 'open' | 'close',
7055
): Promise<LendTransactionReceipt> {
71-
const { userId, amount, tokenAddress, marketId } = params
56+
const { idToken, amount, tokenAddress, marketId } = params
7257

7358
try {
74-
const wallet = await getWallet(userId)
59+
const wallet = await getWallet(idToken)
7560
if (!wallet) {
76-
const error = `Wallet not found for user ID: ${userId}`
61+
const error = `Wallet not found`
7762
console.error('[executePositionV1] ERROR:', error)
7863
throw new Error(error)
7964
}

0 commit comments

Comments
 (0)