@@ -6,19 +6,11 @@ import {
6
6
type WalletData ,
7
7
} from "@coinbase/coinbase-sdk" ;
8
8
import { isAddress } from "viem" ;
9
+ import { validateEnvironment } from "." ;
9
10
import { getWalletData , saveWalletData } from "./storage" ;
10
- import type { XMTPUser } from "./types" ;
11
11
12
- const coinbaseApiKeyName = process . env . CDP_API_KEY_NAME ;
13
- let coinbaseApiKeyPrivateKey = process . env . CDP_API_KEY_PRIVATE_KEY ;
14
- const networkId = process . env . NETWORK_ID ?? "base-sepolia" ;
15
-
16
- if ( ! coinbaseApiKeyName || ! coinbaseApiKeyPrivateKey || ! networkId ) {
17
- console . error (
18
- "Either networkId, CDP_API_KEY_NAME or CDP_API_KEY_PRIVATE_KEY must be set" ,
19
- ) ;
20
- process . exit ( 1 ) ;
21
- }
12
+ const { coinbaseApiKeyName, coinbaseApiKeyPrivateKey, networkId } =
13
+ validateEnvironment ( ) ;
22
14
23
15
class WalletStorage {
24
16
async get ( inboxId : string ) : Promise < string | undefined > {
@@ -56,14 +48,10 @@ class WalletStorage {
56
48
57
49
// Initialize Coinbase SDK
58
50
function initializeCoinbaseSDK ( ) : boolean {
59
- // Replace \\n with actual newlines if present in the private key
60
- if ( coinbaseApiKeyPrivateKey ) {
61
- coinbaseApiKeyPrivateKey = coinbaseApiKeyPrivateKey . replace ( / \\ n / g, "\n" ) ;
62
- }
63
51
try {
64
52
Coinbase . configure ( {
65
- apiKeyName : coinbaseApiKeyName as string ,
66
- privateKey : coinbaseApiKeyPrivateKey as string ,
53
+ apiKeyName : coinbaseApiKeyName ,
54
+ privateKey : coinbaseApiKeyPrivateKey ,
67
55
} ) ;
68
56
console . log ( "Coinbase SDK initialized successfully, network:" , networkId ) ;
69
57
return true ;
@@ -79,7 +67,6 @@ export type AgentWalletData = {
79
67
id : string ;
80
68
wallet : Wallet ;
81
69
data : WalletData ;
82
- human_address : string ;
83
70
agent_address : string ;
84
71
blockchain ?: string ;
85
72
state ?: string ;
@@ -89,21 +76,14 @@ export type AgentWalletData = {
89
76
// Wallet service class based on cointoss implementation
90
77
export class WalletService {
91
78
private walletStorage : WalletStorage ;
92
- private humanAddress : string ;
93
79
private inboxId : string ;
94
80
private sdkInitialized : boolean ;
95
81
96
- constructor ( xmtpUser : XMTPUser ) {
82
+ constructor ( inboxId : string ) {
97
83
this . sdkInitialized = initializeCoinbaseSDK ( ) ;
98
84
this . walletStorage = new WalletStorage ( ) ;
99
- this . humanAddress = xmtpUser . address ;
100
- this . inboxId = xmtpUser . inboxId ;
101
- console . log (
102
- "WalletService initialized with sender address" ,
103
- this . humanAddress ,
104
- "and inboxId" ,
105
- this . inboxId ,
106
- ) ;
85
+ this . inboxId = inboxId ;
86
+ console . log ( "WalletService initialized with sender inboxId" , this . inboxId ) ;
107
87
}
108
88
109
89
async createWallet ( ) : Promise < AgentWalletData > {
@@ -135,27 +115,17 @@ export class WalletService {
135
115
const address = await wallet . getDefaultAddress ( ) ;
136
116
const walletAddress = address . getId ( ) ;
137
117
138
- // Make the wallet address visible in the logs for funding
139
- console . log ( "-----------------------------------------------------" ) ;
140
- console . log ( `NEW WALLET CREATED FOR USER: ${ this . humanAddress } ` ) ;
141
- console . log ( `WALLET ADDRESS: ${ walletAddress } ` ) ;
142
- console . log ( `NETWORK: ${ networkId } ` ) ;
143
- console . log ( `SEND FUNDS TO THIS ADDRESS TO TEST: ${ walletAddress } ` ) ;
144
- console . log ( "-----------------------------------------------------" ) ;
145
-
146
118
const walletInfo : AgentWalletData = {
147
119
id : walletAddress ,
148
120
wallet : wallet ,
149
121
data : data ,
150
- human_address : this . humanAddress ,
151
122
agent_address : walletAddress ,
152
123
inboxId : this . inboxId ,
153
124
} ;
154
125
155
126
console . log ( "Saving wallet data to storage..." ) ;
156
127
const walletInfoToStore = {
157
128
data : data ,
158
- human_address : this . humanAddress ,
159
129
agent_address : walletAddress ,
160
130
inboxId : this . inboxId ,
161
131
} ;
@@ -222,7 +192,6 @@ export class WalletService {
222
192
id : importedWallet . getId ( ) ?? "" ,
223
193
wallet : importedWallet ,
224
194
data : walletInfo . data ,
225
- human_address : walletInfo . human_address ,
226
195
agent_address : walletInfo . agent_address ,
227
196
inboxId : walletInfo . inboxId ,
228
197
} ;
@@ -286,23 +255,21 @@ export class WalletService {
286
255
287
256
async transfer (
288
257
inboxId : string ,
289
- humanAddress : string ,
290
258
toAddress : string ,
291
259
amount : number ,
292
260
) : Promise < Transfer | undefined > {
293
- humanAddress = humanAddress . toLowerCase ( ) ;
294
261
toAddress = toAddress . toLowerCase ( ) ;
295
262
296
263
console . log ( "📤 TRANSFER INITIATED" ) ;
297
264
console . log ( `💸 Amount: ${ amount } USDC` ) ;
298
- console . log ( `🔍 From user: ${ humanAddress } ` ) ;
265
+ console . log ( `🔍 From user: ${ inboxId } ` ) ;
299
266
console . log ( `🔍 To: ${ toAddress } ` ) ;
300
267
301
268
// Get the source wallet
302
- console . log ( `🔑 Retrieving source wallet for user: ${ humanAddress } ...` ) ;
269
+ console . log ( `🔑 Retrieving source wallet for user: ${ inboxId } ...` ) ;
303
270
const from = await this . getWallet ( inboxId ) ;
304
271
if ( ! from ) {
305
- console . error ( `❌ No wallet found for sender: ${ humanAddress } ` ) ;
272
+ console . error ( `❌ No wallet found for sender: ${ inboxId } ` ) ;
306
273
return undefined ;
307
274
}
308
275
console . log ( `✅ Source wallet found: ${ from . agent_address } ` ) ;
@@ -365,9 +332,6 @@ export class WalletService {
365
332
try {
366
333
await transfer . wait ( ) ;
367
334
console . log ( `✅ Transfer completed successfully!` ) ;
368
- console . log (
369
- `📝 Transaction details: ${ JSON . stringify ( transfer , null , 2 ) } ` ,
370
- ) ;
371
335
} catch ( err ) {
372
336
if ( err instanceof TimeoutError ) {
373
337
console . log (
0 commit comments