moneyos 0.2.1 → 0.3.2

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/dist/index.d.ts CHANGED
@@ -1,13 +1,5 @@
1
- import { Hex, Address } from 'viem';
1
+ import { Address, Hex, Account } from 'viem';
2
2
 
3
- interface MoneyOSConfig {
4
- /** Default chain ID (e.g. 42161 for Arbitrum) */
5
- chainId: number;
6
- /** RPC URL override. Uses public RPC if not set. */
7
- rpcUrl?: string;
8
- /** Private key for signing transactions (hex string) */
9
- privateKey?: Hex;
10
- }
11
3
  interface Balance {
12
4
  token: string;
13
5
  symbol: string;
@@ -68,14 +60,190 @@ interface Chain {
68
60
  blockExplorer: string;
69
61
  }
70
62
 
63
+ interface Token {
64
+ symbol: string;
65
+ name: string;
66
+ decimals: number;
67
+ addresses: Record<number, Address>;
68
+ }
69
+ declare const NATIVE_TOKEN_ADDRESS: Address;
70
+ declare const tokens: Record<string, Token>;
71
+ declare function getToken(symbol: string): Token | undefined;
72
+ declare function getTokenAddress(symbol: string, chainId: number): Address | undefined;
73
+
74
+ declare const chains: Record<string, Chain>;
75
+ declare const defaultChain: Chain;
76
+ declare function getChain(idOrName: number | string): Chain | undefined;
77
+
78
+ interface CallRequest {
79
+ to: Address;
80
+ data?: Hex;
81
+ value?: bigint;
82
+ chainId: number;
83
+ }
84
+ interface ExecutionResult {
85
+ hash: Hex;
86
+ chainId: number;
87
+ }
88
+ interface ExecutionClient {
89
+ mode: "eoa" | "smart-account" | "delegated";
90
+ getAddress(): Address;
91
+ send(call: CallRequest): Promise<ExecutionResult>;
92
+ sendBatch?(calls: CallRequest[]): Promise<ExecutionResult>;
93
+ capabilities(): {
94
+ sponsoredGas: boolean;
95
+ batching: boolean;
96
+ simulation: boolean;
97
+ };
98
+ }
99
+ interface ReadClient {
100
+ getBalance(params: {
101
+ address: Address;
102
+ chainId: number;
103
+ }): Promise<bigint>;
104
+ readContract<T = unknown>(params: {
105
+ address: Address;
106
+ abi: readonly unknown[];
107
+ functionName: string;
108
+ args?: readonly unknown[];
109
+ chainId: number;
110
+ }): Promise<T>;
111
+ }
112
+ interface AccessAdapter {
113
+ name: string;
114
+ openSession(ctx: SessionContext): Promise<AccessSession>;
115
+ }
116
+ interface AccessSession {
117
+ kind: "local" | "social" | "hardware";
118
+ getAddress(): Address;
119
+ }
120
+ interface SessionContext {
121
+ chainId: number;
122
+ rpcUrl?: string;
123
+ }
124
+ interface MoneyOSAction<I = unknown, O = unknown> {
125
+ name: string;
126
+ description: string;
127
+ run(input: I, ctx: ActionContext): Promise<O>;
128
+ }
129
+ interface ActionContext {
130
+ read: ReadClient;
131
+ execute: ExecutionClient;
132
+ assets: AssetRegistry;
133
+ }
134
+ interface AssetRegistry {
135
+ getToken(symbol: string): Token | undefined;
136
+ getTokenAddress(symbol: string, chainId: number): Address | undefined;
137
+ getChain(idOrName: number | string): Chain | undefined;
138
+ nativeTokenAddress: Address;
139
+ }
140
+ interface MoneyOSRuntime {
141
+ read: ReadClient;
142
+ execute: ExecutionClient;
143
+ assets: AssetRegistry;
144
+ config: RuntimeConfig;
145
+ }
146
+ interface RuntimeConfig {
147
+ defaultChainId: number;
148
+ rpcUrl?: string;
149
+ }
150
+ /**
151
+ * Configuration for a MoneyOS instance.
152
+ *
153
+ * Three construction modes, mutually exclusive:
154
+ *
155
+ * 1. Default EOA from private key: pass `privateKey`. A default
156
+ * `EOAExecutor` and `ViemReadClient` are created for you. Legacy shortcut
157
+ * for the common "I have a hex key" case.
158
+ *
159
+ * 2. Pre-loaded signer: pass `signer` — a viem `Account` resolved elsewhere
160
+ * (typically from a `KeyStore`: file, 1Password, hardware, KMS, MPC). A
161
+ * default `EOAExecutor` is built around it. This is the preferred path
162
+ * when key loading is async, because the keystore boundary stays async
163
+ * while `MoneyOS` construction remains synchronous.
164
+ *
165
+ * 3. Injected runtime: pass `execute` (and optionally `read` / `assets`) to
166
+ * plug in custom implementations — e.g. a gasless smart-account executor
167
+ * or a custom read client. Any part you do not inject falls back to the
168
+ * default.
169
+ *
170
+ * `privateKey`, `signer`, and `execute` are mutually exclusive — passing
171
+ * more than one throws.
172
+ */
173
+ interface MoneyOSConfig {
174
+ /** Default chain ID (e.g. 42161 for Arbitrum). */
175
+ chainId: number;
176
+ /** RPC URL override. Uses public RPC if not set. */
177
+ rpcUrl?: string;
178
+ /** Private key for signing transactions (hex string). Creates a default EOAExecutor. Mutually exclusive with `signer` and `execute`. */
179
+ privateKey?: Hex;
180
+ /** Pre-loaded viem `Account`, typically resolved from a `KeyStore`. Creates a default EOAExecutor. Mutually exclusive with `privateKey` and `execute`. */
181
+ signer?: Account;
182
+ /** Inject a custom ExecutionClient (e.g. a gasless smart-account executor). Mutually exclusive with `privateKey` and `signer`. */
183
+ execute?: ExecutionClient;
184
+ /** Inject a custom ReadClient. Defaults to ViemReadClient. */
185
+ read?: ReadClient;
186
+ /** Inject a custom AssetRegistry. Defaults to the built-in registry. */
187
+ assets?: AssetRegistry;
188
+ }
189
+
190
+ /**
191
+ * KeyStore — pluggable backend for loading an EOA signer.
192
+ *
193
+ * A KeyStore decides *how* a signer is obtained. It does not decide how
194
+ * transactions are sent — that remains the responsibility of
195
+ * `ExecutionClient`.
196
+ *
197
+ * The interface is deliberately narrow:
198
+ * - `loadSigner()` returns a viem `Account`, not a raw private key.
199
+ * There is intentionally no `getPrivateKey()` method. Future
200
+ * hardware-, KMS-, or MPC-backed stores must fit the same shape.
201
+ * - `metadata()` is async because some stores may need I/O even to answer
202
+ * questions about the stored key.
203
+ * - `hasKey()` lets callers probe the store without triggering a full
204
+ * `loadSigner()` (which may prompt the user for biometric auth).
205
+ */
206
+ /**
207
+ * Identifies the backend kind of a `KeyStore`. `"custom"` is reserved for
208
+ * stores that don't fit any of the built-in kinds and is not expected to be
209
+ * used in v1.
210
+ */
211
+ type KeyStoreKind = "file" | "hardware" | "kms" | "custom";
212
+ /**
213
+ * Non-secret descriptive information about a stored key. Safe to log or
214
+ * display.
215
+ */
216
+ interface KeyStoreMetadata {
217
+ kind: KeyStoreKind;
218
+ /** Derived EOA address, if the store can produce it without loading the signer. */
219
+ address?: Address;
220
+ /** Optional human-readable label for diagnostics. */
221
+ label?: string;
222
+ }
223
+ interface KeyStore {
224
+ readonly kind: KeyStoreKind;
225
+ metadata(): Promise<KeyStoreMetadata>;
226
+ hasKey(): Promise<boolean>;
227
+ /**
228
+ * Load a viem `Account` that can sign transactions and messages.
229
+ *
230
+ * Returns viem's `Account` type (not the narrower `LocalAccount`) so that
231
+ * future stores backed by hardware wallets, remote KMS, or MPC can return
232
+ * a `toAccount({ signMessage, signTransaction, signTypedData })` wrapper
233
+ * without changing this interface.
234
+ */
235
+ loadSigner(): Promise<Account>;
236
+ }
237
+
71
238
  declare class MoneyOS {
72
- private config;
73
- private publicClients;
74
- private walletClient;
239
+ private read;
240
+ private executor;
241
+ private assets;
242
+ private runtimeConfig;
75
243
  constructor(config: MoneyOSConfig);
76
- private getPublicClient;
77
- private getWalletClient;
244
+ get runtime(): MoneyOSRuntime;
78
245
  get address(): Address;
246
+ private requireExecutor;
79
247
  balance(token: string, options?: {
80
248
  address?: Address;
81
249
  chainId?: number;
@@ -89,21 +257,6 @@ declare class MoneyOS {
89
257
  }): Promise<SwapResult>;
90
258
  }
91
259
 
92
- declare const chains: Record<string, Chain>;
93
- declare const defaultChain: Chain;
94
- declare function getChain(idOrName: number | string): Chain | undefined;
95
-
96
- interface Token {
97
- symbol: string;
98
- name: string;
99
- decimals: number;
100
- addresses: Record<number, Address>;
101
- }
102
- declare const NATIVE_TOKEN_ADDRESS: Address;
103
- declare const tokens: Record<string, Token>;
104
- declare function getToken(symbol: string): Token | undefined;
105
- declare function getTokenAddress(symbol: string, chainId: number): Address | undefined;
106
-
107
260
  declare class OdosProvider implements SwapProvider {
108
261
  name: string;
109
262
  private apiKey?;
@@ -132,4 +285,121 @@ declare class OdosProvider implements SwapProvider {
132
285
  }>;
133
286
  }
134
287
 
135
- export { type Balance, type Chain, MoneyOS, type MoneyOSConfig, NATIVE_TOKEN_ADDRESS, OdosProvider, type SendResult, type SwapProvider, type SwapQuote, type SwapResult, type Token, chains, defaultChain, getChain, getToken, getTokenAddress, tokens };
288
+ declare class ViemReadClient implements ReadClient {
289
+ private clients;
290
+ private config;
291
+ constructor(config: RuntimeConfig);
292
+ private getClient;
293
+ getBalance(params: {
294
+ address: Address;
295
+ chainId: number;
296
+ }): Promise<bigint>;
297
+ readContract<T = unknown>(params: {
298
+ address: Address;
299
+ abi: readonly unknown[];
300
+ functionName: string;
301
+ args?: readonly unknown[];
302
+ chainId: number;
303
+ }): Promise<T>;
304
+ }
305
+ declare class EOAExecutor implements ExecutionClient {
306
+ readonly mode: "eoa";
307
+ private signer;
308
+ private walletClients;
309
+ private publicClients;
310
+ private config;
311
+ constructor(signer: Account, config: RuntimeConfig);
312
+ /**
313
+ * Convenience factory: build an EOAExecutor from a raw private key.
314
+ * Equivalent to `new EOAExecutor(privateKeyToAccount(privateKey), config)`.
315
+ * Kept as a helper so the common "I have a hex key" path stays one line
316
+ * while the constructor itself takes a viem `Account` to accommodate
317
+ * future keystore-backed signers (hardware, KMS, MPC).
318
+ *
319
+ * Attach viem's nonce manager so back-to-back live transactions use a
320
+ * pending-aware nonce source instead of relying on RPC fill behavior.
321
+ */
322
+ static fromPrivateKey(privateKey: Hex, config: RuntimeConfig): EOAExecutor;
323
+ private getWalletClient;
324
+ private getPublicClient;
325
+ getAddress(): Address;
326
+ send(call: CallRequest): Promise<ExecutionResult>;
327
+ capabilities(): {
328
+ sponsoredGas: boolean;
329
+ batching: boolean;
330
+ simulation: boolean;
331
+ };
332
+ }
333
+
334
+ declare class LocalAccessAdapter implements AccessAdapter {
335
+ readonly name = "local";
336
+ private privateKey;
337
+ constructor(privateKey: Hex);
338
+ openSession(_ctx: SessionContext): Promise<AccessSession>;
339
+ }
340
+
341
+ /**
342
+ * FileKeyStore — raw-key JSON helper for library consumers.
343
+ *
344
+ * Reads a raw private key from a JSON file on disk (by default
345
+ * `~/.moneyos/config.json`) and derives a viem `Account` from it.
346
+ *
347
+ * MoneyOS CLI no longer uses plaintext `config.json` wallet storage as its
348
+ * default path. The CLI now uses an encrypted wallet file plus a local unlock
349
+ * session. This helper remains available for advanced SDK use cases and
350
+ * explicit raw-key compatibility paths outside the default CLI flow.
351
+ */
352
+ interface FileKeyStoreOptions {
353
+ /**
354
+ * Absolute path to the JSON config file that holds the private key.
355
+ * Defaults to `~/.moneyos/config.json` for compatibility with earlier
356
+ * raw-key layouts and explicit SDK usage.
357
+ */
358
+ configPath?: string;
359
+ }
360
+ declare class FileKeyStore implements KeyStore {
361
+ readonly kind: "file";
362
+ private configPath;
363
+ constructor(options?: FileKeyStoreOptions);
364
+ hasKey(): Promise<boolean>;
365
+ metadata(): Promise<KeyStoreMetadata>;
366
+ loadSigner(): Promise<Account>;
367
+ /**
368
+ * Read and validate the config file. Returns `undefined` if the file does
369
+ * not exist. Throws with context for any other failure mode (malformed
370
+ * JSON, wrong top-level type, wrong `privateKey` type). The hex format of
371
+ * the key itself is validated later by viem's `privateKeyToAccount`.
372
+ */
373
+ private readConfig;
374
+ }
375
+
376
+ /**
377
+ * Create a MoneyOS instance.
378
+ *
379
+ * Default EOA usage:
380
+ * ```ts
381
+ * const moneyos = createMoneyOS({ chainId: 42161, privateKey: "0x..." });
382
+ * await moneyos.send("USDC", "0x...", "10");
383
+ * ```
384
+ *
385
+ * Custom execution (e.g. gasless smart-account):
386
+ * ```ts
387
+ * import { ParticleExecutor } from "@moneyos/executor-particle";
388
+ * const moneyos = createMoneyOS({
389
+ * chainId: 42161,
390
+ * execute: new ParticleExecutor({ ... }),
391
+ * });
392
+ * // moneyos.send / moneyos.swap transparently use the injected executor
393
+ * ```
394
+ *
395
+ * You can also inject `read` or `assets` if you need custom implementations.
396
+ * `privateKey` and `execute` are mutually exclusive.
397
+ *
398
+ * Future (with tools):
399
+ * ```ts
400
+ * const moneyos = createMoneyOS(config).use(swapTool());
401
+ * ```
402
+ */
403
+ declare function createMoneyOS(config: MoneyOSConfig): MoneyOS;
404
+
405
+ export { type AccessAdapter, type AccessSession, type ActionContext, type AssetRegistry, type Balance, type CallRequest, type Chain, EOAExecutor, type ExecutionClient, type ExecutionResult, FileKeyStore, type FileKeyStoreOptions, type KeyStore, type KeyStoreKind, type KeyStoreMetadata, LocalAccessAdapter, MoneyOS, type MoneyOSAction, type MoneyOSConfig, type MoneyOSRuntime, NATIVE_TOKEN_ADDRESS, OdosProvider, type ReadClient, type RuntimeConfig, type SendResult, type SessionContext, type SwapProvider, type SwapQuote, type SwapResult, type Token, ViemReadClient, chains, createMoneyOS, defaultChain, getChain, getToken, getTokenAddress, tokens };