Commit 2b15636
feat(wallet-cli): add daemon JSON-RPC socket transport (#9108)
## Explanation
`@metamask/wallet-cli` runs a background daemon that hosts a
`@metamask/wallet` instance; the CLI talks to it over a Unix socket.
This PR adds that transport layer. It is purely additive plumbing — no
`mm` subcommand consumes it yet (commands land in a follow-up), so there
is no user-visible behavior change.
- **`socket-line`** — `writeLine`/`readLine` for newline-delimited
framing over a `net.Socket`, with an optional read timeout and listener
cleanup.
- **`daemon-client`** — `sendCommand` opens a connection, writes one
JSON-RPC request, reads one response, and closes; it correlates the
response `id` with the request and retries once on transient connection
errors (`ECONNREFUSED`/`ECONNRESET`). `pingDaemon` is a lightweight
`getStatus` health probe that distinguishes "no daemon" (`absent`) from
"daemon present but unreachable", classifying the latter by failure mode
(`refused` / `timeout` / `permission` / `protocol` / `other`).
- **`rpc-socket-server`** — `startRpcSocketServer` listens on a Unix
socket and dispatches one JSON-RPC request per connection to a handler
map. It intercepts a built-in `shutdown` method, enforces
one-request-per-connection, times out idle connections, and returns a
`close()` handle.
- **`daemon-spawn`** — `ensureDaemon` spawns a detached daemon process
and polls until the socket is responsive, refusing to take over a wedged
or foreign-owned socket. Resolves the entry point from `dist` (prod) or
`src` via `tsx` (dev).
- **`stop-daemon`** — `stopDaemon` escalates from a graceful `shutdown`
RPC through `SIGTERM` to `SIGKILL`, then removes the PID and socket
files best-effort.
- **`prompts`** — `confirmPurge` wraps the ESM-only `@inquirer/confirm`
via dynamic import.
- **`types`** — adds `RpcHandler`, `RpcHandlerMap`, `DaemonStatusInfo`,
and `DaemonSpawnConfig`.
Adds `@inquirer/confirm` and `@metamask/rpc-errors` dependencies. Every
daemon module is covered to the package's 100% coverage thresholds, plus
a `socket-integration.test.ts` that exercises the client and server
together over a real Unix socket (framing, id correlation, the
one-request-per-connection invariant, and real shutdown timing).
### Hardening applied on top of the original branch
A multi-agent review of the ported code surfaced a few issues worth
fixing before any command consumes this transport. These are the only
deviations from the original `rekm/wallet-cli` modules (otherwise lifted
verbatim):
- **Owner-only socket permissions** (`rpc-socket-server.ts`) —
`listen()` created the Unix socket with umask-derived (typically
world-accessible) permissions. Since the daemon hosts an unlocked
wallet, any local user could connect and drive it. The server now
`chmod`s the socket to `0600` immediately after binding.
- **Fail fast on a failed spawn** (`daemon-spawn.ts`) — a `ChildProcess`
`'error'` (bad interpreter, `EACCES`, `ENOENT`) was logged to stderr but
never recorded, so the readiness loop polled for the full 30s and then
threw a generic timeout, discarding the real cause. The error is now
captured and surfaced immediately.
- **No unhandled `'error'` during a write** (`socket-line.ts`) —
`writeLine` relied solely on the write callback; a socket `'error'`
arriving mid-write had no listener and would crash the process instead
of rejecting. It now attaches a one-shot `'error'` listener for the
duration of the write.
- **Test robustness** (`socket-integration.test.ts`) — replaced an
`await import('node:net')` with a static import so the file runs
standalone without `--experimental-vm-modules`.
## References
- The daemon transport modules originate from Erik Marks's wallet-cli
branch (`rekm/wallet-cli`, #8446). They are lifted essentially verbatim
from there, split out into this standalone, additive PR and adjusted to
apply on the current `main` (see the hardening deltas above).
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches local IPC and process lifecycle around an unlocked-wallet
daemon, including owner-only socket permissions and signal-based stop;
additive only with no CLI wiring yet, but mistakes could affect security
or orphan processes once commands land.
>
> **Overview**
> Adds **additive daemon IPC plumbing** to `@metamask/wallet-cli` so the
CLI can talk to a background wallet daemon over a Unix socket. Nothing
in this PR wires it into `mm` subcommands yet.
>
> The stack is newline-delimited JSON-RPC: **`socket-line`** for
framing, **`daemon-client`** (`sendCommand` with request/response id
checks and one retry on transient connect errors; **`pingDaemon`** with
`absent` / `responsive` / categorized `unreachable`), and
**`startRpcSocketServer`** (one request per connection, handler map,
built-in `shutdown`, idle timeouts). **`ensureDaemon`** spawns a
detached process and polls readiness while refusing to replace a wedged
or other-user socket; **`stopDaemon`** escalates shutdown RPC → SIGTERM
→ SIGKILL and cleans up PID/socket files. Shared **`utils`** cover PID
files and signaling; **`confirmPurge`** dynamically imports
`@inquirer/confirm`. **`types.ts`** gains RPC and spawn config types.
>
> **Security / robustness hardening** in this PR: socket files are
**`chmod` 0600** after bind (wallet daemon must not be
world-connectable), failed child **`spawn` errors fail fast** instead of
a 30s timeout, and **`writeLine`** handles mid-write socket errors.
Dependencies **`@metamask/rpc-errors`** and **`@inquirer/confirm`** are
added; changelog documents the transport layer. Broad unit tests plus
**`socket-integration.test.ts`** exercise real sockets end-to-end.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
556cbd7. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 3cbad62 commit 2b15636
19 files changed
Lines changed: 3427 additions & 27 deletions
File tree
- packages/wallet-cli
- src/daemon
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
14 | | - | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
0 commit comments