@obelyzk/sdk 0.5.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.
@@ -0,0 +1,408 @@
1
+ /**
2
+ * Obelysk Privacy SDK
3
+ *
4
+ * Complete privacy-preserving functionality for BitSage including:
5
+ * - ElGamal encryption over STARK curve
6
+ * - Confidential Swap integration
7
+ * - STWO proof generation (via Rust node)
8
+ * - Private SAGE token purchases
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { ObelyskPrivacy, ConfidentialSwapClient } from '@bitsage/sdk/privacy';
13
+ *
14
+ * // Initialize privacy client
15
+ * const privacy = new ObelyskPrivacy();
16
+ *
17
+ * // Generate encryption key pair
18
+ * const keyPair = privacy.generateKeyPair();
19
+ *
20
+ * // Encrypt an amount
21
+ * const encrypted = privacy.encrypt(1000000n, keyPair.publicKey);
22
+ *
23
+ * // Create a confidential swap order
24
+ * const swapClient = new ConfidentialSwapClient({ apiUrl: 'https://api.bitsage.network' });
25
+ * const order = await swapClient.createPrivateOrder({
26
+ * giveAsset: AssetId.SAGE,
27
+ * wantAsset: AssetId.STRK,
28
+ * giveAmount: 100000n * 10n ** 18n,
29
+ * wantAmount: 20000n * 10n ** 18n,
30
+ * });
31
+ * ```
32
+ */
33
+ /**
34
+ * STARK field prime: P = 2^251 + 17 * 2^192 + 1
35
+ */
36
+ declare const FIELD_PRIME: bigint;
37
+ /**
38
+ * STARK curve order (number of points on the curve)
39
+ */
40
+ declare const CURVE_ORDER: bigint;
41
+ /**
42
+ * Generator point G (STARK curve standard generator)
43
+ */
44
+ declare const GENERATOR_G: {
45
+ x: bigint;
46
+ y: bigint;
47
+ };
48
+ /**
49
+ * Second generator point H (for Pedersen commitments)
50
+ * Generated as hash_to_curve("OBELYSK_H")
51
+ */
52
+ declare const GENERATOR_H: {
53
+ x: bigint;
54
+ y: bigint;
55
+ };
56
+ /**
57
+ * Elliptic curve point
58
+ */
59
+ interface ECPoint {
60
+ x: bigint;
61
+ y: bigint;
62
+ }
63
+ /**
64
+ * ElGamal key pair
65
+ */
66
+ interface ElGamalKeyPair {
67
+ /** Private key (scalar) */
68
+ privateKey: bigint;
69
+ /** Public key (EC point) */
70
+ publicKey: ECPoint;
71
+ }
72
+ /**
73
+ * ElGamal ciphertext: (c1, c2) = (r*G, M + r*PK)
74
+ */
75
+ interface ElGamalCiphertext {
76
+ /** First component: r*G */
77
+ c1: ECPoint;
78
+ /** Second component: amount*H + r*PK */
79
+ c2: ECPoint;
80
+ }
81
+ /**
82
+ * Schnorr encryption proof
83
+ */
84
+ interface EncryptionProof {
85
+ /** Commitment point */
86
+ commitment: ECPoint;
87
+ /** Fiat-Shamir challenge */
88
+ challenge: bigint;
89
+ /** Response */
90
+ response: bigint;
91
+ /** Range proof hash (for proving amount is in valid range) */
92
+ rangeProofHash: bigint;
93
+ }
94
+ /**
95
+ * Asset identifier
96
+ */
97
+ declare enum AssetId {
98
+ SAGE = 0,
99
+ USDC = 1,
100
+ STRK = 2,
101
+ ETH = 3,
102
+ BTC = 4
103
+ }
104
+ /**
105
+ * Confidential order for swaps
106
+ */
107
+ interface ConfidentialOrder {
108
+ orderId: bigint;
109
+ maker: string;
110
+ giveAsset: AssetId;
111
+ wantAsset: AssetId;
112
+ encryptedGive: ElGamalCiphertext;
113
+ encryptedWant: ElGamalCiphertext;
114
+ rateCommitment: bigint;
115
+ minFillPct: number;
116
+ expiresAt: number;
117
+ }
118
+ /**
119
+ * Swap proof bundle
120
+ */
121
+ interface SwapProofBundle {
122
+ rangeProof: RangeProof;
123
+ rateProof: RateProof;
124
+ balanceProof: BalanceProof;
125
+ }
126
+ /**
127
+ * Range proof (proves amount is in [0, 2^numBits))
128
+ */
129
+ interface RangeProof {
130
+ bitCommitments: ECPoint[];
131
+ challenge: bigint;
132
+ responses: bigint[];
133
+ numBits: number;
134
+ }
135
+ /**
136
+ * Rate compliance proof
137
+ */
138
+ interface RateProof {
139
+ rateCommitment: ECPoint;
140
+ challenge: bigint;
141
+ responseGive: bigint;
142
+ responseRate: bigint;
143
+ responseBlinding: bigint;
144
+ }
145
+ /**
146
+ * Balance sufficiency proof
147
+ */
148
+ interface BalanceProof {
149
+ balanceCommitment: ECPoint;
150
+ challenge: bigint;
151
+ response: bigint;
152
+ }
153
+ /**
154
+ * Modular addition: (a + b) mod n
155
+ */
156
+ declare function addMod(a: bigint, b: bigint, n?: bigint): bigint;
157
+ /**
158
+ * Modular subtraction: (a - b) mod n
159
+ */
160
+ declare function subMod(a: bigint, b: bigint, n?: bigint): bigint;
161
+ /**
162
+ * Modular multiplication: (a * b) mod n
163
+ */
164
+ declare function mulMod(a: bigint, b: bigint, n?: bigint): bigint;
165
+ /**
166
+ * Modular exponentiation: base^exp mod n
167
+ */
168
+ declare function powMod(base: bigint, exp: bigint, n?: bigint): bigint;
169
+ /**
170
+ * Modular inverse: a^(-1) mod n (using extended Euclidean algorithm)
171
+ */
172
+ declare function invMod(a: bigint, n?: bigint): bigint;
173
+ /**
174
+ * Check if a point is the identity (point at infinity)
175
+ */
176
+ declare function isIdentity(p: ECPoint): boolean;
177
+ /**
178
+ * Point doubling: 2P
179
+ */
180
+ declare function ecDouble(p: ECPoint): ECPoint;
181
+ /**
182
+ * Point addition: P + Q
183
+ */
184
+ declare function ecAdd(p: ECPoint, q: ECPoint): ECPoint;
185
+ /**
186
+ * Scalar multiplication: k * P using double-and-add
187
+ */
188
+ declare function ecMul(k: bigint, p: ECPoint): ECPoint;
189
+ /**
190
+ * Generate cryptographically secure random bytes
191
+ */
192
+ declare function randomBytes(length: number): Uint8Array;
193
+ /**
194
+ * Generate a random scalar in range [1, CURVE_ORDER)
195
+ */
196
+ declare function randomScalar(): bigint;
197
+ /**
198
+ * Poseidon hash (simplified - use cairo-rs or starknet.js for production)
199
+ * This is a placeholder that uses a simpler hash for SDK purposes
200
+ */
201
+ declare function poseidonHash(...inputs: bigint[]): bigint;
202
+ /**
203
+ * Obelysk Privacy - ElGamal encryption and proof generation
204
+ */
205
+ declare class ObelyskPrivacy {
206
+ private apiUrl;
207
+ private _keyPair?;
208
+ constructor(config?: {
209
+ apiUrl?: string;
210
+ });
211
+ /**
212
+ * Set a key pair from a known private key
213
+ */
214
+ setKeyPair(privateKey: bigint): ElGamalKeyPair;
215
+ /**
216
+ * Get the current key pair (generates one if not set)
217
+ */
218
+ getKeyPair(): ElGamalKeyPair;
219
+ /**
220
+ * Generate a new ElGamal key pair
221
+ */
222
+ generateKeyPair(): ElGamalKeyPair;
223
+ /**
224
+ * Derive public key from private key
225
+ */
226
+ derivePublicKey(privateKey: bigint): ECPoint;
227
+ /**
228
+ * Encrypt an amount using ElGamal
229
+ *
230
+ * Ciphertext: (r*G, amount*H + r*PK)
231
+ * - r is random scalar
232
+ * - G is generator
233
+ * - H is second generator (for amount encoding)
234
+ * - PK is recipient public key
235
+ *
236
+ * @param amount - Amount to encrypt
237
+ * @param publicKey - Recipient's public key
238
+ * @param randomness - Optional randomness (auto-generated if not provided)
239
+ */
240
+ encrypt(amount: bigint, publicKey: ECPoint, randomness?: bigint): ElGamalCiphertext;
241
+ /**
242
+ * Decrypt an ElGamal ciphertext
243
+ *
244
+ * Decryption: c2 - privateKey * c1 = amount * H
245
+ * Then solve discrete log to recover amount (only works for small amounts)
246
+ *
247
+ * @param ciphertext - Encrypted ciphertext
248
+ * @param privateKey - Decryption private key
249
+ * @param maxAmount - Maximum expected amount (for discrete log search)
250
+ */
251
+ decrypt(ciphertext: ElGamalCiphertext, privateKey: bigint, maxAmount?: bigint): bigint | null;
252
+ /**
253
+ * Add two ciphertexts homomorphically
254
+ * Enc(a) + Enc(b) = Enc(a + b)
255
+ */
256
+ homomorphicAdd(ciphertext1: ElGamalCiphertext, ciphertext2: ElGamalCiphertext): ElGamalCiphertext;
257
+ /**
258
+ * Scalar multiplication of ciphertext
259
+ * k * Enc(a) = Enc(k * a)
260
+ */
261
+ homomorphicMul(ciphertext: ElGamalCiphertext, scalar: bigint): ElGamalCiphertext;
262
+ /**
263
+ * Create Schnorr encryption proof
264
+ * Proves knowledge of (amount, randomness) such that ciphertext encrypts amount
265
+ */
266
+ createEncryptionProof(amount: bigint, randomness: bigint, publicKey: ECPoint): EncryptionProof;
267
+ /**
268
+ * Verify Schnorr encryption proof
269
+ */
270
+ verifyEncryptionProof(ciphertext: ElGamalCiphertext, proof: EncryptionProof, publicKey: ECPoint): boolean;
271
+ /**
272
+ * Create a Pedersen commitment: amount * G + blinding * H
273
+ */
274
+ pedersenCommit(amount: bigint, blinding: bigint): ECPoint;
275
+ }
276
+ /**
277
+ * Configuration for Confidential Swap client
278
+ */
279
+ interface ConfidentialSwapConfig {
280
+ /** API URL for proof generation */
281
+ apiUrl: string;
282
+ /** Confidential Swap contract address */
283
+ contractAddress?: string;
284
+ /** Starknet RPC URL */
285
+ rpcUrl?: string;
286
+ }
287
+ /**
288
+ * Create order request
289
+ */
290
+ interface CreateOrderRequest {
291
+ giveAsset: AssetId;
292
+ wantAsset: AssetId;
293
+ giveAmount: bigint;
294
+ wantAmount: bigint;
295
+ expiresIn?: number;
296
+ }
297
+ /**
298
+ * Order creation response
299
+ */
300
+ interface CreateOrderResponse {
301
+ orderId: bigint;
302
+ encryptedGive: ElGamalCiphertext;
303
+ encryptedWant: ElGamalCiphertext;
304
+ rateCommitment: bigint;
305
+ proofs: SwapProofBundle;
306
+ transactionHash?: string;
307
+ }
308
+ /**
309
+ * Confidential Swap Client
310
+ *
311
+ * Enables privacy-preserving token swaps using ElGamal encryption and STWO proofs
312
+ */
313
+ declare class ConfidentialSwapClient {
314
+ private config;
315
+ private privacy;
316
+ private keyPair;
317
+ constructor(config?: Partial<ConfidentialSwapConfig>);
318
+ /**
319
+ * Initialize with a key pair for encryption
320
+ */
321
+ withKeyPair(keyPair: ElGamalKeyPair): this;
322
+ /**
323
+ * Generate a new key pair and initialize
324
+ */
325
+ generateKeyPair(): ElGamalKeyPair;
326
+ /**
327
+ * Create a private swap order
328
+ *
329
+ * @param request - Order details
330
+ * @returns Order with encrypted amounts and proofs
331
+ */
332
+ createPrivateOrder(request: CreateOrderRequest): Promise<CreateOrderResponse>;
333
+ /**
334
+ * Request STWO proofs from Rust node
335
+ */
336
+ private requestProofs;
337
+ /**
338
+ * Parse proof response from API
339
+ */
340
+ private parseProofResponse;
341
+ /**
342
+ * Generate proofs locally (fallback)
343
+ */
344
+ private generateLocalProofs;
345
+ /**
346
+ * Get encrypted balance for an address
347
+ */
348
+ getEncryptedBalance(address: string, asset: AssetId): Promise<ElGamalCiphertext | null>;
349
+ /**
350
+ * Decrypt a balance using local key
351
+ */
352
+ decryptBalance(encrypted: ElGamalCiphertext, maxAmount?: bigint): bigint | null;
353
+ /**
354
+ * Get available orders from the contract
355
+ */
356
+ getAvailableOrders(asset?: AssetId): Promise<ConfidentialOrder[]>;
357
+ /**
358
+ * Get order by ID
359
+ */
360
+ getOrder(orderId: bigint): Promise<ConfidentialOrder | null>;
361
+ /**
362
+ * Take an existing order (buyer-side)
363
+ *
364
+ * @param orderId - ID of the order to take
365
+ * @param giveAmount - Amount buyer is paying
366
+ * @param wantAmount - Amount buyer wants to receive
367
+ * @returns Take order response with proofs
368
+ */
369
+ takeOrder(orderId: bigint, giveAmount: bigint, wantAmount: bigint): Promise<TakeOrderResponse>;
370
+ /**
371
+ * Get swap history for an address
372
+ */
373
+ getSwapHistory(address: string): Promise<SwapHistoryEntry[]>;
374
+ /**
375
+ * Decrypt a swap to reveal amounts (for your own swaps only)
376
+ */
377
+ decryptSwap(encryptedGive: ElGamalCiphertext, encryptedWant: ElGamalCiphertext, maxAmount?: bigint): {
378
+ giveAmount: bigint | null;
379
+ wantAmount: bigint | null;
380
+ };
381
+ }
382
+ /**
383
+ * Take order response
384
+ */
385
+ interface TakeOrderResponse {
386
+ success: boolean;
387
+ orderId: bigint;
388
+ matchId: bigint;
389
+ encryptedGive: ElGamalCiphertext;
390
+ encryptedWant: ElGamalCiphertext;
391
+ proofs: SwapProofBundle;
392
+ transactionHash?: string;
393
+ error?: string;
394
+ }
395
+ /**
396
+ * Swap history entry
397
+ */
398
+ interface SwapHistoryEntry {
399
+ orderId: bigint;
400
+ matchId: bigint;
401
+ role: 'maker' | 'taker';
402
+ giveAsset: AssetId;
403
+ wantAsset: AssetId;
404
+ timestamp: number;
405
+ transactionHash: string;
406
+ }
407
+
408
+ export { AssetId, type BalanceProof, CURVE_ORDER, type ConfidentialOrder, ConfidentialSwapClient, type ConfidentialSwapConfig, type CreateOrderRequest, type CreateOrderResponse, type ECPoint, type ElGamalCiphertext, type ElGamalKeyPair, type EncryptionProof, FIELD_PRIME, GENERATOR_G, GENERATOR_H, ObelyskPrivacy, type RangeProof, type RateProof, type SwapHistoryEntry, type SwapProofBundle, type TakeOrderResponse, addMod, ObelyskPrivacy as default, ecAdd, ecDouble, ecMul, invMod, isIdentity, mulMod, poseidonHash, powMod, randomBytes, randomScalar, subMod };