@miden-sdk/miden-sdk 0.13.0-next.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/README.md +194 -0
- package/dist/Cargo-15e14c5a.js +20611 -0
- package/dist/Cargo-15e14c5a.js.map +1 -0
- package/dist/assets/miden_client_web.wasm +0 -0
- package/dist/crates/miden_client_web.d.ts +1826 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.js +774 -0
- package/dist/index.js.map +1 -0
- package/dist/wasm.js +13 -0
- package/dist/wasm.js.map +1 -0
- package/dist/workers/Cargo-15e14c5a-15e14c5a.js +20611 -0
- package/dist/workers/Cargo-15e14c5a-15e14c5a.js.map +1 -0
- package/dist/workers/assets/miden_client_web.wasm +0 -0
- package/dist/workers/web-client-methods-worker.js +322 -0
- package/dist/workers/web-client-methods-worker.js.map +1 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# @miden-sdk/miden-sdk
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The `@miden-sdk/miden-sdk` is a comprehensive software development toolkit (SDK) for interacting with the Miden blockchain and virtual machine from within a web application. It provides developers with everything needed to:
|
|
6
|
+
|
|
7
|
+
- Interact with the Miden chain (e.g. syncing accounts, submitting transactions)
|
|
8
|
+
- Create and manage Miden transactions
|
|
9
|
+
- Run the Miden VM to execute programs
|
|
10
|
+
- Generate zero-knowledge proofs using the Miden Prover (with support for delegated proving)
|
|
11
|
+
- Integrate Miden capabilities seamlessly into browser-based environments
|
|
12
|
+
|
|
13
|
+
Whether you're building a wallet, dApp, or other blockchain-integrated application, this SDK provides the core functionality to bridge your frontend with Miden's powerful ZK architecture.
|
|
14
|
+
|
|
15
|
+
> **Note:** This README provides a high-level overview of the web client SDK.
|
|
16
|
+
> For more detailed documentation, API references, and usage examples, see the documentation [here](../../docs/src/web-client) (TBD).
|
|
17
|
+
|
|
18
|
+
### SDK Structure and Build Process
|
|
19
|
+
|
|
20
|
+
This SDK is published as an NPM package, built from the `web-client` crate. The `web-client` crate is a Rust crate targeting WebAssembly (WASM), and it uses `wasm-bindgen` to generate JavaScript bindings. It depends on the lower-level `rust-client` crate, which implements the core functionality for interacting with the Miden chain.
|
|
21
|
+
|
|
22
|
+
Both a `Cargo.toml` and a `package.json` are present in the `web-client` directory to support Rust compilation and NPM packaging respectively.
|
|
23
|
+
|
|
24
|
+
The build process is powered by a custom `rollup.config.js` file, which orchestrates three main steps:
|
|
25
|
+
|
|
26
|
+
1. **WASM Module Build**: Compiles the `web-client` Rust crate into a WASM module using `@wasm-tool/rollup-plugin-rust`, enabling WebAssembly features such as atomics and bulk memory operations.
|
|
27
|
+
|
|
28
|
+
2. **Worker Build**: Bundles a dedicated web worker file that enables off-main-thread execution for computationally intensive functions.
|
|
29
|
+
|
|
30
|
+
3. **Main Entry Point Build**: Bundles the top-level JavaScript module (`index.js`) which serves as the main API surface for consumers of the SDK. This module also imports `wasm.js`, which
|
|
31
|
+
provides a function to load the wasm module in an async way. Since there's a [known issue](https://github.com/wasm-tool/rollup-plugin-rust?tab=readme-ov-file#usage-with-vite)
|
|
32
|
+
with vite, there's a check to avoid loading the wasm module when SSR is enabled.
|
|
33
|
+
|
|
34
|
+
This setup allows the SDK to be seamlessly consumed in JavaScript environments, particularly in web applications.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
### Stable Version
|
|
39
|
+
|
|
40
|
+
A non-stable version of the SDK is also maintained, which tracks the `next` branch of the Miden client repository (essentially the development branch). To install the pre-release version, run:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
npm i @miden-sdk/miden-sdk
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or using Yarn:
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
yarn add @miden-sdk/miden-sdk
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Pre-release ("next") Version
|
|
53
|
+
|
|
54
|
+
A non-stable version is also maintained. To install the pre-release version, run:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
npm i @miden-sdk/miden-sdk@next
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Or with Yarn:
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
yarn add @miden-sdk/miden-sdk@next
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
> **Note:** The `next` version of the SDK must be used in conjunction with a locally running Miden node built from the `next` branch of the `miden-node` repository. This is necessary because the public testnet runs the stable `main` branch, which may not be compatible with the latest development features in `next`. Instructions to run a local node can be found [here](https://github.com/0xMiden/miden-node/tree/next) on the `next` branch of the `miden-node` repository. Additionally, if you plan to leverage delegated proving in your application, you may need to run a local prover (see [Remote prover instructions](https://github.com/0xMiden/miden-node/tree/next/bin/remote-prover)).
|
|
67
|
+
|
|
68
|
+
## Building and Testing the Web Client
|
|
69
|
+
|
|
70
|
+
If you're interested in contributing to the web client and need to build it locally, you can do so via:
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
yarn install
|
|
74
|
+
yarn build
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
This will:
|
|
78
|
+
|
|
79
|
+
- Install all JavaScript dependencies,
|
|
80
|
+
- Compile the Rust code to WebAssembly,
|
|
81
|
+
- Generate the JavaScript bindings via wasm-bindgen,
|
|
82
|
+
- And bundle the SDK into the dist/ directory using Rollup.
|
|
83
|
+
|
|
84
|
+
To run integration tests after building, use:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
yarn test
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
This runs a suite of integration tests to verify the SDK’s functionality in a web context.
|
|
91
|
+
|
|
92
|
+
### Building the npm package
|
|
93
|
+
|
|
94
|
+
Follow the steps below to produce the contents that get published to npm (`dist/` plus the license file). All commands are executed from `crates/web-client`.
|
|
95
|
+
|
|
96
|
+
1. **Install prerequisites**
|
|
97
|
+
- Install the Rust toolchain version specified in `rust-toolchain.toml`.
|
|
98
|
+
- Install Node.js ≥18 and Yarn.
|
|
99
|
+
2. **Install dependencies**
|
|
100
|
+
```bash
|
|
101
|
+
yarn install
|
|
102
|
+
```
|
|
103
|
+
This installs both the JavaScript tooling and the `@wasm-tool/rollup-plugin-rust` dependency that compiles the Rust crate.
|
|
104
|
+
3. **Build the package**
|
|
105
|
+
```bash
|
|
106
|
+
yarn build
|
|
107
|
+
```
|
|
108
|
+
The `build` script (see `package.json`) performs the following:
|
|
109
|
+
- Removes the previous `dist/` directory (`rimraf dist`).
|
|
110
|
+
- Runs `npm run build-rust-client-js`, which builds the `idxdb-store` TypeScript helper that the SDK imports.
|
|
111
|
+
- Invokes Rollup with `RUSTFLAGS="--cfg getrandom_backend=\"wasm_js\""` so the Rust `getrandom` crate targets browser entropy and so that atomics/bulk-memory WebAssembly features are enabled.
|
|
112
|
+
- Copies the generated TypeScript declarations from `js/types` into `dist/`.
|
|
113
|
+
- Executes `node clean.js` to strip paths from the generated `.js` files, leaving only the artifacts needed on npm.
|
|
114
|
+
4. **Inspect the artifacts**
|
|
115
|
+
- `dist/index.js` is the ESM entry point referenced by `"main"`/`"browser"`/`"exports"`.
|
|
116
|
+
- `dist/index.d.ts` and the rest of the `.d.ts` files provide the TypeScript surface.
|
|
117
|
+
Use `npm pack` if you want to preview the exact tarball that would be published.
|
|
118
|
+
|
|
119
|
+
> Tip: during development you can set `MIDEN_WEB_DEV=true` before running `yarn build` (or run `npm run build-dev`) to skip the clean step and keep extra debugging metadata in the bundled output. This debugging metadata also includes debug symbols for the generated wasm binary
|
|
120
|
+
|
|
121
|
+
### Checking the generated TypeScript bindings
|
|
122
|
+
|
|
123
|
+
The script at `crates/web-client/scripts/check-bindgen-types.js` verifies that every type exported by the generated wasm bindings (`dist/crates/miden_client_web.d.ts`) is re-exported from the public declarations (`js/types/index.d.ts`). Run it after a build with:
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
yarn check:wasm-types
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`WebClient` is intentionally excluded because the wrapper defines its own implementation. If the check reports missing exports, update `js/types/index.d.ts` so consumers get the full generated surface.
|
|
130
|
+
|
|
131
|
+
## Usage
|
|
132
|
+
|
|
133
|
+
The following are just a few simple examples to get started. For more details, see the [API Reference](../../docs/typedoc/web-client/README.md).
|
|
134
|
+
|
|
135
|
+
### Create a New Wallet
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { AccountStorageMode, WebClient } from "@miden-sdk/miden-sdk";
|
|
139
|
+
|
|
140
|
+
// Instantiate web client object
|
|
141
|
+
const webClient = await WebClient.createClient();
|
|
142
|
+
|
|
143
|
+
// Set up newWallet params
|
|
144
|
+
const accountStorageMode = AccountStorageMode.private();
|
|
145
|
+
const mutable = true;
|
|
146
|
+
|
|
147
|
+
// Create new wallet
|
|
148
|
+
const account = await webClient.newWallet(accountStorageMode, mutable);
|
|
149
|
+
|
|
150
|
+
console.log(account.id().toString()); // account id as hex
|
|
151
|
+
console.log(account.isPublic()); // false
|
|
152
|
+
console.log(account.isPrivate()); // true
|
|
153
|
+
console.log(account.isNetwork()); // false
|
|
154
|
+
console.log(account.isFaucet()); // false
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Create and Execute a New Consume Transaction
|
|
158
|
+
|
|
159
|
+
Using https://faucet.testnet.miden.io/, send some public test tokens using the account id logged during the new wallet creation. Consume these tokens like this:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
// Once the faucet finishes minting the tokens, you need to call syncState() so the client knows there is a note available to be consumed. In an actual application, this may need to be in a loop to constantly discover claimable notes.
|
|
163
|
+
await webClient.syncState();
|
|
164
|
+
|
|
165
|
+
// Query the client for consumable notes, and retrieve the id of the new note to be consumed
|
|
166
|
+
let consumableNotes = await webClient.getConsumableNotes(account);
|
|
167
|
+
const noteIdToConsume = consumableNotes[0].inputNoteRecord().id();
|
|
168
|
+
|
|
169
|
+
// Create a consume transaction request object
|
|
170
|
+
const consumeTransactionRequest = webClient.newConsumeTransactionRequest([
|
|
171
|
+
noteIdToConsume,
|
|
172
|
+
]);
|
|
173
|
+
|
|
174
|
+
// Execute, prove, submit, and apply the transaction in one step
|
|
175
|
+
const transactionId = await webClient.submitNewTransaction(
|
|
176
|
+
account,
|
|
177
|
+
consumeTransactionRequest
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
// Need to sync state again (in a loop) until the node verifies the transaction
|
|
181
|
+
await syncState();
|
|
182
|
+
|
|
183
|
+
// Check new account balance
|
|
184
|
+
const updatedAccount = await webClient.getAccount(account);
|
|
185
|
+
const accountBalance = updatedAccount
|
|
186
|
+
.vault()
|
|
187
|
+
.getBalance(/* id of remote faucet */)
|
|
188
|
+
.toString();
|
|
189
|
+
console.log(accountBalance);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|