@sequence0/sdk 0.1.0 → 1.0.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.
Files changed (52) hide show
  1. package/README.md +63 -5
  2. package/dist/chains/bitcoin.d.ts +15 -3
  3. package/dist/chains/bitcoin.d.ts.map +1 -1
  4. package/dist/chains/bitcoin.js +23 -3
  5. package/dist/chains/bitcoin.js.map +1 -1
  6. package/dist/chains/ethereum.d.ts +3 -0
  7. package/dist/chains/ethereum.d.ts.map +1 -1
  8. package/dist/chains/ethereum.js +156 -20
  9. package/dist/chains/ethereum.js.map +1 -1
  10. package/dist/core/client.d.ts +132 -2
  11. package/dist/core/client.d.ts.map +1 -1
  12. package/dist/core/client.js +501 -32
  13. package/dist/core/client.js.map +1 -1
  14. package/dist/core/types.d.ts +55 -3
  15. package/dist/core/types.d.ts.map +1 -1
  16. package/dist/index.d.ts +15 -6
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +35 -3
  19. package/dist/index.js.map +1 -1
  20. package/dist/utils/discovery.d.ts +95 -0
  21. package/dist/utils/discovery.d.ts.map +1 -0
  22. package/dist/utils/discovery.js +212 -0
  23. package/dist/utils/discovery.js.map +1 -0
  24. package/dist/utils/errors.d.ts +26 -0
  25. package/dist/utils/errors.d.ts.map +1 -1
  26. package/dist/utils/errors.js +32 -1
  27. package/dist/utils/errors.js.map +1 -1
  28. package/dist/utils/fee.d.ts +107 -0
  29. package/dist/utils/fee.d.ts.map +1 -0
  30. package/dist/utils/fee.js +220 -0
  31. package/dist/utils/fee.js.map +1 -0
  32. package/dist/utils/http.d.ts +98 -2
  33. package/dist/utils/http.d.ts.map +1 -1
  34. package/dist/utils/http.js +238 -6
  35. package/dist/utils/http.js.map +1 -1
  36. package/dist/utils/logger.d.ts +43 -0
  37. package/dist/utils/logger.d.ts.map +1 -0
  38. package/dist/utils/logger.js +129 -0
  39. package/dist/utils/logger.js.map +1 -0
  40. package/dist/utils/rate-limiter.d.ts +43 -0
  41. package/dist/utils/rate-limiter.d.ts.map +1 -0
  42. package/dist/utils/rate-limiter.js +99 -0
  43. package/dist/utils/rate-limiter.js.map +1 -0
  44. package/dist/utils/validation.d.ts +74 -0
  45. package/dist/utils/validation.d.ts.map +1 -0
  46. package/dist/utils/validation.js +380 -0
  47. package/dist/utils/validation.js.map +1 -0
  48. package/dist/wallet/wallet.d.ts +13 -1
  49. package/dist/wallet/wallet.d.ts.map +1 -1
  50. package/dist/wallet/wallet.js +86 -21
  51. package/dist/wallet/wallet.js.map +1 -1
  52. package/package.json +9 -3
@@ -25,30 +25,86 @@
25
25
  import { NetworkConfig, CreateWalletOptions, HealthResponse, StatusResponse, WalletDetail, SignResultResponse } from './types';
26
26
  import { Wallet } from '../wallet/wallet';
27
27
  import { WsClient } from '../utils/websocket';
