web3-tools-mcp 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -2
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/tools/gas.d.ts +99 -0
- package/dist/tools/gas.d.ts.map +1 -0
- package/dist/tools/gas.js +229 -0
- package/dist/tools/gas.js.map +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +5 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/signatures.d.ts +18 -0
- package/dist/tools/signatures.d.ts.map +1 -1
- package/dist/tools/signatures.js +26 -1
- package/dist/tools/signatures.js.map +1 -1
- package/dist/tools/transactions.d.ts +111 -0
- package/dist/tools/transactions.d.ts.map +1 -0
- package/dist/tools/transactions.js +198 -0
- package/dist/tools/transactions.js.map +1 -0
- package/dist/wallet-server.d.ts +34 -0
- package/dist/wallet-server.d.ts.map +1 -0
- package/dist/wallet-server.js +195 -0
- package/dist/wallet-server.js.map +1 -0
- package/package.json +8 -1
- package/public/wallet-app.js +677 -0
- package/public/wallet.html +723 -0
- package/src/index.ts +8 -0
- package/src/tools/gas.ts +267 -0
- package/src/tools/index.ts +5 -1
- package/src/tools/signatures.ts +34 -1
- package/src/tools/transactions.ts +246 -0
- package/src/wallet-server.ts +236 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import cors from 'cors'
|
|
3
|
+
import { WebSocketServer, WebSocket } from 'ws'
|
|
4
|
+
import { createServer } from 'http'
|
|
5
|
+
import { fileURLToPath } from 'url'
|
|
6
|
+
import { dirname, join } from 'path'
|
|
7
|
+
import { exec } from 'child_process'
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
10
|
+
const __dirname = dirname(__filename)
|
|
11
|
+
|
|
12
|
+
export interface TransactionRequest {
|
|
13
|
+
id: string
|
|
14
|
+
type: 'send_transaction' | 'sign_message' | 'sign_typed_data'
|
|
15
|
+
chain: string
|
|
16
|
+
data: unknown
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TransactionResponse {
|
|
20
|
+
id: string
|
|
21
|
+
success: boolean
|
|
22
|
+
result?: unknown
|
|
23
|
+
error?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class WalletServer {
|
|
27
|
+
private app: express.Application
|
|
28
|
+
private httpServer: ReturnType<typeof createServer>
|
|
29
|
+
private wss: WebSocketServer
|
|
30
|
+
private clients: Set<WebSocket> = new Set()
|
|
31
|
+
private pendingRequests: Map<string, {
|
|
32
|
+
resolve: (value: unknown) => void
|
|
33
|
+
reject: (error: Error) => void
|
|
34
|
+
}> = new Map()
|
|
35
|
+
private port: number
|
|
36
|
+
private isStarted = false
|
|
37
|
+
|
|
38
|
+
constructor(port = 3456) {
|
|
39
|
+
this.port = port
|
|
40
|
+
this.app = express()
|
|
41
|
+
this.httpServer = createServer(this.app)
|
|
42
|
+
this.wss = new WebSocketServer({ server: this.httpServer })
|
|
43
|
+
|
|
44
|
+
this.setupExpress()
|
|
45
|
+
this.setupWebSocket()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private setupExpress() {
|
|
49
|
+
this.app.use(cors())
|
|
50
|
+
this.app.use(express.json())
|
|
51
|
+
this.app.use(express.static(join(__dirname, '..', 'public')))
|
|
52
|
+
|
|
53
|
+
this.app.get('/', (_req, res) => {
|
|
54
|
+
res.sendFile(join(__dirname, '..', 'public', 'wallet.html'))
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
this.app.get('/health', (_req, res) => {
|
|
58
|
+
res.json({
|
|
59
|
+
status: 'ok',
|
|
60
|
+
clients: this.clients.size,
|
|
61
|
+
pendingRequests: this.pendingRequests.size
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private setupWebSocket() {
|
|
67
|
+
this.wss.on('connection', (ws: WebSocket) => {
|
|
68
|
+
console.error('[Wallet Server] Client connected')
|
|
69
|
+
this.clients.add(ws)
|
|
70
|
+
|
|
71
|
+
ws.on('message', (data: Buffer) => {
|
|
72
|
+
try {
|
|
73
|
+
const response: TransactionResponse = JSON.parse(data.toString())
|
|
74
|
+
console.error('[Wallet Server] Received response:', response.id)
|
|
75
|
+
console.error('[Wallet Server] Response data:', JSON.stringify(response))
|
|
76
|
+
|
|
77
|
+
const pending = this.pendingRequests.get(response.id)
|
|
78
|
+
if (pending) {
|
|
79
|
+
if (response.success) {
|
|
80
|
+
pending.resolve(response.result)
|
|
81
|
+
} else {
|
|
82
|
+
pending.reject(new Error(response.error || 'Transaction failed'))
|
|
83
|
+
}
|
|
84
|
+
this.pendingRequests.delete(response.id)
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('[Wallet Server] Error parsing message:', error)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
ws.on('close', () => {
|
|
92
|
+
console.error('[Wallet Server] Client disconnected')
|
|
93
|
+
this.clients.delete(ws)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
ws.on('error', (error) => {
|
|
97
|
+
console.error('[Wallet Server] WebSocket error:', error)
|
|
98
|
+
this.clients.delete(ws)
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async start() {
|
|
104
|
+
if (this.isStarted) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return new Promise<void>((resolve, reject) => {
|
|
109
|
+
this.httpServer.listen(this.port, () => {
|
|
110
|
+
this.isStarted = true
|
|
111
|
+
console.error(`[Wallet Server] Running on http://localhost:${this.port}`)
|
|
112
|
+
resolve()
|
|
113
|
+
}).on('error', (error: NodeJS.ErrnoException) => {
|
|
114
|
+
if (error.code === 'EADDRINUSE') {
|
|
115
|
+
console.error(`[Wallet Server] Port ${this.port} already in use, assuming server is already running`)
|
|
116
|
+
this.isStarted = true
|
|
117
|
+
resolve()
|
|
118
|
+
} else {
|
|
119
|
+
reject(error)
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async stop() {
|
|
126
|
+
return new Promise<void>((resolve) => {
|
|
127
|
+
this.clients.forEach(client => client.close())
|
|
128
|
+
this.wss.close(() => {
|
|
129
|
+
this.httpServer.close(() => {
|
|
130
|
+
this.isStarted = false
|
|
131
|
+
console.error('[Wallet Server] Stopped')
|
|
132
|
+
resolve()
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
openBrowser() {
|
|
139
|
+
const url = `http://localhost:${this.port}`
|
|
140
|
+
const platform = process.platform
|
|
141
|
+
|
|
142
|
+
let command: string
|
|
143
|
+
if (platform === 'darwin') {
|
|
144
|
+
command = `open "${url}"`
|
|
145
|
+
} else if (platform === 'win32') {
|
|
146
|
+
command = `start "${url}"`
|
|
147
|
+
} else {
|
|
148
|
+
command = `xdg-open "${url}"`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
exec(command, (error) => {
|
|
152
|
+
if (error) {
|
|
153
|
+
console.error('[Wallet Server] Failed to open browser:', error)
|
|
154
|
+
console.error(`[Wallet Server] Please open manually: ${url}`)
|
|
155
|
+
} else {
|
|
156
|
+
console.error(`[Wallet Server] Opened browser at ${url}`)
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async sendTransaction(request: TransactionRequest): Promise<unknown> {
|
|
162
|
+
if (!this.isStarted) {
|
|
163
|
+
await this.start()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Always open/focus browser for transaction requests
|
|
167
|
+
this.openBrowser()
|
|
168
|
+
|
|
169
|
+
// Open browser if no clients connected
|
|
170
|
+
if (this.clients.size === 0) {
|
|
171
|
+
// Wait for client to connect (max 30 seconds)
|
|
172
|
+
const timeout = 30000
|
|
173
|
+
const startTime = Date.now()
|
|
174
|
+
while (this.clients.size === 0 && Date.now() - startTime < timeout) {
|
|
175
|
+
await new Promise(resolve => setTimeout(resolve, 500))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.clients.size === 0) {
|
|
179
|
+
throw new Error('No wallet connected. Please open the browser and connect your wallet.')
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return new Promise((resolve, reject) => {
|
|
184
|
+
this.pendingRequests.set(request.id, { resolve, reject })
|
|
185
|
+
|
|
186
|
+
// Send to first available connected client only
|
|
187
|
+
const message = JSON.stringify(request)
|
|
188
|
+
let sent = false
|
|
189
|
+
for (const client of this.clients) {
|
|
190
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
191
|
+
client.send(message)
|
|
192
|
+
sent = true
|
|
193
|
+
break
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!sent) {
|
|
198
|
+
this.pendingRequests.delete(request.id)
|
|
199
|
+
reject(new Error('No active wallet connection'))
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Timeout after 5 minutes
|
|
204
|
+
setTimeout(() => {
|
|
205
|
+
if (this.pendingRequests.has(request.id)) {
|
|
206
|
+
this.pendingRequests.delete(request.id)
|
|
207
|
+
reject(new Error('Transaction request timed out'))
|
|
208
|
+
}
|
|
209
|
+
}, 300000)
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
isConnected(): boolean {
|
|
214
|
+
return this.clients.size > 0
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
getPort(): number {
|
|
218
|
+
return this.port
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Singleton instance
|
|
223
|
+
let walletServer: WalletServer | null = null
|
|
224
|
+
|
|
225
|
+
export function getWalletServer(): WalletServer {
|
|
226
|
+
if (!walletServer) {
|
|
227
|
+
walletServer = new WalletServer()
|
|
228
|
+
}
|
|
229
|
+
return walletServer
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function startWalletServer(): Promise<WalletServer> {
|
|
233
|
+
const server = getWalletServer()
|
|
234
|
+
await server.start()
|
|
235
|
+
return server
|
|
236
|
+
}
|