@1sat/wallet-toolbox 0.0.2 → 0.0.3

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.
@@ -3,7 +3,8 @@ import { Wallet, type WalletStorageManager } from "@bsv/wallet-toolbox/mobile";
3
3
  import type { Chain } from "@bsv/wallet-toolbox/mobile/out/src/sdk/types";
4
4
  import { Outpoint } from "./indexers/Outpoint";
5
5
  import type { Indexer, ParseContext, Txo } from "./indexers/types";
6
- import { OneSatServices, type SyncOutput } from "./services/OneSatServices";
6
+ import { OneSatServices } from "./services/OneSatServices";
7
+ import type { SyncQueueStorage } from "./sync/types";
7
8
  /**
8
9
  * Result of ingestTransaction including parse context for debugging
9
10
  */
@@ -15,53 +16,21 @@ export interface IngestResult {
15
16
  * Events emitted by OneSatWallet during sync operations
16
17
  */
17
18
  export interface OneSatWalletEvents {
19
+ /** Sync started */
18
20
  "sync:start": {
19
- address: string;
20
- fromScore: number;
21
- };
22
- "sync:output": {
23
- address: string;
24
- output: SyncOutput;
25
- };
26
- "sync:skipped": {
27
- address: string;
28
- outpoint: string;
29
- reason: string;
30
- };
31
- "sync:parsed": {
32
- address: string;
33
- txid: string;
34
- parseContext: ParseContext;
35
- internalizedCount: number;
36
- };
37
- "sync:error": {
38
- address: string;
39
- error: Error;
40
- };
41
- "sync:complete": {
42
- address: string;
43
- };
44
- "syncAll:start": {
45
21
  addresses: string[];
46
- fromScore: number;
47
- };
48
- "syncAll:output": {
49
- output: SyncOutput;
50
- };
51
- "syncAll:skipped": {
52
- outpoint: string;
53
- reason: string;
54
- };
55
- "syncAll:parsed": {
56
- txid: string;
57
- parseContext: ParseContext;
58
- internalizedCount: number;
59
22
  };
60
- "syncAll:error": {
61
- error: Error;
23
+ /** Sync progress update */
24
+ "sync:progress": {
25
+ pending: number;
26
+ done: number;
27
+ failed: number;
62
28
  };
63
- "syncAll:complete": {
64
- addresses: string[];
29
+ /** Sync complete (queue empty and stream done) */
30
+ "sync:complete": Record<string, never>;
31
+ /** Sync error */
32
+ "sync:error": {
33
+ message: string;
65
34
  };
66
35
  }
67
36
  type EventCallback<T> = (event: T) => void;
@@ -95,6 +64,15 @@ export interface OneSatWalletArgs {
95
64
  * Automatically start syncing all owner addresses on construction.
96
65
  */
97
66
  autoSync?: boolean;
67
+ /**
68
+ * Sync queue storage for background processing.
69
+ * If provided, enables queue-based sync via sync() method.
70
+ */
71
+ syncQueue?: SyncQueueStorage;
72
+ /**
73
+ * Batch size for queue processing (default: 20).
74
+ */
75
+ syncBatchSize?: number;
98
76
  }
99
77
  /**
100
78
  * OneSatWallet extends the BRC-100 Wallet with 1Sat-specific indexing and services.
@@ -109,8 +87,16 @@ export declare class OneSatWallet extends Wallet {
109
87
  readonly services: OneSatServices;
110
88
  private owners;
111
89
  private listeners;
112
- private activeSyncs;
113
- private activeMultiSync;
90
+ private syncQueue;
91
+ private syncBatchSize;
92
+ private syncRunning;
93
+ private syncStopRequested;
94
+ private activeQueueSync;
95
+ private sseStreamActive;
96
+ private sseUnsubscribe;
97
+ private processorActive;
98
+ private processorStopRequested;
99
+ private streamDone;
114
100
  constructor(args: OneSatWalletArgs);
115
101
  /**
116
102
  * Returns true if this wallet was created with only a public key.
@@ -215,46 +201,87 @@ export declare class OneSatWallet extends Wallet {
215
201
  */
216
202
  broadcast(tx: Transaction, description: string, labels?: string[]): Promise<IngestResult>;
217
203
  /**
218
- * Sync a single address from the 1Sat indexer using Server-Sent Events.
219
- * Runs in the background - use stopSync() or close() to stop.
204
+ * Start queue-based sync for all owner addresses.
205
+ * Requires syncQueue to be provided in constructor args.
220
206
  *
221
- * @param address - The address to sync
207
+ * This method:
208
+ * 1. Opens SSE stream and enqueues outputs
209
+ * 2. Processes queue in batches using Promise.all()
210
+ * 3. Continues until queue is empty and stream is done
211
+ */
212
+ sync(): Promise<void>;
213
+ /**
214
+ * Handle a single output from the SSE stream.
215
+ * Enqueues to the sync queue and updates lastQueuedScore with reorg protection.
222
216
  */
223
- syncAddress(address: string, fromScore?: number): void;
217
+ private handleSyncOutput;
224
218
  /**
225
- * Process a spend transaction during sync.
226
- * Only marks our output as spent - doesn't ingest the spend tx.
227
- * If the spend tx has outputs we own, they'll come through sync separately.
219
+ * Process queue in batches until empty or stopped.
228
220
  */
229
- private processSpendTx;
221
+ private processQueueLoop;
230
222
  /**
231
- * Stop syncing a specific address.
223
+ * Group queue items by txid.
224
+ * @deprecated - claim() now returns items already grouped
232
225
  */
233
- stopSync(address: string): void;
226
+ private groupItemsByTxid;
234
227
  /**
235
- * Close the wallet and cleanup all active sync connections.
228
+ * Process a single txid - ingest transaction and complete queue items.
229
+ * Items are already marked as "processing" by claim().
230
+ */
231
+ private processTxid;
232
+ /**
233
+ * Ingest a transaction with knowledge of which outputs are already spent.
234
+ */
235
+ private ingestWithSpendInfo;
236
+ /**
237
+ * Mark outputs as spent for spend-only queue items.
238
+ */
239
+ private markOutputsSpent;
240
+ /**
241
+ * Stop the sync.
242
+ */
243
+ stopSync(): void;
244
+ /**
245
+ * Close the wallet and cleanup all sync connections.
236
246
  */
237
247
  close(): void;
238
248
  /**
239
- * Start syncing all owner addresses using a single multi-owner SSE connection.
240
- * This is more efficient than syncing each address individually.
241
- *
242
- * @param fromScore - Starting score (for pagination/resumption)
249
+ * Check if sync is currently running.
250
+ */
251
+ isSyncing(): boolean;
252
+ /**
253
+ * Get the sync queue instance (if provided).
254
+ */
255
+ getQueue(): SyncQueueStorage | null;
256
+ /**
257
+ * Start only the SSE stream, enqueueing outputs without processing.
258
+ * Useful for testing to observe queue buildup.
259
+ */
260
+ startStream(): Promise<void>;
261
+ /**
262
+ * Stop the SSE stream.
263
+ */
264
+ stopStream(): void;
265
+ /**
266
+ * Check if SSE stream is active.
267
+ */
268
+ isStreamActive(): boolean;
269
+ /**
270
+ * Check if SSE stream has completed.
243
271
  */
244
- syncAll(fromScore?: number): void;
272
+ isStreamDone(): boolean;
245
273
  /**
246
- * Process a spend transaction during multi-owner sync.
247
- * Only marks our output as spent - doesn't ingest the spend tx.
274
+ * Start only the queue processor, without starting a new SSE stream.
275
+ * Useful for testing to process queued items independently.
248
276
  */
249
- private processSpendTxMulti;
277
+ startProcessor(): Promise<void>;
250
278
  /**
251
- * Stop the multi-owner sync.
279
+ * Stop the queue processor.
252
280
  */
253
- stopSyncAll(): void;
281
+ stopProcessor(): void;
254
282
  /**
255
- * Start syncing each owner address individually.
256
- * For most cases, use syncAll() instead which uses a single connection.
283
+ * Check if queue processor is active.
257
284
  */
258
- syncEach(): void;
285
+ isProcessorActive(): boolean;
259
286
  }
260
287
  export {};
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export { OneSatWallet, type OneSatWalletArgs, type OneSatWalletEvents, type IngestResult, } from "./OneSatWallet";
2
2
  export { OneSatServices, type SyncOutput } from "./services/OneSatServices";
3
- export type { OrdfsMetadata, Capability } from "./services/types";
3
+ export type { OrdfsMetadata, OrdfsContentOptions, OrdfsContentResponse, OrdfsResponseHeaders, Capability, } from "./services/types";
4
4
  export * from "./services/client";
5
5
  export { ReadOnlySigner } from "./signers/ReadOnlySigner";
6
6
  export * from "./indexers";
7
+ export * from "./sync";
8
+ export { WalletStorageManager, StorageProvider, } from "@bsv/wallet-toolbox/mobile";
9
+ export { StorageIdb } from "@bsv/wallet-toolbox/mobile/out/src/storage/StorageIdb";