@veilo/sdk-core 0.1.17 → 0.4.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 +1296 -272
- package/SHIELD_INTEGRATION.md +143 -0
- package/config.d.ts +2 -0
- package/config.js +4 -0
- package/dist/cjs/client.d.ts +407 -0
- package/dist/cjs/client.js +951 -0
- package/dist/cjs/compactNote.d.ts +107 -0
- package/dist/cjs/compactNote.js +167 -0
- package/dist/cjs/config.d.ts +82 -0
- package/dist/cjs/config.js +57 -0
- package/dist/cjs/events.d.ts +77 -0
- package/dist/cjs/events.js +167 -0
- package/dist/cjs/idl/privacy_pool.d.ts +5 -0
- package/dist/cjs/idl/privacy_pool.js +15218 -0
- package/dist/cjs/idl/privacy_pool.json +10313 -0
- package/dist/cjs/index.d.ts +16 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/merkle.d.ts +77 -0
- package/dist/cjs/merkle.js +156 -0
- package/dist/cjs/poseidon.d.ts +29 -0
- package/dist/cjs/poseidon.js +100 -0
- package/dist/cjs/program.d.ts +37 -0
- package/dist/cjs/program.js +61 -0
- package/dist/cjs/proof.d.ts +183 -0
- package/dist/cjs/proof.js +292 -0
- package/dist/cjs/prover.d.ts +54 -0
- package/dist/cjs/prover.js +112 -0
- package/dist/cjs/random.d.ts +16 -0
- package/dist/cjs/random.js +28 -0
- package/dist/cjs/relayer.d.ts +318 -0
- package/dist/cjs/relayer.js +257 -0
- package/dist/cjs/retry.d.ts +32 -0
- package/dist/cjs/retry.js +75 -0
- package/dist/cjs/shield/alt.d.ts +87 -0
- package/dist/cjs/shield/alt.js +194 -0
- package/dist/cjs/shield/computeBudget.d.ts +61 -0
- package/dist/cjs/shield/computeBudget.js +64 -0
- package/dist/cjs/shield/errors.d.ts +58 -0
- package/dist/cjs/shield/errors.js +121 -0
- package/dist/cjs/shield/finalize.d.ts +45 -0
- package/dist/cjs/shield/finalize.js +119 -0
- package/dist/cjs/shield/index.d.ts +35 -0
- package/dist/cjs/shield/index.js +68 -0
- package/dist/cjs/shield/ix.d.ts +54 -0
- package/dist/cjs/shield/ix.js +119 -0
- package/dist/cjs/shield/owner.d.ts +36 -0
- package/dist/cjs/shield/owner.js +126 -0
- package/dist/cjs/shield/ports.d.ts +43 -0
- package/dist/cjs/shield/ports.js +153 -0
- package/dist/cjs/shield/preflight.d.ts +30 -0
- package/dist/cjs/shield/preflight.js +154 -0
- package/dist/cjs/shield/shield.d.ts +68 -0
- package/dist/cjs/shield/shield.js +499 -0
- package/dist/cjs/shield/types.d.ts +202 -0
- package/dist/cjs/shield/types.js +2 -0
- package/dist/cjs/utxo.d.ts +235 -0
- package/dist/cjs/utxo.js +407 -0
- package/dist/esm/client.d.ts +407 -0
- package/dist/esm/client.js +891 -0
- package/dist/esm/compactNote.d.ts +107 -0
- package/dist/esm/compactNote.js +156 -0
- package/dist/esm/config.d.ts +82 -0
- package/dist/esm/config.js +51 -0
- package/dist/esm/events.d.ts +77 -0
- package/dist/esm/events.js +129 -0
- package/dist/esm/idl/privacy_pool.d.ts +5 -0
- package/dist/esm/idl/privacy_pool.js +15216 -0
- package/dist/esm/idl/privacy_pool.json +10313 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +29 -0
- package/dist/esm/merkle.d.ts +77 -0
- package/dist/esm/merkle.js +151 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/poseidon.d.ts +29 -0
- package/dist/esm/poseidon.js +87 -0
- package/dist/esm/program.d.ts +37 -0
- package/dist/esm/program.js +53 -0
- package/dist/esm/proof.d.ts +183 -0
- package/dist/esm/proof.js +281 -0
- package/dist/esm/prover.d.ts +54 -0
- package/dist/esm/prover.js +75 -0
- package/dist/esm/random.d.ts +16 -0
- package/dist/esm/random.js +21 -0
- package/dist/esm/relayer.d.ts +318 -0
- package/dist/esm/relayer.js +249 -0
- package/dist/esm/retry.d.ts +32 -0
- package/dist/esm/retry.js +71 -0
- package/dist/esm/shield/alt.d.ts +87 -0
- package/dist/esm/shield/alt.js +186 -0
- package/dist/esm/shield/computeBudget.d.ts +61 -0
- package/dist/esm/shield/computeBudget.js +61 -0
- package/dist/esm/shield/errors.d.ts +58 -0
- package/dist/esm/shield/errors.js +115 -0
- package/dist/esm/shield/finalize.d.ts +45 -0
- package/dist/esm/shield/finalize.js +83 -0
- package/dist/esm/shield/index.d.ts +35 -0
- package/dist/esm/shield/index.js +32 -0
- package/dist/esm/shield/ix.d.ts +54 -0
- package/dist/esm/shield/ix.js +82 -0
- package/dist/esm/shield/owner.d.ts +36 -0
- package/dist/esm/shield/owner.js +122 -0
- package/dist/esm/shield/ports.d.ts +43 -0
- package/dist/esm/shield/ports.js +147 -0
- package/dist/esm/shield/preflight.d.ts +30 -0
- package/dist/esm/shield/preflight.js +151 -0
- package/dist/esm/shield/shield.d.ts +68 -0
- package/dist/esm/shield/shield.js +492 -0
- package/dist/esm/shield/types.d.ts +202 -0
- package/dist/esm/shield/types.js +1 -0
- package/dist/esm/utxo.d.ts +235 -0
- package/dist/esm/utxo.js +382 -0
- package/dist/src/client.d.ts +407 -0
- package/dist/src/client.js +951 -0
- package/dist/src/compactNote.d.ts +107 -0
- package/dist/src/compactNote.js +167 -0
- package/dist/src/config.d.ts +82 -0
- package/dist/src/config.js +57 -0
- package/dist/src/events.d.ts +77 -0
- package/dist/src/events.js +167 -0
- package/dist/src/idl/privacy_pool.d.ts +5 -0
- package/dist/src/idl/privacy_pool.js +15218 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +67 -0
- package/dist/src/merkle.d.ts +77 -0
- package/dist/src/merkle.js +156 -0
- package/dist/src/poseidon.d.ts +29 -0
- package/dist/src/poseidon.js +100 -0
- package/dist/src/program.d.ts +37 -0
- package/dist/src/program.js +61 -0
- package/dist/src/proof.d.ts +183 -0
- package/dist/src/proof.js +292 -0
- package/dist/src/prover.d.ts +54 -0
- package/dist/src/prover.js +112 -0
- package/dist/src/random.d.ts +16 -0
- package/dist/src/random.js +28 -0
- package/dist/src/relayer.d.ts +318 -0
- package/dist/src/relayer.js +257 -0
- package/dist/src/retry.d.ts +32 -0
- package/dist/src/retry.js +75 -0
- package/dist/src/shield/alt.d.ts +87 -0
- package/dist/src/shield/alt.js +194 -0
- package/dist/src/shield/computeBudget.d.ts +61 -0
- package/dist/src/shield/computeBudget.js +64 -0
- package/dist/src/shield/errors.d.ts +58 -0
- package/dist/src/shield/errors.js +121 -0
- package/dist/src/shield/finalize.d.ts +45 -0
- package/dist/src/shield/finalize.js +119 -0
- package/dist/src/shield/index.d.ts +35 -0
- package/dist/src/shield/index.js +68 -0
- package/dist/src/shield/ix.d.ts +54 -0
- package/dist/src/shield/ix.js +119 -0
- package/dist/src/shield/owner.d.ts +36 -0
- package/dist/src/shield/owner.js +126 -0
- package/dist/src/shield/ports.d.ts +43 -0
- package/dist/src/shield/ports.js +153 -0
- package/dist/src/shield/preflight.d.ts +30 -0
- package/dist/src/shield/preflight.js +154 -0
- package/dist/src/shield/shield.d.ts +68 -0
- package/dist/src/shield/shield.js +499 -0
- package/dist/src/shield/types.d.ts +202 -0
- package/dist/src/shield/types.js +2 -0
- package/dist/src/utxo.d.ts +235 -0
- package/dist/src/utxo.js +407 -0
- package/dist/tests/compact-note.test.d.ts +1 -0
- package/dist/tests/compact-note.test.js +173 -0
- package/dist/tests/config.test.d.ts +1 -0
- package/dist/tests/config.test.js +102 -0
- package/dist/tests/edge-cases.test.d.ts +1 -0
- package/dist/tests/edge-cases.test.js +220 -0
- package/dist/tests/encryption.test.d.ts +1 -0
- package/dist/tests/encryption.test.js +215 -0
- package/dist/tests/events.test.d.ts +1 -0
- package/dist/tests/events.test.js +78 -0
- package/dist/tests/multi-tree.test.d.ts +1 -0
- package/dist/tests/multi-tree.test.js +405 -0
- package/dist/tests/pda.test.d.ts +1 -0
- package/dist/tests/pda.test.js +229 -0
- package/dist/tests/poseidon-builder-parity.test.d.ts +1 -0
- package/dist/tests/poseidon-builder-parity.test.js +72 -0
- package/dist/tests/poseidon.test.d.ts +1 -0
- package/dist/tests/poseidon.test.js +142 -0
- package/dist/tests/proof.test.d.ts +1 -0
- package/dist/tests/proof.test.js +296 -0
- package/dist/tests/relayer.test.d.ts +1 -0
- package/dist/tests/relayer.test.js +271 -0
- package/dist/tests/sdk.integration.test.d.ts +1 -0
- package/dist/tests/sdk.integration.test.js +330 -0
- package/dist/tests/shield-owner.test.d.ts +1 -0
- package/dist/tests/shield-owner.test.js +89 -0
- package/dist/tests/shield-preflight.test.d.ts +1 -0
- package/dist/tests/shield-preflight.test.js +87 -0
- package/dist/tests/shield-realproof.test.d.ts +1 -0
- package/dist/tests/shield-realproof.test.js +272 -0
- package/dist/tests/shield.test.d.ts +1 -0
- package/dist/tests/shield.test.js +403 -0
- package/dist/tests/utxo.test.d.ts +1 -0
- package/dist/tests/utxo.test.js +140 -0
- package/package.json +105 -11
- package/poseidon.d.ts +2 -0
- package/poseidon.js +4 -0
- package/proof.d.ts +2 -0
- package/proof.js +4 -0
- package/prover.d.ts +2 -0
- package/prover.js +4 -0
- package/shield.d.ts +2 -0
- package/shield.js +4 -0
- package/src/client.ts +0 -352
- package/src/config.ts +0 -13
- package/src/index.ts +0 -6
- package/src/merkle.ts +0 -178
- package/src/note.ts +0 -193
- package/src/poseidon.ts +0 -62
- package/src/proof.ts +0 -170
- package/test/script.js +0 -0
- package/test-tsconfig.json +0 -19
- package/tests/note.test.ts +0 -50
- package/tests/sdk.integration.test.ts +0 -210
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
export interface VeiloRelayerConfig {
|
|
2
|
+
/** Base URL of the relayer. Defaults to Veilo's production service. */
|
|
3
|
+
relayerUrl?: string;
|
|
4
|
+
/** API key sent as x-api-key header */
|
|
5
|
+
apiKey: string;
|
|
6
|
+
/** Relayer's NaCl box public key (base64-encoded, 32 bytes) */
|
|
7
|
+
relayerPublicKey: string;
|
|
8
|
+
/** Optional JWT auth token for authenticated endpoints */
|
|
9
|
+
authToken?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Structured error thrown by VeiloRelayerClient on API failures. */
|
|
12
|
+
export declare class VeiloApiError extends Error {
|
|
13
|
+
code: string;
|
|
14
|
+
status: number;
|
|
15
|
+
details?: unknown;
|
|
16
|
+
constructor(code: string, message: string, status: number, details?: unknown);
|
|
17
|
+
}
|
|
18
|
+
/** Retry configuration for the relayer HTTP client. */
|
|
19
|
+
export interface RetryPolicy {
|
|
20
|
+
/** Max number of retry attempts (default: 3) */
|
|
21
|
+
maxRetries: number;
|
|
22
|
+
/** Base delay in ms for exponential backoff (default: 1000) */
|
|
23
|
+
baseDelayMs: number;
|
|
24
|
+
/** HTTP status codes that trigger a retry (default: [429, 502, 503, 504]) */
|
|
25
|
+
retryableStatuses: number[];
|
|
26
|
+
}
|
|
27
|
+
export interface RelayerEncryptedNote {
|
|
28
|
+
noteId: string;
|
|
29
|
+
commitment: string;
|
|
30
|
+
ephemeralPublicKey: string;
|
|
31
|
+
encryptedBlob: string;
|
|
32
|
+
timestamp: number;
|
|
33
|
+
blockHeight?: number;
|
|
34
|
+
txSignature?: string;
|
|
35
|
+
spent?: boolean;
|
|
36
|
+
onchainId?: string;
|
|
37
|
+
spentTx?: string;
|
|
38
|
+
spentAt?: number;
|
|
39
|
+
compactEphemeralKey?: string | null;
|
|
40
|
+
compactBlob?: string | null;
|
|
41
|
+
viewTag?: number | null;
|
|
42
|
+
leafIndex?: number | null;
|
|
43
|
+
mintAddress?: string | null;
|
|
44
|
+
treeId?: number | null;
|
|
45
|
+
}
|
|
46
|
+
export interface QueryNotesRequest {
|
|
47
|
+
lastCheckedTimestamp?: number;
|
|
48
|
+
limit?: number;
|
|
49
|
+
offset?: number;
|
|
50
|
+
walletPublicKey?: string;
|
|
51
|
+
}
|
|
52
|
+
export interface QueryNotesResponse {
|
|
53
|
+
success: boolean;
|
|
54
|
+
notes: RelayerEncryptedNote[];
|
|
55
|
+
total: number;
|
|
56
|
+
limit: number;
|
|
57
|
+
offset: number;
|
|
58
|
+
}
|
|
59
|
+
export interface SaveEncryptedNoteRequest {
|
|
60
|
+
commitment: string;
|
|
61
|
+
ephemeralPublicKey: string;
|
|
62
|
+
encryptedBlob: string;
|
|
63
|
+
timestamp: number;
|
|
64
|
+
blockHeight?: number;
|
|
65
|
+
txSignature?: string;
|
|
66
|
+
recipientWalletPublicKey?: string;
|
|
67
|
+
compactEphemeralKey?: string | null;
|
|
68
|
+
compactBlob?: string | null;
|
|
69
|
+
viewTag?: number | null;
|
|
70
|
+
leafIndex?: number;
|
|
71
|
+
newRoot?: string;
|
|
72
|
+
mintAddress?: string;
|
|
73
|
+
treeId?: number;
|
|
74
|
+
amountRaw?: string;
|
|
75
|
+
}
|
|
76
|
+
export interface SaveEncryptedNoteResponse {
|
|
77
|
+
success: boolean;
|
|
78
|
+
noteId?: string;
|
|
79
|
+
message?: string;
|
|
80
|
+
error?: string;
|
|
81
|
+
}
|
|
82
|
+
export interface MerkleRootResponse {
|
|
83
|
+
success: boolean;
|
|
84
|
+
data: {
|
|
85
|
+
root: string;
|
|
86
|
+
nextIndex: number;
|
|
87
|
+
treeId: number;
|
|
88
|
+
onChainTreeId: number;
|
|
89
|
+
mintAddress: string;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export interface MerkleTreeResponse {
|
|
93
|
+
success: boolean;
|
|
94
|
+
data: {
|
|
95
|
+
treeId: number;
|
|
96
|
+
onChainTreeId: number;
|
|
97
|
+
mintAddress: string;
|
|
98
|
+
height: number;
|
|
99
|
+
nextIndex: number;
|
|
100
|
+
root: string;
|
|
101
|
+
totalCommitments: number;
|
|
102
|
+
leaves: Array<{
|
|
103
|
+
commitment: string;
|
|
104
|
+
index: number;
|
|
105
|
+
txSignature: string;
|
|
106
|
+
slot: number;
|
|
107
|
+
timestamp: string;
|
|
108
|
+
}>;
|
|
109
|
+
reconstructionInfo: {
|
|
110
|
+
instructions: string;
|
|
111
|
+
treeHeight: number;
|
|
112
|
+
hashFunction?: string;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
export interface TransactNote {
|
|
117
|
+
commitment: string;
|
|
118
|
+
privateKey: string;
|
|
119
|
+
publicKey: string;
|
|
120
|
+
blinding: string;
|
|
121
|
+
amount: string;
|
|
122
|
+
nullifier: string;
|
|
123
|
+
leafIndex?: number;
|
|
124
|
+
noteId?: string;
|
|
125
|
+
}
|
|
126
|
+
export interface WithdrawRequest {
|
|
127
|
+
notes: TransactNote[];
|
|
128
|
+
recipient: string;
|
|
129
|
+
amount: string;
|
|
130
|
+
userPublicKey: string;
|
|
131
|
+
mintAddress: string;
|
|
132
|
+
}
|
|
133
|
+
export interface WithdrawResponse {
|
|
134
|
+
success: boolean;
|
|
135
|
+
message: string;
|
|
136
|
+
data?: {
|
|
137
|
+
withdrawRecipient: string;
|
|
138
|
+
withdrawAmount: number;
|
|
139
|
+
changeAmount: number;
|
|
140
|
+
changeNote: {
|
|
141
|
+
commitment: string;
|
|
142
|
+
privateKey: string;
|
|
143
|
+
publicKey: string;
|
|
144
|
+
blinding: string;
|
|
145
|
+
amount: string;
|
|
146
|
+
nullifier: string;
|
|
147
|
+
};
|
|
148
|
+
spentNoteIds?: string[];
|
|
149
|
+
txSignature?: string;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export interface PrivateTransferRequest {
|
|
153
|
+
notes: TransactNote[];
|
|
154
|
+
amount: string;
|
|
155
|
+
recipientUsername: string;
|
|
156
|
+
userPublicKey?: string;
|
|
157
|
+
mintAddress?: string;
|
|
158
|
+
}
|
|
159
|
+
export interface PrivateTransferResponse {
|
|
160
|
+
success: boolean;
|
|
161
|
+
message: string;
|
|
162
|
+
data?: {
|
|
163
|
+
recipient: string;
|
|
164
|
+
transferAmount: number;
|
|
165
|
+
senderChangeAmount: number;
|
|
166
|
+
txSignature: string;
|
|
167
|
+
recipientNote: {
|
|
168
|
+
commitment: string;
|
|
169
|
+
amount: string;
|
|
170
|
+
leafIndex: number;
|
|
171
|
+
savedNoteId: string;
|
|
172
|
+
};
|
|
173
|
+
senderChangeNote: {
|
|
174
|
+
commitment: string;
|
|
175
|
+
privateKey: string;
|
|
176
|
+
publicKey: string;
|
|
177
|
+
blinding: string;
|
|
178
|
+
amount: string;
|
|
179
|
+
leafIndex: number;
|
|
180
|
+
savedNoteId: string;
|
|
181
|
+
} | null;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
export interface PrivateSwapRequest {
|
|
185
|
+
notes: TransactNote[];
|
|
186
|
+
sourceMintAddress: string;
|
|
187
|
+
destMintAddress: string;
|
|
188
|
+
sourceJupiterMint?: string;
|
|
189
|
+
destJupiterMint?: string;
|
|
190
|
+
swapAmountRaw: string;
|
|
191
|
+
minAmountOut: string;
|
|
192
|
+
slippageBps: number;
|
|
193
|
+
userPublicKey: string;
|
|
194
|
+
veiloPublicKey: string;
|
|
195
|
+
}
|
|
196
|
+
export interface PrivateSwapResponse {
|
|
197
|
+
success: boolean;
|
|
198
|
+
message: string;
|
|
199
|
+
data?: {
|
|
200
|
+
txSignature: string;
|
|
201
|
+
swapAmountRaw: string;
|
|
202
|
+
outputAmount: string;
|
|
203
|
+
minAmountOut: string;
|
|
204
|
+
changeAmount: string;
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
export interface ChallengeResponse {
|
|
208
|
+
success: boolean;
|
|
209
|
+
challenge: string;
|
|
210
|
+
expiresAt: number;
|
|
211
|
+
}
|
|
212
|
+
export interface RegisterRequest {
|
|
213
|
+
username: string;
|
|
214
|
+
walletPublicKey: string;
|
|
215
|
+
signature: string;
|
|
216
|
+
challenge: string;
|
|
217
|
+
veiloPublicKey: string;
|
|
218
|
+
}
|
|
219
|
+
export interface RegisterResponse {
|
|
220
|
+
success: boolean;
|
|
221
|
+
token: string;
|
|
222
|
+
message?: string;
|
|
223
|
+
}
|
|
224
|
+
export interface RestoreRequest {
|
|
225
|
+
walletPublicKey: string;
|
|
226
|
+
signature: string;
|
|
227
|
+
challenge: string;
|
|
228
|
+
}
|
|
229
|
+
export interface RestoreResponse {
|
|
230
|
+
success: boolean;
|
|
231
|
+
token: string;
|
|
232
|
+
username: string;
|
|
233
|
+
veiloPublicKey: string;
|
|
234
|
+
}
|
|
235
|
+
export interface CheckUsernameResponse {
|
|
236
|
+
success: boolean;
|
|
237
|
+
available: boolean;
|
|
238
|
+
}
|
|
239
|
+
export interface VeiloPublicKeyResponse {
|
|
240
|
+
success: boolean;
|
|
241
|
+
username: string;
|
|
242
|
+
pubK: string;
|
|
243
|
+
veiloPublicKey: string;
|
|
244
|
+
}
|
|
245
|
+
export interface PreRegisterKeyResponse {
|
|
246
|
+
success: boolean;
|
|
247
|
+
veiloPublicKey: string;
|
|
248
|
+
}
|
|
249
|
+
export interface DeleteUserDataResponse {
|
|
250
|
+
success: boolean;
|
|
251
|
+
message: string;
|
|
252
|
+
deletedCount?: number;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* HTTP client for the Veilo relayer API.
|
|
256
|
+
*
|
|
257
|
+
* Handles auth, notes, merkle, and encrypted transact endpoints with
|
|
258
|
+
* automatic retry on transient failures and NaCl box payload encryption.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* const client = new VeiloRelayerClient({
|
|
263
|
+
* apiKey: "your-api-key",
|
|
264
|
+
* relayerPublicKey: "<base64 NaCl public key>",
|
|
265
|
+
* });
|
|
266
|
+
* const root = await client.getMerkleRoot("So111...");
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export declare class VeiloRelayerClient {
|
|
270
|
+
private readonly baseUrl;
|
|
271
|
+
private readonly apiKey;
|
|
272
|
+
private readonly relayerPubKey;
|
|
273
|
+
private authToken;
|
|
274
|
+
private readonly retry;
|
|
275
|
+
constructor(config: VeiloRelayerConfig, retry?: Partial<RetryPolicy>);
|
|
276
|
+
/** Update the auth token (e.g. after register/restore). */
|
|
277
|
+
setAuthToken(token: string): void;
|
|
278
|
+
private headers;
|
|
279
|
+
private request;
|
|
280
|
+
private encrypt;
|
|
281
|
+
/** Request a signature challenge for wallet authentication. */
|
|
282
|
+
getChallenge(walletPublicKey: string): Promise<ChallengeResponse>;
|
|
283
|
+
/** Register a new account with a signed challenge. Returns a JWT auth token. */
|
|
284
|
+
register(data: RegisterRequest): Promise<RegisterResponse>;
|
|
285
|
+
/** Restore an existing account using a signed challenge. Returns a JWT auth token. */
|
|
286
|
+
restore(data: RestoreRequest): Promise<RestoreResponse>;
|
|
287
|
+
/** Check whether a username is available for registration. */
|
|
288
|
+
checkUsername(username: string): Promise<CheckUsernameResponse>;
|
|
289
|
+
/** Look up a user's Veilo public key by username or wallet public key. */
|
|
290
|
+
getVeiloPublicKey(params: {
|
|
291
|
+
username: string;
|
|
292
|
+
publicKey?: never;
|
|
293
|
+
} | {
|
|
294
|
+
publicKey: string;
|
|
295
|
+
username?: never;
|
|
296
|
+
}): Promise<VeiloPublicKeyResponse>;
|
|
297
|
+
/**
|
|
298
|
+
* Provision (or retrieve) the Veilo spend key paired with a Solana address.
|
|
299
|
+
* Public and idempotent; the response contains public key material only.
|
|
300
|
+
*/
|
|
301
|
+
preRegisterKey(solanaAddress: string): Promise<PreRegisterKeyResponse>;
|
|
302
|
+
/** Save an encrypted note to the relayer's database. */
|
|
303
|
+
saveEncryptedNote(data: SaveEncryptedNoteRequest): Promise<SaveEncryptedNoteResponse>;
|
|
304
|
+
/** Query encrypted notes for the authenticated user. Requires auth token. */
|
|
305
|
+
queryEncryptedNotes(params?: QueryNotesRequest): Promise<QueryNotesResponse>;
|
|
306
|
+
/** Delete all stored data for the authenticated user. Requires auth token. */
|
|
307
|
+
deleteUserData(): Promise<DeleteUserDataResponse>;
|
|
308
|
+
/** Fetch the current Merkle root for a pool, optionally filtered by mint and tree ID. */
|
|
309
|
+
getMerkleRoot(mintAddress?: string, treeId?: number): Promise<MerkleRootResponse>;
|
|
310
|
+
/** Fetch the full Merkle tree (all leaves) for a pool. */
|
|
311
|
+
getMerkleTree(mintAddress?: string, treeId?: number): Promise<MerkleTreeResponse>;
|
|
312
|
+
/** Submit an encrypted withdrawal request to the relayer. Not retried on failure. */
|
|
313
|
+
submitWithdraw(data: WithdrawRequest): Promise<WithdrawResponse>;
|
|
314
|
+
/** Submit an encrypted private transfer request. Not retried on failure. */
|
|
315
|
+
submitPrivateTransfer(data: PrivateTransferRequest): Promise<PrivateTransferResponse>;
|
|
316
|
+
/** Submit an encrypted private swap request. Not retried on failure. */
|
|
317
|
+
submitPrivateSwap(data: PrivateSwapRequest): Promise<PrivateSwapResponse>;
|
|
318
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import nacl from "tweetnacl";
|
|
2
|
+
const DEFAULT_RELAYER_URL = "https://relayer-server.onrender.com";
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// Structured Error
|
|
5
|
+
// =============================================================================
|
|
6
|
+
/** Structured error thrown by VeiloRelayerClient on API failures. */
|
|
7
|
+
export class VeiloApiError extends Error {
|
|
8
|
+
constructor(code, message, status, details) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "VeiloApiError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.details = details;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const DEFAULT_RETRY = {
|
|
17
|
+
maxRetries: 3,
|
|
18
|
+
baseDelayMs: 1000,
|
|
19
|
+
retryableStatuses: [429, 502, 503, 504],
|
|
20
|
+
};
|
|
21
|
+
// =============================================================================
|
|
22
|
+
// Base64 helpers (no dependency on tweetnacl-util)
|
|
23
|
+
// =============================================================================
|
|
24
|
+
function toBase64(bytes) {
|
|
25
|
+
if (typeof Buffer !== "undefined") {
|
|
26
|
+
return Buffer.from(bytes).toString("base64");
|
|
27
|
+
}
|
|
28
|
+
return btoa(String.fromCharCode(...bytes));
|
|
29
|
+
}
|
|
30
|
+
function fromBase64(str) {
|
|
31
|
+
if (typeof Buffer !== "undefined") {
|
|
32
|
+
return new Uint8Array(Buffer.from(str, "base64"));
|
|
33
|
+
}
|
|
34
|
+
const binary = atob(str);
|
|
35
|
+
const bytes = new Uint8Array(binary.length);
|
|
36
|
+
for (let i = 0; i < binary.length; i++)
|
|
37
|
+
bytes[i] = binary.charCodeAt(i);
|
|
38
|
+
return bytes;
|
|
39
|
+
}
|
|
40
|
+
// =============================================================================
|
|
41
|
+
// Encryption
|
|
42
|
+
// =============================================================================
|
|
43
|
+
/**
|
|
44
|
+
* Encrypt a JSON-serializable payload for the relayer using NaCl box.
|
|
45
|
+
* Layout: [ephemeralPubKey (32)] [nonce (24)] [ciphertext (...)]
|
|
46
|
+
*/
|
|
47
|
+
function encryptForRelayer(data, relayerPublicKey) {
|
|
48
|
+
const ephemeral = nacl.box.keyPair();
|
|
49
|
+
const message = new TextEncoder().encode(JSON.stringify(data));
|
|
50
|
+
const nonce = nacl.randomBytes(nacl.box.nonceLength);
|
|
51
|
+
const ciphertext = nacl.box(message, nonce, relayerPublicKey, ephemeral.secretKey);
|
|
52
|
+
const payload = new Uint8Array(ephemeral.publicKey.length + nonce.length + ciphertext.length);
|
|
53
|
+
payload.set(ephemeral.publicKey, 0);
|
|
54
|
+
payload.set(nonce, ephemeral.publicKey.length);
|
|
55
|
+
payload.set(ciphertext, ephemeral.publicKey.length + nonce.length);
|
|
56
|
+
return toBase64(payload);
|
|
57
|
+
}
|
|
58
|
+
// =============================================================================
|
|
59
|
+
// Relayer Client
|
|
60
|
+
// =============================================================================
|
|
61
|
+
/**
|
|
62
|
+
* HTTP client for the Veilo relayer API.
|
|
63
|
+
*
|
|
64
|
+
* Handles auth, notes, merkle, and encrypted transact endpoints with
|
|
65
|
+
* automatic retry on transient failures and NaCl box payload encryption.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const client = new VeiloRelayerClient({
|
|
70
|
+
* apiKey: "your-api-key",
|
|
71
|
+
* relayerPublicKey: "<base64 NaCl public key>",
|
|
72
|
+
* });
|
|
73
|
+
* const root = await client.getMerkleRoot("So111...");
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class VeiloRelayerClient {
|
|
77
|
+
constructor(config, retry) {
|
|
78
|
+
this.baseUrl = (config.relayerUrl?.trim() || DEFAULT_RELAYER_URL).replace(/\/+$/, "");
|
|
79
|
+
this.apiKey = config.apiKey;
|
|
80
|
+
this.relayerPubKey = fromBase64(config.relayerPublicKey);
|
|
81
|
+
this.authToken = config.authToken;
|
|
82
|
+
this.retry = { ...DEFAULT_RETRY, ...retry };
|
|
83
|
+
}
|
|
84
|
+
/** Update the auth token (e.g. after register/restore). */
|
|
85
|
+
setAuthToken(token) {
|
|
86
|
+
this.authToken = token;
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// Internal helpers
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
headers(authenticated) {
|
|
92
|
+
const h = {
|
|
93
|
+
"Content-Type": "application/json",
|
|
94
|
+
};
|
|
95
|
+
if (this.apiKey)
|
|
96
|
+
h["x-api-key"] = this.apiKey;
|
|
97
|
+
if (authenticated && this.authToken) {
|
|
98
|
+
h["Authorization"] = `Bearer ${this.authToken}`;
|
|
99
|
+
}
|
|
100
|
+
return h;
|
|
101
|
+
}
|
|
102
|
+
async request(method, path, opts = {}) {
|
|
103
|
+
const url = `${this.baseUrl}/api${path}`;
|
|
104
|
+
const { body, authenticated = false, retryable = true } = opts;
|
|
105
|
+
const maxAttempts = retryable ? this.retry.maxRetries + 1 : 1;
|
|
106
|
+
let lastError;
|
|
107
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
108
|
+
if (attempt > 0) {
|
|
109
|
+
const delay = this.retry.baseDelayMs * Math.pow(2, attempt - 1) +
|
|
110
|
+
Math.random() * 200;
|
|
111
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
112
|
+
}
|
|
113
|
+
let res;
|
|
114
|
+
try {
|
|
115
|
+
res = await fetch(url, {
|
|
116
|
+
method,
|
|
117
|
+
headers: this.headers(authenticated),
|
|
118
|
+
body: body != null ? JSON.stringify(body) : undefined,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
lastError = new VeiloApiError("NETWORK_ERROR", err.message || "Network request failed", 0);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const data = await res.json().catch(() => ({}));
|
|
126
|
+
if (res.ok && data.success !== false) {
|
|
127
|
+
return data;
|
|
128
|
+
}
|
|
129
|
+
lastError = new VeiloApiError(data.error || "UNKNOWN", data.message || data.error || `Request failed: ${res.status}`, res.status, data);
|
|
130
|
+
// Only retry on transient statuses
|
|
131
|
+
if (!this.retry.retryableStatuses.includes(res.status)) {
|
|
132
|
+
throw lastError;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw lastError;
|
|
136
|
+
}
|
|
137
|
+
encrypt(data) {
|
|
138
|
+
return encryptForRelayer({
|
|
139
|
+
...data,
|
|
140
|
+
timestamp: Date.now(),
|
|
141
|
+
nonce: toBase64(nacl.randomBytes(32)),
|
|
142
|
+
}, this.relayerPubKey);
|
|
143
|
+
}
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
// Auth
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
/** Request a signature challenge for wallet authentication. */
|
|
148
|
+
async getChallenge(walletPublicKey) {
|
|
149
|
+
return this.request("POST", "/auth/challenge", {
|
|
150
|
+
body: { walletPublicKey },
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/** Register a new account with a signed challenge. Returns a JWT auth token. */
|
|
154
|
+
async register(data) {
|
|
155
|
+
return this.request("POST", "/auth/register", { body: data });
|
|
156
|
+
}
|
|
157
|
+
/** Restore an existing account using a signed challenge. Returns a JWT auth token. */
|
|
158
|
+
async restore(data) {
|
|
159
|
+
return this.request("POST", "/auth/restore", { body: data });
|
|
160
|
+
}
|
|
161
|
+
/** Check whether a username is available for registration. */
|
|
162
|
+
async checkUsername(username) {
|
|
163
|
+
return this.request("GET", `/auth/checkUsername?username=${encodeURIComponent(username)}`);
|
|
164
|
+
}
|
|
165
|
+
/** Look up a user's Veilo public key by username or wallet public key. */
|
|
166
|
+
async getVeiloPublicKey(params) {
|
|
167
|
+
const query = "username" in params && params.username
|
|
168
|
+
? `username=${encodeURIComponent(params.username)}`
|
|
169
|
+
: `publicKey=${encodeURIComponent(params.publicKey)}`;
|
|
170
|
+
return this.request("GET", `/auth/veiloPublicKey?${query}`);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Provision (or retrieve) the Veilo spend key paired with a Solana address.
|
|
174
|
+
* Public and idempotent; the response contains public key material only.
|
|
175
|
+
*/
|
|
176
|
+
async preRegisterKey(solanaAddress) {
|
|
177
|
+
return this.request("POST", "/auth/pre-register-key", {
|
|
178
|
+
body: { solanaAddress },
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
// Notes
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
184
|
+
/** Save an encrypted note to the relayer's database. */
|
|
185
|
+
async saveEncryptedNote(data) {
|
|
186
|
+
return this.request("POST", "/notes/save", { body: data });
|
|
187
|
+
}
|
|
188
|
+
/** Query encrypted notes for the authenticated user. Requires auth token. */
|
|
189
|
+
async queryEncryptedNotes(params = {}) {
|
|
190
|
+
return this.request("POST", "/notes/query", {
|
|
191
|
+
body: params,
|
|
192
|
+
authenticated: true,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/** Delete all stored data for the authenticated user. Requires auth token. */
|
|
196
|
+
async deleteUserData() {
|
|
197
|
+
return this.request("POST", "/notes/delete", { authenticated: true });
|
|
198
|
+
}
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Merkle
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
/** Fetch the current Merkle root for a pool, optionally filtered by mint and tree ID. */
|
|
203
|
+
async getMerkleRoot(mintAddress, treeId) {
|
|
204
|
+
const params = new URLSearchParams();
|
|
205
|
+
if (mintAddress)
|
|
206
|
+
params.append("mintAddress", mintAddress);
|
|
207
|
+
if (treeId !== undefined)
|
|
208
|
+
params.append("treeId", treeId.toString());
|
|
209
|
+
const qs = params.toString();
|
|
210
|
+
return this.request("GET", `/merkle/root${qs ? `?${qs}` : ""}`);
|
|
211
|
+
}
|
|
212
|
+
/** Fetch the full Merkle tree (all leaves) for a pool. */
|
|
213
|
+
async getMerkleTree(mintAddress, treeId) {
|
|
214
|
+
const params = new URLSearchParams();
|
|
215
|
+
if (mintAddress)
|
|
216
|
+
params.append("mintAddress", mintAddress);
|
|
217
|
+
if (treeId !== undefined)
|
|
218
|
+
params.append("treeId", treeId.toString());
|
|
219
|
+
const qs = params.toString();
|
|
220
|
+
return this.request("GET", `/merkle/tree${qs ? `?${qs}` : ""}`);
|
|
221
|
+
}
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
// Transact (encrypted payloads)
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
/** Submit an encrypted withdrawal request to the relayer. Not retried on failure. */
|
|
226
|
+
async submitWithdraw(data) {
|
|
227
|
+
const encryptedPayload = this.encrypt(data);
|
|
228
|
+
return this.request("POST", "/transact/withdra", {
|
|
229
|
+
body: { encryptedPayload },
|
|
230
|
+
retryable: false,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/** Submit an encrypted private transfer request. Not retried on failure. */
|
|
234
|
+
async submitPrivateTransfer(data) {
|
|
235
|
+
const encryptedPayload = this.encrypt(data);
|
|
236
|
+
return this.request("POST", "/transact/private-transfer", {
|
|
237
|
+
body: { encryptedPayload },
|
|
238
|
+
retryable: false,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/** Submit an encrypted private swap request. Not retried on failure. */
|
|
242
|
+
async submitPrivateSwap(data) {
|
|
243
|
+
const encryptedPayload = this.encrypt(data);
|
|
244
|
+
return this.request("POST", "/transact/private-swap", {
|
|
245
|
+
body: { encryptedPayload },
|
|
246
|
+
retryable: false,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for retry behavior on Solana RPC calls.
|
|
3
|
+
*/
|
|
4
|
+
export interface RpcRetryPolicy {
|
|
5
|
+
/** Maximum number of retry attempts (default: 3) */
|
|
6
|
+
maxRetries: number;
|
|
7
|
+
/** Base delay in ms for exponential backoff (default: 1000) */
|
|
8
|
+
baseDelayMs: number;
|
|
9
|
+
/** Maximum delay cap in ms (default: 10000) */
|
|
10
|
+
maxDelayMs: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const DEFAULT_RPC_RETRY: RpcRetryPolicy;
|
|
13
|
+
/**
|
|
14
|
+
* Execute an async function with retries on transient RPC failures.
|
|
15
|
+
*
|
|
16
|
+
* Retries on: BlockhashNotFound, HTTP 429/502/503/504, timeouts, connection resets.
|
|
17
|
+
* Does NOT retry on: program errors (6xxx), signature verification failures, or other
|
|
18
|
+
* deterministic errors that would fail again.
|
|
19
|
+
*
|
|
20
|
+
* @param fn The async operation to execute
|
|
21
|
+
* @param policy Retry configuration (uses sensible defaults if omitted)
|
|
22
|
+
* @returns The result of the async operation
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const txSig = await withRpcRetry(
|
|
27
|
+
* () => program.methods.transact(...).accounts({...}).signers([...]).rpc(),
|
|
28
|
+
* { maxRetries: 5 },
|
|
29
|
+
* );
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function withRpcRetry<T>(fn: () => Promise<T>, policy?: Partial<RpcRetryPolicy>): Promise<T>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// -----------------------------------------------------------------------------
|
|
2
|
+
// RPC Retry / Resilience
|
|
3
|
+
// -----------------------------------------------------------------------------
|
|
4
|
+
export const DEFAULT_RPC_RETRY = {
|
|
5
|
+
maxRetries: 3,
|
|
6
|
+
baseDelayMs: 1000,
|
|
7
|
+
maxDelayMs: 10000,
|
|
8
|
+
};
|
|
9
|
+
/** Error messages / patterns that indicate a transient RPC failure worth retrying. */
|
|
10
|
+
const RETRYABLE_PATTERNS = [
|
|
11
|
+
"blockhash not found",
|
|
12
|
+
"block height exceeded",
|
|
13
|
+
"blockhash already expired",
|
|
14
|
+
"node is behind",
|
|
15
|
+
"service unavailable",
|
|
16
|
+
"too many requests",
|
|
17
|
+
"429",
|
|
18
|
+
"502",
|
|
19
|
+
"503",
|
|
20
|
+
"504",
|
|
21
|
+
"timeout",
|
|
22
|
+
"timed out",
|
|
23
|
+
"econnreset",
|
|
24
|
+
"econnrefused",
|
|
25
|
+
"socket hang up",
|
|
26
|
+
"fetch failed",
|
|
27
|
+
];
|
|
28
|
+
function isRetryableError(error) {
|
|
29
|
+
const msg = (error instanceof Error ? error.message : String(error)).toLowerCase();
|
|
30
|
+
return RETRYABLE_PATTERNS.some((p) => msg.includes(p));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Execute an async function with retries on transient RPC failures.
|
|
34
|
+
*
|
|
35
|
+
* Retries on: BlockhashNotFound, HTTP 429/502/503/504, timeouts, connection resets.
|
|
36
|
+
* Does NOT retry on: program errors (6xxx), signature verification failures, or other
|
|
37
|
+
* deterministic errors that would fail again.
|
|
38
|
+
*
|
|
39
|
+
* @param fn The async operation to execute
|
|
40
|
+
* @param policy Retry configuration (uses sensible defaults if omitted)
|
|
41
|
+
* @returns The result of the async operation
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const txSig = await withRpcRetry(
|
|
46
|
+
* () => program.methods.transact(...).accounts({...}).signers([...]).rpc(),
|
|
47
|
+
* { maxRetries: 5 },
|
|
48
|
+
* );
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export async function withRpcRetry(fn, policy) {
|
|
52
|
+
const { maxRetries, baseDelayMs, maxDelayMs } = {
|
|
53
|
+
...DEFAULT_RPC_RETRY,
|
|
54
|
+
...policy,
|
|
55
|
+
};
|
|
56
|
+
let lastError;
|
|
57
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
58
|
+
try {
|
|
59
|
+
return await fn();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
lastError = error;
|
|
63
|
+
if (attempt >= maxRetries || !isRetryableError(error)) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
const delay = Math.min(baseDelayMs * Math.pow(2, attempt) + Math.random() * 200, maxDelayMs);
|
|
67
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw lastError;
|
|
71
|
+
}
|