@zbase-protocol/core 0.1.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.
- package/LICENSE +201 -0
- package/NOTICE +20 -0
- package/README.md +118 -0
- package/dist/account.d.ts +79 -0
- package/dist/account.js +140 -0
- package/dist/experimental.d.ts +27 -0
- package/dist/experimental.js +23 -0
- package/dist/facilitatorClient.d.ts +171 -0
- package/dist/facilitatorClient.js +146 -0
- package/dist/forwardingNotes.d.ts +103 -0
- package/dist/forwardingNotes.js +149 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +23 -0
- package/dist/merkle.d.ts +48 -0
- package/dist/merkle.js +84 -0
- package/dist/noteScanner.d.ts +206 -0
- package/dist/noteScanner.js +422 -0
- package/dist/notes.d.ts +305 -0
- package/dist/notes.js +547 -0
- package/dist/npk.d.ts +121 -0
- package/dist/npk.js +166 -0
- package/dist/proofs.d.ts +66 -0
- package/dist/proofs.js +45 -0
- package/dist/stealth.d.ts +136 -0
- package/dist/stealth.js +387 -0
- package/dist/swapPlan.d.ts +52 -0
- package/dist/swapPlan.js +104 -0
- package/dist/types.d.ts +172 -0
- package/dist/types.js +4 -0
- package/dist/viewingKeyHD.d.ts +108 -0
- package/dist/viewingKeyHD.js +176 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Goh Ee Sheng / zBase contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
zBase
|
|
2
|
+
Copyright 2026 Goh Ee Sheng / zBase contributors
|
|
3
|
+
|
|
4
|
+
This product includes software developed by 0xbow
|
|
5
|
+
(https://github.com/0xbow-io/privacy-pools-core), licensed under the
|
|
6
|
+
Apache License, Version 2.0.
|
|
7
|
+
|
|
8
|
+
The Privacy Pools cryptographic primitives and contract patterns
|
|
9
|
+
(commitment/nullifier scheme, association-set Merkle construction, and the
|
|
10
|
+
Groth16 withdrawal circuit lineage) are adapted from that codebase, which
|
|
11
|
+
implements the Privacy Pools research co-authored by Vitalik Buterin.
|
|
12
|
+
|
|
13
|
+
zBase adds, as new work: the x402 payment-facilitator layer, the
|
|
14
|
+
Association Set Provider (ASP) screening + threshold-postman tooling, the
|
|
15
|
+
agent-identity layer, the UTXO/shielded-transfer extension, and a Solana
|
|
16
|
+
(Anchor) port. The Solana program crate name `zx402_privacy_pool` is
|
|
17
|
+
retained for IDL stability; the product brand is zBase.
|
|
18
|
+
|
|
19
|
+
Licensed under the Apache License, Version 2.0. You may obtain a copy of
|
|
20
|
+
the License at http://www.apache.org/licenses/LICENSE-2.0
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @zbase-protocol/core
|
|
2
|
+
|
|
3
|
+
> Chain-agnostic ZK privacy primitives for x402 AI agent payments.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@zbase-protocol/core)
|
|
6
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
7
|
+
|
|
8
|
+
🧪 **Status: testnet.** Currently live against Base Sepolia via the hosted facilitator at [zbase.app](https://zbase.app). Mainnet ships on first signed pilot. The SDK code itself is chain-agnostic — `facilitatorURL` and `networkId` are configurable.
|
|
9
|
+
|
|
10
|
+
## Privacy model (honest)
|
|
11
|
+
|
|
12
|
+
Before you integrate, know exactly what this gives your users.
|
|
13
|
+
|
|
14
|
+
**Provides today (live on Base Sepolia):**
|
|
15
|
+
- **Sender/receiver unlinkability.** The recipient address has no on-chain link to the depositor's wallet. The link is broken by a Groth16 proof of pool membership + a nullifier that marks the note spent.
|
|
16
|
+
- **Compliance-gated privacy.** Only deposits from clean addresses (ASP-approved subset) can withdraw. This is the Vitalik-Buterin-coauthored design, not Tornado Cash.
|
|
17
|
+
|
|
18
|
+
**Does NOT provide today:**
|
|
19
|
+
- **Amount hiding.** `_value` is emitted in plaintext on both `Deposited` and the withdrawal/settle event. Anyone watching the chain sees deposit amounts and withdrawal amounts — only the *link* between them is broken.
|
|
20
|
+
- **Timing-correlation defense.** The decoy scheduler (`scripts/decoy-scheduler.ts`) exists but is not running in prod. A single withdrawal in a quiet window is trivially attributable.
|
|
21
|
+
- **Amount-frequency-attack defense.** A `$99.99` deposit followed by a `$99.99` settle is correlatable.
|
|
22
|
+
|
|
23
|
+
**Roadmap (scaffolded, not deployed):**
|
|
24
|
+
- UTXO notes (`circuits/note_spend.circom`, `createNote` / `splitNote` / `mergeNotes` in this SDK) → true amount hiding. Needs trusted setup ceremony.
|
|
25
|
+
- Threshold-signed ASP (`ThresholdEntrypoint.sol`) → distributed compliance trust.
|
|
26
|
+
- Decoy scheduler in prod → defeats FIFO temporal de-anon.
|
|
27
|
+
|
|
28
|
+
**Anonymity-set size is the privacy metric, not TVL.** The pool can hold 0 USDC and still provide full anonymity to new deposits — the commitment Merkle tree grows on every deposit and never shrinks on withdrawal. Low TVL only blocks *other* users' withdrawals, not your privacy.
|
|
29
|
+
|
|
30
|
+
Full deep-dive: **[zbase.gitbook.io/threat-model](https://zbase.gitbook.io/threat-model)**.
|
|
31
|
+
|
|
32
|
+
zBase is built on Vitalik Buterin's [Privacy Pools research](https://github.com/0xbow-io/privacy-pools-core) (Apache 2.0, audited reference implementation by 0xbow). This package contains the chain-agnostic primitives shared across all chain implementations.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install @zbase-protocol/core
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quickstart
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import {
|
|
44
|
+
generateDepositSecrets,
|
|
45
|
+
computeCommitment,
|
|
46
|
+
computeNullifierHash,
|
|
47
|
+
} from "@zbase-protocol/core";
|
|
48
|
+
|
|
49
|
+
// 1. Generate fresh nullifier + secret for a deposit
|
|
50
|
+
const secrets = generateDepositSecrets();
|
|
51
|
+
|
|
52
|
+
// 2. Compute the commitment hash (what goes on-chain)
|
|
53
|
+
// value is atomic USDC (6 decimals): 1_000_000 = 1 USDC
|
|
54
|
+
const commitment = computeCommitment(
|
|
55
|
+
1_000_000n,
|
|
56
|
+
secrets.nullifier,
|
|
57
|
+
secrets.secret
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// 3. Settle a private payment via the hosted facilitator
|
|
61
|
+
const response = await fetch("https://zbase.app/api/facilitator/settle", {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: { "Content-Type": "application/json" },
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
paymentDetails: {
|
|
66
|
+
scheme: "exact",
|
|
67
|
+
networkId: "eip155:84532", // Base Sepolia
|
|
68
|
+
payTo: "0xProviderAddress",
|
|
69
|
+
maxAmountRequired: "500000", // 0.5 USDC
|
|
70
|
+
},
|
|
71
|
+
zbaseDeposit: { ...secrets, value: "990000", label: "...", commitment: "..." },
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For a full end-to-end test (deposit → settle → withdraw) on Base Sepolia, see [`scripts/integration-sdk-vs-facilitator.mjs`](https://github.com/goheesheng/zBase/blob/main/scripts/integration-sdk-vs-facilitator.mjs).
|
|
77
|
+
|
|
78
|
+
## What's exported
|
|
79
|
+
|
|
80
|
+
| Area | Symbols |
|
|
81
|
+
|---|---|
|
|
82
|
+
| **Account / secrets** | `generateDepositSecrets`, `computeCommitment`, `computePrecommitment`, `computeNullifierHash`, `computeLabel`, `feToBE32`, `SNARK_SCALAR_FIELD`, `createAccount`, `serializeAccount`, `deserializeAccount` |
|
|
83
|
+
| **ZK proofs** | `generateWithdrawalProof`, `verifyProofLocally` (Groth16 via snarkjs) |
|
|
84
|
+
| **Merkle tree** | `buildMerkleTree`, `generateMerkleProof`, `getTreeDepth` (LeanIMT) |
|
|
85
|
+
| **UTXO notes** (scaffold) | `createNote`, `commitmentOf`, `nullifierHashOf`, `splitNote`, `mergeNotes`, `planSpend`, `serializeNote`, `deserializeNote`, `generateViewingKey`, `encryptNote`, `decryptNote`, `scanNotes` |
|
|
86
|
+
| **ERC-5564 stealth** | `generateMetaAddress`, `parseMetaAddress`, `deriveStealthAddress`, `scanForPayments`, `computeStealthPrivateKey`, `STEALTH_SCHEME_ID` |
|
|
87
|
+
| **Privacy scanner** | `analyzeTransfers` |
|
|
88
|
+
|
|
89
|
+
Full TypeScript types exported alongside. 38 named exports total.
|
|
90
|
+
|
|
91
|
+
## Status of the hosted facilitator
|
|
92
|
+
|
|
93
|
+
The default facilitator at `https://zbase.app` runs on **Base Sepolia** today. Mainnet ships on first signed pilot — same code path, env-var flip only. Check live status:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
curl https://zbase.app/api/facilitator/supported
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## How to test
|
|
100
|
+
|
|
101
|
+
Three paths, depending on persona:
|
|
102
|
+
|
|
103
|
+
- **Browser**: [zbase.app/test](https://zbase.app/test) — connect wallet, get Sepolia USDC, deposit + withdraw privately
|
|
104
|
+
- **Agent (curl)**: pure HTTP, no SDK required. See [`/api/skill`](https://zbase.app/api/skill) for AI coding agents
|
|
105
|
+
- **SDK**: this package. See `scripts/integration-sdk-vs-facilitator.mjs` in the repo for a runnable smoke test
|
|
106
|
+
|
|
107
|
+
## Links
|
|
108
|
+
|
|
109
|
+
- 🌐 [zbase.app](https://zbase.app)
|
|
110
|
+
- 📖 [Documentation (GitBook)](https://zbase.gitbook.io)
|
|
111
|
+
- 🐙 [GitHub](https://github.com/goheesheng/zBase)
|
|
112
|
+
- 🐦 [@zbase__](https://twitter.com/zbase__)
|
|
113
|
+
- 🐰 Companion: [@zbase-protocol/svm (Solana)](https://www.npmjs.com/package/@zbase-protocol/svm)
|
|
114
|
+
- 🤖 Companion: [@zbase-protocol/mcp (Claude Desktop / Cursor)](https://www.npmjs.com/package/@zbase-protocol/mcp)
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
Apache 2.0. Built on Vitalik Buterin's [Privacy Pools research](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4563364) and the [0xbow reference implementation](https://github.com/0xbow-io/privacy-pools-core).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zbase-protocol/core — Chain-agnostic account management
|
|
3
|
+
*
|
|
4
|
+
* Hashing matches the upstream Privacy Pools circom circuit exactly:
|
|
5
|
+
* * `precommitment = Poseidon(2)([nullifier, secret])`
|
|
6
|
+
* * `commitment = Poseidon(3)([value, label, precommitment])`
|
|
7
|
+
* * `nullifierHash = Poseidon(1)([nullifier])`
|
|
8
|
+
* * `label = keccak256(scope || nonce_be32) % SNARK_SCALAR_FIELD`
|
|
9
|
+
* See packages/circuits/circuits/commitment.circom in the 0xbow/privacy-pools-core
|
|
10
|
+
* repo for ground truth. Any deviation here breaks proof verification.
|
|
11
|
+
*/
|
|
12
|
+
import type { AccountSecrets, ZX402AccountData, ChainType } from "./types.js";
|
|
13
|
+
export declare const SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
14
|
+
/**
|
|
15
|
+
* Generate a fresh nullifier + secret.
|
|
16
|
+
* The full commitment depends on `value` and `label`, both of which only
|
|
17
|
+
* become known at deposit time — so this returns just the secrets.
|
|
18
|
+
*/
|
|
19
|
+
export declare function generateDepositSecrets(): {
|
|
20
|
+
nullifier: string;
|
|
21
|
+
secret: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* `precommitment = Poseidon(2)([nullifier, secret])`.
|
|
25
|
+
* Sent to the on-chain `deposit` instruction. The pool then computes
|
|
26
|
+
* `commitment = Poseidon(3)([value, label, precommitment])` itself, so the
|
|
27
|
+
* depositor cannot lie about which value/label their commitment claims.
|
|
28
|
+
*/
|
|
29
|
+
export declare function computePrecommitment(nullifier: string, secret: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* `commitment = Poseidon(3)([value, label, precommitment])`. Mirrors the
|
|
32
|
+
* `CommitmentHasher` template in `commitment.circom`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function computeCommitment(value: string | bigint, label: string | bigint, precommitment: string | bigint): string;
|
|
35
|
+
/**
|
|
36
|
+
* `nullifierHash = Poseidon(1)([nullifier])`. Used as the spend marker
|
|
37
|
+
* on-chain (and as PDA seed on Solana). Output is the second public signal
|
|
38
|
+
* of the withdraw circuit.
|
|
39
|
+
*/
|
|
40
|
+
export declare function computeNullifierHash(nullifier: string | bigint): string;
|
|
41
|
+
/**
|
|
42
|
+
* `label = keccak256(scope || nonce_be32) % SNARK_SCALAR_FIELD`. Mirrors the
|
|
43
|
+
* EVM pool's on-chain label derivation and the SVM program's
|
|
44
|
+
* `crypto::compute_label`. Both `scope` and `nonce` are encoded as 32-byte
|
|
45
|
+
* big-endian values to match the on-chain Solana implementation.
|
|
46
|
+
*/
|
|
47
|
+
export declare function computeLabel(scope: string | bigint, nonce: number | bigint): string;
|
|
48
|
+
/**
|
|
49
|
+
* Encode a field element as 32-byte big-endian. Required by the on-chain
|
|
50
|
+
* Solana program (and by `groth16-solana` for proof public inputs).
|
|
51
|
+
*/
|
|
52
|
+
export declare function feToBE32(value: string | bigint): Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Create a full account from secrets + on-chain data.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createAccount(secrets: AccountSecrets, chain: ChainType, depositTxHash?: string): ZX402AccountData;
|
|
57
|
+
/**
|
|
58
|
+
* Serialize account to JSON for storage.
|
|
59
|
+
*/
|
|
60
|
+
export declare function serializeAccount(account: ZX402AccountData): string;
|
|
61
|
+
/**
|
|
62
|
+
* Deserialize account from JSON.
|
|
63
|
+
*/
|
|
64
|
+
export declare function deserializeAccount(json: string): ZX402AccountData;
|
|
65
|
+
/**
|
|
66
|
+
* Generate a uniformly-random field element in the BN254/BN128 scalar field,
|
|
67
|
+
* via REJECTION SAMPLING (audit F7).
|
|
68
|
+
*
|
|
69
|
+
* The naive `random256() % SNARK_SCALAR_FIELD` is biased: 2^256 is not a
|
|
70
|
+
* multiple of the field order, so the lowest values in [0, 2^256 mod r) are
|
|
71
|
+
* slightly more likely. For per-note `nullifier`/`secret` values that bias is a
|
|
72
|
+
* small but real statistical weakness. Rejection sampling — redraw whenever the
|
|
73
|
+
* 256-bit sample is >= the largest multiple of r below 2^256 — yields an exactly
|
|
74
|
+
* uniform result. The reject probability is < ~6e-39 per draw (r is ~2^253.6),
|
|
75
|
+
* so this terminates in one iteration in practice.
|
|
76
|
+
*
|
|
77
|
+
* Uses `crypto.getRandomValues` (CSPRNG), never Math.random.
|
|
78
|
+
*/
|
|
79
|
+
export declare function randomFieldElement(): bigint;
|
package/dist/account.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zbase-protocol/core — Chain-agnostic account management
|
|
3
|
+
*
|
|
4
|
+
* Hashing matches the upstream Privacy Pools circom circuit exactly:
|
|
5
|
+
* * `precommitment = Poseidon(2)([nullifier, secret])`
|
|
6
|
+
* * `commitment = Poseidon(3)([value, label, precommitment])`
|
|
7
|
+
* * `nullifierHash = Poseidon(1)([nullifier])`
|
|
8
|
+
* * `label = keccak256(scope || nonce_be32) % SNARK_SCALAR_FIELD`
|
|
9
|
+
* See packages/circuits/circuits/commitment.circom in the 0xbow/privacy-pools-core
|
|
10
|
+
* repo for ground truth. Any deviation here breaks proof verification.
|
|
11
|
+
*/
|
|
12
|
+
import { poseidon1, poseidon2, poseidon3 } from "poseidon-lite";
|
|
13
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
14
|
+
export const SNARK_SCALAR_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
15
|
+
/**
|
|
16
|
+
* Generate a fresh nullifier + secret.
|
|
17
|
+
* The full commitment depends on `value` and `label`, both of which only
|
|
18
|
+
* become known at deposit time — so this returns just the secrets.
|
|
19
|
+
*/
|
|
20
|
+
export function generateDepositSecrets() {
|
|
21
|
+
return {
|
|
22
|
+
nullifier: randomFieldElement().toString(),
|
|
23
|
+
secret: randomFieldElement().toString(),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* `precommitment = Poseidon(2)([nullifier, secret])`.
|
|
28
|
+
* Sent to the on-chain `deposit` instruction. The pool then computes
|
|
29
|
+
* `commitment = Poseidon(3)([value, label, precommitment])` itself, so the
|
|
30
|
+
* depositor cannot lie about which value/label their commitment claims.
|
|
31
|
+
*/
|
|
32
|
+
export function computePrecommitment(nullifier, secret) {
|
|
33
|
+
return poseidon2([BigInt(nullifier), BigInt(secret)]).toString();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `commitment = Poseidon(3)([value, label, precommitment])`. Mirrors the
|
|
37
|
+
* `CommitmentHasher` template in `commitment.circom`.
|
|
38
|
+
*/
|
|
39
|
+
export function computeCommitment(value, label, precommitment) {
|
|
40
|
+
return poseidon3([BigInt(value), BigInt(label), BigInt(precommitment)]).toString();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* `nullifierHash = Poseidon(1)([nullifier])`. Used as the spend marker
|
|
44
|
+
* on-chain (and as PDA seed on Solana). Output is the second public signal
|
|
45
|
+
* of the withdraw circuit.
|
|
46
|
+
*/
|
|
47
|
+
export function computeNullifierHash(nullifier) {
|
|
48
|
+
return poseidon1([BigInt(nullifier)]).toString();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* `label = keccak256(scope || nonce_be32) % SNARK_SCALAR_FIELD`. Mirrors the
|
|
52
|
+
* EVM pool's on-chain label derivation and the SVM program's
|
|
53
|
+
* `crypto::compute_label`. Both `scope` and `nonce` are encoded as 32-byte
|
|
54
|
+
* big-endian values to match the on-chain Solana implementation.
|
|
55
|
+
*/
|
|
56
|
+
export function computeLabel(scope, nonce) {
|
|
57
|
+
const buf = new Uint8Array(32 + 8);
|
|
58
|
+
feToBE32(BigInt(scope)).forEach((b, i) => (buf[i] = b));
|
|
59
|
+
// 8 bytes big-endian nonce
|
|
60
|
+
let n = BigInt(nonce);
|
|
61
|
+
for (let i = 7; i >= 0; i--) {
|
|
62
|
+
buf[32 + i] = Number(n & 0xffn);
|
|
63
|
+
n >>= 8n;
|
|
64
|
+
}
|
|
65
|
+
const h = keccak_256(buf);
|
|
66
|
+
let x = 0n;
|
|
67
|
+
for (const b of h)
|
|
68
|
+
x = (x << 8n) | BigInt(b);
|
|
69
|
+
return (x % SNARK_SCALAR_FIELD).toString();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Encode a field element as 32-byte big-endian. Required by the on-chain
|
|
73
|
+
* Solana program (and by `groth16-solana` for proof public inputs).
|
|
74
|
+
*/
|
|
75
|
+
export function feToBE32(value) {
|
|
76
|
+
const out = new Uint8Array(32);
|
|
77
|
+
let v = BigInt(value);
|
|
78
|
+
if (v < 0n)
|
|
79
|
+
throw new Error("feToBE32: negative");
|
|
80
|
+
for (let i = 31; i >= 0; i--) {
|
|
81
|
+
out[i] = Number(v & 0xffn);
|
|
82
|
+
v >>= 8n;
|
|
83
|
+
}
|
|
84
|
+
if (v !== 0n)
|
|
85
|
+
throw new Error("feToBE32: value > 2^256");
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Create a full account from secrets + on-chain data.
|
|
90
|
+
*/
|
|
91
|
+
export function createAccount(secrets, chain, depositTxHash) {
|
|
92
|
+
return {
|
|
93
|
+
secrets,
|
|
94
|
+
depositTxHash,
|
|
95
|
+
chain,
|
|
96
|
+
withdrawn: false,
|
|
97
|
+
createdAt: Date.now(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Serialize account to JSON for storage.
|
|
102
|
+
*/
|
|
103
|
+
export function serializeAccount(account) {
|
|
104
|
+
return JSON.stringify(account);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Deserialize account from JSON.
|
|
108
|
+
*/
|
|
109
|
+
export function deserializeAccount(json) {
|
|
110
|
+
return JSON.parse(json);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Generate a uniformly-random field element in the BN254/BN128 scalar field,
|
|
114
|
+
* via REJECTION SAMPLING (audit F7).
|
|
115
|
+
*
|
|
116
|
+
* The naive `random256() % SNARK_SCALAR_FIELD` is biased: 2^256 is not a
|
|
117
|
+
* multiple of the field order, so the lowest values in [0, 2^256 mod r) are
|
|
118
|
+
* slightly more likely. For per-note `nullifier`/`secret` values that bias is a
|
|
119
|
+
* small but real statistical weakness. Rejection sampling — redraw whenever the
|
|
120
|
+
* 256-bit sample is >= the largest multiple of r below 2^256 — yields an exactly
|
|
121
|
+
* uniform result. The reject probability is < ~6e-39 per draw (r is ~2^253.6),
|
|
122
|
+
* so this terminates in one iteration in practice.
|
|
123
|
+
*
|
|
124
|
+
* Uses `crypto.getRandomValues` (CSPRNG), never Math.random.
|
|
125
|
+
*/
|
|
126
|
+
export function randomFieldElement() {
|
|
127
|
+
// Largest multiple of r that fits in 256 bits; samples >= this are rejected.
|
|
128
|
+
const TWO_256 = 1n << 256n;
|
|
129
|
+
const limit = TWO_256 - (TWO_256 % SNARK_SCALAR_FIELD);
|
|
130
|
+
const bytes = new Uint8Array(32);
|
|
131
|
+
for (;;) {
|
|
132
|
+
crypto.getRandomValues(bytes);
|
|
133
|
+
let value = 0n;
|
|
134
|
+
for (const byte of bytes)
|
|
135
|
+
value = (value << 8n) | BigInt(byte);
|
|
136
|
+
if (value < limit)
|
|
137
|
+
return value % SNARK_SCALAR_FIELD;
|
|
138
|
+
// else: in the biased tail — redraw (astronomically rare).
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zbase-protocol/core/experimental — UNFINISHED UTXO surface. NOT PRODUCTION.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ These variable-amount UTXO primitives are a PRE-DEPLOYMENT scaffold. The UTXO
|
|
5
|
+
* pool is not deployed (routes return 501) and the trusted-setup ceremony has not
|
|
6
|
+
* run. The 2026-07-09 SDK audit found integrator fund-lock footguns here:
|
|
7
|
+
* - the note wire format is inconsistent (packCiphertext is length-prefixed but
|
|
8
|
+
* the scanner decodes a fixed bytes32[4]) → a recipient may be unable to
|
|
9
|
+
* decrypt their own note;
|
|
10
|
+
* - createNote defaults NPK material to 0 → a note addressable to nobody;
|
|
11
|
+
* - deriveViewingKeyFromSeed diverges from the mnemonic path for non-64-byte
|
|
12
|
+
* seeds → recovery can silently fail.
|
|
13
|
+
*
|
|
14
|
+
* This surface lives on a SEPARATE subpath (not the main entry) so no integrator
|
|
15
|
+
* reaches it by accident: you must explicitly `import ... from
|
|
16
|
+
* "@zbase-protocol/core/experimental"` and thereby accept it is unfinished. Do
|
|
17
|
+
* NOT rely on the UTXO wire format or these APIs against real funds until the
|
|
18
|
+
* pool + ceremony land and these are re-audited.
|
|
19
|
+
*/
|
|
20
|
+
export type { Note, ViewingKeyPair, EncryptedNote } from "./notes.js";
|
|
21
|
+
export { createNote, createDummyNote, commitmentOf, npkOf, nullifierHashOf, splitNote, mergeNotes, planSpend, consolidateNotes, serializeNote, deserializeNote, generateViewingKey, encryptNote, decryptNote, scanNotes, NoteValueError, commitmentAAD, encryptNoteForTransfer, decryptNoteWithAAD, packCiphertext, unpackCiphertext, CIPHERTEXT_WIRE_VERSION, } from "./notes.js";
|
|
22
|
+
export type { DerivedNPK, DeriveRecipientNPKParams } from "./npk.js";
|
|
23
|
+
export { deriveRecipientNPK, recoverViewingPKBlind, bytesToField } from "./npk.js";
|
|
24
|
+
export type { ViewingKey } from "./viewingKeyHD.js";
|
|
25
|
+
export { ZBASE_VIEWING_KEY_PATH_PREFIX, DEFAULT_MNEMONIC_STRENGTH_BITS, generateNewMnemonic, deriveViewingKeyFromMnemonic, deriveViewingKeyFromSeed, exportMnemonic, mnemonicFromEntropy, isValidMnemonic, } from "./viewingKeyHD.js";
|
|
26
|
+
export type { ScannerEvent, ScannerConnectedEvent, ScannerSpentEvent, ScannerTransferredInEvent, ScannerTransferredOtherEvent, ScannerErrorEvent, ScannerOptions, DecodedTransferredLog, } from "./noteScanner.js";
|
|
27
|
+
export { createScanner, decodeSpentLog, decodeTransferredLog, tryDecryptTransferred, scanTransferredLogs, SPENT_TOPIC, TRANSFERRED_TOPIC, } from "./noteScanner.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zbase-protocol/core/experimental — UNFINISHED UTXO surface. NOT PRODUCTION.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ These variable-amount UTXO primitives are a PRE-DEPLOYMENT scaffold. The UTXO
|
|
5
|
+
* pool is not deployed (routes return 501) and the trusted-setup ceremony has not
|
|
6
|
+
* run. The 2026-07-09 SDK audit found integrator fund-lock footguns here:
|
|
7
|
+
* - the note wire format is inconsistent (packCiphertext is length-prefixed but
|
|
8
|
+
* the scanner decodes a fixed bytes32[4]) → a recipient may be unable to
|
|
9
|
+
* decrypt their own note;
|
|
10
|
+
* - createNote defaults NPK material to 0 → a note addressable to nobody;
|
|
11
|
+
* - deriveViewingKeyFromSeed diverges from the mnemonic path for non-64-byte
|
|
12
|
+
* seeds → recovery can silently fail.
|
|
13
|
+
*
|
|
14
|
+
* This surface lives on a SEPARATE subpath (not the main entry) so no integrator
|
|
15
|
+
* reaches it by accident: you must explicitly `import ... from
|
|
16
|
+
* "@zbase-protocol/core/experimental"` and thereby accept it is unfinished. Do
|
|
17
|
+
* NOT rely on the UTXO wire format or these APIs against real funds until the
|
|
18
|
+
* pool + ceremony land and these are re-audited.
|
|
19
|
+
*/
|
|
20
|
+
export { createNote, createDummyNote, commitmentOf, npkOf, nullifierHashOf, splitNote, mergeNotes, planSpend, consolidateNotes, serializeNote, deserializeNote, generateViewingKey, encryptNote, decryptNote, scanNotes, NoteValueError, commitmentAAD, encryptNoteForTransfer, decryptNoteWithAAD, packCiphertext, unpackCiphertext, CIPHERTEXT_WIRE_VERSION, } from "./notes.js";
|
|
21
|
+
export { deriveRecipientNPK, recoverViewingPKBlind, bytesToField } from "./npk.js";
|
|
22
|
+
export { ZBASE_VIEWING_KEY_PATH_PREFIX, DEFAULT_MNEMONIC_STRENGTH_BITS, generateNewMnemonic, deriveViewingKeyFromMnemonic, deriveViewingKeyFromSeed, exportMnemonic, mnemonicFromEntropy, isValidMnemonic, } from "./viewingKeyHD.js";
|
|
23
|
+
export { createScanner, decodeSpentLog, decodeTransferredLog, tryDecryptTransferred, scanTransferredLogs, SPENT_TOPIC, TRANSFERRED_TOPIC, } from "./noteScanner.js";
|