@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,348 @@
1
+ /**
2
+ * Note (shielded commitment) utilities for UTXOpia
3
+ *
4
+ * A Note represents a shielded deposit with:
5
+ * - nullifier: Random secret for spending
6
+ * - secret: Additional entropy
7
+ * - amount: Value in satoshis
8
+ *
9
+ * Hash values (commitment, nullifierHash) are computed by circom circuits
10
+ * using Poseidon. This SDK stores the raw secrets and optionally
11
+ * accepts pre-computed hash values.
12
+ */
13
+ /**
14
+ * Note structure for shielded amounts
15
+ *
16
+ * Core secrets (always required):
17
+ * - amount, nullifier, secret
18
+ *
19
+ * Computed values (from circom circuits):
20
+ * - note, commitment, nullifierHash
21
+ * These are 0n until computed by circuits
22
+ */
23
+ export interface Note {
24
+ amount: bigint;
25
+ nullifier: bigint;
26
+ secret: bigint;
27
+ note: bigint;
28
+ commitment: bigint;
29
+ nullifierHash: bigint;
30
+ nullifierBytes: Uint8Array;
31
+ secretBytes: Uint8Array;
32
+ commitmentBytes: Uint8Array;
33
+ nullifierHashBytes: Uint8Array;
34
+ }
35
+ /**
36
+ * Serializable note data (for storage/transmission)
37
+ * Only stores essential secrets - hashes recomputed by circuits
38
+ */
39
+ export interface SerializedNote {
40
+ amount: string;
41
+ nullifier: string;
42
+ secret: string;
43
+ commitment?: string;
44
+ nullifierHash?: string;
45
+ }
46
+ /**
47
+ * Generate a new note with random nullifier and secret
48
+ *
49
+ * Note: Hash values are NOT computed here. They will be computed
50
+ * by the circom circuits during proof generation.
51
+ *
52
+ * @param amountSats - Amount in satoshis
53
+ * @returns Note with secrets (hashes are 0n until circuit execution)
54
+ */
55
+ export declare function generateNote(amountSats: bigint): Note;
56
+ /**
57
+ * Create a note from known secrets
58
+ *
59
+ * @param nullifier - Nullifier field element
60
+ * @param secret - Secret field element
61
+ * @param amountSats - Amount in satoshis
62
+ * @param commitment - Optional pre-computed commitment (from circuit)
63
+ * @param nullifierHash - Optional pre-computed nullifier hash (from circuit)
64
+ * @returns Note structure
65
+ */
66
+ export declare function createNoteFromSecrets(nullifier: bigint, secret: bigint, amountSats: bigint, commitment?: bigint, nullifierHash?: bigint): Note;
67
+ /**
68
+ * Update note with computed hash values from circom circuit execution
69
+ *
70
+ * @param note - Note to update
71
+ * @param commitment - Commitment from circuit output
72
+ * @param nullifierHash - Nullifier hash from circuit output
73
+ * @returns Updated note
74
+ */
75
+ export declare function updateNoteWithHashes(note: Note, commitment: bigint, nullifierHash: bigint): Note;
76
+ /**
77
+ * Serialize a note for storage or transmission
78
+ *
79
+ * Only stores the essential data (amount, nullifier, secret).
80
+ * Optionally includes pre-computed hash values.
81
+ */
82
+ export declare function serializeNote(note: Note): SerializedNote;
83
+ /**
84
+ * Deserialize and restore a note from stored data
85
+ */
86
+ export declare function deserializeNote(data: SerializedNote): Note;
87
+ /**
88
+ * Format satoshis as BTC string
89
+ */
90
+ export declare function formatBtc(sats: bigint): string;
91
+ /**
92
+ * Parse BTC string to satoshis
93
+ */
94
+ export declare function parseBtc(btcString: string): bigint;
95
+ /**
96
+ * Check if a note has computed hash values
97
+ */
98
+ export declare function noteHasComputedHashes(note: Note): boolean;
99
+ /**
100
+ * Get the public key X coordinate from a note's nullifier (used as private key)
101
+ *
102
+ * In the unified model: pubKey = nullifier * BASE8 (on Baby Jubjub curve)
103
+ * Returns pubKey.x for commitment computation
104
+ */
105
+ export declare function getNotePublicKeyX(note: Note): bigint;
106
+ /**
107
+ * Compute the commitment for a note
108
+ *
109
+ * commitment = Poseidon(pubKeyX, amount)
110
+ * where pubKeyX = (nullifier * G).x
111
+ *
112
+ * Returns the note with commitment fields populated
113
+ */
114
+ export declare function computeNoteCommitment(note: Note): Note;
115
+ /**
116
+ * Compute the nullifier and nullifier hash for a note at a given leaf index
117
+ *
118
+ * nullifier = Poseidon(privKey, leafIndex)
119
+ * nullifierHash = Poseidon(nullifier)
120
+ *
121
+ * @returns Object with computed nullifier values
122
+ */
123
+ export declare function computeNoteNullifier(note: Note, leafIndex: bigint): {
124
+ nullifier: bigint;
125
+ nullifierHash: bigint;
126
+ nullifierHashBytes: Uint8Array;
127
+ };
128
+ /**
129
+ * Derive a note deterministically from a seed phrase/name and index.
130
+ *
131
+ * This allows recovery of all notes from just the seed + index.
132
+ * Similar to HD wallets in Bitcoin.
133
+ *
134
+ * The seed is first hashed to create a 32-byte master key, then
135
+ * nullifier and secret are derived from that master key.
136
+ *
137
+ * @param seed - Seed phrase, name, or password (any string)
138
+ * @param index - Note index (0, 1, 2, ...)
139
+ * @param amountSats - Amount in satoshis
140
+ * @returns Note with deterministically derived secrets
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * // Derive notes from a name
145
+ * const note0 = deriveNote("albertgogogo", 0, 100_000n);
146
+ * const note1 = deriveNote("albertgogogo", 1, 50_000n);
147
+ *
148
+ * // Later: recover the same notes
149
+ * const recovered = deriveNote("albertgogogo", 0, 100_000n);
150
+ * // recovered.nullifier === note0.nullifier ✓
151
+ * ```
152
+ */
153
+ export declare function deriveNote(seed: string, index: number, amountSats: bigint): Note;
154
+ /**
155
+ * Derive master key from seed (32 bytes)
156
+ *
157
+ * master = SHA256(seed)
158
+ *
159
+ * You can cache this and use deriveNoteFromMaster() for efficiency.
160
+ */
161
+ export declare function deriveMasterKey(seed: string): Uint8Array;
162
+ /**
163
+ * Derive a note from a pre-computed master key
164
+ *
165
+ * More efficient if deriving many notes from the same seed.
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * const master = deriveMasterKey("albertgogogo");
170
+ * const note0 = deriveNoteFromMaster(master, 0, 100_000n);
171
+ * const note1 = deriveNoteFromMaster(master, 1, 50_000n);
172
+ * ```
173
+ */
174
+ export declare function deriveNoteFromMaster(master: Uint8Array, index: number, amountSats: bigint): Note;
175
+ /**
176
+ * Derive multiple notes at once from a seed
177
+ *
178
+ * More efficient than calling deriveNote() multiple times
179
+ * because master key is computed only once.
180
+ *
181
+ * @param seed - Seed phrase
182
+ * @param amounts - Array of amounts for each note
183
+ * @param startIndex - Starting index (default 0)
184
+ * @returns Array of derived notes
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * // Create a wallet with 3 notes
189
+ * const notes = deriveNotes("albertgogogo", [100_000n, 50_000n, 25_000n]);
190
+ * ```
191
+ */
192
+ export declare function deriveNotes(seed: string, amounts: bigint[], startIndex?: number): Note[];
193
+ /**
194
+ * Check the strength of a seed phrase
195
+ *
196
+ * Returns estimated bits of entropy.
197
+ * Recommended: >= 80 bits for moderate security, >= 128 bits for high security
198
+ */
199
+ export declare function estimateSeedStrength(seed: string): {
200
+ bits: number;
201
+ strength: "weak" | "moderate" | "strong" | "very_strong";
202
+ warning?: string;
203
+ };
204
+ /**
205
+ * Simple note data structure (for circom circuit inputs)
206
+ *
207
+ * NOTE: When using circom circuits, the commitment is computed INSIDE the circuit
208
+ * using Poseidon. The SDK just provides the raw note data.
209
+ */
210
+ export interface NoteData {
211
+ nullifier: bigint;
212
+ secret: bigint;
213
+ amount: bigint;
214
+ }
215
+ /**
216
+ * Create a simple note (alias for generateNote but returns NoteData)
217
+ *
218
+ * Use this to generate note data for circom circuit inputs.
219
+ *
220
+ * @param amount - Amount in satoshis
221
+ * @returns NoteData with nullifier, secret, and amount
222
+ */
223
+ export declare function createNote(amount: bigint): NoteData;
224
+ /**
225
+ * Prepare withdrawal - creates change note for remaining balance
226
+ *
227
+ * PRIVACY: All withdrawals are indistinguishable on-chain.
228
+ * Change amount can be >= 0, and the commitment always looks random.
229
+ *
230
+ * @param inputNote - Note being spent
231
+ * @param withdrawAmount - Amount to withdraw
232
+ * @returns Change note with remaining balance (can be 0)
233
+ */
234
+ export declare function prepareWithdrawal(inputNote: NoteData, withdrawAmount: bigint): {
235
+ changeNote: NoteData;
236
+ changeAmount: bigint;
237
+ };
238
+ /**
239
+ * Stealth note structure for dual-key ECDH system
240
+ *
241
+ * Key differences from basic Note:
242
+ * - Uses random value instead of nullifier/secret for commitment
243
+ * - Stores ephemeral spending pubkey for proof generation
244
+ * - Nullifier derived from (spendingPrivKey, leafIndex) in circuit
245
+ */
246
+ export interface StealthNote {
247
+ /** Amount in satoshis */
248
+ amount: bigint;
249
+ /** Random value for commitment */
250
+ random: bigint;
251
+ /** Ephemeral spending public key (from sender) */
252
+ ephemeralSpendPubX: bigint;
253
+ ephemeralSpendPubY: bigint;
254
+ /** Leaf index in Merkle tree (set when commitment added on-chain) */
255
+ leafIndex: number;
256
+ /** Note public key = Poseidon(ECDHShared.x, ECDHShared.y) */
257
+ notePubKey: bigint;
258
+ /** Commitment = Poseidon(notePubKey, amount, random) */
259
+ commitment: bigint;
260
+ /** Byte representations */
261
+ randomBytes: Uint8Array;
262
+ commitmentBytes: Uint8Array;
263
+ }
264
+ /**
265
+ * Serializable stealth note data
266
+ */
267
+ export interface SerializedStealthNote {
268
+ amount: string;
269
+ random: string;
270
+ ephemeralSpendPubX: string;
271
+ ephemeralSpendPubY: string;
272
+ leafIndex: number;
273
+ notePubKey?: string;
274
+ commitment?: string;
275
+ }
276
+ /**
277
+ * Create a stealth note from scanned announcement data
278
+ *
279
+ * @param amount - Decrypted amount
280
+ * @param random - Decrypted random value
281
+ * @param ephemeralSpendPub - Sender's ephemeral pubkey
282
+ * @param leafIndex - Merkle tree leaf index
283
+ * @returns StealthNote structure
284
+ */
285
+ export declare function createStealthNote(amount: bigint, random: bigint, ephemeralSpendPub: {
286
+ x: bigint;
287
+ y: bigint;
288
+ }, leafIndex: number): StealthNote;
289
+ /**
290
+ * Update stealth note with computed values from circuit
291
+ */
292
+ export declare function updateStealthNoteWithHashes(note: StealthNote, notePubKey: bigint, commitment: bigint): StealthNote;
293
+ /**
294
+ * Serialize stealth note for storage
295
+ */
296
+ export declare function serializeStealthNote(note: StealthNote): SerializedStealthNote;
297
+ /**
298
+ * Deserialize stealth note from storage
299
+ */
300
+ export declare function deserializeStealthNote(data: SerializedStealthNote): StealthNote;
301
+ /**
302
+ * Check if stealth note has computed hashes
303
+ */
304
+ export declare function stealthNoteHasComputedHashes(note: StealthNote): boolean;
305
+ /**
306
+ * JoinSplit note structure
307
+ *
308
+ * Commitment = Poseidon(npk, token, amount)
309
+ * where npk = Poseidon(MPK, random)
310
+ * and MPK = Poseidon(pkX, pkY, nullifyingKey)
311
+ */
312
+ export interface JoinSplitNote {
313
+ /** Note public key: Poseidon(MPK, random) */
314
+ npk: bigint;
315
+ /** Token identifier: computeTokenId(mintBytes) */
316
+ token: bigint;
317
+ /** Amount in token's native units */
318
+ amount: bigint;
319
+ /** Blinding factor (used in NPK derivation) */
320
+ random: bigint;
321
+ /** Merkle tree leaf position (-1 if not yet inserted) */
322
+ leafIndex: number;
323
+ /** Commitment: Poseidon(npk, token, amount) */
324
+ commitment: bigint;
325
+ }
326
+ /**
327
+ * Create a JoinSplit note from key components
328
+ *
329
+ * @param tokenId - Token identifier from computeTokenId(mintBytes)
330
+ */
331
+ export declare function createJoinSplitNote(mpk: bigint, random: bigint, amount: bigint, tokenId: bigint, leafIndex?: number): JoinSplitNote;
332
+ /**
333
+ * Compute nullifier for a JoinSplit note
334
+ */
335
+ export declare function computeJoinSplitNoteNullifier(nullifyingKey: bigint, leafIndex: number): bigint;
336
+ /**
337
+ * Serialize a JoinSplit note for storage
338
+ */
339
+ export interface SerializedJoinSplitNote {
340
+ npk: string;
341
+ token: string;
342
+ amount: string;
343
+ random: string;
344
+ leafIndex: number;
345
+ commitment: string;
346
+ }
347
+ export declare function serializeJoinSplitNote(note: JoinSplitNote): SerializedJoinSplitNote;
348
+ export declare function deserializeJoinSplitNote(data: SerializedJoinSplitNote): JoinSplitNote;