@1sat/wallet-toolbox 0.0.1 → 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.
Files changed (34) hide show
  1. package/README.md +21 -21
  2. package/dist/OneSatWallet.d.ts +209 -54
  3. package/dist/index.d.ts +6 -4
  4. package/dist/index.js +34940 -32915
  5. package/dist/indexers/Bsv21Indexer.d.ts +5 -5
  6. package/dist/indexers/CosignIndexer.d.ts +3 -3
  7. package/dist/indexers/FundIndexer.d.ts +3 -3
  8. package/dist/indexers/InscriptionIndexer.d.ts +3 -2
  9. package/dist/indexers/LockIndexer.d.ts +3 -3
  10. package/dist/indexers/MapIndexer.d.ts +2 -3
  11. package/dist/indexers/OpNSIndexer.d.ts +2 -2
  12. package/dist/indexers/OrdLockIndexer.d.ts +3 -3
  13. package/dist/indexers/OriginIndexer.d.ts +19 -5
  14. package/dist/indexers/SigmaIndexer.d.ts +14 -2
  15. package/dist/indexers/index.d.ts +5 -6
  16. package/dist/indexers/types.d.ts +24 -10
  17. package/dist/services/OneSatServices.d.ts +39 -61
  18. package/dist/services/client/ArcadeClient.d.ts +45 -0
  19. package/dist/services/client/BaseClient.d.ts +32 -0
  20. package/dist/services/client/BeefClient.d.ts +26 -0
  21. package/dist/services/client/Bsv21Client.d.ts +46 -0
  22. package/dist/services/client/ChaintracksClient.d.ts +62 -0
  23. package/dist/services/client/OrdfsClient.d.ts +47 -0
  24. package/dist/services/client/OwnerClient.d.ts +41 -0
  25. package/dist/services/client/TxoClient.d.ts +41 -0
  26. package/dist/services/client/index.d.ts +8 -0
  27. package/dist/services/types.d.ts +253 -0
  28. package/dist/signers/ReadOnlySigner.d.ts +1 -1
  29. package/dist/sync/IndexedDbSyncQueue.d.ts +30 -0
  30. package/dist/sync/SqliteSyncQueue.d.ts +54 -0
  31. package/dist/sync/index.d.ts +3 -0
  32. package/dist/sync/types.d.ts +129 -0
  33. package/package.json +6 -3
  34. package/dist/indexers/TransactionParser.d.ts +0 -53
package/README.md CHANGED
@@ -186,6 +186,26 @@ const services = new OneSatServices("main", "https://ordfs.network");
186
186
  | `getBsv21TokenByTxid(tokenId, txid)` | Get BSV21 token data |
187
187
  | `getBsv21TokenDetails(tokenId)` | Get BSV21 token metadata (cached) |
188
188
 
189
+ #### Available Clients
190
+
191
+ OneSatServices exposes specialized API clients as public properties:
192
+
193
+ | Client | Property | Purpose |
194
+ |--------|----------|---------|
195
+ | Chaintracks | `.chaintracks` | Block headers and chain tracking |
196
+ | BEEF | `.beef` | Raw transactions and BEEF proofs |
197
+ | Arcade | `.arcade` | Transaction broadcasting |
198
+ | TXO | `.txo` | Transaction output queries |
199
+ | Owner | `.owner` | Address queries and SSE sync |
200
+ | OrdFS | `.ordfs` | Content and inscription metadata |
201
+ | BSV21 | `.bsv21` | BSV21 token data |
202
+
203
+ ```typescript
204
+ const services = new OneSatServices("main");
205
+ const rawTx = await services.beef.getRawTx(txid);
206
+ const metadata = await services.ordfs.getMetadata(outpoint);
207
+ ```
208
+
189
209
  ---
190
210
 
191
211
  ### ReadOnlySigner
@@ -242,25 +262,6 @@ Tags enable filtered queries via `listOutputs({ tags })`. Tags are only added fo
242
262
 
243
263
  ---
244
264
 
245
- ### TransactionParser
246
-
247
- Runs indexers over transactions to extract basket/tags for wallet-toolbox storage.
248
-
249
- ```typescript
250
- import { TransactionParser, FundIndexer, OriginIndexer } from "@1sat/wallet-toolbox";
251
-
252
- const parser = new TransactionParser(
253
- [new FundIndexer(owners, "mainnet"), new OriginIndexer(owners, "mainnet", services)],
254
- owners,
255
- services
256
- );
257
-
258
- const result = await parser.parse(transaction, true);
259
- // result.outputs: ParsedOutput[] with vout, basket, tags, customInstructions
260
- ```
261
-
262
- ---
263
-
264
265
  ## Project Status
