@veil-cash/sdk 0.1.0

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 ADDED
@@ -0,0 +1,446 @@
1
+ # @veil-cash/sdk
2
+
3
+ SDK and CLI for interacting with [Veil Cash](https://veil.cash) privacy pools on Base.
4
+
5
+ Generate keypairs, register, deposit, withdraw, and transfer ETH privately.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @veil-cash/sdk
11
+ # or
12
+ yarn add @veil-cash/sdk
13
+ # or
14
+ pnpm add @veil-cash/sdk
15
+ ```
16
+
17
+ For global CLI access:
18
+ ```bash
19
+ npm install -g @veil-cash/sdk
20
+ ```
21
+
22
+ ## CLI Quick Start
23
+
24
+ ```bash
25
+ # 1. Generate and save your Veil keypair
26
+ veil init
27
+
28
+ # 2. Set your Ethereum wallet key (for signing transactions)
29
+ export WALLET_KEY=0x...
30
+
31
+ # 3. Register your deposit key (one-time)
32
+ veil register
33
+
34
+ # 4. Deposit ETH
35
+ veil deposit 0.1
36
+
37
+ # 5. Check your balance
38
+ veil balance
39
+
40
+ # 6. Withdraw to any address
41
+ veil withdraw 0.05 0xRecipientAddress
42
+
43
+ # 7. Transfer privately to another registered user
44
+ veil transfer 0.02 0xRecipientAddress
45
+ ```
46
+
47
+ ## CLI Commands
48
+
49
+ ### `veil init`
50
+
51
+ Generate a new Veil keypair.
52
+
53
+ ```bash
54
+ veil init # Interactive, saves to .env.veil
55
+ veil init --force # Overwrite existing without prompting
56
+ veil init --json # Output as JSON (no prompts, no file save)
57
+ veil init --out path # Save to custom path
58
+ veil init --no-save # Print keypair without saving
59
+ ```
60
+
61
+ ### `veil keypair`
62
+
63
+ Generate and show a new keypair as JSON (does not save).
64
+
65
+ ```bash
66
+ veil keypair
67
+ # {"veilPrivateKey":"0x...","depositKey":"0x..."}
68
+ ```
69
+
70
+ ### `veil register`
71
+
72
+ Register your deposit key on-chain (one-time per address).
73
+
74
+ ```bash
75
+ veil register # Signs & sends
76
+ veil register --json # JSON output
77
+ veil register --unsigned --address 0x... # Unsigned payload for agents
78
+ ```
79
+
80
+ ### `veil deposit <amount>`
81
+
82
+ Deposit ETH into the privacy pool.
83
+
84
+ ```bash
85
+ veil deposit 0.1 # Signs & sends (JSON output)
86
+ veil deposit 0.1 --unsigned # Unsigned payload for agents
87
+ veil deposit 0.1 --quiet # Suppress progress output
88
+ ```
89
+
90
+ Output:
91
+ ```json
92
+ {
93
+ "success": true,
94
+ "hash": "0x...",
95
+ "amount": "0.1",
96
+ "blockNumber": "12345678",
97
+ "gasUsed": "150000"
98
+ }
99
+ ```
100
+
101
+ ### `veil balance`
102
+
103
+ Show both queue and private balances.
104
+
105
+ ```bash
106
+ veil balance # Show all balances
107
+ veil balance --quiet # Suppress progress output
108
+ ```
109
+
110
+ Output:
111
+ ```json
112
+ {
113
+ "address": "0x...",
114
+ "depositKey": "0x...",
115
+ "totalBalance": "0.15",
116
+ "totalBalanceWei": "150000000000000000",
117
+ "private": {
118
+ "balance": "0.10",
119
+ "balanceWei": "100000000000000000",
120
+ "utxoCount": 2,
121
+ "utxos": [
122
+ { "index": 5, "amount": "0.05" },
123
+ { "index": 8, "amount": "0.05" }
124
+ ]
125
+ },
126
+ "queue": {
127
+ "balance": "0.05",
128
+ "balanceWei": "50000000000000000",
129
+ "count": 1,
130
+ "deposits": [
131
+ { "nonce": 42, "amount": "0.05", "status": "pending" }
132
+ ]
133
+ }
134
+ }
135
+ ```
136
+
137
+ ### `veil withdraw <amount> <recipient>`
138
+
139
+ Withdraw from the privacy pool to any public address.
140
+
141
+ ```bash
142
+ veil withdraw 0.05 0xRecipientAddress
143
+ veil withdraw 0.05 0xRecipientAddress --quiet
144
+ ```
145
+
146
+ Output:
147
+ ```json
148
+ {
149
+ "success": true,
150
+ "transactionHash": "0x...",
151
+ "blockNumber": 12345678,
152
+ "amount": "0.05",
153
+ "recipient": "0x...",
154
+ "type": "withdraw"
155
+ }
156
+ ```
157
+
158
+ ### `veil transfer <amount> <recipient>`
159
+
160
+ Transfer privately to another registered Veil user.
161
+
162
+ ```bash
163
+ veil transfer 0.02 0xRecipientAddress
164
+ veil transfer 0.02 0xRecipientAddress --quiet
165
+ ```
166
+
167
+ Output:
168
+ ```json
169
+ {
170
+ "success": true,
171
+ "transactionHash": "0x...",
172
+ "blockNumber": 12345678,
173
+ "amount": "0.02",
174
+ "recipient": "0x...",
175
+ "type": "transfer"
176
+ }
177
+ ```
178
+
179
+ ### `veil merge <amount>`
180
+
181
+ Consolidate multiple small UTXOs into one (self-transfer).
182
+
183
+ ```bash
184
+ veil merge 0.1 # Merge UTXOs totaling 0.1 ETH
185
+ veil merge 0.1 --quiet
186
+ ```
187
+
188
+ ## Environment Variables
189
+
190
+ The CLI uses two config files:
191
+
192
+ | File | Purpose |
193
+ |------|---------|
194
+ | `.env.veil` | Veil keypair (VEIL_KEY, DEPOSIT_KEY) - created by `veil init` |
195
+ | `.env` | Wallet config (WALLET_KEY, RPC_URL) - your existing config |
196
+
197
+ ### Variables
198
+
199
+ | Variable | Description |
200
+ |----------|-------------|
201
+ | `VEIL_KEY` | Your Veil private key (for ZK proofs, withdrawals, transfers) |
202
+ | `DEPOSIT_KEY` | Your Veil deposit key (public, for register/deposit) |
203
+ | `WALLET_KEY` | Ethereum wallet private key (for signing transactions) |
204
+ | `RPC_URL` | Base RPC URL (optional, defaults to public RPC) |
205
+
206
+ ## Error Handling
207
+
208
+ All CLI commands output JSON with standardized error codes:
209
+
210
+ ```json
211
+ {
212
+ "success": false,
213
+ "errorCode": "VEIL_KEY_MISSING",
214
+ "error": "VEIL_KEY required. Use --veil-key or set VEIL_KEY env"
215
+ }
216
+ ```
217
+
218
+ ### Error Codes
219
+
220
+ | Code | Description |
221
+ |------|-------------|
222
+ | `VEIL_KEY_MISSING` | VEIL_KEY not provided |
223
+ | `WALLET_KEY_MISSING` | WALLET_KEY not provided |
224
+ | `DEPOSIT_KEY_MISSING` | DEPOSIT_KEY not provided |
225
+ | `INVALID_ADDRESS` | Invalid Ethereum address format |
226
+ | `INVALID_AMOUNT` | Invalid or below minimum amount |
227
+ | `INSUFFICIENT_BALANCE` | Not enough ETH balance |
228
+ | `USER_NOT_REGISTERED` | Recipient not registered in Veil |
229
+ | `NO_UTXOS` | No unspent UTXOs available |
230
+ | `RELAY_ERROR` | Error from relayer service |
231
+ | `RPC_ERROR` | RPC/network error |
232
+ | `CONTRACT_ERROR` | Smart contract reverted |
233
+ | `UNKNOWN_ERROR` | Unexpected error |
234
+
235
+ ## SDK Quick Start
236
+
237
+ ```typescript
238
+ import { Keypair, buildRegisterTx, buildDepositETHTx, withdraw, transfer } from '@veil-cash/sdk';
239
+ import { createWalletClient, http } from 'viem';
240
+ import { base } from 'viem/chains';
241
+ import { privateKeyToAccount } from 'viem/accounts';
242
+
243
+ // 1. Generate a Veil keypair (do this once, save securely!)
244
+ const keypair = new Keypair();
245
+ console.log('Veil Private Key:', keypair.privkey); // SAVE THIS!
246
+ console.log('Deposit Key:', keypair.depositKey()); // Register this on-chain
247
+
248
+ // 2. Setup your Ethereum wallet
249
+ const account = privateKeyToAccount('0x...');
250
+ const client = createWalletClient({
251
+ account,
252
+ chain: base,
253
+ transport: http(),
254
+ });
255
+
256
+ // 3. Register your deposit key (one-time)
257
+ const registerTx = buildRegisterTx(keypair.depositKey(), account.address);
258
+ await client.sendTransaction(registerTx);
259
+
260
+ // 4. Deposit ETH
261
+ const depositTx = buildDepositETHTx({
262
+ depositKey: keypair.depositKey(),
263
+ amount: '0.1', // 0.1 ETH
264
+ });
265
+ await client.sendTransaction({
266
+ ...depositTx,
267
+ value: depositTx.value,
268
+ });
269
+
270
+ // 5. Withdraw (sent via relayer, no wallet signing needed)
271
+ const withdrawResult = await withdraw({
272
+ amount: '0.05',
273
+ recipient: '0xRecipientAddress',
274
+ keypair,
275
+ });
276
+
277
+ // 6. Transfer privately
278
+ const transferResult = await transfer({
279
+ amount: '0.02',
280
+ recipientAddress: '0xRecipientAddress',
281
+ senderKeypair: keypair,
282
+ });
283
+ ```
284
+
285
+ ## SDK API Reference
286
+
287
+ ### Keypair
288
+
289
+ ```typescript
290
+ import { Keypair } from '@veil-cash/sdk';
291
+
292
+ // Generate new keypair
293
+ const keypair = new Keypair();
294
+
295
+ // Restore from saved Veil private key
296
+ const restored = new Keypair(savedVeilKey);
297
+
298
+ // Get deposit key (for registration)
299
+ keypair.depositKey(); // '0x...' (130 hex chars)
300
+
301
+ // Veil private key (store securely!)
302
+ keypair.privkey; // '0x...'
303
+ ```
304
+
305
+ ### Transaction Builders
306
+
307
+ ```typescript
308
+ import { buildRegisterTx, buildDepositETHTx } from '@veil-cash/sdk';
309
+
310
+ // Register deposit key (one-time)
311
+ const registerTx = buildRegisterTx(depositKey, ownerAddress);
312
+ // → { to: '0x...', data: '0x...' }
313
+
314
+ // Deposit ETH
315
+ const depositTx = buildDepositETHTx({
316
+ depositKey: keypair.depositKey(),
317
+ amount: '0.1',
318
+ });
319
+ // → { to: '0x...', data: '0x...', value: 100000000000000000n }
320
+ ```
321
+
322
+ ### Withdraw & Transfer
323
+
324
+ ```typescript
325
+ import { withdraw, transfer, mergeUtxos } from '@veil-cash/sdk';
326
+
327
+ // Withdraw to public address
328
+ const withdrawResult = await withdraw({
329
+ amount: '0.05',
330
+ recipient: '0xRecipientAddress',
331
+ keypair,
332
+ onProgress: (stage, detail) => console.log(stage, detail),
333
+ });
334
+
335
+ // Transfer to another registered user
336
+ const transferResult = await transfer({
337
+ amount: '0.02',
338
+ recipientAddress: '0xRecipientAddress',
339
+ senderKeypair: keypair,
340
+ });
341
+
342
+ // Merge UTXOs (consolidate small balances)
343
+ const mergeResult = await mergeUtxos({
344
+ amount: '0.1',
345
+ keypair,
346
+ });
347
+ ```
348
+
349
+ ### Balance Queries
350
+
351
+ ```typescript
352
+ import { getQueueBalance, getPrivateBalance } from '@veil-cash/sdk';
353
+
354
+ // Check queue balance (pending deposits)
355
+ const queueBalance = await getQueueBalance({
356
+ address: '0x...',
357
+ });
358
+
359
+ // Check private balance (requires keypair)
360
+ const privateBalance = await getPrivateBalance({
361
+ keypair,
362
+ });
363
+ ```
364
+
365
+ ### Addresses
366
+
367
+ ```typescript
368
+ import { getAddresses } from '@veil-cash/sdk';
369
+
370
+ const addresses = getAddresses();
371
+ console.log(addresses.entry); // Entry contract
372
+ console.log(addresses.ethPool); // ETH pool
373
+ console.log(addresses.ethQueue); // ETH queue
374
+ ```
375
+
376
+ ## For AI Agents
377
+
378
+ This SDK is designed to work with AI agent frameworks like [Bankr](https://bankr.bot).
379
+
380
+ ### Non-Interactive CLI
381
+
382
+ All commands output JSON and support non-interactive usage:
383
+
384
+ ```bash
385
+ # Generate keypair as JSON (no prompts, no file save)
386
+ veil init --json
387
+
388
+ # Get unsigned transaction payloads for agent signing
389
+ veil register --unsigned --address 0x...
390
+ veil deposit 0.1 --unsigned
391
+
392
+ # Suppress progress output for clean JSON
393
+ veil balance --quiet
394
+ veil withdraw 0.05 0xRecipient --quiet
395
+ ```
396
+
397
+ ### Bankr Integration
398
+
399
+ Use `--unsigned` to get Bankr-compatible transaction payloads:
400
+
401
+ ```bash
402
+ veil deposit 0.1 --unsigned
403
+ # {"to":"0x...","data":"0x...","value":"100000000000000000","chainId":8453}
404
+ ```
405
+
406
+ The `--unsigned` flag outputs the [Bankr arbitrary transaction format](https://github.com/BankrBot/moltbot-skills/blob/main/bankr/references/arbitrary-transaction.md).
407
+
408
+ ### Programmatic SDK Usage
409
+
410
+ ```typescript
411
+ import { Keypair, buildDepositETHTx, withdraw } from '@veil-cash/sdk';
412
+
413
+ // For deposits: build transaction, let agent sign via Bankr
414
+ const keypair = new Keypair(veilKey);
415
+ const tx = buildDepositETHTx({
416
+ depositKey: keypair.depositKey(),
417
+ amount: '0.1',
418
+ });
419
+ // → { to, data, value } - pass to Bankr for signing
420
+
421
+ // For withdrawals: SDK handles ZK proofs, submits to relayer
422
+ const result = await withdraw({
423
+ amount: '0.05',
424
+ recipient: '0xRecipient',
425
+ keypair,
426
+ });
427
+ // → { success, transactionHash, blockNumber }
428
+ ```
429
+
430
+ ## Deposit Flow
431
+
432
+ 1. **Generate Keypair**: Run `veil init` to create and save your Veil keypair
433
+ 2. **Register**: Run `veil register` to link your deposit key on-chain (one-time)
434
+ 3. **Deposit**: Run `veil deposit <amount>` to send ETH
435
+ 4. **Wait**: The Veil deposit engine processes your deposit
436
+ 5. **Done**: Your deposit is accepted into the privacy pool
437
+
438
+ ## Withdrawal Flow
439
+
440
+ 1. **Check Balance**: Run `veil balance` to see your private balance
441
+ 2. **Withdraw**: Run `veil withdraw <amount> <recipient>`
442
+ 3. **Done**: The SDK builds ZK proofs and submits via the relayer
443
+
444
+ ## License
445
+
446
+ MIT