@utxopia/sdk 0.1.0-alpha.1

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.
Files changed (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +171 -0
  3. package/package.json +108 -0
  4. package/packages/btc-client/src/esplora-client.ts +153 -0
  5. package/packages/btc-client/src/index.ts +3 -0
  6. package/packages/btc-client/src/op-return.ts +93 -0
  7. package/packages/btc-client/src/types.ts +112 -0
  8. package/packages/sdk/README.md +277 -0
  9. package/packages/sdk/dist/announcement-client.d.ts +64 -0
  10. package/packages/sdk/dist/announcement-client.js +337 -0
  11. package/packages/sdk/dist/auditor-ciphertext.d.ts +72 -0
  12. package/packages/sdk/dist/auditor-ciphertext.js +135 -0
  13. package/packages/sdk/dist/auditor.d.ts +115 -0
  14. package/packages/sdk/dist/auditor.js +292 -0
  15. package/packages/sdk/dist/bitcoin/ika.d.ts +47 -0
  16. package/packages/sdk/dist/bitcoin/ika.js +74 -0
  17. package/packages/sdk/dist/bitcoin/index.d.ts +2 -0
  18. package/packages/sdk/dist/bitcoin/index.js +1 -0
  19. package/packages/sdk/dist/bound-params.d.ts +96 -0
  20. package/packages/sdk/dist/bound-params.js +249 -0
  21. package/packages/sdk/dist/chadbuffer.d.ts +124 -0
  22. package/packages/sdk/dist/chadbuffer.js +405 -0
  23. package/packages/sdk/dist/claim-link.d.ts +30 -0
  24. package/packages/sdk/dist/claim-link.js +50 -0
  25. package/packages/sdk/dist/client.d.ts +233 -0
  26. package/packages/sdk/dist/client.js +416 -0
  27. package/packages/sdk/dist/commitment-tree.d.ts +268 -0
  28. package/packages/sdk/dist/commitment-tree.js +564 -0
  29. package/packages/sdk/dist/config.d.ts +177 -0
  30. package/packages/sdk/dist/config.js +426 -0
  31. package/packages/sdk/dist/core/esplora.d.ts +110 -0
  32. package/packages/sdk/dist/core/esplora.js +187 -0
  33. package/packages/sdk/dist/core/mempool.d.ts +60 -0
  34. package/packages/sdk/dist/core/mempool.js +107 -0
  35. package/packages/sdk/dist/crypto-babyjub.d.ts +105 -0
  36. package/packages/sdk/dist/crypto-babyjub.js +312 -0
  37. package/packages/sdk/dist/crypto-ed25519.d.ts +116 -0
  38. package/packages/sdk/dist/crypto-ed25519.js +235 -0
  39. package/packages/sdk/dist/crypto.d.ts +59 -0
  40. package/packages/sdk/dist/crypto.js +140 -0
  41. package/packages/sdk/dist/event-client.d.ts +64 -0
  42. package/packages/sdk/dist/event-client.js +165 -0
  43. package/packages/sdk/dist/events.d.ts +127 -0
  44. package/packages/sdk/dist/events.js +305 -0
  45. package/packages/sdk/dist/explorer.d.ts +86 -0
  46. package/packages/sdk/dist/explorer.js +197 -0
  47. package/packages/sdk/dist/index.d.ts +61 -0
  48. package/packages/sdk/dist/index.js +216 -0
  49. package/packages/sdk/dist/instructions.d.ts +1125 -0
  50. package/packages/sdk/dist/instructions.js +1760 -0
  51. package/packages/sdk/dist/keys.d.ts +467 -0
  52. package/packages/sdk/dist/keys.js +799 -0
  53. package/packages/sdk/dist/logger.d.ts +10 -0
  54. package/packages/sdk/dist/logger.js +40 -0
  55. package/packages/sdk/dist/magicblock.d.ts +90 -0
  56. package/packages/sdk/dist/magicblock.js +164 -0
  57. package/packages/sdk/dist/merkle.d.ts +82 -0
  58. package/packages/sdk/dist/merkle.js +141 -0
  59. package/packages/sdk/dist/note.d.ts +348 -0
  60. package/packages/sdk/dist/note.js +483 -0
  61. package/packages/sdk/dist/pda.d.ts +160 -0
  62. package/packages/sdk/dist/pda.js +384 -0
  63. package/packages/sdk/dist/pool-state.d.ts +100 -0
  64. package/packages/sdk/dist/pool-state.js +126 -0
  65. package/packages/sdk/dist/poseidon.d.ts +53 -0
  66. package/packages/sdk/dist/poseidon.js +136 -0
  67. package/packages/sdk/dist/prover/index.d.ts +10 -0
  68. package/packages/sdk/dist/prover/index.js +10 -0
  69. package/packages/sdk/dist/prover/mobile.d.ts +45 -0
  70. package/packages/sdk/dist/prover/mobile.js +227 -0
  71. package/packages/sdk/dist/prover/web.d.ts +136 -0
  72. package/packages/sdk/dist/prover/web.js +597 -0
  73. package/packages/sdk/dist/psbt.d.ts +73 -0
  74. package/packages/sdk/dist/psbt.js +202 -0
  75. package/packages/sdk/dist/selective-disclosure.d.ts +181 -0
  76. package/packages/sdk/dist/selective-disclosure.js +172 -0
  77. package/packages/sdk/dist/sender-memo.d.ts +149 -0
  78. package/packages/sdk/dist/sender-memo.js +250 -0
  79. package/packages/sdk/dist/sns-resolver.d.ts +121 -0
  80. package/packages/sdk/dist/sns-resolver.js +229 -0
  81. package/packages/sdk/dist/solana/connection.d.ts +77 -0
  82. package/packages/sdk/dist/solana/connection.js +133 -0
  83. package/packages/sdk/dist/solana/priority-fee.d.ts +78 -0
  84. package/packages/sdk/dist/solana/priority-fee.js +137 -0
  85. package/packages/sdk/dist/stealth.d.ts +429 -0
  86. package/packages/sdk/dist/stealth.js +703 -0
  87. package/packages/sdk/dist/taproot.d.ts +172 -0
  88. package/packages/sdk/dist/taproot.js +494 -0
  89. package/packages/sdk/dist/token-registry.d.ts +96 -0
  90. package/packages/sdk/dist/token-registry.js +122 -0
  91. package/packages/sdk/dist/utils/encoding.d.ts +19 -0
  92. package/packages/sdk/dist/utils/encoding.js +29 -0
  93. package/packages/sdk/dist/vk-registry.d.ts +100 -0
  94. package/packages/sdk/dist/vk-registry.js +212 -0
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Commitment Tree Client
3
+ *
4
+ * Implements an incremental Merkle tree matching the on-chain logic exactly.
5
+ * Uses frontier array + pre-computed zero hashes (Tornado Cash/Semaphore pattern).
6
+ *
7
+ * The tree supports up to 2^20 (~1M) leaf commitments.
8
+ * Standard Merkle path proofs compatible with ZK circuits.
9
+ */
10
+ import { initPoseidon } from "./poseidon";
11
+ export { initPoseidon };
12
+ export declare const TREE_DEPTH = 16;
13
+ export declare const ROOT_HISTORY_SIZE = 100;
14
+ export declare const MAX_LEAVES: bigint;
15
+ export declare const COMMITMENT_TREE_DISCRIMINATOR = 5;
16
+ /**
17
+ * Pre-computed zero hashes for each level of the tree
18
+ * ZERO[0] = 0 (empty leaf)
19
+ * ZERO[i] = Poseidon(ZERO[i-1], ZERO[i-1])
20
+ *
21
+ * These values MUST match the contract's ZERO_HASHES exactly!
22
+ * Using Circom-compatible Poseidon (matches Solana's sol_poseidon).
23
+ */
24
+ export declare const ZERO_HASHES: bigint[];
25
+ /**
26
+ * On-chain commitment tree state
27
+ */
28
+ export interface CommitmentTreeState {
29
+ discriminator: number;
30
+ bump: number;
31
+ currentRoot: Uint8Array;
32
+ nextIndex: bigint;
33
+ frontier: Uint8Array[];
34
+ rootHistory: Uint8Array[];
35
+ rootHistoryIndex: number;
36
+ }
37
+ /**
38
+ * Parse commitment tree account data
39
+ *
40
+ * On-chain layout:
41
+ * - discriminator: 1 byte
42
+ * - bump: 1 byte
43
+ * - padding: 6 bytes
44
+ * - current_root: 32 bytes
45
+ * - next_index: 8 bytes
46
+ * - frontier: 20 * 32 = 640 bytes
47
+ * - root_history: 100 * 32 = 3200 bytes
48
+ * - root_history_index: 4 bytes
49
+ * - reserved: 60 bytes
50
+ */
51
+ export declare function parseCommitmentTreeData(data: Uint8Array): CommitmentTreeState;
52
+ /**
53
+ * Check if a root is valid (current or in history)
54
+ */
55
+ export declare function isValidRoot(state: CommitmentTreeState, root: Uint8Array): boolean;
56
+ /**
57
+ * Local Commitment Tree Index (Incremental Merkle Tree)
58
+ *
59
+ * Uses the same algorithm as the on-chain contract:
60
+ * - Frontier array to track rightmost filled nodes at each level
61
+ * - Pre-computed zero hashes for empty subtrees
62
+ *
63
+ * This produces the same root as the on-chain tree.
64
+ */
65
+ export declare class CommitmentTreeIndex {
66
+ private commitments;
67
+ private leaves;
68
+ private frontier;
69
+ private currentRoot;
70
+ private nextIndex;
71
+ constructor();
72
+ /**
73
+ * Add a commitment to the index
74
+ * Uses the same algorithm as on-chain insert_leaf()
75
+ */
76
+ addCommitment(commitment: bigint, amount: bigint): bigint;
77
+ /**
78
+ * Get merkle proof for a commitment
79
+ *
80
+ * Returns siblings and path indices for ZK circuit verification.
81
+ */
82
+ getMerkleProof(commitment: bigint): {
83
+ siblings: bigint[];
84
+ indices: number[];
85
+ leafIndex: bigint;
86
+ root: bigint;
87
+ } | null;
88
+ /**
89
+ * Compute merkle proof for a leaf at given index
90
+ * Optimized: only computes nodes needed for the proof path
91
+ */
92
+ private computeMerkleProof;
93
+ /**
94
+ * Get commitment info by hex string
95
+ */
96
+ getCommitment(commitmentHex: string): {
97
+ index: bigint;
98
+ amount: bigint;
99
+ } | null;
100
+ /**
101
+ * Get current merkle root
102
+ */
103
+ getRoot(): bigint;
104
+ /**
105
+ * Get root as bytes for comparison with on-chain
106
+ */
107
+ getRootBytes(): Uint8Array;
108
+ /**
109
+ * Get number of commitments
110
+ */
111
+ size(): number;
112
+ /**
113
+ * Get next leaf index
114
+ */
115
+ getNextIndex(): bigint;
116
+ /**
117
+ * Get tree status (for API compatibility)
118
+ */
119
+ getStatus(): {
120
+ root: string;
121
+ nextIndex: number;
122
+ size: number;
123
+ };
124
+ /**
125
+ * Get path (merkle proof) for a commitment by hex string
126
+ * Convenience wrapper around getMerkleProof for API compatibility
127
+ */
128
+ getPath(commitmentHex: string): {
129
+ siblings: string[];
130
+ indices: number[];
131
+ leafIndex: string;
132
+ root: string;
133
+ } | null;
134
+ /**
135
+ * Export index for persistence
136
+ */
137
+ export(): {
138
+ commitments: [string, {
139
+ index: string;
140
+ amount: string;
141
+ }][];
142
+ };
143
+ /**
144
+ * Import index from persistence
145
+ */
146
+ import(data: {
147
+ commitments: [string, {
148
+ index: string;
149
+ amount: string;
150
+ }][];
151
+ }): void;
152
+ /**
153
+ * Import from on-chain state
154
+ * Reconstructs tree state from on-chain frontier and root
155
+ */
156
+ importFromOnChainState(state: CommitmentTreeState): void;
157
+ }
158
+ /**
159
+ * Fetch commitment tree state from Solana
160
+ */
161
+ export declare function fetchCommitmentTree(connection: {
162
+ getAccountInfo: (pubkey: unknown) => Promise<{
163
+ data: Uint8Array;
164
+ } | null>;
165
+ }, commitmentTreePDA: unknown): Promise<CommitmentTreeState | null>;
166
+ /**
167
+ * Get or create the global commitment index
168
+ */
169
+ export declare function getCommitmentIndex(): CommitmentTreeIndex;
170
+ /**
171
+ * Save the global commitment index to localStorage
172
+ */
173
+ export declare function saveCommitmentIndex(): void;
174
+ /**
175
+ * RPC client interface for on-chain queries
176
+ * Compatible with @solana/web3.js Connection and Helius enhanced RPC
177
+ */
178
+ export interface RpcClient {
179
+ getProgramAccounts(programId: string, config?: {
180
+ filters?: Array<{
181
+ memcmp: {
182
+ offset: number;
183
+ bytes: string;
184
+ };
185
+ } | {
186
+ dataSize: number;
187
+ }>;
188
+ encoding?: string;
189
+ }): Promise<Array<{
190
+ pubkey: string;
191
+ account: {
192
+ data: Uint8Array | string;
193
+ };
194
+ }>>;
195
+ }
196
+ /**
197
+ * On-chain merkle proof format
198
+ */
199
+ export interface OnChainMerkleProof {
200
+ siblings: bigint[];
201
+ indices: number[];
202
+ leafIndex: number;
203
+ root: bigint;
204
+ commitment: bigint;
205
+ }
206
+ /**
207
+ * Build commitment tree from indexed commitment data.
208
+ *
209
+ * Stealth announcements are now emitted as events, not stored as PDAs.
210
+ * This function accepts a commitments map (leafIndex → commitment bigint)
211
+ * from the event indexer and builds the local Merkle tree.
212
+ *
213
+ * @param _rpc - RPC client (unused, kept for API compatibility)
214
+ * @param _programId - UTXOpia program ID (unused)
215
+ * @param options - commitments map from indexer
216
+ */
217
+ export declare function buildCommitmentTreeFromChain(_rpc: RpcClient, _programId: string, options?: {
218
+ maxLeafIndex?: number;
219
+ commitments?: Map<number, bigint>;
220
+ }): Promise<CommitmentTreeIndex>;
221
+ /**
222
+ * Get leaf index for a commitment.
223
+ *
224
+ * Commitments are emitted as sol_log_data events (not stored in PDAs).
225
+ * Use scanUnifiedNotes() to get leafIndex for your notes, or query the indexer API.
226
+ * This function builds the full tree from provided commitments to find the index.
227
+ *
228
+ * @param rpc - RPC client
229
+ * @param programId - UTXOpia program ID
230
+ * @param commitment - Commitment to find
231
+ * @returns Leaf index or -1 if not found
232
+ */
233
+ export declare function fetchLeafIndexForCommitment(rpc: RpcClient, programId: string, commitment: bigint): Promise<number>;
234
+ /**
235
+ * Fetch merkle proof for a commitment from on-chain data
236
+ *
237
+ * Builds tree from chain and computes merkle proof for the given commitment.
238
+ * For better performance with multiple proofs, use buildCommitmentTreeFromChain
239
+ * once and call getMerkleProof on the resulting tree.
240
+ *
241
+ * @param rpc - RPC client
242
+ * @param programId - UTXOpia program ID
243
+ * @param commitment - Commitment to get proof for
244
+ * @returns Merkle proof or null if commitment not found
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * const proof = await fetchMerkleProofForCommitment(connection, programId, myCommitment);
249
+ * if (proof) {
250
+ * // Use proof for ZK circuit
251
+ * const claimInputs = {
252
+ * merkleRoot: proof.root,
253
+ * merkleProof: { siblings: proof.siblings, indices: proof.indices },
254
+ * leafIndex: BigInt(proof.leafIndex),
255
+ * // ...
256
+ * };
257
+ * }
258
+ * ```
259
+ */
260
+ export declare function fetchMerkleProofForCommitment(rpc: RpcClient, programId: string, commitment: bigint): Promise<OnChainMerkleProof | null>;
261
+ /**
262
+ * Fetch merkle proof using cached tree (more efficient for multiple lookups)
263
+ *
264
+ * @param tree - Pre-built commitment tree from buildCommitmentTreeFromChain
265
+ * @param commitment - Commitment to get proof for
266
+ * @returns Merkle proof or null if not found
267
+ */
268
+ export declare function getMerkleProofFromTree(tree: CommitmentTreeIndex, commitment: bigint): OnChainMerkleProof | null;