@cloak.dev/sdk 0.2.6-staging.f92dd96 → 0.2.7
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.
- package/CHANGELOG.md +65 -3
- package/README.md +108 -7
- package/dist/index.cjs +211 -83
- package/dist/index.d.cts +146 -19
- package/dist/index.d.ts +146 -19
- package/dist/index.js +209 -83
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,38 @@ All notable changes to `@cloak.dev/sdk` are documented here. The format follows
|
|
|
5
5
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). Releases before 0.2.6 are described in
|
|
6
6
|
the README's "Version and compatibility" section and in the git history.
|
|
7
7
|
|
|
8
|
+
## [0.2.7]
|
|
9
|
+
|
|
10
|
+
Pairs with the same mainnet program (`zh1eLd6rSphLejbFfJEneUwzHRfMKxgzrgkfwA6qRkW`) and the same
|
|
11
|
+
0.2.0 circuit bundle as 0.2.6. Note format, hashing and the relay wire format are unchanged; no
|
|
12
|
+
public signature changes. Works with the relay from the partner-program release (services#208).
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
* Internal partner (affiliate) code, used by Cloak's own web and mobile apps (#61):
|
|
17
|
+
`TransactOptions.affiliateCode` is trimmed and lower-cased once at the flow boundary, before it
|
|
18
|
+
enters the request body and the signed relay digest, because the relay only counts
|
|
19
|
+
`^[a-z0-9_]{3,24}$` and silently ignores anything else. A code that is still invalid is left out
|
|
20
|
+
of the request and reported through the new optional `TransactOptions.onWarning(message)`; it
|
|
21
|
+
never fails the request. A code that was already valid is sent exactly as before, so its digest
|
|
22
|
+
is unchanged. `transact`, `transfer`, `partialWithdraw`, `fullWithdraw`, `swapUtxo`,
|
|
23
|
+
`swapWithChange` and `transactBatch` all prepare the option once; which requests carry it is
|
|
24
|
+
unchanged (a withdrawal with a recipient, and a swap intake; never a deposit, a
|
|
25
|
+
shield-to-shield send or a `partialWithdraw` merge step). In `transactBatch` the batch's code
|
|
26
|
+
applies to every fee-bearing item and an item's own `options.affiliateCode` overrides it.
|
|
27
|
+
* Internal `TransactOptions.relayClientId` (for Cloak's mobile app, e.g. `"cloak-mobile/ios/1.4.0"`)
|
|
28
|
+
sends `X-Cloak-Client` with `POST /transact` and `POST /transact_swap`, replacing a same-named
|
|
29
|
+
`relayHeaders` entry. A value that does not match `cloak-mobile/(ios|android)/MAJOR.MINOR.PATCH`
|
|
30
|
+
is left out and reported through `onWarning`; it never fails the request. Browser callers leave
|
|
31
|
+
it unset.
|
|
32
|
+
* Internal helpers `normalizeAffiliateCode(raw): string | null` and
|
|
33
|
+
`isValidRelayClientId(value)` are exported for the apps.
|
|
34
|
+
|
|
35
|
+
### Fixed
|
|
36
|
+
|
|
37
|
+
* Scan results label BRS (`BRSxQRUaGswjLs7ewcH7uXj3r7SgmfKSSLLXyCKHZtUo`) notes as `BRS` instead
|
|
38
|
+
of a truncated mint (#58). Label only; amounts and decimals are unchanged.
|
|
39
|
+
|
|
8
40
|
## [0.2.6]
|
|
9
41
|
|
|
10
42
|
Pairs with the same mainnet program (`zh1eLd6rSphLejbFfJEneUwzHRfMKxgzrgkfwA6qRkW`) and the same
|
|
@@ -91,6 +123,10 @@ it. These notes are still found by `matchDepositNote`, `matchSwapRefundLeaf`, `m
|
|
|
91
123
|
* The paymaster client judges the relay-issued voucher expiry on the relay clock.
|
|
92
124
|
* With no sample (a relay that predates the header, or no relay reachable) every stamp falls back to
|
|
93
125
|
`Date.now()`, the 0.2.5 behaviour.
|
|
126
|
+
* A relay clock more than 24 hours from the machine's is ignored (the bound was a year). The clock
|
|
127
|
+
dates the proof-bound chain-note timestamp, which stays on chain and shows in history and
|
|
128
|
+
compliance reports, so a relay must not be able to move it by months. A machine more than 24
|
|
129
|
+
hours off falls back to its own clock, and the relay's auth window decides.
|
|
94
130
|
|
|
95
131
|
### Note Index v1 (restore without walking the chain)
|
|
96
132
|
|
|
@@ -103,8 +139,8 @@ of every confirmed Cloak transaction that the relay indexes once, server side. A
|
|
|
103
139
|
#### Added
|
|
104
140
|
|
|
105
141
|
* `recoverNotesFromIndex({ relayUrl, connection, programId, poolMints, viewingKeyNk,
|
|
106
|
-
ownerUtxoKeypair, cursor?, legacyCutoffLeaf?, concurrency?, onProgress?,
|
|
107
|
-
`{ notes, atRisk, pending, cursor, stats }`. It downloads every page (the only request
|
|
142
|
+
ownerUtxoKeypair, cursor?, legacyViewingKeyNks?, legacyCutoffLeaf?, concurrency?, onProgress?,
|
|
143
|
+
signal? })` returns `{ notes, atRisk, pending, cursor, stats, warnings }`. It downloads every page (the only request
|
|
108
144
|
parameters are the epoch and the page number, so the relay learns nothing about which notes are
|
|
109
145
|
the wallet's), opens each item locally, rebuilds the tree of every listed pool from the index's
|
|
110
146
|
own leaf stream and checks it against the chain, and computes spent status from the index's
|
|
@@ -114,6 +150,16 @@ of every confirmed Cloak transaction that the relay indexes once, server side. A
|
|
|
114
150
|
leaf in a root-verified tree. Notes owned by the wallet keypair go to `notes`; legacy notes whose
|
|
115
151
|
owner key is derivable from `nk` go to `atRisk` with `legacyExposed: true`, never into `notes`.
|
|
116
152
|
Candidates that cannot be verified yet are counted in `pending` and carried in the cursor.
|
|
153
|
+
`pending` depends on what the wallet owns: gate polls on the public head (epoch, watermark), never
|
|
154
|
+
on `pending`, or the download pattern tells the relay which client owns an item it tampered with.
|
|
155
|
+
* `legacyViewingKeyNks` (at most 8): other viewing keys older clients of the wallet encrypted chain
|
|
156
|
+
notes under (older mobile builds used SHA-512 of the public wallet address, or the embedded
|
|
157
|
+
wallet's nsk). Each item is tried under the canonical `nk` first, then under each legacy key, in
|
|
158
|
+
the same download: no extra request, still one `getMultipleAccounts`. Everything found only
|
|
159
|
+
through a legacy key is returned in `atRisk` whatever its owner key, a legacy key never opens a
|
|
160
|
+
delivered note, and nothing it opens can enter `notes`. A migration (a canonical-`nk` spend of a
|
|
161
|
+
legacy note) marks the legacy note spent and returns its change, which is found by conservation
|
|
162
|
+
over the legacy input.
|
|
117
163
|
* The cursor (format v2) is JSON with no key material; persist it for incremental syncs. An epoch
|
|
118
164
|
change, a broken hash chain or an invalid cursor re-syncs from page 0; a head behind the cursor
|
|
119
165
|
throws the retryable `NoteIndexHeadBehindError`.
|
|
@@ -127,10 +173,26 @@ of every confirmed Cloak transaction that the relay indexes once, server side. A
|
|
|
127
173
|
* The relay is untrusted. It can hide items but cannot inject, alter or forge a note. A hidden
|
|
128
174
|
nullifier can make a spent note look unspent, so restored notes must be spent through
|
|
129
175
|
`transact`, `partialWithdraw`, `fullWithdraw` or `swapUtxo`, which run `preflightNullifiers`
|
|
130
|
-
before proving. `
|
|
176
|
+
before proving. `submitTransactToRelay` documents that it does not (as does the internal
|
|
177
|
+
`buildTransactInstruction`, which is not exported).
|
|
131
178
|
* Note Index reads never set the relay clock. Complete pages are immutable and cacheable for a
|
|
132
179
|
year, so a cached response replays a stale `x-cloak-server-time`; learning from it would date
|
|
133
180
|
later signed requests in the past.
|
|
181
|
+
* One malformed, crafted or undecryptable entry never aborts a restore or a sync (QA2 F9). A legacy
|
|
182
|
+
viewing key may be public, so anyone could deposit to the owner key derived from it and spend
|
|
183
|
+
that deposit with a chain note under the same key carrying a salt of 0 (or over 96 bits); the
|
|
184
|
+
output-1 conservation path threw on it and every restore passing that key failed, for good.
|
|
185
|
+
Such an entry is now skipped and counted in `stats.skipped` (with a generic warning), and so is
|
|
186
|
+
anything else a matcher throws for one item, and a known-kind item whose body fails its format
|
|
187
|
+
checks while its framing in the page is intact (`parseNoteIndexPage(bytes, expect,
|
|
188
|
+
{ skipMalformedItems: true })`, which also reports `skippedMalformed` and each item's
|
|
189
|
+
`itemPositions`). Page-level faults (header, framing, hash chain, head) still fail the run.
|
|
190
|
+
* What a restored note is checked against, precisely: it opens under AES-GCM keyed by HKDF(nk,
|
|
191
|
+
the item's own `comm0`), its commitment recomputes from the opened values, and it sits at its
|
|
192
|
+
leaf in a chain-authenticated tree. The proof-bound `chainNoteHash` is NOT re-checked: an index
|
|
193
|
+
item does not carry it (nor the `extDataHash` its preimage needs), and fetching it would cost RPC
|
|
194
|
+
per transaction. It would add nothing: a chain note written for one transaction never opens
|
|
195
|
+
when the relay pairs it with another, so a mispairing can hide notes but never yield a wrong one.
|
|
134
196
|
|
|
135
197
|
### Fixed
|
|
136
198
|
|
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@ per-mint pool, send privately inside the pool, withdraw to any address, or swap
|
|
|
5
5
|
USDC/USDT with the output landing in a public token account. Groth16 proofs are generated
|
|
6
6
|
client-side (snarkjs) and verified on-chain by the Cloak program.
|
|
7
7
|
|
|
8
|
-
Version 0.2.
|
|
8
|
+
Version 0.2.7 targets the mainnet program deployed on 2026-08-24 and the ceremony-frozen
|
|
9
9
|
`cloak-transaction-0.2.0` circuit bundle. See [Version and compatibility](#version-and-compatibility).
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
npm install @cloak.dev/sdk@^0.2.
|
|
14
|
+
npm install @cloak.dev/sdk@^0.2.7
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
The SDK is built on [`@solana/kit`](https://github.com/anza-xyz/kit) and re-exports the pieces an
|
|
@@ -540,6 +540,10 @@ scan.deliveredNotes; // notes sent to this wallet (amount + blinding; the
|
|
|
540
540
|
scan.transactions; // history rows for compliance reporting
|
|
541
541
|
```
|
|
542
542
|
|
|
543
|
+
`scanTransactions` walks the chain with paid RPC calls per wallet. To restore a wallet or pick up
|
|
544
|
+
received notes, prefer [the Note Index](#restore-from-the-note-index-no-chain-walk), which costs one
|
|
545
|
+
RPC request; keep `scanTransactions` for compliance history.
|
|
546
|
+
|
|
543
547
|
`scanRecipientDeliveryNotes` does the delivery sweep alone, returning `{ notes, rpcCalls }`
|
|
544
548
|
instead of a full scan result. Deposit recovery applies to deposits built with
|
|
545
549
|
`createRecoverableDepositUtxo` (the default in the examples); a deposit built with
|
|
@@ -563,7 +567,7 @@ a base58 `mint` (set only when the scan was given `ownerUtxoPublicKey`) and no k
|
|
|
563
567
|
keypair is yours.
|
|
564
568
|
|
|
565
569
|
```ts
|
|
566
|
-
import { address } from "@cloak.dev/sdk";
|
|
570
|
+
import { address, fetchCommitments, NATIVE_SOL_MINT, type Utxo } from "@cloak.dev/sdk";
|
|
567
571
|
|
|
568
572
|
const entries = await fetchCommitments(relayUrl, { mint: NATIVE_SOL_MINT });
|
|
569
573
|
const indexOf = (commitment: bigint) =>
|
|
@@ -602,6 +606,98 @@ the `Utxo` from one as shown under [Timed-out swaps](#timed-out-swaps).
|
|
|
602
606
|
|
|
603
607
|
`toComplianceReport(scan)` and `formatComplianceCsv(report)` turn a scan into a report.
|
|
604
608
|
|
|
609
|
+
## Restore from the Note Index (no chain walk)
|
|
610
|
+
|
|
611
|
+
`scanTransactions` walks the program's signature history and fetches every transaction over RPC:
|
|
612
|
+
thousands of paid calls per wallet. From 0.2.6 the relay also serves a **Note Index**: one bulk,
|
|
613
|
+
unauthenticated, cacheable feed of every confirmed Cloak transaction (encrypted notes, leaves,
|
|
614
|
+
nullifiers), the same bytes for everyone. `recoverNotesFromIndex` downloads all of it, opens each
|
|
615
|
+
item locally under the wallet's keys, rebuilds every listed pool's tree from the index's own leaf
|
|
616
|
+
stream, and checks those trees with exactly **one** RPC request (`getMultipleAccounts` of the pool
|
|
617
|
+
tree accounts). Spent status comes from the nullifiers in the index, so no nullifier of the wallet
|
|
618
|
+
reaches the relay or the RPC, and the only request parameters are the epoch and a page number.
|
|
619
|
+
|
|
620
|
+
It needs a relay that serves `GET /note-index/v1/head` (the 0.2.6 relay with the index enabled); an
|
|
621
|
+
older relay answers 404. Use it for restore after a reinstall, for received private sends, and for
|
|
622
|
+
change made on another device.
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
import {
|
|
626
|
+
address,
|
|
627
|
+
CLOAK_PROGRAM_ID,
|
|
628
|
+
fetchNoteIndexHead,
|
|
629
|
+
NATIVE_SOL_MINT,
|
|
630
|
+
recoverNotesFromIndex,
|
|
631
|
+
type NoteIndexCursor,
|
|
632
|
+
} from "@cloak.dev/sdk";
|
|
633
|
+
|
|
634
|
+
const USDC = address("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
|
|
635
|
+
const USDT = address("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB");
|
|
636
|
+
let savedCursor: NoteIndexCursor | undefined = loadCursor(); // your own private storage
|
|
637
|
+
|
|
638
|
+
const result = await recoverNotesFromIndex({
|
|
639
|
+
relayUrl, // must be on this build's relay allowlist
|
|
640
|
+
connection, // used for ONE getMultipleAccounts
|
|
641
|
+
programId: CLOAK_PROGRAM_ID,
|
|
642
|
+
poolMints: [NATIVE_SOL_MINT, USDC, USDT], // every pool the app uses, not only the ones with notes
|
|
643
|
+
viewingKeyNk: nk,
|
|
644
|
+
ownerUtxoKeypair: owner, // with its real private key
|
|
645
|
+
cursor: savedCursor, // omit for a full sync from page 0
|
|
646
|
+
onProgress: ({ pagesDone, pagesTotal }) => console.log(`${pagesDone}/${pagesTotal}`),
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
result.notes; // verified, unspent, owned by `owner`: ready to spend (leaf index included)
|
|
650
|
+
result.atRisk; // verified, unspent, but spendable or readable by nk holders (legacyExposed: true): migrate
|
|
651
|
+
result.pending; // candidates not verifiable yet; they travel inside the cursor
|
|
652
|
+
result.cursor; // persist it and pass it back next time
|
|
653
|
+
result.stats; // pages, items, bytes, rpcCalls (1), ms, ...
|
|
654
|
+
result.warnings; // non-fatal events, e.g. a re-sync from page 0
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
**What you can trust.** A note is returned only when it opens under the wallet's keys, its
|
|
658
|
+
commitment recomputes from the opened values and the wallet's owner key, and it sits at its leaf in
|
|
659
|
+
a tree the chain authenticates. A dishonest relay can hide items; it cannot inject, alter or
|
|
660
|
+
re-point a note. A hidden nullifier can make a spent note look unspent, so spend restored notes
|
|
661
|
+
through `transact`, `transfer`, `partialWithdraw`, `fullWithdraw` or `swapUtxo`, which run
|
|
662
|
+
`preflightNullifiers` before proving.
|
|
663
|
+
One malformed or crafted entry (anyone can write a note under a public legacy viewing key) is
|
|
664
|
+
skipped and counted in `stats.skipped`, never fatal; only a broken page, hash chain or head fails
|
|
665
|
+
the run.
|
|
666
|
+
|
|
667
|
+
**The cursor.** JSON, no key material, but it records which index items are the wallet's: store it
|
|
668
|
+
as privately as the notes, one per wallet and relay origin, and never hand one wallet's cursor to
|
|
669
|
+
another wallet or another owner key. Write it only after the notes it covers are stored. An epoch
|
|
670
|
+
change, a broken hash chain or an invalid cursor re-syncs from page 0 by itself.
|
|
671
|
+
|
|
672
|
+
**Polling.** Fetch the head (tiny and cacheable) and run a recovery only when it moved:
|
|
673
|
+
|
|
674
|
+
```ts
|
|
675
|
+
const head = await fetchNoteIndexHead(relayUrl);
|
|
676
|
+
const moved = !savedCursor || savedCursor.epoch !== head.epoch.toString(10) || savedCursor.watermark !== head.watermark;
|
|
677
|
+
if (moved) {
|
|
678
|
+
/* recoverNotesFromIndex({ ..., cursor: savedCursor }) */
|
|
679
|
+
}
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
Decide on the head alone. `pending` depends on what the wallet owns (a relay can make it non-zero
|
|
683
|
+
for the owner of one item), so a poll that re-downloads while `pending > 0` tells the relay which
|
|
684
|
+
client owns that item. Pending candidates are retried on the next run the head triggers.
|
|
685
|
+
|
|
686
|
+
**Errors.** `NoteIndexHeadBehindError` is retryable: a cached head from another CDN edge trails the
|
|
687
|
+
cursor; keep the cursor and try again later. `NoteIndexFormatError` means the relay served data that
|
|
688
|
+
did not verify; keep the cursor and report it. A 404 on the head means the relay does not serve the
|
|
689
|
+
index.
|
|
690
|
+
|
|
691
|
+
**Notes from older clients.** Notes whose owner key an SDK before 0.2.6 derived from nk come back in
|
|
692
|
+
`atRisk`. If an older version of your app encrypted chain notes under a different viewing key, pass
|
|
693
|
+
those keys as `legacyViewingKeyNks` (at most 8): they are tried in the same download at no extra
|
|
694
|
+
request, and everything found only through one of them is returned in `atRisk`, never in `notes`.
|
|
695
|
+
A legacy key never opens a delivered note. `legacyCutoffLeaf` (per mint) ignores legacy-shaped notes
|
|
696
|
+
at or after a leaf, for example the first leaf after your app stopped writing them.
|
|
697
|
+
|
|
698
|
+
From a browser the relay answers only the origins its CORS policy allows; servers and native apps
|
|
699
|
+
are not affected.
|
|
700
|
+
|
|
605
701
|
## Nullifiers and spent checks
|
|
606
702
|
|
|
607
703
|
The nullifier export is `computeUtxoNullifier(utxo)`, the circuit's scheme:
|
|
@@ -694,11 +790,13 @@ relay endpoint and the localhost-RPC check.
|
|
|
694
790
|
|
|
695
791
|
## Version and compatibility
|
|
696
792
|
|
|
697
|
-
- SDK 0.2.
|
|
793
|
+
- SDK 0.2.7 pairs with the mainnet program at `zh1eLd6rSphLejbFfJEneUwzHRfMKxgzrgkfwA6qRkW`
|
|
698
794
|
(deployed 2026-08-24) and the 0.2.0 circuit bundle — the program and circuit bundle are unchanged
|
|
699
|
-
since 0.2.0; only the SDK package version has moved.
|
|
700
|
-
|
|
701
|
-
|
|
795
|
+
since 0.2.0; only the SDK package version has moved. The 0.2.5 flows were exercised on mainnet on
|
|
796
|
+
2026-08-25: deposit, private send and withdraw on SOL, USDC and USDT; swaps SOL to USDC and USDT;
|
|
797
|
+
viewing-key discovery followed by a spend; keypair-only deposit recovery. The 0.2.6 API (the
|
|
798
|
+
required `ownerUtxoKeypair`, the relay clock and the Note Index restore) was exercised end to end
|
|
799
|
+
against a local mainnet fork with the 0.2.6 relay, not yet on mainnet.
|
|
702
800
|
- 0.2.2 adds the opt-in `TransactOptions.relaySupplementalAlt` (see [SPL pools](#spl-pools-usdc-usdt)):
|
|
703
801
|
off by default, and existing behavior is unchanged when it is left unset.
|
|
704
802
|
- 0.2.5 moves the SDK from `@solana/web3.js` to `@solana/kit`. The program, the circuit bundle,
|
|
@@ -712,6 +810,9 @@ relay endpoint and the localhost-RPC check.
|
|
|
712
810
|
relay answers them with 401. 0.2.6 also makes nk view-only for notes created from 0.2.6 on, which
|
|
713
811
|
changes several public signatures (`ownerUtxoKeypair` is now required by every spend flow); the
|
|
714
812
|
full list is in [CHANGELOG.md](CHANGELOG.md).
|
|
813
|
+
- 0.2.7 changes no wire format, hash or public signature. Scan results label BRS notes as `BRS`,
|
|
814
|
+
and Cloak's own apps get stricter handling of the internal partner code (see
|
|
815
|
+
[CHANGELOG.md](CHANGELOG.md)).
|
|
715
816
|
- Proofs generated with the 0.1.x circuits are rejected by the mainnet program. SDK 0.1.x builds
|
|
716
817
|
(which ship the 0.1.0 bundle) are not usable against it; upgrade to 0.2.x.
|
|
717
818
|
- The note-based `CloakSDK` class is removed in 0.2.0. It targeted the pre-0.2.0 instruction layout
|