@shieldedpools/sdk 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +31 -2
- package/dist/address.d.ts +8 -0
- package/dist/address.js +20 -0
- package/dist/connector.d.ts +5 -0
- package/dist/connector.js +73 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/dist/pool.d.ts +57 -0
- package/dist/pool.js +83 -0
- package/package.json +73 -4
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 reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and 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 Additional Liability. 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 additional liability.
|
|
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 [yyyy] [name of copyright owner]
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Shielded Pool SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A browser SDK for Shielded Pool, a 1:1 shielded wrapper for Midnight's unshielded tokens. A connected wallet owns keys, proving, balancing and submission. This SDK builds the contract calls and hands them to the wallet.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @shieldedpools/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { connectorProviders, contractKeys, NIGHT, shield } from "@shieldedpools/sdk";
|
|
13
|
+
|
|
14
|
+
const keys = contractKeys(new URL("/zk/", location.href).href);
|
|
15
|
+
const providers = await connectorProviders(connected, keys);
|
|
16
|
+
|
|
17
|
+
const txId = await shield(providers, contractAddress, NIGHT, 1_000_000n);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`connected` is the wallet's connector API, version 4.1. Amounts are `bigint`s in a token's smallest units. `unshield` takes a recipient from `parseUnshieldedAddress`, `locked` reads what a contract holds, `isImmutable` checks what `maintenanceAuthority` returns, and `wrappedColor` names a wrapped token for the wallet's balances and transfers.
|
|
21
|
+
|
|
22
|
+
Your build needs:
|
|
23
|
+
|
|
24
|
+
- A `Buffer` global, set before the SDK loads.
|
|
25
|
+
- WebAssembly support. With Vite, that's `vite-plugin-wasm` and a `build.target` of `esnext`.
|
|
26
|
+
- The `compiler`, `keys` and `zkir` directories from `@shieldedpools/contracts/contract/managed/shielded-pools`, served at the URL you pass to `contractKeys`.
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
Copyright 2026 The Shielded Pool Authors. Apache-2.0: see [`LICENSE`](LICENSE). Contributions are made under the same license.
|
|
31
|
+
|
|
32
|
+
The license doesn't cover the Shielded Pool names or logos. Don't imply that your project is official or endorsed, and give a modified version its own name.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The two keys a shielded address is made of, in hex. */
|
|
2
|
+
export interface ShieldedKeys {
|
|
3
|
+
coinPublicKey: string;
|
|
4
|
+
encryptionPublicKey: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function parseShieldedAddress(text: string, networkId: string): ShieldedKeys;
|
|
7
|
+
/** The 32 bytes a contract sees as this address's `UserAddress`. */
|
|
8
|
+
export declare function parseUnshieldedAddress(text: string, networkId: string): Uint8Array;
|
package/dist/address.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { bech32m, hex } from "@scure/base";
|
|
2
|
+
function decode(text, type, networkId, length) {
|
|
3
|
+
const { prefix, bytes } = bech32m.decodeToBytes(text, false);
|
|
4
|
+
const expected = networkId === "mainnet" ? `mn_${type}` : `mn_${type}_${networkId}`;
|
|
5
|
+
if (prefix !== expected || bytes.length !== length) {
|
|
6
|
+
throw new Error(`Not a ${type} address on ${networkId}: ${text}`);
|
|
7
|
+
}
|
|
8
|
+
return bytes;
|
|
9
|
+
}
|
|
10
|
+
export function parseShieldedAddress(text, networkId) {
|
|
11
|
+
const bytes = decode(text, "shield-addr", networkId, 64);
|
|
12
|
+
return {
|
|
13
|
+
coinPublicKey: hex.encode(bytes.subarray(0, 32)),
|
|
14
|
+
encryptionPublicKey: hex.encode(bytes.subarray(32)),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** The 32 bytes a contract sees as this address's `UserAddress`. */
|
|
18
|
+
export function parseUnshieldedAddress(text, networkId) {
|
|
19
|
+
return decode(text, "addr", networkId, 32);
|
|
20
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ConnectedAPI } from "@midnightntwrk/dapp-connector-api";
|
|
2
|
+
import type { ContractProviders } from "@midnight-ntwrk/midnight-js-contracts";
|
|
3
|
+
import { type ZKConfigProvider } from "@midnight-ntwrk/midnight-js-types";
|
|
4
|
+
/** Providers for `shield`, `unshield` and `attach` that go through a connected wallet. */
|
|
5
|
+
export declare function connectorProviders(connected: ConnectedAPI, zkConfigProvider: ZKConfigProvider<string>): Promise<ContractProviders>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as ledger from "@midnightntwrk/ledger-v9";
|
|
2
|
+
import { dappConnectorProofProvider } from "@midnight-ntwrk/midnight-js-dapp-connector-proof-provider";
|
|
3
|
+
import { indexerPublicDataProvider } from "@midnight-ntwrk/midnight-js-indexer-public-data-provider";
|
|
4
|
+
import { createMidnightProvider, createWalletProvider, } from "@midnight-ntwrk/midnight-js-types";
|
|
5
|
+
import { hex } from "@scure/base";
|
|
6
|
+
import { parseShieldedAddress } from "./address.js";
|
|
7
|
+
// A connected wallet holds the keys, proves, balances and signs. This side never sees a seed or a
|
|
8
|
+
// secret key, and only reaches the wallet through the connector API.
|
|
9
|
+
const TX_TTL_SECONDS = 30 * 60;
|
|
10
|
+
const toHex = (tx) => hex.encode(tx.serialize());
|
|
11
|
+
function deserializeSealed(tx) {
|
|
12
|
+
return ledger.Transaction.deserialize("signature", "proof", "binding", hex.decode(tx));
|
|
13
|
+
}
|
|
14
|
+
// midnight-js wants a transaction's id, but the connector's submit returns nothing.
|
|
15
|
+
function transactionId(tx) {
|
|
16
|
+
const id = tx.identifiers().at(-1);
|
|
17
|
+
if (id === undefined)
|
|
18
|
+
throw new Error("The wallet returned a transaction with no identifier");
|
|
19
|
+
return id;
|
|
20
|
+
}
|
|
21
|
+
async function submit(connected, tx) {
|
|
22
|
+
await connected.submitTransaction(toHex(tx));
|
|
23
|
+
return transactionId(tx);
|
|
24
|
+
}
|
|
25
|
+
// The contract has no witnesses, so its private state is always empty. midnight-js also makes a
|
|
26
|
+
// signing key whenever it attaches, but the contract has no maintenance committee for it to be on,
|
|
27
|
+
// so it controls nothing and doesn't need to outlive this object. A connected wallet never deploys.
|
|
28
|
+
function emptyPrivateState() {
|
|
29
|
+
const states = new Map();
|
|
30
|
+
const signingKeys = new Map();
|
|
31
|
+
const unsupported = async () => {
|
|
32
|
+
throw new Error("A connected wallet has no private state to export or import");
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
setContractAddress: () => undefined,
|
|
36
|
+
set: async (id, state) => void states.set(id, state),
|
|
37
|
+
get: async (id) => states.get(id) ?? null,
|
|
38
|
+
remove: async (id) => void states.delete(id),
|
|
39
|
+
clear: async () => states.clear(),
|
|
40
|
+
setSigningKey: async (address, key) => void signingKeys.set(address, key),
|
|
41
|
+
getSigningKey: async (address) => signingKeys.get(address) ?? null,
|
|
42
|
+
removeSigningKey: async (address) => void signingKeys.delete(address),
|
|
43
|
+
clearSigningKeys: async () => signingKeys.clear(),
|
|
44
|
+
exportPrivateStates: unsupported,
|
|
45
|
+
importPrivateStates: unsupported,
|
|
46
|
+
exportSigningKeys: unsupported,
|
|
47
|
+
importSigningKeys: unsupported,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Providers for `shield`, `unshield` and `attach` that go through a connected wallet. */
|
|
51
|
+
export async function connectorProviders(connected, zkConfigProvider) {
|
|
52
|
+
const { indexerUri, indexerWsUri, networkId } = await connected.getConfiguration();
|
|
53
|
+
// The connector gives its public keys in Bech32m and midnight-js wants hex.
|
|
54
|
+
const { shieldedAddress } = await connected.getShieldedAddresses();
|
|
55
|
+
const keys = parseShieldedAddress(shieldedAddress, networkId);
|
|
56
|
+
const costModel = ledger.CostModel.initialCostModel();
|
|
57
|
+
return {
|
|
58
|
+
config: { networkId, ttlSeconds: TX_TTL_SECONDS },
|
|
59
|
+
privateStateProvider: emptyPrivateState(),
|
|
60
|
+
publicDataProvider: indexerPublicDataProvider(indexerUri, indexerWsUri),
|
|
61
|
+
zkConfigProvider,
|
|
62
|
+
proofProvider: await dappConnectorProofProvider(connected, zkConfigProvider, costModel),
|
|
63
|
+
walletProvider: createWalletProvider({
|
|
64
|
+
getCoinPublicKey: () => keys.coinPublicKey,
|
|
65
|
+
getEncryptionPublicKey: () => keys.encryptionPublicKey,
|
|
66
|
+
balanceTx: async (tx) => {
|
|
67
|
+
const balanced = await connected.balanceUnsealedTransaction(toHex(tx));
|
|
68
|
+
return deserializeSealed(balanced.tx);
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
midnightProvider: createMidnightProvider((tx) => submit(connected, tx)),
|
|
72
|
+
};
|
|
73
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The published SDK. A connected wallet owns keys, proving, balancing and submission, so nothing
|
|
2
|
+
// here may need Node, a seed or a filesystem.
|
|
3
|
+
export { parseUnshieldedAddress } from "./address.js";
|
|
4
|
+
export { connectorProviders } from "./connector.js";
|
|
5
|
+
export { contractKeys, isImmutable, locked, maintenanceAuthority, NIGHT, shield, unshield, wrappedColor, } from "./pool.js";
|
package/dist/pool.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { CompiledContract, type Contract as CompactContract } from "@midnight-ntwrk/compact-js";
|
|
2
|
+
import { type ContractProviders } from "@midnight-ntwrk/midnight-js-contracts";
|
|
3
|
+
import type { ZKConfigProvider } from "@midnight-ntwrk/midnight-js-types";
|
|
4
|
+
import { type Circuits } from "@shieldedpools/contracts";
|
|
5
|
+
/** NIGHT's colour. Tokens are identified by colour, as 64 hex characters. */
|
|
6
|
+
export declare const NIGHT: string;
|
|
7
|
+
export declare const compiled: CompiledContract.CompiledContract<CompactContract.Any, any, never>;
|
|
8
|
+
export declare const PRIVATE_STATE_ID = "shielded-pool";
|
|
9
|
+
/**
|
|
10
|
+
* Keys and ZKIR for `connectorProviders`, fetched from `baseUrl`, an absolute URL. It has to serve
|
|
11
|
+
* the contract's `compiler`, `keys` and `zkir` directories, and is checked against this release.
|
|
12
|
+
*/
|
|
13
|
+
export declare function contractKeys(baseUrl: string): ZKConfigProvider<string>;
|
|
14
|
+
/**
|
|
15
|
+
* The two circuits, narrowed from the SDK's Contract.Any view. Their arguments come from the
|
|
16
|
+
* generated types, so a change to a circuit's signature fails to compile here.
|
|
17
|
+
*/
|
|
18
|
+
interface WrapperCircuits {
|
|
19
|
+
shield(...args: CircuitArgs<"shield">): Promise<CallResult>;
|
|
20
|
+
unshield(...args: CircuitArgs<"unshield">): Promise<CallResult>;
|
|
21
|
+
}
|
|
22
|
+
type CircuitArgs<K extends keyof Circuits<unknown>> = Parameters<Circuits<unknown>[K]> extends [unknown, ...infer Args] ? Args : never;
|
|
23
|
+
type CallResult = {
|
|
24
|
+
public: {
|
|
25
|
+
txId: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** The colour of `token`'s wrapped token. It must match `tokenType()` in the contract. */
|
|
29
|
+
export declare function wrappedColor(contractAddress: string, token: string): string;
|
|
30
|
+
export declare function attach(providers: ContractProviders, contractAddress: string): Promise<WrapperCircuits>;
|
|
31
|
+
/** Locks `amount` units of `token` and mints the same amount of its wrapped token to this wallet. */
|
|
32
|
+
export declare function shield(providers: ContractProviders, contractAddress: string, token: string, amount: bigint): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Pays `amount` units of `token`'s wrapped token into the contract, where they are retired,
|
|
35
|
+
* and releases the same amount of `token` to `recipient`.
|
|
36
|
+
*
|
|
37
|
+
* The coin is fresh: the wallet funds it from whatever wrapped coins it holds and
|
|
38
|
+
* keeps the change, so the amount does not have to match an earlier `shield`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function unshield(providers: ContractProviders, contractAddress: string, token: string, amount: bigint, recipient: Uint8Array): Promise<string>;
|
|
41
|
+
export interface Authority {
|
|
42
|
+
committee: string[];
|
|
43
|
+
threshold: number;
|
|
44
|
+
counter: bigint;
|
|
45
|
+
}
|
|
46
|
+
/** Who can replace this contract's circuits, and with them take everything it holds. */
|
|
47
|
+
export declare function maintenanceAuthority(providers: ContractProviders, contractAddress: string): Promise<Authority>;
|
|
48
|
+
/**
|
|
49
|
+
* Whether nobody can ever change this contract. An update needs `threshold` signatures from
|
|
50
|
+
* different committee positions, so a threshold above the committee's size can't be met. An empty
|
|
51
|
+
* committee isn't enough: the ledger accepts a threshold of 0, and then anyone can change the
|
|
52
|
+
* contract without signing.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isImmutable({ committee, threshold }: Authority): boolean;
|
|
55
|
+
/** The `token` backing its wrapped token, read from the contract's public balance. */
|
|
56
|
+
export declare function locked(providers: ContractProviders, contractAddress: string, token: string): Promise<bigint>;
|
|
57
|
+
export {};
|
package/dist/pool.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { CompiledContract } from "@midnight-ntwrk/compact-js";
|
|
2
|
+
import { findDeployedContract, getUnshieldedBalances, } from "@midnight-ntwrk/midnight-js-contracts";
|
|
3
|
+
import { createShieldedCoinInfo, encodeShieldedCoinInfo, nativeToken, rawTokenType, } from "@midnightntwrk/ledger-v9";
|
|
4
|
+
import { FetchZkConfigProvider } from "@midnight-ntwrk/midnight-js-fetch-zk-config-provider";
|
|
5
|
+
import { Contract } from "@shieldedpools/contracts";
|
|
6
|
+
import { hex } from "@scure/base";
|
|
7
|
+
/** NIGHT's colour. Tokens are identified by colour, as 64 hex characters. */
|
|
8
|
+
export const NIGHT = nativeToken().raw;
|
|
9
|
+
// The generated class and the SDK's contract type disagree on circuit shapes, so
|
|
10
|
+
// the SDK's generics are bound to Contract.Any here and nowhere else. Proofs get their keys from
|
|
11
|
+
// the providers: nothing reads the assets path, but the type requires one.
|
|
12
|
+
export const compiled = CompiledContract.withCompiledFileAssets(CompiledContract.withVacantWitnesses(CompiledContract.make("shielded-pools", Contract)), "shielded-pools");
|
|
13
|
+
export const PRIVATE_STATE_ID = "shielded-pool";
|
|
14
|
+
// SHA-256 of the contract's compiler/contract-manifest.json, which lists every key and ZKIR file
|
|
15
|
+
// by hash. Pinning it here means whoever serves those files can't swap them, even with a matching
|
|
16
|
+
// manifest. A unit test fails if this drifts from the contracts package this SDK is locked to.
|
|
17
|
+
const CONTRACT_MANIFEST_SHA256 = "6b939d17f4ebd6d985d28aa76b252058cb851994916fac09f9abef591477183e";
|
|
18
|
+
/**
|
|
19
|
+
* Keys and ZKIR for `connectorProviders`, fetched from `baseUrl`, an absolute URL. It has to serve
|
|
20
|
+
* the contract's `compiler`, `keys` and `zkir` directories, and is checked against this release.
|
|
21
|
+
*/
|
|
22
|
+
export function contractKeys(baseUrl) {
|
|
23
|
+
return new FetchZkConfigProvider(baseUrl, {
|
|
24
|
+
expectedManifestHash: CONTRACT_MANIFEST_SHA256,
|
|
25
|
+
fetchFunc: globalThis.fetch.bind(globalThis),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** The colour of `token`'s wrapped token. It must match `tokenType()` in the contract. */
|
|
29
|
+
export function wrappedColor(contractAddress, token) {
|
|
30
|
+
return rawTokenType(hex.decode(token), contractAddress);
|
|
31
|
+
}
|
|
32
|
+
export async function attach(providers, contractAddress) {
|
|
33
|
+
const found = await findDeployedContract(providers, {
|
|
34
|
+
compiledContract: compiled,
|
|
35
|
+
contractAddress,
|
|
36
|
+
privateStateId: PRIVATE_STATE_ID,
|
|
37
|
+
initialPrivateState: {},
|
|
38
|
+
});
|
|
39
|
+
return found.callTx;
|
|
40
|
+
}
|
|
41
|
+
/** Locks `amount` units of `token` and mints the same amount of its wrapped token to this wallet. */
|
|
42
|
+
export async function shield(providers, contractAddress, token, amount) {
|
|
43
|
+
const contract = await attach(providers, contractAddress);
|
|
44
|
+
const nonce = crypto.getRandomValues(new Uint8Array(32));
|
|
45
|
+
const result = await contract.shield(hex.decode(token), amount, nonce);
|
|
46
|
+
return result.public.txId;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Pays `amount` units of `token`'s wrapped token into the contract, where they are retired,
|
|
50
|
+
* and releases the same amount of `token` to `recipient`.
|
|
51
|
+
*
|
|
52
|
+
* The coin is fresh: the wallet funds it from whatever wrapped coins it holds and
|
|
53
|
+
* keeps the change, so the amount does not have to match an earlier `shield`.
|
|
54
|
+
*/
|
|
55
|
+
export async function unshield(providers, contractAddress, token, amount, recipient) {
|
|
56
|
+
const contract = await attach(providers, contractAddress);
|
|
57
|
+
const color = wrappedColor(contractAddress, token);
|
|
58
|
+
const coin = encodeShieldedCoinInfo(createShieldedCoinInfo(color, amount));
|
|
59
|
+
const result = await contract.unshield(hex.decode(token), coin, { bytes: recipient });
|
|
60
|
+
return result.public.txId;
|
|
61
|
+
}
|
|
62
|
+
/** Who can replace this contract's circuits, and with them take everything it holds. */
|
|
63
|
+
export async function maintenanceAuthority(providers, contractAddress) {
|
|
64
|
+
const contract = await providers.publicDataProvider.queryContractState(contractAddress);
|
|
65
|
+
if (contract?.version !== "v9")
|
|
66
|
+
throw new Error(`No ledger 9 contract at ${contractAddress}`);
|
|
67
|
+
const { committee, threshold, counter } = contract.state.maintenanceAuthority;
|
|
68
|
+
return { committee: committee.map((key) => key.value), threshold, counter };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether nobody can ever change this contract. An update needs `threshold` signatures from
|
|
72
|
+
* different committee positions, so a threshold above the committee's size can't be met. An empty
|
|
73
|
+
* committee isn't enough: the ledger accepts a threshold of 0, and then anyone can change the
|
|
74
|
+
* contract without signing.
|
|
75
|
+
*/
|
|
76
|
+
export function isImmutable({ committee, threshold }) {
|
|
77
|
+
return threshold > committee.length;
|
|
78
|
+
}
|
|
79
|
+
/** The `token` backing its wrapped token, read from the contract's public balance. */
|
|
80
|
+
export async function locked(providers, contractAddress, token) {
|
|
81
|
+
const held = await getUnshieldedBalances(providers.publicDataProvider, contractAddress);
|
|
82
|
+
return held.find(({ tokenType }) => tokenType === token)?.balance ?? 0n;
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shieldedpools/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A browser SDK for Shielded Pool contract calls through a connected Midnight wallet.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/shieldedpools/shielded-pool-sdk.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=24"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.build.json",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"test": "node --disable-warning=ExperimentalWarning --test test/unit/*.test.ts",
|
|
31
|
+
"test:live": "compact compile +0.35.0 test/e2e/fixtures/minter.compact test/e2e/managed/minter && node --disable-warning=ExperimentalWarning --test --test-force-exit test/e2e/live.test.ts",
|
|
32
|
+
"test:live:connector": "E2E_CONNECTOR=1 npm run test:live",
|
|
33
|
+
"network:up": "docker compose up -d --wait",
|
|
34
|
+
"network:down": "docker compose down",
|
|
35
|
+
"prepublishOnly": "npm run build && npm test",
|
|
36
|
+
"release": "npm run build && npm test && mkdir -p release && npm pack --pack-destination release",
|
|
37
|
+
"test:browser": "npm run build && node --disable-warning=ExperimentalWarning test/browser/run.ts"
|
|
38
|
+
},
|
|
39
|
+
"overrides": {
|
|
40
|
+
"effect": "3.22.4"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@midnight-ntwrk/compact-js": "3.0.0-rc.3",
|
|
44
|
+
"@midnight-ntwrk/midnight-js-contracts": "5.0.0-rc.5",
|
|
45
|
+
"@midnight-ntwrk/midnight-js-dapp-connector-proof-provider": "5.0.0-rc.5",
|
|
46
|
+
"@midnight-ntwrk/midnight-js-fetch-zk-config-provider": "5.0.0-rc.5",
|
|
47
|
+
"@midnight-ntwrk/midnight-js-indexer-public-data-provider": "5.0.0-rc.5",
|
|
48
|
+
"@midnight-ntwrk/midnight-js-types": "5.0.0-rc.5",
|
|
49
|
+
"@midnightntwrk/dapp-connector-api": "4.1.0-beta.1",
|
|
50
|
+
"@midnightntwrk/ledger-v9": "1.0.0-rc.5",
|
|
51
|
+
"@scure/base": "2.4.0",
|
|
52
|
+
"@shieldedpools/contracts": "0.1.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@midnight-ntwrk/compact-runtime": "0.20.0",
|
|
56
|
+
"@midnight-ntwrk/midnight-js-http-client-proof-provider": "5.0.0-rc.5",
|
|
57
|
+
"@midnight-ntwrk/midnight-js-level-private-state-provider": "5.0.0-rc.5",
|
|
58
|
+
"@midnight-ntwrk/midnight-js-node-zk-config-provider": "5.0.0-rc.5",
|
|
59
|
+
"@midnightntwrk/wallet-sdk-abstractions": "3.0.0-rc.1",
|
|
60
|
+
"@midnightntwrk/wallet-sdk-address-format": "4.0.0-rc.0",
|
|
61
|
+
"@midnightntwrk/wallet-sdk-dust-wallet": "5.0.0-rc.1",
|
|
62
|
+
"@midnightntwrk/wallet-sdk-facade": "5.0.0-rc.1",
|
|
63
|
+
"@midnightntwrk/wallet-sdk-hd": "3.1.0-rc.0",
|
|
64
|
+
"@midnightntwrk/wallet-sdk-shielded": "4.0.0-rc.1",
|
|
65
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet": "4.0.0-rc.1",
|
|
66
|
+
"@types/node": "24.19.2",
|
|
67
|
+
"@types/ws": "8.18.2",
|
|
68
|
+
"buffer": "6.0.3",
|
|
69
|
+
"rxjs": "7.8.2",
|
|
70
|
+
"typescript": "5.9.3",
|
|
71
|
+
"vite": "8.3.5",
|
|
72
|
+
"vite-plugin-wasm": "3.6.0",
|
|
73
|
+
"ws": "8.22.0"
|
|
74
|
+
}
|
|
75
|
+
}
|