11import type { Context } from 'hono'
22import type { Address } from 'viem'
3+ import { getAddress } from 'viem'
34import { z } from 'zod'
45
56import type {
@@ -18,15 +19,30 @@ const UserIdParamSchema = z.object({
1819 } ) ,
1920} )
2021
22+ const WalletAddressParamSchema = z . object ( {
23+ params : z . object ( {
24+ walletAddress : z
25+ . string ( )
26+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
27+ . trim ( ) ,
28+ } ) ,
29+ } )
30+
2131const FundWalletRequestSchema = z . object ( {
2232 params : z . object ( {
23- userId : z . string ( ) . min ( 1 , 'User ID is required' ) . trim ( ) ,
33+ walletAddress : z
34+ . string ( )
35+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
36+ . trim ( ) ,
2437 } ) ,
2538} )
2639
2740const SendTokensRequestSchema = z . object ( {
2841 body : z . object ( {
29- walletId : z . string ( ) . min ( 1 , 'walletId is required' ) ,
42+ walletAddress : z
43+ . string ( )
44+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
45+ . trim ( ) ,
3046 amount : z . number ( ) . positive ( 'amount must be positive' ) ,
3147 recipientAddress : z
3248 . string ( )
@@ -56,11 +72,11 @@ export class WalletController {
5672 const {
5773 params : { userId } ,
5874 } = validation . data
59- const { privyAddress , smartWalletAddress } =
75+ const { signerAddress , smartWalletAddress } =
6076 await walletService . createWallet ( )
6177
6278 return c . json ( {
63- privyAddress ,
79+ signerAddress ,
6480 smartWalletAddress,
6581 userId,
6682 } satisfies CreateWalletResponse )
@@ -81,27 +97,27 @@ export class WalletController {
8197 */
8298 async getWallet ( c : Context ) {
8399 try {
84- const validation = await validateRequest ( c , UserIdParamSchema )
100+ const validation = await validateRequest ( c , WalletAddressParamSchema )
85101 if ( ! validation . success ) return validation . response
86102
87103 const {
88- params : { userId } ,
104+ params : { walletAddress } ,
89105 } = validation . data
90- const wallet = await walletService . getWallet ( userId )
106+ const wallet = await walletService . getWallet ( getAddress ( walletAddress ) )
91107
92108 if ( ! wallet ) {
93109 return c . json (
94110 {
95111 error : 'Wallet not found' ,
96- message : `No wallet found for user ${ userId } ` ,
112+ message : `No wallet found for user ${ walletAddress } ` ,
97113 } ,
98114 404 ,
99115 )
100116 }
101117
102118 return c . json ( {
103119 address : wallet . address ,
104- userId,
120+ userId : walletAddress ,
105121 } satisfies GetWalletResponse )
106122 } catch ( error ) {
107123 console . error ( error )
@@ -153,13 +169,13 @@ export class WalletController {
153169 */
154170 async getBalance ( c : Context ) {
155171 try {
156- const validation = await validateRequest ( c , UserIdParamSchema )
172+ const validation = await validateRequest ( c , WalletAddressParamSchema )
157173 if ( ! validation . success ) return validation . response
158174
159175 const {
160- params : { userId } ,
176+ params : { walletAddress } ,
161177 } = validation . data
162- const balance = await walletService . getBalance ( userId )
178+ const balance = await walletService . getBalance ( getAddress ( walletAddress ) )
163179
164180 return c . json ( { balance : serializeBigInt ( balance ) } )
165181 } catch ( error ) {
@@ -183,10 +199,10 @@ export class WalletController {
183199 if ( ! validation . success ) return validation . response
184200
185201 const {
186- params : { userId } ,
202+ params : { walletAddress } ,
187203 } = validation . data
188204
189- const result = await walletService . fundWallet ( userId )
205+ const result = await walletService . fundWallet ( getAddress ( walletAddress ) )
190206
191207 return c . json ( result )
192208 } catch ( error ) {
@@ -209,11 +225,11 @@ export class WalletController {
209225 if ( ! validation . success ) return validation . response
210226
211227 const {
212- body : { walletId , amount, recipientAddress } ,
228+ body : { walletAddress , amount, recipientAddress } ,
213229 } = validation . data
214230
215231 const transactionData = await walletService . sendTokens (
216- walletId ,
232+ getAddress ( walletAddress ) ,
217233 amount ,
218234 recipientAddress as Address ,
219235 )
0 commit comments