265
266
 
266
267
  ### Completed
@@ -269,12 +270,12 @@ const result = await parser.parse(transaction, true);
269
270
  - [x] Read-only mode via public key
270
271
  - [x] OneSatServices (WalletServices implementation)
271
272
  - [x] All indexers migrated from yours-wallet
272
- - [x] TransactionParser for indexed ingestion
273
273
  - [x] `ingestTransaction()` method
274
274
  - [x] `syncAddress()` / `syncAll()` synchronization
275
275
  - [x] Event system for sync progress
276
276
  - [x] `broadcast()` method
277
277
  - [x] `getChainTracker()` implementation
278
+ - [x] Modular API client architecture
278
279
 
279
280
  ### TODO
280
281
 
@@ -287,7 +288,6 @@ const result = await parser.parse(transaction, true);
287
288
  - [ ] `getScriptHashHistory()`
288
289
  - [ ] `hashToHeader()`
289
290
  - [ ] `nLockTimeIsFinal()`
290
- - [ ] Improve basket/tag extraction in TransactionParser
291
291
  - [ ] Tests
292
292
  - [ ] Integration with yours-wallet
293
293
 
@@ -1,37 +1,37 @@
1
- import { Wallet, WalletStorageManager } from "@bsv/wallet-toolbox/mobile";
1
+ import { type PrivateKey, Transaction } from "@bsv/sdk";
2
+ import { Wallet, type WalletStorageManager } from "@bsv/wallet-toolbox/mobile";
2
3
  import type { Chain } from "@bsv/wallet-toolbox/mobile/out/src/sdk/types";
