@no-witness-labs/midday-sdk 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/README.md +186 -0
- package/dist/Client.d.ts +146 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +239 -0
- package/dist/Client.js.map +1 -0
- package/dist/Config.d.ts +23 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +45 -0
- package/dist/Config.js.map +1 -0
- package/dist/Providers.d.ts +31 -0
- package/dist/Providers.d.ts.map +1 -0
- package/dist/Providers.js +54 -0
- package/dist/Providers.js.map +1 -0
- package/dist/Wallet.d.ts +21 -0
- package/dist/Wallet.d.ts.map +1 -0
- package/dist/Wallet.js +58 -0
- package/dist/Wallet.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/package.json +83 -0
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# midday-sdk
|
|
2
|
+
|
|
3
|
+
Developer-friendly SDK for building dapps on Midnight Network.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @no-witness-labs/midday-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import * as Midday from '@no-witness-labs/midday-sdk';
|
|
15
|
+
|
|
16
|
+
// Create client (uses local network + dev wallet by default)
|
|
17
|
+
const client = await Midday.Client.create();
|
|
18
|
+
|
|
19
|
+
// Load and deploy a contract
|
|
20
|
+
const counter = await (await client.contractFrom('build/simple-counter')).deploy();
|
|
21
|
+
|
|
22
|
+
// Call contract actions
|
|
23
|
+
await counter.call('increment');
|
|
24
|
+
|
|
25
|
+
// Read state
|
|
26
|
+
const state = await counter.ledgerState();
|
|
27
|
+
console.log(state.counter);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Configuration
|
|
31
|
+
|
|
32
|
+
### Network Configuration
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// Local network (default)
|
|
36
|
+
const client = await Midday.Client.create();
|
|
37
|
+
|
|
38
|
+
// Custom network via config
|
|
39
|
+
const client = await Midday.Client.create({
|
|
40
|
+
networkConfig: {
|
|
41
|
+
networkId: 'testnet',
|
|
42
|
+
indexer: 'https://indexer.testnet.midnight.network/graphql',
|
|
43
|
+
indexerWS: 'wss://indexer.testnet.midnight.network/graphql/ws',
|
|
44
|
+
node: 'wss://node.testnet.midnight.network',
|
|
45
|
+
proofServer: 'https://proof.testnet.midnight.network',
|
|
46
|
+
},
|
|
47
|
+
seed: 'your-64-char-hex-seed',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Or via environment variables
|
|
51
|
+
// MIDNIGHT_INDEXER=https://...
|
|
52
|
+
// MIDNIGHT_INDEXER_WS=wss://...
|
|
53
|
+
// MIDNIGHT_NODE=wss://...
|
|
54
|
+
// MIDNIGHT_PROOF_SERVER=https://...
|
|
55
|
+
// MIDNIGHT_NETWORK_ID=testnet
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Wallet Seed
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Local network uses dev wallet by default
|
|
62
|
+
const client = await Midday.Client.create();
|
|
63
|
+
|
|
64
|
+
// Custom seed
|
|
65
|
+
const client = await Midday.Client.create({
|
|
66
|
+
seed: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Storage Configuration
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const client = await Midday.Client.create({
|
|
74
|
+
storage: {
|
|
75
|
+
path: '.data/my-app-state',
|
|
76
|
+
password: 'secure-password',
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Or via environment variable
|
|
81
|
+
// MIDNIGHT_STORAGE_PASSWORD=secure-password
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Contract Operations
|
|
85
|
+
|
|
86
|
+
### Deploy a New Contract
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const contract = await (await client.contractFrom('build/my-contract')).deploy();
|
|
90
|
+
console.log(`Deployed at: ${contract.address}`);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Join an Existing Contract
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const contract = await (await client.contractFrom('build/my-contract')).join(address);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Path Resolution Options
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Relative to cwd (default)
|
|
103
|
+
await client.contractFrom('build/my-contract');
|
|
104
|
+
|
|
105
|
+
// Relative to project root (finds package.json)
|
|
106
|
+
await client.contractFrom('build/my-contract', { from: 'project' });
|
|
107
|
+
|
|
108
|
+
// Relative to current file
|
|
109
|
+
await client.contractFrom('../build/my-contract', { from: import.meta.url });
|
|
110
|
+
|
|
111
|
+
// Absolute path
|
|
112
|
+
await client.contractFrom('/absolute/path/to/build/my-contract');
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### With Witnesses
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const contract = await (
|
|
119
|
+
await client.contractFrom('build/my-contract', {
|
|
120
|
+
witnesses: {
|
|
121
|
+
my_witness_function: myImplementation,
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
).deploy();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Call Actions
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const result = await contract.call('increment');
|
|
131
|
+
console.log(`TX Hash: ${result.txHash}`);
|
|
132
|
+
console.log(`Block: ${result.blockHeight}`);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Read State
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// Parsed state via ledger
|
|
139
|
+
const state = await contract.ledgerState();
|
|
140
|
+
|
|
141
|
+
// Raw state
|
|
142
|
+
const rawState = await contract.state();
|
|
143
|
+
|
|
144
|
+
// State at specific block
|
|
145
|
+
const historicalState = await contract.ledgerStateAt(blockHeight);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Environment Variables
|
|
149
|
+
|
|
150
|
+
| Variable | Description |
|
|
151
|
+
|----------|-------------|
|
|
152
|
+
| `MIDNIGHT_INDEXER` | Indexer GraphQL HTTP endpoint |
|
|
153
|
+
| `MIDNIGHT_INDEXER_WS` | Indexer GraphQL WebSocket endpoint |
|
|
154
|
+
| `MIDNIGHT_NODE` | Node WebSocket endpoint |
|
|
155
|
+
| `MIDNIGHT_PROOF_SERVER` | Proof server HTTP endpoint |
|
|
156
|
+
| `MIDNIGHT_NETWORK_ID` | Network ID (e.g., 'undeployed', 'testnet') |
|
|
157
|
+
| `MIDNIGHT_STORAGE_PASSWORD` | Private state storage password |
|
|
158
|
+
|
|
159
|
+
## API Reference
|
|
160
|
+
|
|
161
|
+
### Modules
|
|
162
|
+
|
|
163
|
+
- `Midday.Client` - High-level client for contract interactions
|
|
164
|
+
- `Midday.Config` - Network configuration utilities
|
|
165
|
+
- `Midday.Wallet` - Wallet initialization and management
|
|
166
|
+
- `Midday.Providers` - Low-level provider setup
|
|
167
|
+
|
|
168
|
+
### Types
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import type {
|
|
172
|
+
ClientConfig,
|
|
173
|
+
MidnightClient,
|
|
174
|
+
ContractBuilder,
|
|
175
|
+
ConnectedContract,
|
|
176
|
+
CallResult,
|
|
177
|
+
NetworkConfig,
|
|
178
|
+
WalletContext,
|
|
179
|
+
ContractProviders,
|
|
180
|
+
StorageConfig,
|
|
181
|
+
} from '@no-witness-labs/midday-sdk';
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
MIT
|
package/dist/Client.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level client for interacting with Midnight Network contracts.
|
|
3
|
+
*
|
|
4
|
+
* Provides a simple API for deploying, joining, and calling contracts.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { type Logger } from 'pino';
|
|
10
|
+
import type { NetworkConfig } from './Config.js';
|
|
11
|
+
import type { ContractProviders, StorageConfig } from './Providers.js';
|
|
12
|
+
import type { WalletContext } from './Wallet.js';
|
|
13
|
+
export interface ClientConfig {
|
|
14
|
+
/** Network to connect to (default: 'local') */
|
|
15
|
+
network?: string;
|
|
16
|
+
/** Custom network configuration (overrides network preset) */
|
|
17
|
+
networkConfig?: NetworkConfig;
|
|
18
|
+
/** Wallet seed (defaults to dev wallet for local) */
|
|
19
|
+
seed?: string;
|
|
20
|
+
/** Storage configuration */
|
|
21
|
+
storage?: StorageConfig;
|
|
22
|
+
/** Enable logging (default: true) */
|
|
23
|
+
logging?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface ContractFromOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Base for path resolution:
|
|
28
|
+
* - undefined/'cwd': relative to process.cwd() (default)
|
|
29
|
+
* - 'project': relative to project root (finds package.json)
|
|
30
|
+
* - string (URL): relative to caller's import.meta.url
|
|
31
|
+
*/
|
|
32
|
+
from?: string | 'project' | 'cwd';
|
|
33
|
+
/** Witnesses for the contract */
|
|
34
|
+
witnesses?: Record<string, unknown>;
|
|
35
|
+
/** Override privateStateId (defaults to directory name) */
|
|
36
|
+
privateStateId?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface DeployOptions {
|
|
39
|
+
/** Initial private state (defaults to {}) */
|
|
40
|
+
initialPrivateState?: unknown;
|
|
41
|
+
}
|
|
42
|
+
export interface JoinOptions {
|
|
43
|
+
/** Initial private state (defaults to {}) */
|
|
44
|
+
initialPrivateState?: unknown;
|
|
45
|
+
}
|
|
46
|
+
export interface MidnightClient {
|
|
47
|
+
/** Raw wallet context for advanced use */
|
|
48
|
+
readonly wallet: WalletContext;
|
|
49
|
+
/** Network configuration */
|
|
50
|
+
readonly networkConfig: NetworkConfig;
|
|
51
|
+
/** Logger instance */
|
|
52
|
+
readonly logger: Logger;
|
|
53
|
+
/** Load a contract from a build directory path */
|
|
54
|
+
contractFrom(basePath: string, options?: ContractFromOptions): Promise<ContractBuilder>;
|
|
55
|
+
/** Wait for a transaction to be finalized on-chain by its hash */
|
|
56
|
+
waitForTx(txHash: string): Promise<FinalizedTxData>;
|
|
57
|
+
}
|
|
58
|
+
export interface FinalizedTxData {
|
|
59
|
+
txHash: string;
|
|
60
|
+
blockHeight: number;
|
|
61
|
+
blockHash: string;
|
|
62
|
+
}
|
|
63
|
+
export interface ContractBuilder {
|
|
64
|
+
/** The loaded contract module */
|
|
65
|
+
readonly module: LoadedContractModule;
|
|
66
|
+
/** Deploy a new instance */
|
|
67
|
+
deploy(options?: DeployOptions): Promise<ConnectedContract>;
|
|
68
|
+
/** Join an existing deployed contract */
|
|
69
|
+
join(address: string, options?: JoinOptions): Promise<ConnectedContract>;
|
|
70
|
+
}
|
|
71
|
+
export interface LoadedContractModule {
|
|
72
|
+
Contract: new (witnesses: unknown) => unknown;
|
|
73
|
+
ledger: (state: unknown) => unknown;
|
|
74
|
+
zkConfigPath: string;
|
|
75
|
+
privateStateId: string;
|
|
76
|
+
witnesses: Record<string, unknown>;
|
|
77
|
+
}
|
|
78
|
+
export interface ConnectedContract {
|
|
79
|
+
/** The deployed contract address */
|
|
80
|
+
readonly address: string;
|
|
81
|
+
/** The underlying contract instance */
|
|
82
|
+
readonly instance: unknown;
|
|
83
|
+
/** The loaded module (for ledger access) */
|
|
84
|
+
readonly module: LoadedContractModule;
|
|
85
|
+
/** Raw providers */
|
|
86
|
+
readonly providers: ContractProviders;
|
|
87
|
+
/** Logger */
|
|
88
|
+
readonly logger: Logger;
|
|
89
|
+
/** Call a contract circuit - waits for transaction to be finalized */
|
|
90
|
+
call(action: string, ...args: unknown[]): Promise<CallResult>;
|
|
91
|
+
/** Query contract public state (raw) - latest */
|
|
92
|
+
state(): Promise<unknown>;
|
|
93
|
+
/** Query contract public state at specific block height (raw) */
|
|
94
|
+
stateAt(blockHeight: number): Promise<unknown>;
|
|
95
|
+
/** Query contract public state (parsed via ledger) - latest */
|
|
96
|
+
ledgerState(): Promise<unknown>;
|
|
97
|
+
/** Query contract public state at specific block height (parsed via ledger) */
|
|
98
|
+
ledgerStateAt(blockHeight: number): Promise<unknown>;
|
|
99
|
+
}
|
|
100
|
+
export interface CallResult {
|
|
101
|
+
txHash: string;
|
|
102
|
+
blockHeight: number;
|
|
103
|
+
status: string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create a Midnight client for interacting with contracts
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* import * as Midday from '@no-witness-labs/midday-sdk';
|
|
111
|
+
*
|
|
112
|
+
* // Simple - local network with dev wallet
|
|
113
|
+
* const client = await Midday.Client.create();
|
|
114
|
+
*
|
|
115
|
+
* // Custom seed
|
|
116
|
+
* const client = await Midday.Client.create({
|
|
117
|
+
* seed: 'your-64-char-hex-seed'
|
|
118
|
+
* });
|
|
119
|
+
*
|
|
120
|
+
* // Custom network endpoints via env vars or config
|
|
121
|
+
* const client = await Midday.Client.create({
|
|
122
|
+
* networkConfig: {
|
|
123
|
+
* networkId: 'testnet',
|
|
124
|
+
* indexer: 'https://indexer.testnet.midnight.network/graphql',
|
|
125
|
+
* indexerWS: 'wss://indexer.testnet.midnight.network/graphql/ws',
|
|
126
|
+
* node: 'wss://node.testnet.midnight.network',
|
|
127
|
+
* proofServer: 'https://proof.testnet.midnight.network',
|
|
128
|
+
* }
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Load and deploy a contract
|
|
132
|
+
* const counter = await (await client.contractFrom('build/simple-counter')).deploy();
|
|
133
|
+
*
|
|
134
|
+
* // Call actions
|
|
135
|
+
* await counter.call('increment');
|
|
136
|
+
*
|
|
137
|
+
* // Read state
|
|
138
|
+
* const state = await counter.ledgerState();
|
|
139
|
+
* console.log(state.counter);
|
|
140
|
+
*
|
|
141
|
+
* // Join existing contract
|
|
142
|
+
* const existing = await (await client.contractFrom('build/simple-counter')).join(address);
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export declare function create(config?: ClientConfig): Promise<MidnightClient>;
|
|
146
|
+
//# sourceMappingURL=Client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../src/Client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,OAAa,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AAMzC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACvE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAMjD,MAAM,WAAW,YAAY;IAC3B,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,qDAAqD;IACrD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4BAA4B;IAC5B,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,qCAAqC;IACrC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,KAAK,CAAC;IAClC,iCAAiC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACpC,2DAA2D;IAC3D,cAAc,CAAC,EAAE,MA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AC;IACvB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,iBAAiB;IAChC,oCAAoC;IACpC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,uCAAuC;IACvC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAC;IACtC,oBAAoB;IACpB,QAAQ,CAAC,SAAS,EAAE,iBAAiB,CAAC;IACtC,aAAa;IACb,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,sEAAsE;IACtE,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAE9D,iDAAiD;IACjD,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAE1B,iEAAiE;IACjE,OAAO,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE/C,+DAA+D;IAC/D,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAEhC,+EAA+E;IAC/E,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACtD;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;CAChB;AAsDD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAsB,MAAM,CAAC,MAAM,GAAE,YAAiB,GAAG,OAAO,CAAC,cAAc,CAAC,CA8D/E"}
|
package/dist/Client.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level client for interacting with Midnight Network contracts.
|
|
3
|
+
*
|
|
4
|
+
* Provides a simple API for deploying, joining, and calling contracts.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import * as path from 'path';
|
|
10
|
+
import * as fs from 'fs';
|
|
11
|
+
import { fileURLToPath } from 'url';
|
|
12
|
+
import { deployContract, findDeployedContract } from '@midnight-ntwrk/midnight-js-contracts';
|
|
13
|
+
import pino from 'pino';
|
|
14
|
+
import { build as buildPretty } from 'pino-pretty';
|
|
15
|
+
import * as Config from './Config.js';
|
|
16
|
+
import * as Wallet from './Wallet.js';
|
|
17
|
+
import * as Providers from './Providers.js';
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// Path Resolution
|
|
20
|
+
// =============================================================================
|
|
21
|
+
function findProjectRoot(startDir = process.cwd()) {
|
|
22
|
+
let current = startDir;
|
|
23
|
+
while (current !== path.dirname(current)) {
|
|
24
|
+
if (fs.existsSync(path.join(current, 'package.json'))) {
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
current = path.dirname(current);
|
|
28
|
+
}
|
|
29
|
+
throw new Error('Could not find project root (no package.json found)');
|
|
30
|
+
}
|
|
31
|
+
function resolvePath(basePath, options) {
|
|
32
|
+
if (path.isAbsolute(basePath)) {
|
|
33
|
+
return basePath;
|
|
34
|
+
}
|
|
35
|
+
const from = options?.from ?? 'cwd';
|
|
36
|
+
if (from === 'cwd') {
|
|
37
|
+
return path.resolve(process.cwd(), basePath);
|
|
38
|
+
}
|
|
39
|
+
if (from === 'project') {
|
|
40
|
+
const projectRoot = findProjectRoot();
|
|
41
|
+
return path.resolve(projectRoot, basePath);
|
|
42
|
+
}
|
|
43
|
+
// Assume it's an import.meta.url
|
|
44
|
+
const callerDir = path.dirname(fileURLToPath(from));
|
|
45
|
+
return path.resolve(callerDir, basePath);
|
|
46
|
+
}
|
|
47
|
+
// =============================================================================
|
|
48
|
+
// Logger
|
|
49
|
+
// =============================================================================
|
|
50
|
+
function createLogger(enabled) {
|
|
51
|
+
if (!enabled) {
|
|
52
|
+
return pino.default({ level: 'silent' });
|
|
53
|
+
}
|
|
54
|
+
const pretty = buildPretty({ colorize: true, sync: true });
|
|
55
|
+
return pino.default({ level: 'info' }, pretty);
|
|
56
|
+
}
|
|
57
|
+
// =============================================================================
|
|
58
|
+
// Client Implementation
|
|
59
|
+
// =============================================================================
|
|
60
|
+
/**
|
|
61
|
+
* Create a Midnight client for interacting with contracts
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import * as Midday from '@no-witness-labs/midday-sdk';
|
|
66
|
+
*
|
|
67
|
+
* // Simple - local network with dev wallet
|
|
68
|
+
* const client = await Midday.Client.create();
|
|
69
|
+
*
|
|
70
|
+
* // Custom seed
|
|
71
|
+
* const client = await Midday.Client.create({
|
|
72
|
+
* seed: 'your-64-char-hex-seed'
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* // Custom network endpoints via env vars or config
|
|
76
|
+
* const client = await Midday.Client.create({
|
|
77
|
+
* networkConfig: {
|
|
78
|
+
* networkId: 'testnet',
|
|
79
|
+
* indexer: 'https://indexer.testnet.midnight.network/graphql',
|
|
80
|
+
* indexerWS: 'wss://indexer.testnet.midnight.network/graphql/ws',
|
|
81
|
+
* node: 'wss://node.testnet.midnight.network',
|
|
82
|
+
* proofServer: 'https://proof.testnet.midnight.network',
|
|
83
|
+
* }
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* // Load and deploy a contract
|
|
87
|
+
* const counter = await (await client.contractFrom('build/simple-counter')).deploy();
|
|
88
|
+
*
|
|
89
|
+
* // Call actions
|
|
90
|
+
* await counter.call('increment');
|
|
91
|
+
*
|
|
92
|
+
* // Read state
|
|
93
|
+
* const state = await counter.ledgerState();
|
|
94
|
+
* console.log(state.counter);
|
|
95
|
+
*
|
|
96
|
+
* // Join existing contract
|
|
97
|
+
* const existing = await (await client.contractFrom('build/simple-counter')).join(address);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export async function create(config = {}) {
|
|
101
|
+
const { network = 'local', networkConfig: customNetworkConfig, seed, storage, logging = true } = config;
|
|
102
|
+
const logger = createLogger(logging);
|
|
103
|
+
// Resolve network configuration
|
|
104
|
+
const networkConfig = customNetworkConfig ?? Config.getNetworkConfig(network);
|
|
105
|
+
// Resolve seed (use dev wallet only for local network)
|
|
106
|
+
const walletSeed = seed ?? (network === 'local' ? Config.DEV_WALLET_SEED : undefined);
|
|
107
|
+
if (!walletSeed) {
|
|
108
|
+
throw new Error('Wallet seed is required for non-local networks. Provide via config.seed or MIDNIGHT_WALLET_SEED env var.');
|
|
109
|
+
}
|
|
110
|
+
// Initialize wallet
|
|
111
|
+
logger.info('Initializing wallet...');
|
|
112
|
+
const walletContext = await Wallet.init(walletSeed, networkConfig);
|
|
113
|
+
await Wallet.waitForSync(walletContext);
|
|
114
|
+
logger.info('Wallet synced');
|
|
115
|
+
return {
|
|
116
|
+
wallet: walletContext,
|
|
117
|
+
networkConfig,
|
|
118
|
+
logger,
|
|
119
|
+
async contractFrom(basePath, options) {
|
|
120
|
+
const zkConfigPath = resolvePath(basePath, options);
|
|
121
|
+
if (!fs.existsSync(zkConfigPath)) {
|
|
122
|
+
throw new Error(`Contract path not found: ${zkConfigPath}`);
|
|
123
|
+
}
|
|
124
|
+
const contractPath = path.join(zkConfigPath, 'contract', 'index.js');
|
|
125
|
+
if (!fs.existsSync(contractPath)) {
|
|
126
|
+
throw new Error(`Contract module not found: ${contractPath}`);
|
|
127
|
+
}
|
|
128
|
+
const contractModule = await import(contractPath);
|
|
129
|
+
const module = {
|
|
130
|
+
Contract: contractModule.Contract,
|
|
131
|
+
ledger: contractModule.ledger,
|
|
132
|
+
zkConfigPath,
|
|
133
|
+
privateStateId: options?.privateStateId ?? path.basename(zkConfigPath),
|
|
134
|
+
witnesses: options?.witnesses ?? {},
|
|
135
|
+
};
|
|
136
|
+
const providers = Providers.create(walletContext, zkConfigPath, networkConfig, storage);
|
|
137
|
+
return createContractBuilder(module, providers, logger);
|
|
138
|
+
},
|
|
139
|
+
async waitForTx(txHash) {
|
|
140
|
+
const providers = Providers.create(walletContext, process.cwd(), networkConfig, storage);
|
|
141
|
+
const data = await providers.publicDataProvider.watchForTxData(txHash);
|
|
142
|
+
return {
|
|
143
|
+
txHash: data.txHash,
|
|
144
|
+
blockHeight: data.blockHeight,
|
|
145
|
+
blockHash: data.blockHash,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
// =============================================================================
|
|
151
|
+
// Contract Builder
|
|
152
|
+
// =============================================================================
|
|
153
|
+
function createContractBuilder(module, providers, logger) {
|
|
154
|
+
return {
|
|
155
|
+
module,
|
|
156
|
+
async deploy(options) {
|
|
157
|
+
const { initialPrivateState = {} } = options ?? {};
|
|
158
|
+
logger.info('Deploying contract...');
|
|
159
|
+
const ContractClass = module.Contract;
|
|
160
|
+
const contract = new ContractClass(module.witnesses);
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
162
|
+
const deployed = await deployContract(providers, {
|
|
163
|
+
contract,
|
|
164
|
+
privateStateId: module.privateStateId,
|
|
165
|
+
initialPrivateState,
|
|
166
|
+
});
|
|
167
|
+
const address = deployed.deployTxData.public.contractAddress;
|
|
168
|
+
logger.info(`Contract deployed at: ${address}`);
|
|
169
|
+
return createConnectedContract(address, deployed, module, providers, logger);
|
|
170
|
+
},
|
|
171
|
+
async join(address, options) {
|
|
172
|
+
const { initialPrivateState = {} } = options ?? {};
|
|
173
|
+
logger.info(`Joining contract at ${address}...`);
|
|
174
|
+
const ContractClass = module.Contract;
|
|
175
|
+
const contract = new ContractClass(module.witnesses);
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
const deployed = await findDeployedContract(providers, {
|
|
178
|
+
contractAddress: address,
|
|
179
|
+
contract,
|
|
180
|
+
privateStateId: module.privateStateId,
|
|
181
|
+
initialPrivateState,
|
|
182
|
+
});
|
|
183
|
+
logger.info('Contract joined');
|
|
184
|
+
return createConnectedContract(address, deployed, module, providers, logger);
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function createConnectedContract(address, instance, module, providers, logger) {
|
|
189
|
+
return {
|
|
190
|
+
address,
|
|
191
|
+
instance,
|
|
192
|
+
module,
|
|
193
|
+
providers,
|
|
194
|
+
logger,
|
|
195
|
+
async call(action, ...args) {
|
|
196
|
+
logger.info(`Calling ${action}()...`);
|
|
197
|
+
const deployed = instance;
|
|
198
|
+
const callTx = deployed.callTx;
|
|
199
|
+
if (!callTx || typeof callTx[action] !== 'function') {
|
|
200
|
+
throw new Error(`Unknown action: ${action}. Available: ${Object.keys(callTx || {}).join(', ')}`);
|
|
201
|
+
}
|
|
202
|
+
const txData = await callTx[action](...args);
|
|
203
|
+
logger.info('Transaction submitted');
|
|
204
|
+
logger.info(` TX Hash: ${txData.public.txHash}`);
|
|
205
|
+
logger.info(` Block: ${txData.public.blockHeight}`);
|
|
206
|
+
return {
|
|
207
|
+
txHash: txData.public.txHash,
|
|
208
|
+
blockHeight: txData.public.blockHeight,
|
|
209
|
+
status: txData.public.status,
|
|
210
|
+
};
|
|
211
|
+
},
|
|
212
|
+
async state() {
|
|
213
|
+
const contractState = await providers.publicDataProvider.queryContractState(address);
|
|
214
|
+
if (!contractState) {
|
|
215
|
+
throw new Error(`Contract state not found at ${address}`);
|
|
216
|
+
}
|
|
217
|
+
return contractState.data;
|
|
218
|
+
},
|
|
219
|
+
async stateAt(blockHeight) {
|
|
220
|
+
const contractState = await providers.publicDataProvider.queryContractState(address, {
|
|
221
|
+
type: 'blockHeight',
|
|
222
|
+
blockHeight,
|
|
223
|
+
});
|
|
224
|
+
if (!contractState) {
|
|
225
|
+
throw new Error(`Contract state not found at ${address} at block ${blockHeight}`);
|
|
226
|
+
}
|
|
227
|
+
return contractState.data;
|
|
228
|
+
},
|
|
229
|
+
async ledgerState() {
|
|
230
|
+
const data = await this.state();
|
|
231
|
+
return module.ledger(data);
|
|
232
|
+
},
|
|
233
|
+
async ledgerStateAt(blockHeight) {
|
|
234
|
+
const data = await this.stateAt(blockHeight);
|
|
235
|
+
return module.ledger(data);
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Client.js","sourceRoot":"","sources":["../src/Client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7F,OAAO,IAAqB,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,KAAK,IAAI,WAAW,EAAmB,MAAM,aAAa,CAAC;AAEpE,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAsH5C,gFAAgF;AAChF,kBAAkB;AAClB,gFAAgF;AAEhF,SAAS,eAAe,CAAC,WAAmB,OAAO,CAAC,GAAG,EAAE;IACvD,IAAI,OAAO,GAAG,QAAQ,CAAC;IACvB,OAAO,OAAO,KAAK,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACzC,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC;YACtD,OAAO,OAAO,CAAC;QACjB,CAAC;QACD,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;AACzE,CAAC;AA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CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC;YACrF,IAAI,CAAC,aAAa,EAAE,CAAC;gBACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;YACD,OAAO,aAAa,CAAC,IAAI,CAAC;QAC5B,CAAC;QAED,KAAK,CAAC,OAAO,CAAC,WAAmB;YAC/B,MAAM,aAAa,GAAG,MAAM,SAAS,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,OAAO,EAAE;gBACnF,IAAI,EAAE,aAAa;gBACnB,WAAW;aACZ,CAAC,CAAC;YACH,IAAI,CAAC,aAAa,EAAE,CAAC;gBACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,OAAO,aAAa,WAAW,EAAE,CAAC,CAAC;YACpF,CAAC;YACD,OAAO,aAAa,CAAC,IAAI,CAAC;QAC5B,CAAC;QAED,KAAK,CAAC,WAAW;YACf,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;QAED,KAAK,CAAC,aAAa,CAAC,WAAmB;YACrC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YAC7C,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/Config.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network configuration and constants for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export interface NetworkConfig {
|
|
8
|
+
networkId: string;
|
|
9
|
+
indexer: string;
|
|
10
|
+
indexerWS: string;
|
|
11
|
+
node: string;
|
|
12
|
+
proofServer: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const NETWORKS: Record<string, NetworkConfig>;
|
|
15
|
+
/**
|
|
16
|
+
* Get network configuration from environment or defaults
|
|
17
|
+
*/
|
|
18
|
+
export declare function getNetworkConfig(network?: string): NetworkConfig;
|
|
19
|
+
/**
|
|
20
|
+
* Genesis wallet seed for local development (DO NOT use in production)
|
|
21
|
+
*/
|
|
22
|
+
export declare const DEV_WALLET_SEED = "0000000000000000000000000000000000000000000000000000000000000001";
|
|
23
|
+
//# sourceMappingURL=Config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../src/Config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CASzC,CAAC;AAEX;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,MAAgB,GAAG,aAAa,CAwBzE;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,qEAAqE,CAAC"}
|
package/dist/Config.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network configuration and constants for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export const NETWORKS = {
|
|
8
|
+
local: {
|
|
9
|
+
networkId: 'undeployed',
|
|
10
|
+
indexer: 'http://localhost:8088/api/v3/graphql',
|
|
11
|
+
indexerWS: 'ws://localhost:8088/api/v3/graphql/ws',
|
|
12
|
+
node: 'ws://localhost:9944',
|
|
13
|
+
proofServer: 'http://localhost:6300',
|
|
14
|
+
},
|
|
15
|
+
// Add testnet/mainnet configs when available
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Get network configuration from environment or defaults
|
|
19
|
+
*/
|
|
20
|
+
export function getNetworkConfig(network = 'local') {
|
|
21
|
+
// Check for environment variable overrides first
|
|
22
|
+
if (process.env.MIDNIGHT_INDEXER ||
|
|
23
|
+
process.env.MIDNIGHT_INDEXER_WS ||
|
|
24
|
+
process.env.MIDNIGHT_NODE ||
|
|
25
|
+
process.env.MIDNIGHT_PROOF_SERVER) {
|
|
26
|
+
return {
|
|
27
|
+
networkId: process.env.MIDNIGHT_NETWORK_ID || 'undeployed',
|
|
28
|
+
indexer: process.env.MIDNIGHT_INDEXER || NETWORKS.local.indexer,
|
|
29
|
+
indexerWS: process.env.MIDNIGHT_INDEXER_WS || NETWORKS.local.indexerWS,
|
|
30
|
+
node: process.env.MIDNIGHT_NODE || NETWORKS.local.node,
|
|
31
|
+
proofServer: process.env.MIDNIGHT_PROOF_SERVER || NETWORKS.local.proofServer,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// Use predefined network config
|
|
35
|
+
const config = NETWORKS[network];
|
|
36
|
+
if (!config) {
|
|
37
|
+
throw new Error(`Unknown network: ${network}. Available: ${Object.keys(NETWORKS).join(', ')}`);
|
|
38
|
+
}
|
|
39
|
+
return config;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Genesis wallet seed for local development (DO NOT use in production)
|
|
43
|
+
*/
|
|
44
|
+
export const DEV_WALLET_SEED = '0000000000000000000000000000000000000000000000000000000000000001';
|
|
45
|
+
//# sourceMappingURL=Config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Config.js","sourceRoot":"","sources":["../src/Config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,MAAM,CAAC,MAAM,QAAQ,GAAkC;IACrD,KAAK,EAAE;QACL,SAAS,EAAE,YAAY;QACvB,OAAO,EAAE,sCAAsC;QAC/C,SAAS,EAAE,uCAAuC;QAClD,I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creates the provider stack needed for deploying and interacting with contracts.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { indexerPublicDataProvider } from '@midnight-ntwrk/midnight-js-indexer-public-data-provider';
|
|
10
|
+
import { httpClientProofProvider } from '@midnight-ntwrk/midnight-js-http-client-proof-provider';
|
|
11
|
+
import { levelPrivateStateProvider } from '@midnight-ntwrk/midnight-js-level-private-state-provider';
|
|
12
|
+
import { NodeZkConfigProvider } from '@midnight-ntwrk/midnight-js-node-zk-config-provider';
|
|
13
|
+
import type { WalletProvider, MidnightProvider } from '@midnight-ntwrk/midnight-js-types';
|
|
14
|
+
import type { NetworkConfig } from './Config.js';
|
|
15
|
+
import type { WalletContext } from './Wallet.js';
|
|
16
|
+
export interface StorageConfig {
|
|
17
|
+
/** Path for LevelDB private state storage (default: '.data/midnight-level-db') */
|
|
18
|
+
path?: string;
|
|
19
|
+
/** Storage password (default from MIDNIGHT_STORAGE_PASSWORD env or generated) */
|
|
20
|
+
password?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface ContractProviders {
|
|
23
|
+
walletProvider: WalletProvider;
|
|
24
|
+
midnightProvider: MidnightProvider;
|
|
25
|
+
publicDataProvider: ReturnType<typeof indexerPublicDataProvider>;
|
|
26
|
+
privateStateProvider: ReturnType<typeof levelPrivateStateProvider>;
|
|
27
|
+
proofProvider: ReturnType<typeof httpClientProofProvider>;
|
|
28
|
+
zkConfigProvider: NodeZkConfigProvider<string>;
|
|
29
|
+
}
|
|
30
|
+
export declare function create(walletContext: WalletContext, zkConfigPath: string, networkConfig: NetworkConfig, storageConfig?: StorageConfig): ContractProviders;
|
|
31
|
+
//# sourceMappingURL=Providers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Providers.d.ts","sourceRoot":"","sources":["../src/Providers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,uBAAuB,EAAE,MAAM,wDAAwD,CAAC;AACjG,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,oBAAoB,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAyB,MAAM,mCAAmC,CAAC;AAEjH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,MAAM,WAAW,aAAa;IAC5B,kFAAkF;IAClF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iFAAiF;IACjF,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,gBAAgB,CAAC;IACnC,kBAAkB,EAAE,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;IACjE,oBAAoB,EAAE,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;IACnE,aAAa,EAAE,UAAU,CAAC,OAAO,uBAAuB,CAAC,CAAC;IAC1D,gBAAgB,EAAE,oBAAoB,CAAC,MAAM,CAAC,CAAC;CAChD;AAED,wBAAgB,MAAM,CACpB,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,aAAa,EAC5B,aAAa,GAAE,aAAkB,GAChC,iBAAiB,CAoDnB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider setup for contract interactions on Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Creates the provider stack needed for deploying and interacting with contracts.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { setNetworkId } from '@midnight-ntwrk/midnight-js-network-id';
|
|
10
|
+
import { indexerPublicDataProvider } from '@midnight-ntwrk/midnight-js-indexer-public-data-provider';
|
|
11
|
+
import { httpClientProofProvider } from '@midnight-ntwrk/midnight-js-http-client-proof-provider';
|
|
12
|
+
import { levelPrivateStateProvider } from '@midnight-ntwrk/midnight-js-level-private-state-provider';
|
|
13
|
+
import { NodeZkConfigProvider } from '@midnight-ntwrk/midnight-js-node-zk-config-provider';
|
|
14
|
+
export function create(walletContext, zkConfigPath, networkConfig, storageConfig = {}) {
|
|
15
|
+
// Set network ID
|
|
16
|
+
setNetworkId(networkConfig.networkId);
|
|
17
|
+
// Storage configuration
|
|
18
|
+
const storagePath = storageConfig.path || '.data/midnight-level-db';
|
|
19
|
+
const storagePassword = storageConfig.password || process.env.MIDNIGHT_STORAGE_PASSWORD || '1234567890123456';
|
|
20
|
+
// Wallet provider - handles transaction balancing
|
|
21
|
+
const walletProvider = {
|
|
22
|
+
getCoinPublicKey: () => walletContext.shieldedSecretKeys.coinPublicKey,
|
|
23
|
+
getEncryptionPublicKey: () => walletContext.shieldedSecretKeys.encryptionPublicKey,
|
|
24
|
+
balanceTx: async (tx, newCoins, ttl) => {
|
|
25
|
+
const txTtl = ttl ?? new Date(Date.now() + 30 * 60 * 1000);
|
|
26
|
+
const provingRecipe = await walletContext.wallet.balanceTransaction(walletContext.shieldedSecretKeys, walletContext.dustSecretKey, tx, txTtl);
|
|
27
|
+
return provingRecipe;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
// Midnight provider - handles transaction submission
|
|
31
|
+
const midnightProvider = {
|
|
32
|
+
submitTx: async (tx) => await walletContext.wallet.submitTransaction(tx),
|
|
33
|
+
};
|
|
34
|
+
// Public data provider - reads from indexer
|
|
35
|
+
const publicDataProvider = indexerPublicDataProvider(networkConfig.indexer, networkConfig.indexerWS);
|
|
36
|
+
// Private state provider - local encrypted storage
|
|
37
|
+
const privateStateProvider = levelPrivateStateProvider({
|
|
38
|
+
privateStateStoreName: storagePath,
|
|
39
|
+
privateStoragePasswordProvider: () => storagePassword,
|
|
40
|
+
});
|
|
41
|
+
// Proof provider - generates ZK proofs
|
|
42
|
+
const proofProvider = httpClientProofProvider(networkConfig.proofServer);
|
|
43
|
+
// ZK config provider - circuit keys and config
|
|
44
|
+
const zkConfigProvider = new NodeZkConfigProvider(zkConfigPath);
|
|
45
|
+
return {
|
|
46
|
+
walletProvider,
|
|
47
|
+
midnightProvider,
|
|
48
|
+
publicDataProvider,
|
|
49
|
+
privateStateProvider,
|
|
50
|
+
proofProvider,
|
|
51
|
+
zkConfigProvider,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=Providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Providers.js","sourceRoot":"","sources":["../src/Providers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,uBAAuB,EAAE,MAAM,wDAAwD,CAAC;AACjG,OAAO,EAAE,yBAAyB,EAAE,MAAM,0DAA0D,CAAC;AACrG,OAAO,EAAE,oBAAoB,EAAE,MAAM,qDAAqD,CAAC;AAsB3F,MAAM,UAAU,MAAM,CACpB,aAA4B,EAC5B,YAAoB,EACpB,aAA4B,EAC5B,gBAA+B,EAAE;IAEjC,iBAAiB;IACjB,YAAY,CAAC,aAAa,CAAC,SAAyB,CAAC,CAAC;IAEtD,wBAAwB;IACxB,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,IAAI,yBAAyB,CAAC;IACpE,MAAM,eAAe,GAAG,aAAa,CAAC,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,yBAAyB,IAAI,kBAAkB,CAAC;IAE9G,kDAAkD;IAClD,MAAM,cAAc,GAAmB;QACrC,gBAAgB,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,kBAAkB,CAAC,a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@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet initialization and management for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Handles the three-layer wallet system: shielded, dust, and unshielded wallets.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import * as ledger from '@midnight-ntwrk/ledger-v6';
|
|
10
|
+
import { WalletFacade } from '@midnight-ntwrk/wallet-sdk-facade';
|
|
11
|
+
import { createKeystore } from '@midnight-ntwrk/wallet-sdk-unshielded-wallet';
|
|
12
|
+
import type { NetworkConfig } from './Config.js';
|
|
13
|
+
export interface WalletContext {
|
|
14
|
+
wallet: WalletFacade;
|
|
15
|
+
shieldedSecretKeys: ledger.ZswapSecretKeys;
|
|
16
|
+
dustSecretKey: ledger.DustSecretKey;
|
|
17
|
+
unshieldedKeystore: ReturnType<typeof createKeystore>;
|
|
18
|
+
}
|
|
19
|
+
export declare function init(seed: string, networkConfig: NetworkConfig): Promise<WalletContext>;
|
|
20
|
+
export declare function waitForSync(walletContext: WalletContext): Promise<void>;
|
|
21
|
+
//# sourceMappingURL=Wallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Wallet.d.ts","sourceRoot":"","sources":["../src/Wallet.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,MAAM,MAAM,2BAA2B,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAIjE,OAAO,EACL,cAAc,EAIf,MAAM,8CAA8C,CAAC;AAEtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,YAAY,CAAC;IACrB,kBAAkB,EAAE,MAAM,CAAC,eAAe,CAAC;IAC3C,aAAa,EAAE,MAAM,CAAC,aAAa,CAAC;IACpC,kBAAkB,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;CACvD;AAED,wBAAsB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CA+C7F;AAED,wBAAsB,WAAW,CAAC,aAAa,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7E"}
|
package/dist/Wallet.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet initialization and management for Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* Handles the three-layer wallet system: shielded, dust, and unshielded wallets.
|
|
5
|
+
*
|
|
6
|
+
* @since 0.1.0
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import * as Rx from 'rxjs';
|
|
10
|
+
import * as ledger from '@midnight-ntwrk/ledger-v6';
|
|
11
|
+
import { WalletFacade } from '@midnight-ntwrk/wallet-sdk-facade';
|
|
12
|
+
import { HDWallet, Roles } from '@midnight-ntwrk/wallet-sdk-hd';
|
|
13
|
+
import { ShieldedWallet } from '@midnight-ntwrk/wallet-sdk-shielded';
|
|
14
|
+
import { DustWallet } from '@midnight-ntwrk/wallet-sdk-dust-wallet';
|
|
15
|
+
import { createKeystore, PublicKey as UnshieldedPublicKey, UnshieldedWallet, InMemoryTransactionHistoryStorage, } from '@midnight-ntwrk/wallet-sdk-unshielded-wallet';
|
|
16
|
+
export async function init(seed, networkConfig) {
|
|
17
|
+
const seedBuffer = Buffer.from(seed, 'hex');
|
|
18
|
+
const configuration = {
|
|
19
|
+
networkId: networkConfig.networkId,
|
|
20
|
+
costParameters: {
|
|
21
|
+
additionalFeeOverhead: 300000000000000000n,
|
|
22
|
+
feeBlocksMargin: 5,
|
|
23
|
+
},
|
|
24
|
+
relayURL: new URL(networkConfig.node),
|
|
25
|
+
provingServerUrl: new URL(networkConfig.proofServer),
|
|
26
|
+
indexerClientConnection: {
|
|
27
|
+
indexerHttpUrl: networkConfig.indexer,
|
|
28
|
+
indexerWsUrl: networkConfig.indexerWS,
|
|
29
|
+
},
|
|
30
|
+
indexerUrl: networkConfig.indexerWS,
|
|
31
|
+
};
|
|
32
|
+
const hdWallet = HDWallet.fromSeed(Uint8Array.from(seedBuffer));
|
|
33
|
+
if (hdWallet.type !== 'seedOk')
|
|
34
|
+
throw new Error('Failed to initialize HDWallet');
|
|
35
|
+
const derivationResult = hdWallet.hdWallet
|
|
36
|
+
.selectAccount(0)
|
|
37
|
+
.selectRoles([Roles.Zswap, Roles.NightExternal, Roles.Dust])
|
|
38
|
+
.deriveKeysAt(0);
|
|
39
|
+
if (derivationResult.type !== 'keysDerived')
|
|
40
|
+
throw new Error('Failed to derive keys');
|
|
41
|
+
hdWallet.hdWallet.clear();
|
|
42
|
+
const shieldedSecretKeys = ledger.ZswapSecretKeys.fromSeed(derivationResult.keys[Roles.Zswap]);
|
|
43
|
+
const dustSecretKey = ledger.DustSecretKey.fromSeed(derivationResult.keys[Roles.Dust]);
|
|
44
|
+
const unshieldedKeystore = createKeystore(derivationResult.keys[Roles.NightExternal], configuration.networkId);
|
|
45
|
+
const shieldedWallet = ShieldedWallet(configuration).startWithSecretKeys(shieldedSecretKeys);
|
|
46
|
+
const dustWallet = DustWallet(configuration).startWithSecretKey(dustSecretKey, ledger.LedgerParameters.initialParameters().dust);
|
|
47
|
+
const unshieldedWallet = UnshieldedWallet({
|
|
48
|
+
...configuration,
|
|
49
|
+
txHistoryStorage: new InMemoryTransactionHistoryStorage(),
|
|
50
|
+
}).startWithPublicKey(UnshieldedPublicKey.fromKeyStore(unshieldedKeystore));
|
|
51
|
+
const wallet = new WalletFacade(shieldedWallet, unshieldedWallet, dustWallet);
|
|
52
|
+
await wallet.start(shieldedSecretKeys, dustSecretKey);
|
|
53
|
+
return { wallet, shieldedSecretKeys, dustSecretKey, unshieldedKeystore };
|
|
54
|
+
}
|
|
55
|
+
export async function waitForSync(walletContext) {
|
|
56
|
+
await Rx.firstValueFrom(walletContext.wallet.state().pipe(Rx.filter((s) => s.isSynced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AAkB,CAAC,CAAC,CAAC;IAE5E,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,cAAc,EAAE,gBAAgB,EAAE,UAAU,CAAC,CAAC;IAC9E,MAAM,MAAM,CAAC,KAAK,CAAC,kBAAkB,EAAE,aAAa,CAAC,CAAC;IAEtD,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,aAAa,EAAE,kBAAkB,EAAE,CAAC;AAC3E,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,aAA4B;IAC5D,MAAM,EAAE,CAAC,cAAc,CAAC,aAAa,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC3F,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Midday SDK - Developer-friendly SDK for building dapps on Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import * as Midday from '@no-witness-labs/midday-sdk';
|
|
7
|
+
*
|
|
8
|
+
* const client = await Midday.Client.create();
|
|
9
|
+
* const contract = await (await client.contractFrom('build/my-contract')).deploy();
|
|
10
|
+
* await contract.call('myAction', arg1, arg2);
|
|
11
|
+
* const state = await contract.ledgerState();
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * as Client from './Client.js';
|
|
18
|
+
export * as Config from './Config.js';
|
|
19
|
+
export * as Wallet from './Wallet.js';
|
|
20
|
+
export * as Providers from './Providers.js';
|
|
21
|
+
export type { ClientConfig, MidnightClient, ContractBuilder, ConnectedContract, CallResult, FinalizedTxData } from './Client.js';
|
|
22
|
+
export type { NetworkConfig } from './Config.js';
|
|
23
|
+
export type { WalletContext } from './Wallet.js';
|
|
24
|
+
export type { ContractProviders, StorageConfig } from './Providers.js';
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAG5C,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,iBAAiB,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACjI,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Midday SDK - Developer-friendly SDK for building dapps on Midnight Network.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import * as Midday from '@no-witness-labs/midday-sdk';
|
|
7
|
+
*
|
|
8
|
+
* const client = await Midday.Client.create();
|
|
9
|
+
* const contract = await (await client.contractFrom('build/my-contract')).deploy();
|
|
10
|
+
* await contract.call('myAction', arg1, arg2);
|
|
11
|
+
* const state = await contract.ledgerState();
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * as Client from './Client.js';
|
|
18
|
+
export * as Config from './Config.js';
|
|
19
|
+
export * as Wallet from './Wallet.js';
|
|
20
|
+
export * as Providers from './Providers.js';
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@no-witness-labs/midday-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Developer-friendly SDK for building dapps on Midnight Network",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"docs",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@midnight-ntwrk/ledger-v6": "6.1.0-alpha.6",
|
|
21
|
+
"@midnight-ntwrk/midnight-js-contracts": "3.0.0-alpha.11",
|
|
22
|
+
"@midnight-ntwrk/midnight-js-http-client-proof-provider": "3.0.0-alpha.11",
|
|
23
|
+
"@midnight-ntwrk/midnight-js-indexer-public-data-provider": "3.0.0-alpha.11",
|
|
24
|
+
"@midnight-ntwrk/midnight-js-level-private-state-provider": "3.0.0-alpha.11",
|
|
25
|
+
"@midnight-ntwrk/midnight-js-network-id": "3.0.0-alpha.11",
|
|
26
|
+
"@midnight-ntwrk/midnight-js-node-zk-config-provider": "3.0.0-alpha.11",
|
|
27
|
+
"@midnight-ntwrk/midnight-js-types": "3.0.0-alpha.11",
|
|
28
|
+
"@midnight-ntwrk/wallet-sdk-dust-wallet": "1.0.0-beta.11",
|
|
29
|
+
"@midnight-ntwrk/wallet-sdk-facade": "1.0.0-beta.12",
|
|
30
|
+
"@midnight-ntwrk/wallet-sdk-hd": "3.0.0-beta.7",
|
|
31
|
+
"@midnight-ntwrk/wallet-sdk-shielded": "1.0.0-beta.12",
|
|
32
|
+
"@midnight-ntwrk/wallet-sdk-unshielded-wallet": "1.0.0-beta.14",
|
|
33
|
+
"pino": "^8.16.0",
|
|
34
|
+
"pino-pretty": "^10.2.3",
|
|
35
|
+
"rxjs": "^7.8.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@changesets/cli": "^2.28.1",
|
|
39
|
+
"typedoc": "^0.28.7",
|
|
40
|
+
"typedoc-plugin-markdown": "^4.6.3",
|
|
41
|
+
"@eslint/js": "^9.34.0",
|
|
42
|
+
"@types/node": "^22.0.0",
|
|
43
|
+
"eslint": "^9.34.0",
|
|
44
|
+
"typescript": "^5.6.0",
|
|
45
|
+
"typescript-eslint": "^8.33.0",
|
|
46
|
+
"vitest": "^3.2.4"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=22.0.0"
|
|
50
|
+
},
|
|
51
|
+
"keywords": [
|
|
52
|
+
"midnight",
|
|
53
|
+
"midnight-network",
|
|
54
|
+
"zk-proof",
|
|
55
|
+
"blockchain",
|
|
56
|
+
"privacy",
|
|
57
|
+
"sdk"
|
|
58
|
+
],
|
|
59
|
+
"author": "No Witness Labs",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"homepage": "https://github.com/no-witness-labs/midday-sdk#readme",
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/no-witness-labs/midday-sdk.git"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public",
|
|
68
|
+
"provenance": true
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc",
|
|
72
|
+
"clean": "rm -rf dist docs",
|
|
73
|
+
"type-check": "tsc --noEmit",
|
|
74
|
+
"lint": "eslint src/",
|
|
75
|
+
"docgen": "typedoc",
|
|
76
|
+
"docgen:html": "typedoc --options typedoc.html.json",
|
|
77
|
+
"test": "vitest run",
|
|
78
|
+
"test:watch": "vitest",
|
|
79
|
+
"changeset": "changeset",
|
|
80
|
+
"changeset:version": "changeset version",
|
|
81
|
+
"changeset:publish": "pnpm build && changeset publish"
|
|
82
|
+
}
|
|
83
|
+
}
|