28
+ import { UnsignedFeeTx } from '../utils/fee';
28
29
  export declare class Sequence0 {
29
30
  private config;
30
31
  private http;
31
32
  private ws;
33
+ private discovery;
34
+ private feeManager;
35
+ private resolvedAgentUrl;
36
+ private circuitBreaker;
37
+ /** Map of agent API URL -> timestamp when the agent was marked as failed */
38
+ private failedAgents;
39
+ /** Optional signer for wallet ownership proofs */
40
+ private ownerSigner;
41
+ /** Owner's Ethereum address (derived from private key when provided) */
42
+ private ownerAddress;
32
43
  /**
33
44
  * Create a new Sequence0 SDK client
34
45
  *
46
+ * On testnet, connects to the Sequence0-operated agent by default.
47
+ * On mainnet, auto-discovers agents from the on-chain AgentRegistry.
48
+ *
35
49
  * @example
36
50
  * ```typescript
51
+ * // Mainnet -- auto-discovers agents from on-chain registry
37
52
  * const s0 = new Sequence0({ network: 'mainnet' });
53
+ *
54
+ * // Or specify a specific agent
55
+ * const s0 = new Sequence0({ network: 'mainnet', agentUrl: 'http://my-agent:8080' });
56
+ *
57
+ * // With debug logging and custom rate limit
58
+ * const s0 = new Sequence0({
59
+ * network: 'mainnet',
60
+ * debug: true,
61
+ * maxRetries: 5,
62
+ * rateLimiter: { maxRequestsPerSecond: 20 },
63
+ * });
38
64
  * ```
39
65
  */
40
66
  constructor(config: NetworkConfig);
67
+ /**
68
+ * Resolve an agent URL -- uses direct URL if set, otherwise discovers from registry.
69
+ * Filters out recently-failed agents during discovery.
70
+ */
71
+ private getHttp;
72
+ /**
73
+ * Select a healthy agent from the registry, filtering out
74
+ * agents that failed within the last AGENT_EXCLUSION_TTL ms.
75
+ */
76
+ private selectAgent;
77
+ /**
78
+ * Mark the current agent as failed and switch to a different one.
79
+ * Called internally when a request to the current agent fails
80
+ * after exhausting retries or when the circuit breaker trips.
81
+ */
82
+ private failoverToNextAgent;
83
+ /**
84
+ * Remove expired entries from the failed agents map.
85
+ */
86
+ private pruneExpiredFailures;
87
+ /**
88
+ * Create a new HttpClient with the shared circuit breaker and current config.
89
+ */
90
+ private createHttpClient;
91
+ /**
92
+ * Execute an HTTP request with automatic agent failover.
93
+ * If the current agent's circuit breaker trips or all retries fail,
94
+ * tries to failover to a different agent (up to 2 failover attempts).
95
+ */
96
+ private withFailover;
41
97
  /**
42
98
  * Create a new threshold wallet via DKG ceremony
43
99
  *
44
100
  * Initiates Distributed Key Generation with the agent network.
45
- * The private key is never assembled each agent holds a share.
101
+ * The private key is never assembled -- each agent holds a share.
46
102
  *
47
103
  * @example
48
104
  * ```typescript
49
105
  * const wallet = await s0.createWallet({ chain: 'ethereum' });
50
106
  * console.log(wallet.address); // 0x...
51
- * console.log(wallet.threshold); // { t: 17, n: 24 }
107
+ * console.log(wallet.threshold); // { t: 16, n: 24 }
52
108
  * ```
53
109
  */
54
110
  createWallet(options: CreateWalletOptions): Promise<Wallet>;
@@ -104,6 +160,63 @@ export declare class Sequence0 {
104
160
  * Request key refresh for a wallet (proactive security)
105
161
  */
106
162
  refreshKeys(walletId: string): Promise<void>;
163
+ /**
164
+ * Discover active agents from the on-chain registry
165
+ */
166
+ discoverAgents(): Promise<import("../utils/discovery").AgentInfo[]>;
167
+ /**
168
+ * Discover all active agents from the on-chain registry (paginated).
169
+ * Fetches all pages when there are more than 100 agents.
170
+ */
171
+ discoverAllAgents(): Promise<import("../utils/discovery").AgentInfo[]>;
172
+ /**
173
+ * Get the current per-signature fee from the FeeCollector contract.
174
+ *
175
+ * The fee is paid on the Sequence0 chain (chain ID 800801) in native S0
176
+ * tokens. 80% goes to the signing agents, 10% to protocol treasury,
177
+ * 10% to the reserve fund.
178
+ *
179
+ * Returns 0n on testnet (no fees).
180
+ *
181
+ * @returns Fee in wei as bigint
182
+ *
183
+ * @example
184
+ * ```typescript
185
+ * const fee = await s0.getSignatureFee();
186
+ * console.log(`Fee: ${fee} wei`);
187
+ * ```
188
+ */
189
+ getSignatureFee(): Promise<bigint>;
190
+ /**
191
+ * Build an unsigned fee-payment transaction for a wallet's signing committee.
192
+ *
193
+ * This looks up the wallet's committee from the agent network, resolves
194
+ * each committee member's Ethereum payment address from the on-chain
195
+ * AgentRegistry, and builds an unsigned `collectFee()` transaction for
196
+ * the FeeCollector contract.
197
+ *
198
+ * **The app developer must sign and send this transaction themselves** on the
199
+ * Sequence0 chain (RPC: https://rpc.sequence0.network, chain ID: 800801).
200
+ * They need S0 native tokens to cover the fee.
201
+ *
202
+ * Returns null on testnet (no fees).
203
+ *
204
+ * @param walletId - The wallet ID that will be signed with
205
+ * @returns Unsigned transaction `{ to, data, value }` or null if no fee required
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * const feeTx = await s0.buildFeeTx('my-wallet-id');
210
+ * if (feeTx) {
211
+ * // Sign and send with your own wallet on the Sequence0 chain
212
+ * const tx = await signer.sendTransaction(feeTx);
213
+ * await tx.wait();
214
+ * }
215
+ * // Now request the signature
216
+ * const sig = await s0.signAndWait('my-wallet-id', messageHex);
217
+ * ```
218
+ */
219
+ buildFeeTx(walletId: string): Promise<UnsignedFeeTx | null>;
107
220
  /**
108
221
  * Subscribe to real-time events from the agent network
109
222
  *
@@ -115,9 +228,26 @@ export declare class Sequence0 {
115
228
  * ```
116
229
  */
117
230
  subscribe(walletId?: string): Promise<WsClient>;
231
+ /**
232
+ * Clean up all resources (HTTP client, rate limiter, WebSocket, circuit breaker).
233
+ * Call this when you are done using the SDK.
234
+ */
235
+ destroy(): void;
118
236
  private getWsClient;
119
237
  private getRpcUrl;
120
238
  private getCurveForChain;
239
+ /**
240
+ * Build an ownership proof for a sign request.
241
+ *
242
+ * The proof is: sign( keccak256( wallet_id_bytes + message_hex_bytes + timestamp_be_bytes ) )
243
+ *
244
+ * - wallet_id_bytes: UTF-8 encoding of the wallet ID string
245
+ * - message_hex_bytes: the raw bytes of the hex-encoded message (i.e. the message as passed to /sign)
246
+ * - timestamp_be_bytes: 8-byte big-endian encoding of the Unix epoch second
247
+ *
248
+ * Returns null when no ownerSigner is configured (backwards compatible).
249
+ */
250
+ private signOwnershipProof;
121
251
  private generateWalletId;
122
252
  }
123
253
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/core/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EACH,aAAa,EACb,mBAAmB,EAEnB,cAAc,EACd,cAAc,EAEd,YAAY,EAGZ,kBAAkB,EACrB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,EAAgB,MAAM,kBAAkB,CAAC;AAExD,OAAO,EAAE,QAAQ,EAAa,MAAM,oBAAoB,CAAC;AA+BzD,qBAAa,SAAS;IAClB,OAAO,CAAC,MAAM,CAA2D;IACzE,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,EAAE,CAAyB;IAEnC;;;;;;;OAOG;gBACS,MAAM,EAAE,aAAa;IAqBjC;;;;;;;;;;;;OAYG;IACG,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,MAAM,CAAC;IAiEjE;;;;;;;;;;OAUG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAsBlD;;OAEG;IACG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