@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,567 @@
1
+ import { RpcProvider, Account } from 'starknet';
2
+ import ObelyskPrivacy, { ecAdd as ecAdd$1, ecMul as ecMul$1, randomScalar as randomScalar$1, invMod, ECPoint } from '../privacy/index.js';
3
+ export { CURVE_ORDER, ElGamalCiphertext, ElGamalKeyPair, FIELD_PRIME, GENERATOR_G, GENERATOR_H } from '../privacy/index.js';
4
+
5
+ interface DepositParams {
6
+ /** Token symbol (eth, strk, usdc, wbtc, sage) */
7
+ token: string;
8
+ /** Human-readable amount (e.g. "1.5") */
9
+ amount: string;
10
+ }
11
+ interface DepositResult {
12
+ /** Transaction hash */
13
+ txHash: string;
14
+ /** Note data (SAVE THIS — needed for withdrawal) */
15
+ note: PrivacyNote;
16
+ /** Commitment hash (on-chain identifier) */
17
+ commitmentHash: string;
18
+ }
19
+ interface WithdrawParams {
20
+ /** The note from a previous deposit */
21
+ note: PrivacyNote;
22
+ /** Recipient address (can be different from depositor for anonymity) */
23
+ recipient: string;
24
+ /** Merkle proof (array of sibling hashes from leaf to root) */
25
+ merkleProof: string[];
26
+ /** Leaf index in the Merkle tree */
27
+ leafIndex: number;
28
+ }
29
+ interface WithdrawResult {
30
+ txHash: string;
31
+ amount: string;
32
+ token: string;
33
+ }
34
+ /** Serializable privacy note — MUST be saved by the user */
35
+ interface PrivacyNote {
36
+ /** Token symbol */
37
+ token: string;
38
+ /** Amount in wei (bigint as string) */
39
+ amount: string;
40
+ /** Pedersen blinding factor (bigint as string) */
41
+ blinding: string;
42
+ /** Nullifier secret (bigint as string) */
43
+ nullifierSecret: string;
44
+ /** Commitment point x (bigint as string) */
45
+ commitmentX: string;
46
+ /** Commitment point y (bigint as string) */
47
+ commitmentY: string;
48
+ /** Poseidon hash of commitment (felt252 hex) */
49
+ commitmentHash: string;
50
+ /** Leaf index in Merkle tree (set after deposit confirms) */
51
+ leafIndex?: number;
52
+ /** Deposit transaction hash */
53
+ depositTxHash?: string;
54
+ /** Timestamp */
55
+ createdAt: number;
56
+ }
57
+ declare class PrivacyPoolClient {
58
+ private readonly obelysk;
59
+ constructor(obelysk: ObelyskClient);
60
+ /**
61
+ * Deposit tokens into a privacy pool.
62
+ *
63
+ * Creates a Pedersen commitment and range proof, then calls the
64
+ * privacy pool contract's deposit function.
65
+ *
66
+ * @returns DepositResult with the note (SAVE THIS for withdrawal)
67
+ */
68
+ deposit(params: DepositParams): Promise<DepositResult>;
69
+ /**
70
+ * Withdraw tokens from a privacy pool.
71
+ *
72
+ * Requires the original note, a Merkle proof of inclusion,
73
+ * and the leaf index. The recipient can be any address.
74
+ */
75
+ withdraw(params: WithdrawParams): Promise<WithdrawResult>;
76
+ /**
77
+ * Read the current global deposit Merkle root of a privacy pool.
78
+ */
79
+ getMerkleRoot(token: string): Promise<string>;
80
+ /**
81
+ * Get pool stats: (total_deposits, total_withdrawals, total_deposited_amount, total_withdrawn_amount)
82
+ */
83
+ getPoolStats(token: string): Promise<{
84
+ totalDeposits: number;
85
+ totalWithdrawals: number;
86
+ totalDeposited: bigint;
87
+ totalWithdrawn: bigint;
88
+ }>;
89
+ /**
90
+ * Get the current leaf count (number of deposits) in a privacy pool.
91
+ */
92
+ getLeafCount(token: string): Promise<number>;
93
+ /**
94
+ * Check if a nullifier has been used (prevents double-withdrawal).
95
+ */
96
+ isNullifierUsed(token: string, nullifier: string): Promise<boolean>;
97
+ /**
98
+ * Check if a deposit commitment is valid.
99
+ */
100
+ isDepositValid(token: string, commitment: string): Promise<boolean>;
101
+ /**
102
+ * Get fee info for a privacy pool (bps, recipient, accumulated).
103
+ */
104
+ getFeeInfo(token: string): Promise<{
105
+ feeBps: number;
106
+ feeRecipient: string;
107
+ accumulated: bigint;
108
+ }>;
109
+ /**
110
+ * Fetch a Merkle proof for a given leaf index from on-chain data.
111
+ * Reconstructs the tree from deposit events.
112
+ */
113
+ fetchMerkleProof(token: string, leafIndex: number): Promise<{
114
+ proof: string[];
115
+ root: string;
116
+ }>;
117
+ }
118
+
119
+ interface DarkPoolDepositParams {
120
+ token: string;
121
+ amount: string;
122
+ }
123
+ interface CommitOrderParams {
124
+ /** Trading pair e.g. "SAGE/STRK" */
125
+ pair: string;
126
+ /** Order side */
127
+ side: 'buy' | 'sell';
128
+ /** Price in 18-decimal fixed point (human-readable string) */
129
+ price: string;
130
+ /** Amount in token decimals (human-readable string) */
131
+ amount: string;
132
+ }
133
+ interface RevealOrderParams {
134
+ /** Order data from commitOrder result */
135
+ orderData: DarkPoolOrderData;
136
+ }
137
+ interface DarkPoolOrderData {
138
+ orderId: string;
139
+ pair: string;
140
+ side: 'buy' | 'sell';
141
+ price: string;
142
+ amount: string;
143
+ salt: string;
144
+ amountBlinding: string;
145
+ commitHash: string;
146
+ epoch: number;
147
+ }
148
+ interface EpochInfo {
149
+ epoch: number;
150
+ phase: 'Commit' | 'Reveal' | 'Settle' | 'Closed';
151
+ blocksRemaining: number;
152
+ }
153
+ declare class DarkPoolClient {
154
+ private readonly obelysk;
155
+ constructor(obelysk: ObelyskClient);
156
+ private get darkPoolAddress();
157
+ /**
158
+ * Deposit tokens into the DarkPool for trading.
159
+ *
160
+ * Encrypts the amount using ElGamal and generates an AE hint
161
+ * for O(1) balance decryption.
162
+ */
163
+ deposit(params: DarkPoolDepositParams): Promise<{
164
+ txHash: string;
165
+ }>;
166
+ /**
167
+ * Commit an order in the current commit phase.
168
+ *
169
+ * Returns order data that must be saved for the reveal phase.
170
+ */
171
+ commitOrder(params: CommitOrderParams): Promise<DarkPoolOrderData>;
172
+ /**
173
+ * Reveal a previously committed order in the reveal phase.
174
+ */
175
+ revealOrder(params: RevealOrderParams): Promise<{
176
+ txHash: string;
177
+ }>;
178
+ /**
179
+ * Get current epoch info from the contract.
180
+ */
181
+ getEpochInfo(): Promise<EpochInfo>;
182
+ /**
183
+ * Get the total number of orders ever placed.
184
+ */
185
+ getOrderCount(): Promise<number>;
186
+ /**
187
+ * Get order IDs for a given epoch.
188
+ */
189
+ getEpochOrders(epochId: number): Promise<string[]>;
190
+ /**
191
+ * Get supported trading pairs.
192
+ */
193
+ getSupportedPairs(): Promise<Array<{
194
+ giveAsset: string;
195
+ wantAsset: string;
196
+ }>>;
197
+ /**
198
+ * Get fee info (bps, recipient). Returns null if fee module not yet active.
199
+ */
200
+ getFeeInfo(): Promise<{
201
+ feeBps: number;
202
+ feeRecipient: string;
203
+ } | null>;
204
+ /**
205
+ * Withdraw tokens from the DarkPool.
206
+ */
207
+ withdraw(params: {
208
+ token: string;
209
+ amount: string;
210
+ }): Promise<{
211
+ txHash: string;
212
+ }>;
213
+ }
214
+
215
+ interface StealthSendParams {
216
+ /** Recipient's Starknet address (must be registered in StealthRegistry) */
217
+ to: string;
218
+ /** Token symbol */
219
+ token: string;
220
+ /** Human-readable amount */
221
+ amount: string;
222
+ }
223
+ interface StealthMetaAddress {
224
+ spendPublicKey: {
225
+ x: bigint;
226
+ y: bigint;
227
+ };
228
+ viewPublicKey: {
229
+ x: bigint;
230
+ y: bigint;
231
+ };
232
+ }
233
+ declare class StealthClient {
234
+ private readonly obelysk;
235
+ constructor(obelysk: ObelyskClient);
236
+ private get registryAddress();
237
+ /**
238
+ * Look up a recipient's stealth meta-address from the registry.
239
+ */
240
+ lookupRecipient(address: string): Promise<StealthMetaAddress | null>;
241
+ /**
242
+ * Send tokens to a stealth address.
243
+ *
244
+ * Generates ephemeral key, derives stealth address via ECDH,
245
+ * sends tokens + publishes ephemeral pubkey on-chain.
246
+ */
247
+ send(params: StealthSendParams): Promise<{
248
+ txHash: string;
249
+ stealthAddress: string;
250
+ ephemeralPubKey: {
251
+ x: string;
252
+ y: string;
253
+ };
254
+ }>;
255
+ /**
256
+ * Check if an address has a registered stealth meta-address.
257
+ */
258
+ hasMetaAddress(address: string): Promise<boolean>;
259
+ /**
260
+ * Get the total number of stealth payment announcements.
261
+ */
262
+ getAnnouncementCount(): Promise<number>;
263
+ /**
264
+ * Get the total number of registered workers/recipients.
265
+ */
266
+ getRegisteredCount(): Promise<number>;
267
+ /**
268
+ * Check if a stealth payment announcement has been claimed.
269
+ */
270
+ isClaimed(announcementIndex: number): Promise<boolean>;
271
+ /**
272
+ * Register your stealth meta-address in the registry.
273
+ */
274
+ register(spendPubKey: {
275
+ x: bigint;
276
+ y: bigint;
277
+ }, viewPubKey: {
278
+ x: bigint;
279
+ y: bigint;
280
+ }): Promise<{
281
+ txHash: string;
282
+ }>;
283
+ }
284
+
285
+ /** Modular reduction (always positive) */
286
+ declare function mod(a: bigint, m: bigint): bigint;
287
+ /** Modular inverse via extended Euclidean algorithm */
288
+ declare const modInverse: typeof invMod;
289
+ /** EC point addition on STARK curve */
290
+ declare const ecAdd: typeof ecAdd$1;
291
+ /** EC scalar multiplication on STARK curve */
292
+ declare const ecMul: typeof ecMul$1;
293
+ /** Cryptographically secure random scalar in [1, CURVE_ORDER) */
294
+ declare const randomScalar: typeof randomScalar$1;
295
+ /** Pedersen commitment: C = value * G + blinding * H */
296
+ declare function pedersenCommit(value: bigint, blinding: bigint): ECPoint;
297
+ /** ElGamal encrypt: (r*G, amount*H + r*PK) */
298
+ declare function elgamalEncrypt(amount: bigint, publicKey: ECPoint): {
299
+ c1: ECPoint;
300
+ c2: ECPoint;
301
+ randomness: bigint;
302
+ };
303
+ /** Schnorr proof of knowledge of r such that c1 = r*G */
304
+ declare function createEncryptionProof(r: bigint, c1: ECPoint): {
305
+ commitment: ECPoint;
306
+ challenge: bigint;
307
+ response: bigint;
308
+ };
309
+ /** Generate AE hint for O(1) decryption */
310
+ declare function createAEHint(amount: bigint, sharedSecret: bigint): {
311
+ encryptedAmount: bigint;
312
+ mac: bigint;
313
+ };
314
+ /** Hash for commitment (Poseidon of point coordinates) */
315
+ declare function commitmentToHash(point: ECPoint): string;
316
+ /** Derive nullifier from secret */
317
+ declare function deriveNullifier(nullifierSecret: bigint, commitmentHash: string): string;
318
+
319
+ interface TransferParams {
320
+ /** Recipient's Starknet address */
321
+ to: string;
322
+ /** Recipient's ElGamal public key */
323
+ recipientPublicKey: ECPoint;
324
+ /** Amount (human-readable) */
325
+ amount: string;
326
+ /** Token symbol */
327
+ token: string;
328
+ }
329
+ declare class ConfidentialTransferClient {
330
+ private readonly obelysk;
331
+ constructor(obelysk: ObelyskClient);
332
+ private get contractAddress();
333
+ /**
334
+ * Send a confidential transfer with ElGamal encrypted amount.
335
+ *
336
+ * Generates encryption proof (Schnorr) to prove the ciphertext is well-formed.
337
+ */
338
+ transfer(params: TransferParams): Promise<{
339
+ txHash: string;
340
+ }>;
341
+ /**
342
+ * Read your encrypted balance from the contract.
343
+ */
344
+ getEncryptedBalance(address: string, token: string): Promise<{
345
+ c1: ECPoint;
346
+ c2: ECPoint;
347
+ } | null>;
348
+ }
349
+
350
+ type GpuTier = 'Consumer' | 'Professional' | 'DataCenter' | 'H100';
351
+ interface WorkerStake {
352
+ amount: bigint;
353
+ gpuTier: GpuTier;
354
+ stakedAt: number;
355
+ isActive: boolean;
356
+ successCount: number;
357
+ failCount: number;
358
+ }
359
+ interface StakingConfig {
360
+ minStakeConsumer: bigint;
361
+ minStakeProfessional: bigint;
362
+ minStakeDataCenter: bigint;
363
+ minStakeH100: bigint;
364
+ cooldownPeriod: number;
365
+ slashPercent: number;
366
+ rewardRateBps: number;
367
+ }
368
+ declare class ProverStakingClient {
369
+ private readonly obelysk;
370
+ constructor(obelysk: ObelyskClient);
371
+ private get contractAddress();
372
+ /**
373
+ * Get a worker's current stake info.
374
+ */
375
+ getStake(workerAddress: string): Promise<WorkerStake>;
376
+ /**
377
+ * Check if a worker is eligible (staked enough, not in cooldown).
378
+ */
379
+ isEligible(workerAddress: string): Promise<boolean>;
380
+ /**
381
+ * Get minimum stake required for a GPU tier.
382
+ */
383
+ getMinStake(tier: GpuTier): Promise<bigint>;
384
+ /**
385
+ * Get total SAGE staked across all workers.
386
+ */
387
+ getTotalStaked(): Promise<bigint>;
388
+ /**
389
+ * Get total SAGE slashed.
390
+ */
391
+ getTotalSlashed(): Promise<bigint>;
392
+ /**
393
+ * Get staking configuration.
394
+ */
395
+ getConfig(): Promise<StakingConfig>;
396
+ /**
397
+ * Stake SAGE tokens to become a prover.
398
+ */
399
+ stake(amount: string, tier: GpuTier): Promise<{
400
+ txHash: string;
401
+ }>;
402
+ /**
403
+ * Request unstaking (starts cooldown period).
404
+ */
405
+ requestUnstake(amount: string): Promise<{
406
+ txHash: string;
407
+ }>;
408
+ /**
409
+ * Complete unstaking after cooldown period.
410
+ */
411
+ completeUnstake(): Promise<{
412
+ txHash: string;
413
+ }>;
414
+ /**
415
+ * Claim accumulated staking rewards.
416
+ */
417
+ claimRewards(): Promise<{
418
+ txHash: string;
419
+ }>;
420
+ }
421
+
422
+ /**
423
+ * Privacy Router Client
424
+ *
425
+ * Read-only views into the Obelysk Privacy Router — the core private
426
+ * balance, transfer, and audit infrastructure.
427
+ */
428
+
429
+ interface PrivateAccount {
430
+ publicKey: ECPoint;
431
+ encryptedBalance: {
432
+ c1: ECPoint;
433
+ c2: ECPoint;
434
+ };
435
+ registered: boolean;
436
+ }
437
+ declare class PrivacyRouterClient {
438
+ private readonly obelysk;
439
+ constructor(obelysk: ObelyskClient);
440
+ private get contractAddress();
441
+ /**
442
+ * Get a registered private account's info.
443
+ */
444
+ getAccount(address: string): Promise<PrivateAccount | null>;
445
+ /** Check if a nullifier has been spent. */
446
+ isNullifierUsed(nullifier: string): Promise<boolean>;
447
+ /** Get the current nullifier tree root. */
448
+ getNullifierTreeRoot(): Promise<string>;
449
+ /** Get the current epoch of the privacy router. */
450
+ getCurrentEpoch(): Promise<number>;
451
+ /** Get total nullifier count. */
452
+ getNullifierCount(): Promise<number>;
453
+ /** Check if an asset ID is supported. */
454
+ isAssetSupported(assetId: number): Promise<boolean>;
455
+ /** Get the token address for an asset ID. */
456
+ getAssetToken(assetId: number): Promise<string>;
457
+ /** Get the number of auditors. */
458
+ getAuditorCount(): Promise<number>;
459
+ /** Check if an address is a registered auditor. */
460
+ isAuditor(address: string): Promise<boolean>;
461
+ /** Get the audit approval threshold. */
462
+ getAuditThreshold(): Promise<number>;
463
+ /** Get the large transfer threshold. */
464
+ getLargeTransferThreshold(): Promise<bigint>;
465
+ /** Check if the Lean IMT is active. */
466
+ isLeanIMTActive(): Promise<boolean>;
467
+ /** Get pending audit/disclosure request count. */
468
+ getPendingRequestCount(): Promise<number>;
469
+ }
470
+
471
+ /**
472
+ * Shielded Swap Router Client
473
+ *
474
+ * Read swap router state (pools, fees, swap count).
475
+ * Actual swaps require withdrawal proofs and are executed via the full
476
+ * privacy pool → swap → re-deposit flow.
477
+ */
478
+
479
+ declare class ShieldedSwapClient {
480
+ private readonly obelysk;
481
+ constructor(obelysk: ObelyskClient);
482
+ private get contractAddress();
483
+ /** Get total number of shielded swaps executed. */
484
+ getSwapCount(): Promise<number>;
485
+ /** Get the privacy pool address registered for a token. */
486
+ getPool(tokenAddress: string): Promise<string | null>;
487
+ /** Get the Ekubo core address (AMM integration). */
488
+ getEkuboCore(): Promise<string>;
489
+ /** Get swap fee info (bps, recipient). Returns null if not active. */
490
+ getSwapFeeInfo(): Promise<{
491
+ feeBps: number;
492
+ feeRecipient: string;
493
+ } | null>;
494
+ /** Get contract owner. */
495
+ getOwner(): Promise<string>;
496
+ }
497
+
498
+ /**
499
+ * Obelysk Protocol Contract Addresses & Token Registry
500
+ *
501
+ * All addresses inlined for npm consumers — no external JSON dependency.
502
+ */
503
+ type ObelyskNetwork = 'sepolia' | 'mainnet';
504
+ declare function getRpcUrl(network: ObelyskNetwork): string;
505
+ declare function getContracts(network: ObelyskNetwork): Record<string, any>;
506
+ declare const MAINNET_TOKENS: Record<string, string>;
507
+ declare const MAINNET_PRIVACY_POOLS: Record<string, string>;
508
+ /** Token decimals */
509
+ declare const TOKEN_DECIMALS: Record<string, number>;
510
+ /** DarkPool asset IDs (felt252) */
511
+ declare const DARKPOOL_ASSET_IDS: Record<string, string>;
512
+ /** VM31 asset IDs */
513
+ declare const VM31_ASSET_IDS: Record<string, number>;
514
+ /** Parse token amount to bigint */
515
+ declare function parseAmount(amount: string | number, token: string): bigint;
516
+ /** Format bigint to human-readable string */
517
+ declare function formatAmount(amount: bigint, token: string): string;
518
+
519
+ /**
520
+ * Main Obelysk Protocol Client
521
+ *
522
+ * Orchestrates all protocol operations: privacy pools, dark pool,
523
+ * stealth payments, confidential transfers, staking, router, and swaps.
524
+ */
525
+
526
+ interface ObelyskConfig {
527
+ /** Network to connect to */
528
+ network: ObelyskNetwork;
529
+ /** starknet.js Account (for signing transactions) */
530
+ account?: Account;
531
+ /** Optional custom RPC URL (strongly recommended — default public RPCs have limits) */
532
+ rpcUrl?: string;
533
+ /** Optional privacy key pair (hex private key). If omitted, generated fresh. */
534
+ privacyPrivateKey?: string;
535
+ }
536
+ declare class ObelyskClient {
537
+ readonly network: ObelyskNetwork;
538
+ readonly provider: RpcProvider;
539
+ readonly account?: Account;
540
+ readonly privacy: ObelyskPrivacy;
541
+ readonly contracts: Record<string, string>;
542
+ readonly tokens: Record<string, string>;
543
+ readonly privacyPools: Record<string, string>;
544
+ /** Privacy Pool operations (deposit/withdraw shielded tokens) */
545
+ readonly privacyPool: PrivacyPoolClient;
546
+ /** DarkPool batch auction trading */
547
+ readonly darkPool: DarkPoolClient;
548
+ /** Stealth address payments */
549
+ readonly stealth: StealthClient;
550
+ /** Confidential peer-to-peer transfers */
551
+ readonly confidentialTransfer: ConfidentialTransferClient;
552
+ /** SAGE prover staking (stake/unstake/rewards) */
553
+ readonly staking: ProverStakingClient;
554
+ /** Privacy Router (private balances, nullifiers, auditing) */
555
+ readonly router: PrivacyRouterClient;
556
+ /** Shielded swap router (privacy-preserving AMM swaps) */
557
+ readonly swap: ShieldedSwapClient;
558
+ constructor(config: ObelyskConfig);
559
+ /** Get token address by symbol */
560
+ getTokenAddress(symbol: string): string;
561
+ /** Get privacy pool address for a token */
562
+ getPrivacyPoolAddress(symbol: string): string;
563
+ /** Require an account is set */
564
+ requireAccount(): Account;
565
+ }
566
+
567
+ export { type CommitOrderParams, ConfidentialTransferClient, DARKPOOL_ASSET_IDS, DarkPoolClient, type DarkPoolDepositParams, type DarkPoolOrderData, type DepositParams, type DepositResult, ECPoint, type EpochInfo, type GpuTier, MAINNET_PRIVACY_POOLS, MAINNET_TOKENS, ObelyskClient, type ObelyskConfig, type ObelyskNetwork, ObelyskPrivacy, type PrivacyNote, PrivacyPoolClient, PrivacyRouterClient, type PrivateAccount, ProverStakingClient, type RevealOrderParams, ShieldedSwapClient, type StakingConfig, StealthClient, type StealthMetaAddress, type StealthSendParams, TOKEN_DECIMALS, type TransferParams, VM31_ASSET_IDS, type WithdrawParams, type WithdrawResult, type WorkerStake, commitmentToHash, createAEHint, createEncryptionProof, deriveNullifier, ecAdd, ecMul, elgamalEncrypt, formatAmount, getContracts, getRpcUrl, mod, modInverse, parseAmount, pedersenCommit, randomScalar };