Skip to content

Commit 567be1a

Browse files
docs: update onboarding docs
1 parent 120a6a2 commit 567be1a

1 file changed

Lines changed: 292 additions & 10 deletions

File tree

docs/onchain-governance.md

Lines changed: 292 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# On-Chain Governance
22

3-
This guide covers setting up on-chain governance for dstack using smart contracts on Ethereum.
3+
This guide covers setting up on-chain governance for dstack using smart contracts on Ethereum or NEAR Protocol.
44

55
## Overview
66

@@ -9,14 +9,27 @@ On-chain governance adds:
99
- **Decentralized trust**: No single operator controls keys
1010
- **Transparent policies**: Anyone can verify authorization rules on-chain
1111

12+
dstack supports two blockchain platforms for on-chain governance:
13+
- **Ethereum**: Using Solidity smart contracts (see [Ethereum Auth](#ethereum-auth) section)
14+
- **NEAR Protocol**: Using Rust smart contracts (see [NEAR Auth](#near-auth) section)
15+
1216
## Prerequisites
1317

1418
- Production dstack deployment with KMS and Gateway as CVMs (see [Deployment Guide](./deployment.md))
19+
20+
**For Ethereum:**
1521
- Ethereum wallet with funds on Sepolia testnet (or your target network)
1622
- Node.js and npm installed
1723
- Alchemy API key (for Sepolia) - get one at https://www.alchemy.com/
1824

19-
## Deploy DstackKms Contract
25+
**For NEAR:**
26+
- NEAR account with funds on testnet or mainnet
27+
- Bun runtime (or Node.js) installed
28+
- NEAR RPC endpoint access (default: `https://free.rpc.fastnear.com`)
29+
30+
## Ethereum Auth
31+
32+
### Deploy DstackKms Contract
2033

2134
```bash
2235
cd dstack/kms/auth-eth
@@ -43,7 +56,7 @@ export PRIVATE_KEY="<your-private-key>"
4356
export ALCHEMY_API_KEY="<your-alchemy-key>"
4457
```
4558

46-
## Configure KMS for On-Chain Auth
59+
### Configure KMS for On-Chain Auth
4760

4861
The KMS CVM includes an auth-api service that connects to your DstackKms contract. Configure it via environment variables in the KMS CVM:
4962

@@ -56,7 +69,7 @@ Note: The auth-api uses `KMS_CONTRACT_ADDR`, while Hardhat tasks use `KMS_CONTRA
5669

5770
The auth-api validates boot requests against the smart contract. See [Deployment Guide](./deployment.md#2-deploy-kms-as-cvm) for complete setup instructions.
5871

59-
## Whitelist OS Image
72+
### Whitelist OS Image
6073

6174
```bash
6275
npx hardhat kms:add-image --network sepolia 0x<os-image-hash>
@@ -66,7 +79,7 @@ Output: `Image added successfully`
6679

6780
The `os_image_hash` is in the `digest.txt` file from the guest OS image build (see [Building Guest Images](./deployment.md#building-guest-images)).
6881

69-
## Register Gateway App
82+
### Register Gateway App
7083

7184
```bash
7285
npx hardhat kms:create-app --network sepolia --allow-any-device
@@ -103,7 +116,7 @@ npx hardhat app:add-hash --network sepolia --app-id <app-id> <compose-hash>
103116

104117
Output: `Compose hash added successfully`
105118

106-
## Register Apps On-Chain
119+
### Register Apps On-Chain
107120

108121
For each app you want to deploy:
109122

@@ -133,9 +146,9 @@ npx hardhat app:add-hash --network sepolia --app-id <app-id> <compose-hash>
133146

134147
Use the App ID when deploying through the VMM dashboard or [VMM CLI](./vmm-cli-user-guide.md).
135148

136-
## Smart Contract Reference
149+
### Ethereum Smart Contract Reference
137150

138-
### DstackKms (Main Contract)
151+
#### DstackKms (Main Contract)
139152

140153
The central governance contract that manages OS image whitelisting, app registration, and KMS authorization.
141154

@@ -149,7 +162,7 @@ The central governance contract that manages OS image whitelisting, app registra
149162
| `isAppAllowed(AppBootInfo)` | Check if an app is allowed to boot |
150163
| `isKmsAllowed(AppBootInfo)` | Check if KMS is allowed to boot |
151164

152-
### DstackApp (Per-App Contract)
165+
#### DstackApp (Per-App Contract)
153166

154167
Each app has its own contract controlling which compose hashes and devices are allowed.
155168

@@ -163,7 +176,7 @@ Each app has its own contract controlling which compose hashes and devices are a
163176
| `isAppAllowed(AppBootInfo)` | Check if app can boot with given config |
164177
| `disableUpgrades()` | Permanently disable contract upgrades |
165178

166-
### AppBootInfo Structure
179+
#### AppBootInfo Structure
167180

168181
Both `isAppAllowed` and `isKmsAllowed` take an `AppBootInfo` struct:
169182

@@ -183,6 +196,275 @@ struct AppBootInfo {
183196

184197
Source: [`kms/auth-eth/contracts/`](../kms/auth-eth/contracts/)
185198

199+
## NEAR Auth
200+
201+
### Deploy NEAR KMS Contract
202+
203+
The NEAR KMS contract must be deployed to a NEAR account. You can deploy it using NEAR CLI or the deployment scripts in the `near-kms` repository.
204+
205+
**Prerequisites:**
206+
- NEAR account with sufficient balance (for contract deployment and storage)
207+
- NEAR CLI installed and configured
208+
- MPC contract ID (for key derivation)
209+
210+
**Deploy the contract:**
211+
212+
```bash
213+
cd near-kms/contracts/kms
214+
near deploy --wasmFile res/near_dstack_kms.wasm \
215+
--accountId <your-kms-account-id> \
216+
--initFunction new \
217+
--initArgs '{"owner_id": "<owner-account-id>", "mpc_contract_id": "<mpc-contract-id>", "mpc_domain_id": 2}'
218+
```
219+
220+
Note the KMS contract account ID (e.g., `kms.dstack.testnet`).
221+
222+
Set environment variables for subsequent commands:
223+
224+
```bash
225+
export KMS_CONTRACT_ID="<kms-contract-account-id>"
226+
export NEAR_ACCOUNT_ID="<your-near-account-id>"
227+
export NEAR_PRIVATE_KEY="ed25519:<your-private-key>"
228+
export NEAR_NETWORK_ID="testnet" # or "mainnet"
229+
export NEAR_RPC_URL="https://free.rpc.fastnear.com" # optional, auto-detected if not set
230+
```
231+
232+
### Configure KMS for NEAR Auth
233+
234+
The KMS CVM can be configured to use NEAR contracts in two ways:
235+
236+
**Option 1: Direct NEAR integration (recommended)**
237+
238+
Configure via TOML config file:
239+
240+
```toml
241+
[core.auth_api]
242+
type = "near"
243+
244+
[core.auth_api.near]
245+
url = "http://auth-near:3000" # Optional: auth-near webhook service URL
246+
rpc_url = "https://free.rpc.fastnear.com"
247+
network_id = "testnet"
248+
contract_id = "<kms-contract-account-id>"
249+
mpc_contract_id = "<mpc-contract-id>" # Optional: for MPC key derivation
250+
mpc_domain_id = 2 # Optional: default is 2
251+
```
252+
253+
**Option 2: Via webhook service**
254+
255+
Deploy the `auth-near` webhook service and configure KMS to use it:
256+
257+
```toml
258+
[core.auth_api]
259+
type = "webhook"
260+
261+
[core.auth_api.webhook]
262+
url = "http://auth-near:3000"
263+
```
264+
265+
The `auth-near` service validates boot requests against NEAR smart contracts. See [auth-near README](../kms/auth-near/README.md) for complete setup instructions.
266+
267+
### Whitelist OS Image
268+
269+
Add an OS image hash to the KMS contract's allowed list:
270+
271+
```bash
272+
cd dstack/kms/auth-near
273+
bun install
274+
bun cli.ts add-os-image 0x<os-image-hash>
275+
```
276+
277+
Output: `✅ OS image hash added successfully`
278+
279+
The `os_image_hash` is in the `digest.txt` file from the guest OS image build (see [Building Guest Images](./deployment.md#building-guest-images)).
280+
281+
**Remove an OS image:**
282+
283+
```bash
284+
bun cli.ts remove-os-image 0x<os-image-hash>
285+
```
286+
287+
### Register Gateway App
288+
289+
Deploy and register a gateway app contract:
290+
291+
```bash
292+
bun cli.ts deploy <app-id> <owner-id> \
293+
--allow-any-device \
294+
--compose-hash 0x<compose-hash> \
295+
--deposit 30
296+
```
297+
298+
Sample output:
299+
300+
```
301+
✅ App contract deployed successfully!
302+
App Account: <app-id>.<kms-contract-id>
303+
Transaction: <transaction-hash>
304+
```
305+
306+
Note the App Account ID (e.g., `gateway.kms.dstack.testnet`).
307+
308+
Set it as the gateway app:
309+
310+
```bash
311+
near call <kms-contract-id> set_gateway_app_id \
312+
'{"app_id": "<app-account-id>"}' \
313+
--accountId <your-account-id> \
314+
--deposit 1
315+
```
316+
317+
Output: `Gateway App ID set successfully`
318+
319+
**Add compose hash to gateway:**
320+
321+
```bash
322+
bun cli.ts add-hash <app-account-id> 0x<compose-hash>
323+
```
324+
325+
### Register Apps On-Chain
326+
327+
For each app you want to deploy:
328+
329+
#### Create App
330+
331+
Deploy and register an app contract:
332+
333+
```bash
334+
bun cli.ts deploy <app-id> <owner-id> \
335+
--allow-any-device \
336+
--compose-hash 0x<compose-hash> \
337+
--deposit 30
338+
```
339+
340+
Note the App Account ID from the output (format: `<app-id>.<kms-contract-id>`).
341+
342+
#### Add Compose Hash
343+
344+
Compute your app's compose hash:
345+
346+
```bash
347+
sha256sum /path/to/your-app-compose.json | awk '{print "0x"$1}'
348+
```
349+
350+
Then add it:
351+
352+
```bash
353+
bun cli.ts add-hash <app-account-id> 0x<compose-hash>
354+
```
355+
356+
#### Add Device ID (Optional)
357+
358+
If not using `--allow-any-device`, add specific device IDs:
359+
360+
```bash
361+
near call <app-account-id> add_device \
362+
'{"device_id": "0x<device-id>"}' \
363+
--accountId <your-account-id> \
364+
--deposit 1
365+
```
366+
367+
#### Deploy via VMM
368+
369+
Use the App Account ID when deploying through the VMM dashboard or [VMM CLI](./vmm-cli-user-guide.md).
370+
371+
### NEAR Smart Contract Reference
372+
373+
#### KMS Contract (Main Contract)
374+
375+
The central governance contract that manages OS image whitelisting, app registration, and KMS authorization.
376+
377+
| Function | Description |
378+
|----------|-------------|
379+
| `new(owner_id, mpc_contract_id, mpc_domain_id)` | Initialize the KMS contract |
380+
| `add_os_image_hash(os_image_hash)` | Whitelist an OS image hash |
381+
| `remove_os_image_hash(os_image_hash)` | Remove an OS image from whitelist |
382+
| `add_kms_aggregated_mr(mr_aggregated)` | Whitelist an aggregated MR for KMS |
383+
| `remove_kms_aggregated_mr(mr_aggregated)` | Remove an aggregated MR from whitelist |
384+
| `add_kms_device(device_id)` | Whitelist a device ID for KMS |
385+
| `remove_kms_device(device_id)` | Remove a device ID from whitelist |
386+
| `add_kms_compose_hash(compose_hash)` | Whitelist a compose hash for KMS |
387+
| `remove_kms_compose_hash(compose_hash)` | Remove a compose hash from whitelist |
388+
| `set_gateway_app_id(app_id)` | Set the trusted Gateway app ID |
389+
| `register_app(app_id, owner_id, ...)` | Deploy and register an app contract |
390+
| `is_app_registered(app_id)` | Check if an app is registered |
391+
| `is_kms_allowed(AppBootInfo)` | Check if KMS is allowed to boot |
392+
| `request_kms_root_key(...)` | Request KMS root key from MPC network |
393+
394+
#### App Contract (Per-App Contract)
395+
396+
Each app has its own contract controlling which compose hashes and devices are allowed.
397+
398+
| Function | Description |
399+
|----------|-------------|
400+
| `new(owner_id, kms_contract_id, ...)` | Initialize the app contract |
401+
| `add_compose_hash(compose_hash)` | Whitelist a compose hash |
402+
| `remove_compose_hash(compose_hash)` | Remove a compose hash from whitelist |
403+
| `add_device(device_id)` | Whitelist a device ID |
404+
| `remove_device(device_id)` | Remove a device from whitelist |
405+
| `set_allow_any_device(allow)` | Allow any device to run this app |
406+
| `is_app_allowed(AppBootInfo)` | Check if app can boot with given config |
407+
| `disable_upgrades()` | Permanently disable contract upgrades |
408+
409+
#### AppBootInfo Structure
410+
411+
Both `is_app_allowed` and `is_kms_allowed` take an `AppBootInfo` struct:
412+
413+
```rust
414+
pub struct AppBootInfo {
415+
pub app_id: AccountId, // Unique app identifier (account ID)
416+
pub compose_hash: String, // Hash of docker-compose configuration
417+
pub instance_id: AccountId, // Unique instance identifier
418+
pub device_id: String, // Hardware device identifier
419+
pub mr_aggregated: String, // Aggregated measurement register
420+
pub mr_system: String, // System measurement register
421+
pub os_image_hash: String, // OS image hash
422+
pub tcb_status: String, // TCB status (e.g., "UpToDate")
423+
pub advisory_ids: Vec<String>, // Security advisory IDs
424+
}
425+
```
426+
427+
Source: [`near-kms/contracts/kms/`](../../near-kms/contracts/kms/)
428+
429+
### NEAR CLI Commands Reference
430+
431+
The `auth-near` package provides CLI commands for managing NEAR contracts:
432+
433+
**App Management:**
434+
```bash
435+
# Deploy app contract
436+
bun cli.ts deploy <app-id> <owner-id> [options]
437+
438+
# Add compose hash
439+
bun cli.ts add-hash <app-account-id> <compose-hash>
440+
441+
# Remove compose hash
442+
bun cli.ts remove-hash <app-account-id> <compose-hash>
443+
```
444+
445+
**KMS Configuration:**
446+
```bash
447+
# Add OS image hash
448+
bun cli.ts add-os-image <os_image_hash>
449+
450+
# Remove OS image hash
451+
bun cli.ts remove-os-image <os_image_hash>
452+
453+
# Add KMS device ID
454+
bun cli.ts add-device <device_id>
455+
456+
# Remove KMS device ID
457+
bun cli.ts remove-device <device_id>
458+
459+
# Add KMS aggregated MR
460+
bun cli.ts add-mr <mr_aggregated>
461+
462+
# Remove KMS aggregated MR
463+
bun cli.ts remove-mr <mr_aggregated>
464+
```
465+
466+
See [auth-near README](../kms/auth-near/README.md) for detailed CLI documentation.
467+
186468
## See Also
187469

188470
- [Deployment Guide](./deployment.md) - Setting up dstack infrastructure

0 commit comments

Comments
 (0)