11import type { Context } from 'hono'
22import type { Address } from 'viem'
3+ import { getAddress } from 'viem'
34import { z } from 'zod'
45
56import type {
@@ -12,21 +13,30 @@ import { validateRequest } from '../helpers/validation.js'
1213import * as walletService from '../services/wallet.js'
1314import { serializeBigInt } from '../utils/serializers.js'
1415
15- const UserIdParamSchema = z . object ( {
16+ const WalletAddressParamSchema = z . object ( {
1617 params : z . object ( {
17- userId : z . string ( ) . min ( 1 , 'User ID is required' ) . trim ( ) ,
18+ walletAddress : z
19+ . string ( )
20+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
21+ . trim ( ) ,
1822 } ) ,
1923} )
2024
2125const FundWalletRequestSchema = z . object ( {
2226 params : z . object ( {
23- userId : z . string ( ) . min ( 1 , 'User ID is required' ) . trim ( ) ,
27+ walletAddress : z
28+ . string ( )
29+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
30+ . trim ( ) ,
2431 } ) ,
2532} )
2633
2734const SendTokensRequestSchema = z . object ( {
2835 body : z . object ( {
29- walletId : z . string ( ) . min ( 1 , 'walletId is required' ) ,
36+ walletAddress : z
37+ . string ( )
38+ . regex ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / , 'Invalid wallet address format' )
39+ . trim ( ) ,
3040 amount : z . number ( ) . positive ( 'amount must be positive' ) ,
3141 recipientAddress : z
3242 . string ( )
@@ -50,19 +60,12 @@ export class WalletController {
5060 */
5161 async createWallet ( c : Context ) {
5262 try {
53- const validation = await validateRequest ( c , UserIdParamSchema )
54- if ( ! validation . success ) return validation . response
55-
56- const {
57- params : { userId } ,
58- } = validation . data
59- const { privyAddress, smartWalletAddress } =
63+ const { signerAddress, smartWalletAddress } =
6064 await walletService . createWallet ( )
6165
6266 return c . json ( {
63- privyAddress ,
67+ signerAddress ,
6468 smartWalletAddress,
65- userId,
6669 } satisfies CreateWalletResponse )
6770 } catch ( error ) {
6871 console . error ( error )
@@ -81,27 +84,27 @@ export class WalletController {
8184 */
8285 async getWallet ( c : Context ) {
8386 try {
84- const validation = await validateRequest ( c , UserIdParamSchema )
87+ const validation = await validateRequest ( c , WalletAddressParamSchema )
8588 if ( ! validation . success ) return validation . response
8689
8790 const {
88- params : { userId } ,
91+ params : { walletAddress } ,
8992 } = validation . data
90- const wallet = await walletService . getWallet ( userId )
93+ const wallet = await walletService . getWallet ( getAddress ( walletAddress ) )
9194
9295 if ( ! wallet ) {
9396 return c . json (
9497 {
9598 error : 'Wallet not found' ,
96- message : `No wallet found for user ${ userId } ` ,
99+ message : `No wallet found for user ${ walletAddress } ` ,
97100 } ,
98101 404 ,
99102 )
100103 }
101104
102105 return c . json ( {
103106 address : wallet . address ,
104- userId,
107+ userId : walletAddress ,
105108 } satisfies GetWalletResponse )
106109 } catch ( error ) {
107110 console . error ( error )
@@ -153,13 +156,13 @@ export class WalletController {
153156 */
154157 async getBalance ( c : Context ) {
155158 try {
156- const validation = await validateRequest ( c , UserIdParamSchema )
159+ const validation = await validateRequest ( c , WalletAddressParamSchema )
157160 if ( ! validation . success ) return validation . response
158161
159162 const {
160- params : { userId } ,
163+ params : { walletAddress } ,
161164 } = validation . data
162- const balance = await walletService . getBalance ( userId )
165+ const balance = await walletService . getBalance ( getAddress ( walletAddress ) )
163166
164167 return c . json ( { balance : serializeBigInt ( balance ) } )
165168 } catch ( error ) {
@@ -183,10 +186,10 @@ export class WalletController {
183186 if ( ! validation . success ) return validation . response
184187
185188 const {
186- params : { userId } ,
189+ params : { walletAddress } ,
187190 } = validation . data
188191
189- const result = await walletService . fundWallet ( userId )
192+ const result = await walletService . fundWallet ( getAddress ( walletAddress ) )
190193
191194 return c . json ( result )
192195 } catch ( error ) {
@@ -209,11 +212,11 @@ export class WalletController {
209212 if ( ! validation . success ) return validation . response
210213
211214 const {
212- body : { walletId , amount, recipientAddress } ,
215+ body : { walletAddress , amount, recipientAddress } ,
213216 } = validation . data
214217
215218 const transactionData = await walletService . sendTokens (
216- walletId ,
219+ getAddress ( walletAddress ) ,
217220 amount ,
218221 recipientAddress as Address ,
219222 )
0 commit comments