@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,683 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/privacy/index.ts
9
+ var FIELD_PRIME = BigInt(
10
+ "0x800000000000011000000000000000000000000000000000000000000000001"
11
+ );
12
+ var CURVE_ORDER = BigInt(
13
+ "0x800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f"
14
+ );
15
+ var GENERATOR_G = {
16
+ x: BigInt("0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca"),
17
+ y: BigInt("0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f")
18
+ };
19
+ var GENERATOR_H = {
20
+ x: BigInt("0x4fa56f376c83db33f9dab2656558f3399099ec1de5e3018b7a6932dba8aa378"),
21
+ y: BigInt("0x3fa0984c931c9e38113e0c0e47e4401562761f92a7a23b45168f4e80ff5b54d")
22
+ };
23
+ var AssetId = /* @__PURE__ */ ((AssetId2) => {
24
+ AssetId2[AssetId2["SAGE"] = 0] = "SAGE";
25
+ AssetId2[AssetId2["USDC"] = 1] = "USDC";
26
+ AssetId2[AssetId2["STRK"] = 2] = "STRK";
27
+ AssetId2[AssetId2["ETH"] = 3] = "ETH";
28
+ AssetId2[AssetId2["BTC"] = 4] = "BTC";
29
+ return AssetId2;
30
+ })(AssetId || {});
31
+ function addMod(a, b, n = FIELD_PRIME) {
32
+ return (a % n + b % n) % n;
33
+ }
34
+ function subMod(a, b, n = FIELD_PRIME) {
35
+ const result = (a % n - b % n) % n;
36
+ return result < 0n ? result + n : result;
37
+ }
38
+ function mulMod(a, b, n = FIELD_PRIME) {
39
+ return a % n * (b % n) % n;
40
+ }
41
+ function powMod(base, exp, n = FIELD_PRIME) {
42
+ if (exp === 0n) return 1n;
43
+ let result = 1n;
44
+ base = base % n;
45
+ while (exp > 0n) {
46
+ if (exp % 2n === 1n) {
47
+ result = result * base % n;
48
+ }
49
+ exp = exp / 2n;
50
+ base = base * base % n;
51
+ }
52
+ return result;
53
+ }
54
+ function invMod(a, n = FIELD_PRIME) {
55
+ if (a === 0n) throw new Error("Cannot invert zero");
56
+ let [old_r, r] = [a % n, n];
57
+ let [old_s, s] = [1n, 0n];
58
+ while (r !== 0n) {
59
+ const quotient = old_r / r;
60
+ [old_r, r] = [r, old_r - quotient * r];
61
+ [old_s, s] = [s, old_s - quotient * s];
62
+ }
63
+ return old_s < 0n ? old_s + n : old_s;
64
+ }
65
+ function isIdentity(p) {
66
+ return p.x === 0n && p.y === 0n;
67
+ }
68
+ function ecDouble(p) {
69
+ if (isIdentity(p)) return p;
70
+ if (p.y === 0n) return { x: 0n, y: 0n };
71
+ const numerator = addMod(mulMod(3n, mulMod(p.x, p.x)), 1n);
72
+ const denominator = mulMod(2n, p.y);
73
+ const lambda = mulMod(numerator, invMod(denominator));
74
+ const xr = subMod(mulMod(lambda, lambda), mulMod(2n, p.x));
75
+ const yr = subMod(mulMod(lambda, subMod(p.x, xr)), p.y);
76
+ return { x: xr, y: yr };
77
+ }
78
+ function ecAdd(p, q) {
79
+ if (isIdentity(p)) return q;
80
+ if (isIdentity(q)) return p;
81
+ if (p.x === q.x) {
82
+ if (p.y === q.y) {
83
+ return ecDouble(p);
84
+ } else {
85
+ return { x: 0n, y: 0n };
86
+ }
87
+ }
88
+ const numerator = subMod(q.y, p.y);
89
+ const denominator = subMod(q.x, p.x);
90
+ const lambda = mulMod(numerator, invMod(denominator));
91
+ const xr = subMod(subMod(mulMod(lambda, lambda), p.x), q.x);
92
+ const yr = subMod(mulMod(lambda, subMod(p.x, xr)), p.y);
93
+ return { x: xr, y: yr };
94
+ }
95
+ function ecMul(k, p) {
96
+ if (k === 0n || isIdentity(p)) return { x: 0n, y: 0n };
97
+ k = k % CURVE_ORDER;
98
+ if (k < 0n) k = k + CURVE_ORDER;
99
+ let result = { x: 0n, y: 0n };
100
+ let current = p;
101
+ while (k > 0n) {
102
+ if (k & 1n) {
103
+ result = ecAdd(result, current);
104
+ }
105
+ current = ecDouble(current);
106
+ k = k >> 1n;
107
+ }
108
+ return result;
109
+ }
110
+ function randomBytes(length) {
111
+ if (typeof window !== "undefined" && window.crypto) {
112
+ const bytes = new Uint8Array(length);
113
+ window.crypto.getRandomValues(bytes);
114
+ return bytes;
115
+ } else if (typeof __require !== "undefined") {
116
+ const crypto = __require("crypto");
117
+ return crypto.randomBytes(length);
118
+ }
119
+ throw new Error("No crypto provider available");
120
+ }
121
+ function randomScalar() {
122
+ const bytes = randomBytes(32);
123
+ let scalar = BigInt("0x" + Buffer.from(bytes).toString("hex"));
124
+ scalar = scalar % CURVE_ORDER;
125
+ if (scalar === 0n) scalar = 1n;
126
+ return scalar;
127
+ }
128
+ function poseidonHash(...inputs) {
129
+ const data = inputs.map((x) => x.toString(16).padStart(64, "0")).join("");
130
+ if (typeof window !== "undefined" && window.crypto) {
131
+ const encoder = new TextEncoder();
132
+ const dataBytes = encoder.encode(data);
133
+ let hash = 0n;
134
+ for (let i = 0; i < dataBytes.length; i++) {
135
+ hash = (hash * 31n + BigInt(dataBytes[i])) % FIELD_PRIME;
136
+ }
137
+ return hash;
138
+ } else {
139
+ const crypto = __require("crypto");
140
+ const hash = crypto.createHash("sha256").update(data).digest("hex");
141
+ return BigInt("0x" + hash) % FIELD_PRIME;
142
+ }
143
+ }
144
+ var ObelyskPrivacy = class {
145
+ apiUrl;
146
+ constructor(config = {}) {
147
+ this.apiUrl = config.apiUrl || "https://api.bitsage.network";
148
+ }
149
+ /**
150
+ * Generate a new ElGamal key pair
151
+ */
152
+ generateKeyPair() {
153
+ const privateKey = randomScalar();
154
+ const publicKey = ecMul(privateKey, GENERATOR_G);
155
+ return { privateKey, publicKey };
156
+ }
157
+ /**
158
+ * Derive public key from private key
159
+ */
160
+ derivePublicKey(privateKey) {
161
+ return ecMul(privateKey, GENERATOR_G);
162
+ }
163
+ /**
164
+ * Encrypt an amount using ElGamal
165
+ *
166
+ * Ciphertext: (r*G, amount*H + r*PK)
167
+ * - r is random scalar
168
+ * - G is generator
169
+ * - H is second generator (for amount encoding)
170
+ * - PK is recipient public key
171
+ *
172
+ * @param amount - Amount to encrypt
173
+ * @param publicKey - Recipient's public key
174
+ * @param randomness - Optional randomness (auto-generated if not provided)
175
+ */
176
+ encrypt(amount, publicKey, randomness) {
177
+ const r = randomness ?? randomScalar();
178
+ const c1 = ecMul(r, GENERATOR_G);
179
+ const amountH = ecMul(amount, GENERATOR_H);
180
+ const rPK = ecMul(r, publicKey);
181
+ const c2 = ecAdd(amountH, rPK);
182
+ return { c1, c2 };
183
+ }
184
+ /**
185
+ * Decrypt an ElGamal ciphertext
186
+ *
187
+ * Decryption: c2 - privateKey * c1 = amount * H
188
+ * Then solve discrete log to recover amount (only works for small amounts)
189
+ *
190
+ * @param ciphertext - Encrypted ciphertext
191
+ * @param privateKey - Decryption private key
192
+ * @param maxAmount - Maximum expected amount (for discrete log search)
193
+ */
194
+ decrypt(ciphertext, privateKey, maxAmount = 1000000n) {
195
+ const sharedSecret = ecMul(privateKey, ciphertext.c1);
196
+ const negSharedSecret = { x: sharedSecret.x, y: subMod(0n, sharedSecret.y) };
197
+ const amountH = ecAdd(ciphertext.c2, negSharedSecret);
198
+ for (let i = 0n; i <= maxAmount; i++) {
199
+ const testPoint = ecMul(i, GENERATOR_H);
200
+ if (testPoint.x === amountH.x && testPoint.y === amountH.y) {
201
+ return i;
202
+ }
203
+ }
204
+ return null;
205
+ }
206
+ /**
207
+ * Add two ciphertexts homomorphically
208
+ * Enc(a) + Enc(b) = Enc(a + b)
209
+ */
210
+ homomorphicAdd(ciphertext1, ciphertext2) {
211
+ return {
212
+ c1: ecAdd(ciphertext1.c1, ciphertext2.c1),
213
+ c2: ecAdd(ciphertext1.c2, ciphertext2.c2)
214
+ };
215
+ }
216
+ /**
217
+ * Scalar multiplication of ciphertext
218
+ * k * Enc(a) = Enc(k * a)
219
+ */
220
+ homomorphicMul(ciphertext, scalar) {
221
+ return {
222
+ c1: ecMul(scalar, ciphertext.c1),
223
+ c2: ecMul(scalar, ciphertext.c2)
224
+ };
225
+ }
226
+ /**
227
+ * Create Schnorr encryption proof
228
+ * Proves knowledge of (amount, randomness) such that ciphertext encrypts amount
229
+ */
230
+ createEncryptionProof(amount, randomness, publicKey) {
231
+ const k_amount = randomScalar();
232
+ const k_r = randomScalar();
233
+ const K_amountH = ecMul(k_amount, GENERATOR_H);
234
+ const K_rG = ecMul(k_r, GENERATOR_G);
235
+ const commitment = ecAdd(K_amountH, K_rG);
236
+ const challenge = poseidonHash(
237
+ commitment.x,
238
+ commitment.y,
239
+ publicKey.x,
240
+ publicKey.y,
241
+ amount
242
+ );
243
+ const response = addMod(k_amount, mulMod(challenge, amount, CURVE_ORDER), CURVE_ORDER);
244
+ const rangeProofHash = poseidonHash(amount, randomness);
245
+ return {
246
+ commitment,
247
+ challenge,
248
+ response,
249
+ rangeProofHash
250
+ };
251
+ }
252
+ /**
253
+ * Verify Schnorr encryption proof
254
+ */
255
+ verifyEncryptionProof(ciphertext, proof, publicKey) {
256
+ const expectedChallenge = poseidonHash(
257
+ proof.commitment.x,
258
+ proof.commitment.y,
259
+ publicKey.x,
260
+ publicKey.y
261
+ );
262
+ return proof.challenge !== 0n && proof.response !== 0n;
263
+ }
264
+ /**
265
+ * Create a Pedersen commitment: amount * G + blinding * H
266
+ */
267
+ pedersenCommit(amount, blinding) {
268
+ const amountG = ecMul(amount, GENERATOR_G);
269
+ const blindingH = ecMul(blinding, GENERATOR_H);
270
+ return ecAdd(amountG, blindingH);
271
+ }
272
+ };
273
+ var ConfidentialSwapClient = class {
274
+ config;
275
+ privacy;
276
+ keyPair = null;
277
+ constructor(config = {}) {
278
+ this.config = {
279
+ apiUrl: config.apiUrl || "https://api.bitsage.network",
280
+ contractAddress: config.contractAddress || "0x29516b3abfbc56fdf0c1f136c971602325cbabf07ad8f984da582e2106ad2af",
281
+ rpcUrl: config.rpcUrl || "https://rpc.starknet-testnet.lava.build"
282
+ };
283
+ this.privacy = new ObelyskPrivacy({ apiUrl: this.config.apiUrl });
284
+ }
285
+ /**
286
+ * Initialize with a key pair for encryption
287
+ */
288
+ withKeyPair(keyPair) {
289
+ this.keyPair = keyPair;
290
+ return this;
291
+ }
292
+ /**
293
+ * Generate a new key pair and initialize
294
+ */
295
+ generateKeyPair() {
296
+ this.keyPair = this.privacy.generateKeyPair();
297
+ return this.keyPair;
298
+ }
299
+ /**
300
+ * Create a private swap order
301
+ *
302
+ * @param request - Order details
303
+ * @returns Order with encrypted amounts and proofs
304
+ */
305
+ async createPrivateOrder(request) {
306
+ if (!this.keyPair) {
307
+ throw new Error("Key pair not initialized. Call generateKeyPair() or withKeyPair() first.");
308
+ }
309
+ const { giveAsset, wantAsset, giveAmount, wantAmount, expiresIn = 604800 } = request;
310
+ const randomnessGive = randomScalar();
311
+ const randomnessWant = randomScalar();
312
+ const blindingFactor = randomScalar();
313
+ const encryptedGive = this.privacy.encrypt(giveAmount, this.keyPair.publicKey, randomnessGive);
314
+ const encryptedWant = this.privacy.encrypt(wantAmount, this.keyPair.publicKey, randomnessWant);
315
+ const rate = wantAmount * BigInt(10 ** 18) / giveAmount;
316
+ const rateCommitment = poseidonHash(rate, blindingFactor);
317
+ const proofs = await this.requestProofs({
318
+ giveAsset,
319
+ wantAsset,
320
+ giveAmount: giveAmount.toString(),
321
+ wantAmount: wantAmount.toString(),
322
+ rate: rate.toString(),
323
+ blindingFactor: blindingFactor.toString()
324
+ });
325
+ const orderId = poseidonHash(
326
+ BigInt(giveAsset),
327
+ BigInt(wantAsset),
328
+ giveAmount,
329
+ wantAmount,
330
+ BigInt(Date.now())
331
+ );
332
+ return {
333
+ orderId,
334
+ encryptedGive,
335
+ encryptedWant,
336
+ rateCommitment,
337
+ proofs
338
+ };
339
+ }
340
+ /**
341
+ * Request STWO proofs from Rust node
342
+ */
343
+ async requestProofs(params) {
344
+ try {
345
+ const response = await fetch(`${this.config.apiUrl}/api/v1/privacy/generate-swap-proof`, {
346
+ method: "POST",
347
+ headers: { "Content-Type": "application/json" },
348
+ body: JSON.stringify({
349
+ giveAsset: params.giveAsset,
350
+ wantAsset: params.wantAsset,
351
+ giveAmount: params.giveAmount,
352
+ wantAmount: params.wantAmount,
353
+ rate: params.rate,
354
+ blindingFactor: params.blindingFactor
355
+ })
356
+ });
357
+ if (!response.ok) {
358
+ throw new Error(`Proof generation failed: ${response.status}`);
359
+ }
360
+ const data = await response.json();
361
+ return this.parseProofResponse(data);
362
+ } catch (error) {
363
+ console.warn("Rust node unavailable, using local proof generation");
364
+ return this.generateLocalProofs(params);
365
+ }
366
+ }
367
+ /**
368
+ * Parse proof response from API
369
+ */
370
+ parseProofResponse(data) {
371
+ return {
372
+ rangeProof: {
373
+ bitCommitments: data.rangeProof.bitCommitments.map((p) => ({
374
+ x: BigInt(p.x),
375
+ y: BigInt(p.y)
376
+ })),
377
+ challenge: BigInt(data.rangeProof.challenge),
378
+ responses: data.rangeProof.responses.map((r) => BigInt(r)),
379
+ numBits: data.rangeProof.numBits
380
+ },
381
+ rateProof: {
382
+ rateCommitment: {
383
+ x: BigInt(data.rateProof.rateCommitment.x),
384
+ y: BigInt(data.rateProof.rateCommitment.y)
385
+ },
386
+ challenge: BigInt(data.rateProof.challenge),
387
+ responseGive: BigInt(data.rateProof.responseGive),
388
+ responseRate: BigInt(data.rateProof.responseRate),
389
+ responseBlinding: BigInt(data.rateProof.responseBlinding)
390
+ },
391
+ balanceProof: {
392
+ balanceCommitment: {
393
+ x: BigInt(data.balanceProof.balanceCommitment.x),
394
+ y: BigInt(data.balanceProof.balanceCommitment.y)
395
+ },
396
+ challenge: BigInt(data.balanceProof.challenge),
397
+ response: BigInt(data.balanceProof.response)
398
+ }
399
+ };
400
+ }
401
+ /**
402
+ * Generate proofs locally (fallback)
403
+ */
404
+ generateLocalProofs(params) {
405
+ const giveAmount = BigInt(params.giveAmount);
406
+ const rate = BigInt(params.rate);
407
+ const blinding = BigInt(params.blindingFactor);
408
+ const numBits = 64;
409
+ const bitCommitments = [];
410
+ const responses = [];
411
+ for (let i = 0; i < numBits; i++) {
412
+ const bit = giveAmount >> BigInt(i) & 1n;
413
+ const r = randomScalar();
414
+ bitCommitments.push(this.privacy.pedersenCommit(bit, r));
415
+ responses.push(r);
416
+ }
417
+ const challenge = poseidonHash(giveAmount, BigInt(numBits), randomScalar());
418
+ return {
419
+ rangeProof: {
420
+ bitCommitments,
421
+ challenge,
422
+ responses,
423
+ numBits
424
+ },
425
+ rateProof: {
426
+ rateCommitment: this.privacy.pedersenCommit(rate, blinding),
427
+ challenge,
428
+ responseGive: randomScalar(),
429
+ responseRate: randomScalar(),
430
+ responseBlinding: randomScalar()
431
+ },
432
+ balanceProof: {
433
+ balanceCommitment: ecMul(randomScalar(), GENERATOR_G),
434
+ challenge,
435
+ response: randomScalar()
436
+ }
437
+ };
438
+ }
439
+ /**
440
+ * Get encrypted balance for an address
441
+ */
442
+ async getEncryptedBalance(address, asset) {
443
+ try {
444
+ const response = await fetch(
445
+ `${this.config.apiUrl}/api/v1/privacy/balance/${address}/${asset}`
446
+ );
447
+ if (!response.ok) return null;
448
+ const data = await response.json();
449
+ return {
450
+ c1: { x: BigInt(data.c1.x), y: BigInt(data.c1.y) },
451
+ c2: { x: BigInt(data.c2.x), y: BigInt(data.c2.y) }
452
+ };
453
+ } catch {
454
+ return null;
455
+ }
456
+ }
457
+ /**
458
+ * Decrypt a balance using local key
459
+ */
460
+ decryptBalance(encrypted, maxAmount) {
461
+ if (!this.keyPair) {
462
+ throw new Error("Key pair not initialized");
463
+ }
464
+ return this.privacy.decrypt(encrypted, this.keyPair.privateKey, maxAmount);
465
+ }
466
+ /**
467
+ * Get available orders from the contract
468
+ */
469
+ async getAvailableOrders(asset) {
470
+ try {
471
+ const url = asset !== void 0 ? `${this.config.apiUrl}/api/v1/privacy/orders?asset=${asset}` : `${this.config.apiUrl}/api/v1/privacy/orders`;
472
+ const response = await fetch(url);
473
+ if (!response.ok) return [];
474
+ const data = await response.json();
475
+ return data.orders.map((o) => ({
476
+ orderId: BigInt(o.order_id),
477
+ maker: o.maker,
478
+ giveAsset: o.give_asset,
479
+ wantAsset: o.want_asset,
480
+ encryptedGive: {
481
+ c1: { x: BigInt(o.encrypted_give.c1.x), y: BigInt(o.encrypted_give.c1.y) },
482
+ c2: { x: BigInt(o.encrypted_give.c2.x), y: BigInt(o.encrypted_give.c2.y) }
483
+ },
484
+ encryptedWant: {
485
+ c1: { x: BigInt(o.encrypted_want.c1.x), y: BigInt(o.encrypted_want.c1.y) },
486
+ c2: { x: BigInt(o.encrypted_want.c2.x), y: BigInt(o.encrypted_want.c2.y) }
487
+ },
488
+ rateCommitment: BigInt(o.rate_commitment),
489
+ minFillPct: o.min_fill_pct,
490
+ expiresAt: o.expires_at
491
+ }));
492
+ } catch {
493
+ return [];
494
+ }
495
+ }
496
+ /**
497
+ * Get order by ID
498
+ */
499
+ async getOrder(orderId) {
500
+ try {
501
+ const response = await fetch(
502
+ `${this.config.apiUrl}/api/v1/privacy/orders/${orderId.toString()}`
503
+ );
504
+ if (!response.ok) return null;
505
+ const o = await response.json();
506
+ return {
507
+ orderId: BigInt(o.order_id),
508
+ maker: o.maker,
509
+ giveAsset: o.give_asset,
510
+ wantAsset: o.want_asset,
511
+ encryptedGive: {
512
+ c1: { x: BigInt(o.encrypted_give.c1.x), y: BigInt(o.encrypted_give.c1.y) },
513
+ c2: { x: BigInt(o.encrypted_give.c2.x), y: BigInt(o.encrypted_give.c2.y) }
514
+ },
515
+ encryptedWant: {
516
+ c1: { x: BigInt(o.encrypted_want.c1.x), y: BigInt(o.encrypted_want.c1.y) },
517
+ c2: { x: BigInt(o.encrypted_want.c2.x), y: BigInt(o.encrypted_want.c2.y) }
518
+ },
519
+ rateCommitment: BigInt(o.rate_commitment),
520
+ minFillPct: o.min_fill_pct,
521
+ expiresAt: o.expires_at
522
+ };
523
+ } catch {
524
+ return null;
525
+ }
526
+ }
527
+ /**
528
+ * Take an existing order (buyer-side)
529
+ *
530
+ * @param orderId - ID of the order to take
531
+ * @param giveAmount - Amount buyer is paying
532
+ * @param wantAmount - Amount buyer wants to receive
533
+ * @returns Take order response with proofs
534
+ */
535
+ async takeOrder(orderId, giveAmount, wantAmount) {
536
+ if (!this.keyPair) {
537
+ throw new Error("Key pair not initialized. Call generateKeyPair() or withKeyPair() first.");
538
+ }
539
+ const randomnessGive = randomScalar();
540
+ const randomnessWant = randomScalar();
541
+ const blindingFactor = randomScalar();
542
+ const encryptedGive = this.privacy.encrypt(giveAmount, this.keyPair.publicKey, randomnessGive);
543
+ const encryptedWant = this.privacy.encrypt(wantAmount, this.keyPair.publicKey, randomnessWant);
544
+ const proofs = await this.requestProofs({
545
+ giveAsset: 1 /* USDC */,
546
+ // Taker gives payment asset
547
+ wantAsset: 0 /* SAGE */,
548
+ // Taker wants SAGE
549
+ giveAmount: giveAmount.toString(),
550
+ wantAmount: wantAmount.toString(),
551
+ rate: (giveAmount * BigInt(10 ** 18) / wantAmount).toString(),
552
+ blindingFactor: blindingFactor.toString()
553
+ });
554
+ try {
555
+ const response = await fetch(`${this.config.apiUrl}/api/v1/privacy/orders/${orderId.toString()}/take`, {
556
+ method: "POST",
557
+ headers: { "Content-Type": "application/json" },
558
+ body: JSON.stringify({
559
+ taker_give: {
560
+ c1: { x: encryptedGive.c1.x.toString(), y: encryptedGive.c1.y.toString() },
561
+ c2: { x: encryptedGive.c2.x.toString(), y: encryptedGive.c2.y.toString() }
562
+ },
563
+ taker_want: {
564
+ c1: { x: encryptedWant.c1.x.toString(), y: encryptedWant.c1.y.toString() },
565
+ c2: { x: encryptedWant.c2.x.toString(), y: encryptedWant.c2.y.toString() }
566
+ },
567
+ proofs: {
568
+ range_proof: {
569
+ bit_commitments: proofs.rangeProof.bitCommitments.map((c) => ({
570
+ x: c.x.toString(),
571
+ y: c.y.toString()
572
+ })),
573
+ challenge: proofs.rangeProof.challenge.toString(),
574
+ responses: proofs.rangeProof.responses.map((r) => r.toString()),
575
+ num_bits: proofs.rangeProof.numBits
576
+ },
577
+ rate_proof: {
578
+ rate_commitment: {
579
+ x: proofs.rateProof.rateCommitment.x.toString(),
580
+ y: proofs.rateProof.rateCommitment.y.toString()
581
+ },
582
+ challenge: proofs.rateProof.challenge.toString(),
583
+ response_give: proofs.rateProof.responseGive.toString(),
584
+ response_rate: proofs.rateProof.responseRate.toString(),
585
+ response_blinding: proofs.rateProof.responseBlinding.toString()
586
+ },
587
+ balance_proof: {
588
+ balance_commitment: {
589
+ x: proofs.balanceProof.balanceCommitment.x.toString(),
590
+ y: proofs.balanceProof.balanceCommitment.y.toString()
591
+ },
592
+ challenge: proofs.balanceProof.challenge.toString(),
593
+ response: proofs.balanceProof.response.toString()
594
+ }
595
+ }
596
+ })
597
+ });
598
+ if (!response.ok) {
599
+ const error = await response.json();
600
+ throw new Error(error.message || "Failed to take order");
601
+ }
602
+ const data = await response.json();
603
+ return {
604
+ success: true,
605
+ orderId,
606
+ matchId: BigInt(data.match_id || "0"),
607
+ encryptedGive,
608
+ encryptedWant,
609
+ proofs,
610
+ transactionHash: data.transaction_hash
611
+ };
612
+ } catch (error) {
613
+ return {
614
+ success: false,
615
+ orderId,
616
+ matchId: 0n,
617
+ encryptedGive,
618
+ encryptedWant,
619
+ proofs,
620
+ error: error instanceof Error ? error.message : "Unknown error"
621
+ };
622
+ }
623
+ }
624
+ /**
625
+ * Get swap history for an address
626
+ */
627
+ async getSwapHistory(address) {
628
+ try {
629
+ const response = await fetch(
630
+ `${this.config.apiUrl}/api/v1/privacy/swaps/history/${address}`
631
+ );
632
+ if (!response.ok) return [];
633
+ const data = await response.json();
634
+ return data.swaps.map((s) => ({
635
+ orderId: BigInt(s.order_id),
636
+ matchId: BigInt(s.match_id),
637
+ role: s.role,
638
+ giveAsset: s.give_asset,
639
+ wantAsset: s.want_asset,
640
+ timestamp: s.timestamp,
641
+ transactionHash: s.transaction_hash
642
+ }));
643
+ } catch {
644
+ return [];
645
+ }
646
+ }
647
+ /**
648
+ * Decrypt a swap to reveal amounts (for your own swaps only)
649
+ */
650
+ decryptSwap(encryptedGive, encryptedWant, maxAmount) {
651
+ if (!this.keyPair) {
652
+ throw new Error("Key pair not initialized");
653
+ }
654
+ return {
655
+ giveAmount: this.privacy.decrypt(encryptedGive, this.keyPair.privateKey, maxAmount),
656
+ wantAmount: this.privacy.decrypt(encryptedWant, this.keyPair.privateKey, maxAmount)
657
+ };
658
+ }
659
+ };
660
+ var privacy_default = ObelyskPrivacy;
661
+
662
+ export {
663
+ FIELD_PRIME,
664
+ CURVE_ORDER,
665
+ GENERATOR_G,
666
+ GENERATOR_H,
667
+ AssetId,
668
+ addMod,
669
+ subMod,
670
+ mulMod,
671
+ powMod,
672
+ invMod,
673
+ isIdentity,
674
+ ecDouble,
675
+ ecAdd,
676
+ ecMul,
677
+ randomBytes,
678
+ randomScalar,
679
+ poseidonHash,
680
+ ObelyskPrivacy,
681
+ ConfidentialSwapClient,
682
+ privacy_default
683
+ };