3
- import { PrivateKey, Transaction, type InternalizeActionResult } from "@bsv/sdk";
4
- export interface SyncStartEvent {
5
- address: string;
6
- fromScore: number;
7
- }
8
- export interface SyncProgressEvent {
9
- address: string;
10
- processed: number;
11
- remaining: number;
12
- currentScore: number;
13
- done: boolean;
14
- }
15
- export interface SyncTxEvent {
16
- address: string;
17
- txid: string;
18
- type: "output" | "spend";
19
- }
20
- export interface SyncErrorEvent {
21
- address: string;
22
- error: Error;
23
- }
24
- export interface SyncCompleteEvent {
25
- address: string;
26
- processed: number;
27
- finalScore: number;
4
+ import { Outpoint } from "./indexers/Outpoint";
5
+ import type { Indexer, ParseContext, Txo } from "./indexers/types";
6
+ import { OneSatServices } from "./services/OneSatServices";
7
+ import type { SyncQueueStorage } from "./sync/types";
8
+ /**
9
+ * Result of ingestTransaction including parse context for debugging
10
+ */
11
+ export interface IngestResult {
12
+ parseContext: ParseContext;
13
+ internalizedCount: number;
28
14
  }
15
+ /**
16
+ * Events emitted by OneSatWallet during sync operations
17
+ */
29
18
  export interface OneSatWalletEvents {
30
- "sync:start": SyncStartEvent;
31
- "sync:progress": SyncProgressEvent;
32
- "sync:tx": SyncTxEvent;
33
- "sync:error": SyncErrorEvent;
34
- "sync:complete": SyncCompleteEvent;
19
+ /** Sync started */
20
+ "sync:start": {
21
+ addresses: string[];
22
+ };
23
+ /** Sync progress update */
24
+ "sync:progress": {
25
+ pending: number;
26
+ done: number;
27
+ failed: number;
28
+ };
29
+ /** Sync complete (queue empty and stream done) */
30
+ "sync:complete": Record<string, never>;
31
+ /** Sync error */
32
+ "sync:error": {
33
+ message: string;
34
+ };
35
35
  }
36
36
  type EventCallback<T> = (event: T) => void;
37
37
  export interface OneSatWalletArgs {
@@ -52,13 +52,27 @@ export interface OneSatWalletArgs {
52
52
  */
53
53
  owners?: Set<string>;
54
54
  /**
55
- * Custom OrdFS server URL (default: https://ordfs.network)
55
+ * Indexers to use for parsing transactions.
56
+ * If not provided, default indexers will be used.
56
57
  */
57
- ordfsUrl?: string;
58
+ indexers?: Indexer[];
58
59
  /**
59
- * Custom 1Sat indexer URL (default: based on chain - mainnet or testnet)
60
+ * Custom 1Sat API URL (default: based on chain - mainnet or testnet)
60
61
  */
61
62
  onesatUrl?: string;
63
+ /**
64
+ * Automatically start syncing all owner addresses on construction.
65
+ */
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;
62
76
  }
63
77
  /**
64
78
  * OneSatWallet extends the BRC-100 Wallet with 1Sat-specific indexing and services.
@@ -69,64 +83,205 @@ export interface OneSatWalletArgs {
69
83
  */
70
84
  export declare class OneSatWallet extends Wallet {
71
85
  private readonly isReadOnly;
72
- private readonly parser;
73
- private readonly oneSatServices;
86
+ private readonly indexers;
87
+ readonly services: OneSatServices;
74
88
  private owners;
75
89
  private listeners;
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;
76
100
  constructor(args: OneSatWalletArgs);
77
101
  /**
78
102
  * Returns true if this wallet was created with only a public key.
79
103
  * Read-only wallets can query but not sign transactions.
80
104
  */
81
105
  get readOnly(): boolean;
106
+ /**
107
+ * Subscribe to wallet events
108
+ */
109
+ on<K extends keyof OneSatWalletEvents>(event: K, callback: EventCallback<OneSatWalletEvents[K]>): void;
110
+ /**
111
+ * Unsubscribe from wallet events
112
+ */
113
+ off<K extends keyof OneSatWalletEvents>(event: K, callback: EventCallback<OneSatWalletEvents[K]>): void;
114
+ /**
115
+ * Emit a wallet event
116
+ */
117
+ private emit;
82
118
  /**
83
119
  * Add an address to the set of owned addresses.
84
120
  * Outputs to these addresses will be indexed.
85
121
  */
86
122
  addOwner(address: string): void;
87
123
  /**
88
- * Subscribe to wallet events.
124
+ * Parse a transaction through indexers without internalizing.
125
+ *
126
+ * This is useful for debugging/testing to see what the indexers produce
127
+ * without actually storing the transaction in the wallet.
128
+ *
129
+ * @param tx - Transaction or txid to parse
130
+ * @param isBroadcasted - Whether this transaction has been broadcast
131
+ * @returns ParseContext with all indexer data
89
132
  */
90
- on<K extends keyof OneSatWalletEvents>(event: K, callback: EventCallback<OneSatWalletEvents[K]>): void;
133
+ parseTransaction(txOrTxid: Transaction | string, isBroadcasted?: boolean): Promise<ParseContext>;
91
134
  /**
92
- * Unsubscribe from wallet events.
135
+ * Parse a single output without full transaction context.
136
+ * Runs all indexers' parse() methods but NOT summarize().
137
+ *
138
+ * @param output - The TransactionOutput to parse
139
+ * @param outpoint - The outpoint identifying this output
140
+ * @returns Txo with all indexer data populated
93
141
  */
94
- off<K extends keyof OneSatWalletEvents>(event: K, callback: EventCallback<OneSatWalletEvents[K]>): void;
95
- private emit;
142
+ parseOutput(output: Transaction["outputs"][0], outpoint: Outpoint): Promise<Txo>;
143
+ /**
144
+ * Load and parse a single output by outpoint.
145
+ * Loads the transaction, extracts the output, and runs indexers on it.
146
+ *
147
+ * @param outpoint - Outpoint string (txid_vout)
148
+ * @returns Txo with all indexer data populated
149
+ */
150
+ loadTxo(outpoint: string): Promise<Txo>;
151
+ /**
152
+ * Run all indexers on a single Txo and populate its data/owner/basket
153
+ */
154
+ runIndexersOnTxo(txo: Txo): Promise<void>;
155
+ /**
156
+ * Parse all inputs - run indexers on source outputs to populate ctx.spends
157
+ */
158
+ private parseInputs;
159
+ /**
160
+ * Load a transaction by txid.
161
+ * Checks storage first, falls back to beef service.
162
+ *
163
+ * @param txid - Transaction ID to load
164
+ * @returns Transaction (without source transactions hydrated)
165
+ */
166
+ loadTransaction(txid: string): Promise<Transaction>;
167
+ /**
168
+ * Load and attach source transactions for all inputs (1 level deep).
169
+ * Modifies the transaction in place.
170
+ */
171
+ hydrateSourceTransactions(tx: Transaction): Promise<void>;
172
+ /**
173
+ * Build minimal parse context from transaction
174
+ */
175
+ buildParseContext(tx: Transaction): ParseContext;
96
176
  /**
97
- * Ingest a transaction by running it through indexers and then internalizing.
177
+ * Ingest a transaction by running it through indexers and writing directly to storage.
98
178
  *
99
179
  * This is the main entry point for adding external transactions to the wallet.
100
180
  * The indexers extract basket, tags, and custom instructions which are then
101
- * passed to the underlying wallet's internalizeAction.
181
+ * written directly to the wallet's storage.
102
182
  *
103
- * @param tx - Transaction or BEEF to ingest
183
+ * Unlike internalizeAction, this method also marks any wallet outputs that are
184
+ * consumed as inputs in the transaction as spent (spentBy, spendable: false).
185
+ *
186
+ * @param tx - Transaction to ingest
104
187
  * @param description - Human-readable description
105
188
  * @param labels - Optional labels for the transaction
106
189
  * @param isBroadcasted - Whether this transaction has been broadcast (affects validation)
190
+ * @returns Result including parse details for all outputs
107
191
  */
108
- ingestTransaction(tx: Transaction, description: string, labels?: string[], isBroadcasted?: boolean): Promise<InternalizeActionResult>;
192
+ ingestTransaction(tx: Transaction, description: string, labels?: string[], isBroadcasted?: boolean): Promise<IngestResult>;
109
193
  /**
110
194
  * Broadcast a transaction and ingest it into the wallet if successful.
111
195
  *
112
196
  * @param tx - Transaction to broadcast
113
197
  * @param description - Human-readable description for the transaction
114
198
  * @param labels - Optional labels for the transaction
115
- * @returns The internalize result if successful
199
+ * @returns The ingest result if successful
116
200
  * @throws Error if broadcast fails
117
201
  */
118
- broadcast(tx: Transaction, description: string, labels?: string[]): Promise<InternalizeActionResult>;
202
+ broadcast(tx: Transaction, description: string, labels?: string[]): Promise<IngestResult>;
119
203
  /**
120
- * Sync a single address from the 1Sat indexer.
121
- * Fetches new outputs and spends, ingesting transactions as needed.
204
+ * Start queue-based sync for all owner addresses.
205
+ * Requires syncQueue to be provided in constructor args.
122
206
  *
123
- * @param address - The address to sync
124
- * @param limit - Max outputs per page (default 100)
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.
216
+ */
217
+ private handleSyncOutput;
218
+ /**
219
+ * Process queue in batches until empty or stopped.
220
+ */
221
+ private processQueueLoop;
222
+ /**
223
+ * Group queue items by txid.
224
+ * @deprecated - claim() now returns items already grouped
225
+ */
226
+ private groupItemsByTxid;
227
+ /**
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.
246
+ */
247
+ close(): void;
248
+ /**
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.
271
+ */
272
+ isStreamDone(): boolean;
273
+ /**
274
+ * Start only the queue processor, without starting a new SSE stream.
275
+ * Useful for testing to process queued items independently.
276
+ */
277
+ startProcessor(): Promise<void>;
278
+ /**
279
+ * Stop the queue processor.
125
280
  */
126
- syncAddress(address: string, limit?: number): Promise<void>;
281
+ stopProcessor(): void;
127
282
  /**
128
- * Sync all owner addresses in parallel.
283
+ * Check if queue processor is active.
129
284
  */
130
- syncAll(): Promise<void>;
285
+ isProcessorActive(): boolean;
131
286
  }
132
287
  export {};
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- export { OneSatWallet, type OneSatWalletArgs, type OneSatWalletEvents, type SyncStartEvent, type SyncProgressEvent, type SyncTxEvent, type SyncErrorEvent, type SyncCompleteEvent, } from "./OneSatWallet";
2
- export { OneSatServices, type OrdfsMetadata } from "./services/OneSatServices";
1
+ export { OneSatWallet, type OneSatWalletArgs, type OneSatWalletEvents, type IngestResult, } from "./OneSatWallet";
2
+ export { OneSatServices, type SyncOutput } from "./services/OneSatServices";
3
+ export type { OrdfsMetadata, OrdfsContentOptions, OrdfsContentResponse, OrdfsResponseHeaders, Capability, } from "./services/types";
4
+ export * from "./services/client";
3
5
  export { ReadOnlySigner } from "./signers/ReadOnlySigner";
4
6
  export * from "./indexers";
5
- export { WalletStorageManager } from "@bsv/wallet-toolbox/mobile";
7
+ export * from "./sync";
8
+ export { WalletStorageManager, StorageProvider, } from "@bsv/wallet-toolbox/mobile";
6
9
  export { StorageIdb } from "@bsv/wallet-toolbox/mobile/out/src/storage/StorageIdb";
7
- export type { Chain } from "@bsv/wallet-toolbox/mobile/out/src/sdk/types";