@unicitylabs/sphere-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/LICENSE +21 -0
- package/README.md +1112 -0
- package/dist/core/index.cjs +7042 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +2548 -0
- package/dist/core/index.d.ts +2548 -0
- package/dist/core/index.js +6946 -0
- package/dist/core/index.js.map +1 -0
- package/dist/impl/browser/index.cjs +7853 -0
- package/dist/impl/browser/index.cjs.map +1 -0
- package/dist/impl/browser/index.d.cts +3016 -0
- package/dist/impl/browser/index.d.ts +3016 -0
- package/dist/impl/browser/index.js +7841 -0
- package/dist/impl/browser/index.js.map +1 -0
- package/dist/impl/nodejs/index.cjs +1767 -0
- package/dist/impl/nodejs/index.cjs.map +1 -0
- package/dist/impl/nodejs/index.d.cts +1091 -0
- package/dist/impl/nodejs/index.d.ts +1091 -0
- package/dist/impl/nodejs/index.js +1722 -0
- package/dist/impl/nodejs/index.js.map +1 -0
- package/dist/index.cjs +7647 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10533 -0
- package/dist/index.d.ts +10533 -0
- package/dist/index.js +7523 -0
- package/dist/index.js.map +1 -0
- package/dist/l1/index.cjs +1545 -0
- package/dist/l1/index.cjs.map +1 -0
- package/dist/l1/index.d.cts +705 -0
- package/dist/l1/index.d.ts +705 -0
- package/dist/l1/index.js +1455 -0
- package/dist/l1/index.js.map +1 -0
- package/package.json +140 -0
|
@@ -0,0 +1,3016 @@
|
|
|
1
|
+
import { StateTransitionClient } from '@unicitylabs/state-transition-sdk/lib/StateTransitionClient';
|
|
2
|
+
import { AggregatorClient } from '@unicitylabs/state-transition-sdk/lib/api/AggregatorClient';
|
|
3
|
+
import { RootTrustBase } from '@unicitylabs/state-transition-sdk/lib/bft/RootTrustBase';
|
|
4
|
+
import { TransferCommitment as TransferCommitment$1 } from '@unicitylabs/state-transition-sdk/lib/transaction/TransferCommitment';
|
|
5
|
+
import { Token as Token$1 } from '@unicitylabs/state-transition-sdk/lib/token/Token';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Cryptographic utilities for SDK2
|
|
9
|
+
*
|
|
10
|
+
* Provides BIP39 mnemonic and BIP32 key derivation functions.
|
|
11
|
+
* Platform-independent - no browser-specific APIs.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
interface KeyPair {
|
|
15
|
+
privateKey: string;
|
|
16
|
+
publicKey: string;
|
|
17
|
+
}
|
|
18
|
+
interface AddressInfo extends KeyPair {
|
|
19
|
+
address: string;
|
|
20
|
+
path: string;
|
|
21
|
+
index: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* TXF (Token eXchange Format) Type Definitions
|
|
26
|
+
* Based on TXF Format Specification v2.0
|
|
27
|
+
*
|
|
28
|
+
* These types define the serialization format for tokens,
|
|
29
|
+
* independent of any UI or storage implementation.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Complete token object in TXF format
|
|
33
|
+
*/
|
|
34
|
+
interface TxfToken {
|
|
35
|
+
version: '2.0';
|
|
36
|
+
genesis: TxfGenesis;
|
|
37
|
+
state: TxfState;
|
|
38
|
+
transactions: TxfTransaction[];
|
|
39
|
+
nametags?: string[];
|
|
40
|
+
_integrity?: TxfIntegrity;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Genesis transaction (initial minting)
|
|
44
|
+
*/
|
|
45
|
+
interface TxfGenesis {
|
|
46
|
+
data: TxfGenesisData;
|
|
47
|
+
inclusionProof: TxfInclusionProof;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Genesis data payload
|
|
51
|
+
*/
|
|
52
|
+
interface TxfGenesisData {
|
|
53
|
+
tokenId: string;
|
|
54
|
+
tokenType: string;
|
|
55
|
+
coinData: [string, string][];
|
|
56
|
+
tokenData: string;
|
|
57
|
+
salt: string;
|
|
58
|
+
recipient: string;
|
|
59
|
+
recipientDataHash: string | null;
|
|
60
|
+
reason: string | null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Current token state
|
|
64
|
+
*/
|
|
65
|
+
interface TxfState {
|
|
66
|
+
data: string;
|
|
67
|
+
predicate: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* State transition transaction
|
|
71
|
+
*/
|
|
72
|
+
interface TxfTransaction {
|
|
73
|
+
previousStateHash: string;
|
|
74
|
+
newStateHash?: string;
|
|
75
|
+
predicate: string;
|
|
76
|
+
inclusionProof: TxfInclusionProof | null;
|
|
77
|
+
data?: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Sparse Merkle Tree inclusion proof
|
|
81
|
+
*/
|
|
82
|
+
interface TxfInclusionProof {
|
|
83
|
+
authenticator: TxfAuthenticator;
|
|
84
|
+
merkleTreePath: TxfMerkleTreePath;
|
|
85
|
+
transactionHash: string;
|
|
86
|
+
unicityCertificate: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Proof authenticator
|
|
90
|
+
*/
|
|
91
|
+
interface TxfAuthenticator {
|
|
92
|
+
algorithm: string;
|
|
93
|
+
publicKey: string;
|
|
94
|
+
signature: string;
|
|
95
|
+
stateHash: string;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Merkle tree path for proof verification
|
|
99
|
+
*/
|
|
100
|
+
interface TxfMerkleTreePath {
|
|
101
|
+
root: string;
|
|
102
|
+
steps: TxfMerkleStep[];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Single step in merkle path
|
|
106
|
+
*/
|
|
107
|
+
interface TxfMerkleStep {
|
|
108
|
+
data: string;
|
|
109
|
+
path: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Token integrity metadata
|
|
113
|
+
*/
|
|
114
|
+
interface TxfIntegrity {
|
|
115
|
+
genesisDataJSONHash: string;
|
|
116
|
+
currentStateHash?: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Nametag data (one per identity)
|
|
120
|
+
*/
|
|
121
|
+
interface NametagData {
|
|
122
|
+
name: string;
|
|
123
|
+
token: object;
|
|
124
|
+
timestamp: number;
|
|
125
|
+
format: string;
|
|
126
|
+
version: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Tombstone entry for tracking spent token states
|
|
130
|
+
*/
|
|
131
|
+
interface TombstoneEntry {
|
|
132
|
+
tokenId: string;
|
|
133
|
+
stateHash: string;
|
|
134
|
+
timestamp: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* SDK2 Core Types
|
|
139
|
+
* Platform-independent type definitions
|
|
140
|
+
*/
|
|
141
|
+
type ProviderStatus = 'disconnected' | 'connecting' | 'connected' | 'error';
|
|
142
|
+
interface ProviderMetadata {
|
|
143
|
+
readonly id: string;
|
|
144
|
+
readonly name: string;
|
|
145
|
+
readonly type: 'local' | 'cloud' | 'p2p' | 'network';
|
|
146
|
+
readonly description?: string;
|
|
147
|
+
}
|
|
148
|
+
interface BaseProvider extends ProviderMetadata {
|
|
149
|
+
connect(config?: unknown): Promise<void>;
|
|
150
|
+
disconnect(): Promise<void>;
|
|
151
|
+
isConnected(): boolean;
|
|
152
|
+
getStatus(): ProviderStatus;
|
|
153
|
+
}
|
|
154
|
+
interface Identity {
|
|
155
|
+
readonly publicKey: string;
|
|
156
|
+
/** L1 address (alpha1...) */
|
|
157
|
+
readonly address: string;
|
|
158
|
+
/** L3 predicate address (DIRECT://...) */
|
|
159
|
+
readonly predicateAddress?: string;
|
|
160
|
+
readonly ipnsName?: string;
|
|
161
|
+
readonly nametag?: string;
|
|
162
|
+
}
|
|
163
|
+
interface FullIdentity extends Identity {
|
|
164
|
+
readonly privateKey: string;
|
|
165
|
+
}
|
|
166
|
+
type TokenStatus = 'pending' | 'confirmed' | 'transferring' | 'spent' | 'invalid';
|
|
167
|
+
interface Token {
|
|
168
|
+
readonly id: string;
|
|
169
|
+
readonly coinId: string;
|
|
170
|
+
readonly symbol: string;
|
|
171
|
+
readonly name: string;
|
|
172
|
+
readonly amount: string;
|
|
173
|
+
status: TokenStatus;
|
|
174
|
+
readonly createdAt: number;
|
|
175
|
+
updatedAt: number;
|
|
176
|
+
readonly sdkData?: string;
|
|
177
|
+
}
|
|
178
|
+
interface TokenBalance {
|
|
179
|
+
readonly coinId: string;
|
|
180
|
+
readonly symbol: string;
|
|
181
|
+
readonly name: string;
|
|
182
|
+
readonly totalAmount: string;
|
|
183
|
+
readonly tokenCount: number;
|
|
184
|
+
readonly decimals: number;
|
|
185
|
+
}
|
|
186
|
+
type TransferStatus = 'pending' | 'submitted' | 'confirmed' | 'delivered' | 'completed' | 'failed';
|
|
187
|
+
interface TransferRequest {
|
|
188
|
+
readonly coinId: string;
|
|
189
|
+
readonly amount: string;
|
|
190
|
+
readonly recipient: string;
|
|
191
|
+
readonly memo?: string;
|
|
192
|
+
}
|
|
193
|
+
interface TransferResult {
|
|
194
|
+
readonly id: string;
|
|
195
|
+
status: TransferStatus;
|
|
196
|
+
readonly tokens: Token[];
|
|
197
|
+
txHash?: string;
|
|
198
|
+
error?: string;
|
|
199
|
+
}
|
|
200
|
+
interface IncomingTransfer {
|
|
201
|
+
readonly id: string;
|
|
202
|
+
readonly senderPubkey: string;
|
|
203
|
+
readonly senderNametag?: string;
|
|
204
|
+
readonly tokens: Token[];
|
|
205
|
+
readonly memo?: string;
|
|
206
|
+
readonly receivedAt: number;
|
|
207
|
+
}
|
|
208
|
+
type PaymentRequestStatus = 'pending' | 'accepted' | 'rejected' | 'paid' | 'expired';
|
|
209
|
+
/**
|
|
210
|
+
* Outgoing payment request (requesting payment from someone)
|
|
211
|
+
*/
|
|
212
|
+
interface PaymentRequest {
|
|
213
|
+
/** Unique request ID */
|
|
214
|
+
readonly id: string;
|
|
215
|
+
/** Amount requested (in smallest units) */
|
|
216
|
+
readonly amount: string;
|
|
217
|
+
/** Coin/token type */
|
|
218
|
+
readonly coinId: string;
|
|
219
|
+
/** Optional message/memo */
|
|
220
|
+
readonly message?: string;
|
|
221
|
+
/** Recipient nametag (who should pay) */
|
|
222
|
+
readonly recipientNametag?: string;
|
|
223
|
+
/** Custom metadata */
|
|
224
|
+
readonly metadata?: Record<string, unknown>;
|
|
225
|
+
/** Expiration timestamp (ms) */
|
|
226
|
+
readonly expiresAt?: number;
|
|
227
|
+
/** Created timestamp */
|
|
228
|
+
readonly createdAt: number;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Incoming payment request (someone requesting payment from us)
|
|
232
|
+
*/
|
|
233
|
+
interface IncomingPaymentRequest$1 {
|
|
234
|
+
/** Event ID from Nostr */
|
|
235
|
+
readonly id: string;
|
|
236
|
+
/** Sender's public key */
|
|
237
|
+
readonly senderPubkey: string;
|
|
238
|
+
/** Sender's nametag (if known) */
|
|
239
|
+
readonly senderNametag?: string;
|
|
240
|
+
/** Amount requested */
|
|
241
|
+
readonly amount: string;
|
|
242
|
+
/** Coin/token type */
|
|
243
|
+
readonly coinId: string;
|
|
244
|
+
/** Symbol for display */
|
|
245
|
+
readonly symbol: string;
|
|
246
|
+
/** Message from sender */
|
|
247
|
+
readonly message?: string;
|
|
248
|
+
/** Who this request is for (our nametag) */
|
|
249
|
+
readonly recipientNametag?: string;
|
|
250
|
+
/** Original request ID from sender */
|
|
251
|
+
readonly requestId: string;
|
|
252
|
+
/** Timestamp */
|
|
253
|
+
readonly timestamp: number;
|
|
254
|
+
/** Current status */
|
|
255
|
+
status: PaymentRequestStatus;
|
|
256
|
+
/** Custom metadata */
|
|
257
|
+
readonly metadata?: Record<string, unknown>;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Result of sending a payment request
|
|
261
|
+
*/
|
|
262
|
+
interface PaymentRequestResult {
|
|
263
|
+
readonly success: boolean;
|
|
264
|
+
readonly requestId?: string;
|
|
265
|
+
readonly eventId?: string;
|
|
266
|
+
readonly error?: string;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Handler for incoming payment requests
|
|
270
|
+
*/
|
|
271
|
+
type PaymentRequestHandler$1 = (request: IncomingPaymentRequest$1) => void;
|
|
272
|
+
/**
|
|
273
|
+
* Response type for payment requests
|
|
274
|
+
*/
|
|
275
|
+
type PaymentRequestResponseType$1 = 'accepted' | 'rejected' | 'paid';
|
|
276
|
+
/**
|
|
277
|
+
* Outgoing payment request (we sent to someone)
|
|
278
|
+
*/
|
|
279
|
+
interface OutgoingPaymentRequest {
|
|
280
|
+
/** Unique request ID */
|
|
281
|
+
readonly id: string;
|
|
282
|
+
/** Nostr event ID */
|
|
283
|
+
readonly eventId: string;
|
|
284
|
+
/** Recipient's public key */
|
|
285
|
+
readonly recipientPubkey: string;
|
|
286
|
+
/** Recipient's nametag (if known) */
|
|
287
|
+
readonly recipientNametag?: string;
|
|
288
|
+
/** Amount requested */
|
|
289
|
+
readonly amount: string;
|
|
290
|
+
/** Coin/token type */
|
|
291
|
+
readonly coinId: string;
|
|
292
|
+
/** Message sent with request */
|
|
293
|
+
readonly message?: string;
|
|
294
|
+
/** Created timestamp */
|
|
295
|
+
readonly createdAt: number;
|
|
296
|
+
/** Current status */
|
|
297
|
+
status: PaymentRequestStatus;
|
|
298
|
+
/** Response data (if received) */
|
|
299
|
+
response?: PaymentRequestResponse;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Response to a payment request
|
|
303
|
+
*/
|
|
304
|
+
interface PaymentRequestResponse {
|
|
305
|
+
/** Response event ID */
|
|
306
|
+
readonly id: string;
|
|
307
|
+
/** Responder's public key */
|
|
308
|
+
readonly responderPubkey: string;
|
|
309
|
+
/** Responder's nametag (if known) */
|
|
310
|
+
readonly responderNametag?: string;
|
|
311
|
+
/** Original request ID */
|
|
312
|
+
readonly requestId: string;
|
|
313
|
+
/** Response type */
|
|
314
|
+
readonly responseType: PaymentRequestResponseType$1;
|
|
315
|
+
/** Optional message */
|
|
316
|
+
readonly message?: string;
|
|
317
|
+
/** Transfer ID (if paid) */
|
|
318
|
+
readonly transferId?: string;
|
|
319
|
+
/** Timestamp */
|
|
320
|
+
readonly timestamp: number;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Handler for payment request responses
|
|
324
|
+
*/
|
|
325
|
+
type PaymentRequestResponseHandler$1 = (response: PaymentRequestResponse) => void;
|
|
326
|
+
interface DirectMessage {
|
|
327
|
+
readonly id: string;
|
|
328
|
+
readonly senderPubkey: string;
|
|
329
|
+
readonly senderNametag?: string;
|
|
330
|
+
readonly recipientPubkey: string;
|
|
331
|
+
readonly recipientNametag?: string;
|
|
332
|
+
readonly content: string;
|
|
333
|
+
readonly timestamp: number;
|
|
334
|
+
isRead: boolean;
|
|
335
|
+
}
|
|
336
|
+
interface BroadcastMessage {
|
|
337
|
+
readonly id: string;
|
|
338
|
+
readonly authorPubkey: string;
|
|
339
|
+
readonly authorNametag?: string;
|
|
340
|
+
readonly content: string;
|
|
341
|
+
readonly timestamp: number;
|
|
342
|
+
readonly tags?: string[];
|
|
343
|
+
}
|
|
344
|
+
type SphereEventType = 'transfer:incoming' | 'transfer:confirmed' | 'transfer:failed' | 'payment_request:incoming' | 'payment_request:accepted' | 'payment_request:rejected' | 'payment_request:paid' | 'payment_request:response' | 'message:dm' | 'message:broadcast' | 'sync:started' | 'sync:completed' | 'sync:provider' | 'sync:error' | 'connection:changed' | 'nametag:registered' | 'identity:changed';
|
|
345
|
+
interface SphereEventMap {
|
|
346
|
+
'transfer:incoming': IncomingTransfer;
|
|
347
|
+
'transfer:confirmed': TransferResult;
|
|
348
|
+
'transfer:failed': TransferResult;
|
|
349
|
+
'payment_request:incoming': IncomingPaymentRequest$1;
|
|
350
|
+
'payment_request:accepted': IncomingPaymentRequest$1;
|
|
351
|
+
'payment_request:rejected': IncomingPaymentRequest$1;
|
|
352
|
+
'payment_request:paid': IncomingPaymentRequest$1;
|
|
353
|
+
'payment_request:response': PaymentRequestResponse;
|
|
354
|
+
'message:dm': DirectMessage;
|
|
355
|
+
'message:broadcast': BroadcastMessage;
|
|
356
|
+
'sync:started': {
|
|
357
|
+
source: string;
|
|
358
|
+
};
|
|
359
|
+
'sync:completed': {
|
|
360
|
+
source: string;
|
|
361
|
+
count: number;
|
|
362
|
+
};
|
|
363
|
+
'sync:provider': {
|
|
364
|
+
providerId: string;
|
|
365
|
+
success: boolean;
|
|
366
|
+
added?: number;
|
|
367
|
+
removed?: number;
|
|
368
|
+
error?: string;
|
|
369
|
+
};
|
|
370
|
+
'sync:error': {
|
|
371
|
+
source: string;
|
|
372
|
+
error: string;
|
|
373
|
+
};
|
|
374
|
+
'connection:changed': {
|
|
375
|
+
provider: string;
|
|
376
|
+
connected: boolean;
|
|
377
|
+
};
|
|
378
|
+
'nametag:registered': {
|
|
379
|
+
nametag: string;
|
|
380
|
+
addressIndex: number;
|
|
381
|
+
};
|
|
382
|
+
'identity:changed': {
|
|
383
|
+
address: string;
|
|
384
|
+
predicateAddress?: string;
|
|
385
|
+
publicKey: string;
|
|
386
|
+
nametag?: string;
|
|
387
|
+
addressIndex: number;
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
type SphereEventHandler<T extends SphereEventType> = (data: SphereEventMap[T]) => void;
|
|
391
|
+
/**
|
|
392
|
+
* Derivation mode determines how child keys are derived:
|
|
393
|
+
* - "bip32": Standard BIP32 with chain code (IL + parentKey) mod n
|
|
394
|
+
* - "legacy_hmac": Legacy Sphere HMAC derivation with chain code
|
|
395
|
+
* - "wif_hmac": Simple HMAC derivation without chain code (webwallet compatibility)
|
|
396
|
+
*/
|
|
397
|
+
type DerivationMode = 'bip32' | 'legacy_hmac' | 'wif_hmac';
|
|
398
|
+
/**
|
|
399
|
+
* Source of wallet creation
|
|
400
|
+
*/
|
|
401
|
+
type WalletSource = 'mnemonic' | 'file' | 'unknown';
|
|
402
|
+
/**
|
|
403
|
+
* Wallet information for backup/export purposes
|
|
404
|
+
*/
|
|
405
|
+
interface WalletInfo {
|
|
406
|
+
readonly source: WalletSource;
|
|
407
|
+
readonly hasMnemonic: boolean;
|
|
408
|
+
readonly hasChainCode: boolean;
|
|
409
|
+
readonly derivationMode: DerivationMode;
|
|
410
|
+
readonly basePath: string;
|
|
411
|
+
readonly address0: string | null;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* JSON export format for wallet backup (v1.0)
|
|
415
|
+
*/
|
|
416
|
+
interface WalletJSON {
|
|
417
|
+
readonly version: '1.0';
|
|
418
|
+
readonly type: 'sphere-wallet';
|
|
419
|
+
readonly createdAt: string;
|
|
420
|
+
readonly wallet: {
|
|
421
|
+
readonly masterPrivateKey?: string;
|
|
422
|
+
readonly chainCode?: string;
|
|
423
|
+
readonly addresses: ReadonlyArray<{
|
|
424
|
+
readonly address: string;
|
|
425
|
+
readonly publicKey: string;
|
|
426
|
+
readonly path: string;
|
|
427
|
+
readonly index: number;
|
|
428
|
+
}>;
|
|
429
|
+
readonly isBIP32: boolean;
|
|
430
|
+
readonly descriptorPath?: string;
|
|
431
|
+
};
|
|
432
|
+
readonly mnemonic?: string;
|
|
433
|
+
readonly encrypted?: boolean;
|
|
434
|
+
readonly source?: WalletSource;
|
|
435
|
+
readonly derivationMode?: DerivationMode;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Options for exporting wallet to JSON
|
|
439
|
+
*/
|
|
440
|
+
interface WalletJSONExportOptions {
|
|
441
|
+
/** Include mnemonic in export (default: true if available) */
|
|
442
|
+
includeMnemonic?: boolean;
|
|
443
|
+
/** Encrypt sensitive data with password */
|
|
444
|
+
password?: string;
|
|
445
|
+
/** Number of addresses to include (default: 1) */
|
|
446
|
+
addressCount?: number;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Storage Provider Interface
|
|
451
|
+
* Platform-independent storage abstraction
|
|
452
|
+
*/
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Basic key-value storage provider
|
|
456
|
+
* All operations are async for platform flexibility
|
|
457
|
+
*/
|
|
458
|
+
interface StorageProvider extends BaseProvider {
|
|
459
|
+
/**
|
|
460
|
+
* Set identity for scoped storage
|
|
461
|
+
*/
|
|
462
|
+
setIdentity(identity: FullIdentity): void;
|
|
463
|
+
/**
|
|
464
|
+
* Get value by key
|
|
465
|
+
*/
|
|
466
|
+
get(key: string): Promise<string | null>;
|
|
467
|
+
/**
|
|
468
|
+
* Set value by key
|
|
469
|
+
*/
|
|
470
|
+
set(key: string, value: string): Promise<void>;
|
|
471
|
+
/**
|
|
472
|
+
* Remove key
|
|
473
|
+
*/
|
|
474
|
+
remove(key: string): Promise<void>;
|
|
475
|
+
/**
|
|
476
|
+
* Check if key exists
|
|
477
|
+
*/
|
|
478
|
+
has(key: string): Promise<boolean>;
|
|
479
|
+
/**
|
|
480
|
+
* Get all keys with optional prefix filter
|
|
481
|
+
*/
|
|
482
|
+
keys(prefix?: string): Promise<string[]>;
|
|
483
|
+
/**
|
|
484
|
+
* Clear all keys with optional prefix filter
|
|
485
|
+
*/
|
|
486
|
+
clear(prefix?: string): Promise<void>;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Storage result types
|
|
490
|
+
*/
|
|
491
|
+
interface SaveResult {
|
|
492
|
+
success: boolean;
|
|
493
|
+
cid?: string;
|
|
494
|
+
error?: string;
|
|
495
|
+
timestamp: number;
|
|
496
|
+
}
|
|
497
|
+
interface LoadResult<T = unknown> {
|
|
498
|
+
success: boolean;
|
|
499
|
+
data?: T;
|
|
500
|
+
error?: string;
|
|
501
|
+
source: 'local' | 'remote' | 'cache';
|
|
502
|
+
timestamp: number;
|
|
503
|
+
}
|
|
504
|
+
interface SyncResult<T = unknown> {
|
|
505
|
+
success: boolean;
|
|
506
|
+
merged?: T;
|
|
507
|
+
added: number;
|
|
508
|
+
removed: number;
|
|
509
|
+
conflicts: number;
|
|
510
|
+
error?: string;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Token-specific storage provider
|
|
514
|
+
* Handles token persistence with sync capabilities
|
|
515
|
+
*/
|
|
516
|
+
interface TokenStorageProvider<TData = unknown> extends BaseProvider {
|
|
517
|
+
/**
|
|
518
|
+
* Set identity for storage scope
|
|
519
|
+
*/
|
|
520
|
+
setIdentity(identity: FullIdentity): void;
|
|
521
|
+
/**
|
|
522
|
+
* Initialize provider (called once after identity is set)
|
|
523
|
+
*/
|
|
524
|
+
initialize(): Promise<boolean>;
|
|
525
|
+
/**
|
|
526
|
+
* Shutdown provider
|
|
527
|
+
*/
|
|
528
|
+
shutdown(): Promise<void>;
|
|
529
|
+
/**
|
|
530
|
+
* Save token data
|
|
531
|
+
*/
|
|
532
|
+
save(data: TData): Promise<SaveResult>;
|
|
533
|
+
/**
|
|
534
|
+
* Load token data
|
|
535
|
+
*/
|
|
536
|
+
load(identifier?: string): Promise<LoadResult<TData>>;
|
|
537
|
+
/**
|
|
538
|
+
* Sync local data with remote
|
|
539
|
+
*/
|
|
540
|
+
sync(localData: TData): Promise<SyncResult<TData>>;
|
|
541
|
+
/**
|
|
542
|
+
* Check if data exists
|
|
543
|
+
*/
|
|
544
|
+
exists?(identifier?: string): Promise<boolean>;
|
|
545
|
+
/**
|
|
546
|
+
* Clear all data
|
|
547
|
+
*/
|
|
548
|
+
clear?(): Promise<boolean>;
|
|
549
|
+
/**
|
|
550
|
+
* Subscribe to storage events
|
|
551
|
+
*/
|
|
552
|
+
onEvent?(callback: StorageEventCallback): () => void;
|
|
553
|
+
/**
|
|
554
|
+
* Save individual token (for file-based storage like lottery pattern)
|
|
555
|
+
*/
|
|
556
|
+
saveToken?(tokenId: string, tokenData: unknown): Promise<void>;
|
|
557
|
+
/**
|
|
558
|
+
* Get individual token
|
|
559
|
+
*/
|
|
560
|
+
getToken?(tokenId: string): Promise<unknown | null>;
|
|
561
|
+
/**
|
|
562
|
+
* List all token IDs
|
|
563
|
+
*/
|
|
564
|
+
listTokenIds?(): Promise<string[]>;
|
|
565
|
+
/**
|
|
566
|
+
* Delete individual token
|
|
567
|
+
*/
|
|
568
|
+
deleteToken?(tokenId: string): Promise<void>;
|
|
569
|
+
}
|
|
570
|
+
type StorageEventType = 'storage:saving' | 'storage:saved' | 'storage:loading' | 'storage:loaded' | 'storage:error' | 'sync:started' | 'sync:completed' | 'sync:conflict' | 'sync:error';
|
|
571
|
+
interface StorageEvent {
|
|
572
|
+
type: StorageEventType;
|
|
573
|
+
timestamp: number;
|
|
574
|
+
data?: unknown;
|
|
575
|
+
error?: string;
|
|
576
|
+
}
|
|
577
|
+
type StorageEventCallback = (event: StorageEvent) => void;
|
|
578
|
+
interface TxfStorageDataBase {
|
|
579
|
+
_meta: TxfMeta;
|
|
580
|
+
_tombstones?: TxfTombstone[];
|
|
581
|
+
_outbox?: TxfOutboxEntry[];
|
|
582
|
+
_sent?: TxfSentEntry[];
|
|
583
|
+
_invalid?: TxfInvalidEntry[];
|
|
584
|
+
[key: `_${string}`]: unknown;
|
|
585
|
+
}
|
|
586
|
+
interface TxfMeta {
|
|
587
|
+
version: number;
|
|
588
|
+
address: string;
|
|
589
|
+
ipnsName?: string;
|
|
590
|
+
formatVersion: string;
|
|
591
|
+
updatedAt: number;
|
|
592
|
+
}
|
|
593
|
+
interface TxfTombstone {
|
|
594
|
+
tokenId: string;
|
|
595
|
+
stateHash: string;
|
|
596
|
+
timestamp: number;
|
|
597
|
+
}
|
|
598
|
+
interface TxfOutboxEntry {
|
|
599
|
+
id: string;
|
|
600
|
+
status: string;
|
|
601
|
+
tokenId: string;
|
|
602
|
+
recipient: string;
|
|
603
|
+
createdAt: number;
|
|
604
|
+
data: unknown;
|
|
605
|
+
}
|
|
606
|
+
interface TxfSentEntry {
|
|
607
|
+
tokenId: string;
|
|
608
|
+
recipient: string;
|
|
609
|
+
txHash: string;
|
|
610
|
+
sentAt: number;
|
|
611
|
+
}
|
|
612
|
+
interface TxfInvalidEntry {
|
|
613
|
+
tokenId: string;
|
|
614
|
+
reason: string;
|
|
615
|
+
detectedAt: number;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Browser LocalStorage Provider
|
|
620
|
+
* Implements StorageProvider using browser localStorage
|
|
621
|
+
*/
|
|
622
|
+
|
|
623
|
+
interface LocalStorageProviderConfig {
|
|
624
|
+
/** Key prefix (default: 'sphere_sdk2_') */
|
|
625
|
+
prefix?: string;
|
|
626
|
+
/** Custom storage instance (for testing/SSR) */
|
|
627
|
+
storage?: Storage;
|
|
628
|
+
/** Enable debug logging */
|
|
629
|
+
debug?: boolean;
|
|
630
|
+
}
|
|
631
|
+
declare class LocalStorageProvider implements StorageProvider {
|
|
632
|
+
readonly id = "localStorage";
|
|
633
|
+
readonly name = "Local Storage";
|
|
634
|
+
readonly type: "local";
|
|
635
|
+
readonly description = "Browser localStorage for single-device persistence";
|
|
636
|
+
private config;
|
|
637
|
+
private identity;
|
|
638
|
+
private status;
|
|
639
|
+
constructor(config?: LocalStorageProviderConfig);
|
|
640
|
+
connect(): Promise<void>;
|
|
641
|
+
disconnect(): Promise<void>;
|
|
642
|
+
isConnected(): boolean;
|
|
643
|
+
getStatus(): ProviderStatus;
|
|
644
|
+
setIdentity(identity: FullIdentity): void;
|
|
645
|
+
get(key: string): Promise<string | null>;
|
|
646
|
+
set(key: string, value: string): Promise<void>;
|
|
647
|
+
remove(key: string): Promise<void>;
|
|
648
|
+
has(key: string): Promise<boolean>;
|
|
649
|
+
keys(prefix?: string): Promise<string[]>;
|
|
650
|
+
clear(prefix?: string): Promise<void>;
|
|
651
|
+
/**
|
|
652
|
+
* Get JSON data
|
|
653
|
+
*/
|
|
654
|
+
getJSON<T>(key: string): Promise<T | null>;
|
|
655
|
+
/**
|
|
656
|
+
* Set JSON data
|
|
657
|
+
*/
|
|
658
|
+
setJSON<T>(key: string, value: T): Promise<void>;
|
|
659
|
+
private getFullKey;
|
|
660
|
+
private ensureConnected;
|
|
661
|
+
private getStorageSafe;
|
|
662
|
+
private log;
|
|
663
|
+
}
|
|
664
|
+
declare function createLocalStorageProvider(config?: LocalStorageProviderConfig): LocalStorageProvider;
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Browser IPFS Storage Provider
|
|
668
|
+
* Implements TokenStorageProvider using IPFS/IPNS for decentralized storage
|
|
669
|
+
*
|
|
670
|
+
* Uses a hybrid approach:
|
|
671
|
+
* - HTTP API to backend gateways for fast publishing/resolution
|
|
672
|
+
* - Helia for browser-based DHT operations as backup
|
|
673
|
+
*/
|
|
674
|
+
|
|
675
|
+
interface IpfsStorageProviderConfig {
|
|
676
|
+
/** IPFS gateway URLs for HTTP API */
|
|
677
|
+
gateways?: string[];
|
|
678
|
+
/** Bootstrap peers for DHT */
|
|
679
|
+
bootstrapPeers?: string[];
|
|
680
|
+
/** Enable IPNS for mutable addressing */
|
|
681
|
+
enableIpns?: boolean;
|
|
682
|
+
/** IPNS publish timeout (ms) */
|
|
683
|
+
ipnsTimeout?: number;
|
|
684
|
+
/** Content fetch timeout (ms) */
|
|
685
|
+
fetchTimeout?: number;
|
|
686
|
+
/** Enable debug logging */
|
|
687
|
+
debug?: boolean;
|
|
688
|
+
}
|
|
689
|
+
declare class IpfsStorageProvider<TData extends TxfStorageDataBase = TxfStorageDataBase> implements TokenStorageProvider<TData> {
|
|
690
|
+
readonly id = "ipfs";
|
|
691
|
+
readonly name = "IPFS Storage";
|
|
692
|
+
readonly type: "p2p";
|
|
693
|
+
readonly description = "Decentralized storage via IPFS/IPNS";
|
|
694
|
+
private config;
|
|
695
|
+
private identity;
|
|
696
|
+
private status;
|
|
697
|
+
private ipnsName;
|
|
698
|
+
private lastCid;
|
|
699
|
+
private eventCallbacks;
|
|
700
|
+
private helia;
|
|
701
|
+
private heliaJson;
|
|
702
|
+
private ipnsKeyPair;
|
|
703
|
+
/** Get the last published CID */
|
|
704
|
+
getLastCid(): string | null;
|
|
705
|
+
private localCache;
|
|
706
|
+
private cacheTimestamp;
|
|
707
|
+
constructor(config?: IpfsStorageProviderConfig);
|
|
708
|
+
connect(): Promise<void>;
|
|
709
|
+
/**
|
|
710
|
+
* Initialize Helia browser IPFS node
|
|
711
|
+
*/
|
|
712
|
+
private initializeHelia;
|
|
713
|
+
disconnect(): Promise<void>;
|
|
714
|
+
isConnected(): boolean;
|
|
715
|
+
getStatus(): ProviderStatus;
|
|
716
|
+
setIdentity(identity: FullIdentity): Promise<void>;
|
|
717
|
+
initialize(): Promise<boolean>;
|
|
718
|
+
shutdown(): Promise<void>;
|
|
719
|
+
save(data: TData): Promise<SaveResult>;
|
|
720
|
+
load(identifier?: string): Promise<LoadResult<TData>>;
|
|
721
|
+
sync(localData: TData): Promise<SyncResult<TData>>;
|
|
722
|
+
exists(): Promise<boolean>;
|
|
723
|
+
clear(): Promise<boolean>;
|
|
724
|
+
onEvent(callback: StorageEventCallback): () => void;
|
|
725
|
+
private testGatewayConnectivity;
|
|
726
|
+
private publishToGateways;
|
|
727
|
+
/**
|
|
728
|
+
* Publish data via Helia (browser DHT)
|
|
729
|
+
*/
|
|
730
|
+
private publishToHelia;
|
|
731
|
+
private publishToGateway;
|
|
732
|
+
private publishIpns;
|
|
733
|
+
private publishIpnsToGateway;
|
|
734
|
+
private resolveIpns;
|
|
735
|
+
private resolveIpnsFromGateway;
|
|
736
|
+
private fetchFromGateways;
|
|
737
|
+
/**
|
|
738
|
+
* Fetch content via Helia (browser DHT)
|
|
739
|
+
*/
|
|
740
|
+
private fetchFromHelia;
|
|
741
|
+
private fetchFromGateway;
|
|
742
|
+
private mergeData;
|
|
743
|
+
private ensureReady;
|
|
744
|
+
/**
|
|
745
|
+
* Simple IPNS name derivation (fallback when libp2p is unavailable)
|
|
746
|
+
*/
|
|
747
|
+
private deriveIpnsNameSimple;
|
|
748
|
+
/**
|
|
749
|
+
* Convert hex string to Uint8Array
|
|
750
|
+
*/
|
|
751
|
+
private hexToBytes;
|
|
752
|
+
private emitEvent;
|
|
753
|
+
private log;
|
|
754
|
+
}
|
|
755
|
+
declare function createIpfsStorageProvider<TData extends TxfStorageDataBase = TxfStorageDataBase>(config?: IpfsStorageProviderConfig): IpfsStorageProvider<TData>;
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* IndexedDB Token Storage Provider for Browser
|
|
759
|
+
* Stores tokens in IndexedDB for persistent browser storage
|
|
760
|
+
*/
|
|
761
|
+
|
|
762
|
+
interface IndexedDBTokenStorageConfig {
|
|
763
|
+
/** Database name prefix (default: 'sphere-token-storage') */
|
|
764
|
+
dbNamePrefix?: string;
|
|
765
|
+
}
|
|
766
|
+
declare class IndexedDBTokenStorageProvider implements TokenStorageProvider<TxfStorageDataBase> {
|
|
767
|
+
readonly id = "indexeddb-token-storage";
|
|
768
|
+
readonly name = "IndexedDB Token Storage";
|
|
769
|
+
readonly type: "local";
|
|
770
|
+
private dbName;
|
|
771
|
+
private db;
|
|
772
|
+
private status;
|
|
773
|
+
private identity;
|
|
774
|
+
constructor(config?: IndexedDBTokenStorageConfig);
|
|
775
|
+
setIdentity(identity: FullIdentity): void;
|
|
776
|
+
initialize(): Promise<boolean>;
|
|
777
|
+
shutdown(): Promise<void>;
|
|
778
|
+
connect(): Promise<void>;
|
|
779
|
+
disconnect(): Promise<void>;
|
|
780
|
+
isConnected(): boolean;
|
|
781
|
+
getStatus(): ProviderStatus;
|
|
782
|
+
load(): Promise<LoadResult<TxfStorageDataBase>>;
|
|
783
|
+
save(data: TxfStorageDataBase): Promise<SaveResult>;
|
|
784
|
+
sync(localData: TxfStorageDataBase): Promise<SyncResult<TxfStorageDataBase>>;
|
|
785
|
+
exists(): Promise<boolean>;
|
|
786
|
+
clear(): Promise<boolean>;
|
|
787
|
+
deleteToken(tokenId: string): Promise<void>;
|
|
788
|
+
saveToken(tokenId: string, tokenData: unknown): Promise<void>;
|
|
789
|
+
getToken(tokenId: string): Promise<unknown | null>;
|
|
790
|
+
listTokenIds(): Promise<string[]>;
|
|
791
|
+
private openDatabase;
|
|
792
|
+
private getFromStore;
|
|
793
|
+
private getAllFromStore;
|
|
794
|
+
private putToStore;
|
|
795
|
+
private deleteFromStore;
|
|
796
|
+
private clearStore;
|
|
797
|
+
}
|
|
798
|
+
declare function createIndexedDBTokenStorageProvider(config?: IndexedDBTokenStorageConfig): IndexedDBTokenStorageProvider;
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* Transport Provider Interface
|
|
802
|
+
* Platform-independent P2P messaging abstraction
|
|
803
|
+
*/
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* P2P messaging transport provider
|
|
807
|
+
*/
|
|
808
|
+
interface TransportProvider extends BaseProvider {
|
|
809
|
+
/**
|
|
810
|
+
* Set identity for signing/encryption
|
|
811
|
+
*/
|
|
812
|
+
setIdentity(identity: FullIdentity): void;
|
|
813
|
+
/**
|
|
814
|
+
* Send encrypted direct message
|
|
815
|
+
* @returns Event ID
|
|
816
|
+
*/
|
|
817
|
+
sendMessage(recipientPubkey: string, content: string): Promise<string>;
|
|
818
|
+
/**
|
|
819
|
+
* Subscribe to incoming direct messages
|
|
820
|
+
* @returns Unsubscribe function
|
|
821
|
+
*/
|
|
822
|
+
onMessage(handler: MessageHandler): () => void;
|
|
823
|
+
/**
|
|
824
|
+
* Send token transfer payload
|
|
825
|
+
* @returns Event ID
|
|
826
|
+
*/
|
|
827
|
+
sendTokenTransfer(recipientPubkey: string, payload: TokenTransferPayload): Promise<string>;
|
|
828
|
+
/**
|
|
829
|
+
* Subscribe to incoming token transfers
|
|
830
|
+
* @returns Unsubscribe function
|
|
831
|
+
*/
|
|
832
|
+
onTokenTransfer(handler: TokenTransferHandler): () => void;
|
|
833
|
+
/**
|
|
834
|
+
* Resolve nametag to public key
|
|
835
|
+
*/
|
|
836
|
+
resolveNametag?(nametag: string): Promise<string | null>;
|
|
837
|
+
/**
|
|
838
|
+
* Register a nametag for this identity
|
|
839
|
+
* @returns true if successful, false if already taken
|
|
840
|
+
*/
|
|
841
|
+
registerNametag?(nametag: string, publicKey: string): Promise<boolean>;
|
|
842
|
+
/**
|
|
843
|
+
* Publish nametag binding
|
|
844
|
+
*/
|
|
845
|
+
publishNametag?(nametag: string, address: string): Promise<void>;
|
|
846
|
+
/**
|
|
847
|
+
* Subscribe to broadcast messages (global/channel)
|
|
848
|
+
*/
|
|
849
|
+
subscribeToBroadcast?(tags: string[], handler: BroadcastHandler): () => void;
|
|
850
|
+
/**
|
|
851
|
+
* Publish broadcast message
|
|
852
|
+
*/
|
|
853
|
+
publishBroadcast?(content: string, tags?: string[]): Promise<string>;
|
|
854
|
+
/**
|
|
855
|
+
* Send payment request to a recipient
|
|
856
|
+
* @returns Event ID
|
|
857
|
+
*/
|
|
858
|
+
sendPaymentRequest?(recipientPubkey: string, request: PaymentRequestPayload): Promise<string>;
|
|
859
|
+
/**
|
|
860
|
+
* Subscribe to incoming payment requests
|
|
861
|
+
* @returns Unsubscribe function
|
|
862
|
+
*/
|
|
863
|
+
onPaymentRequest?(handler: PaymentRequestHandler): () => void;
|
|
864
|
+
/**
|
|
865
|
+
* Send response to a payment request
|
|
866
|
+
* @returns Event ID
|
|
867
|
+
*/
|
|
868
|
+
sendPaymentRequestResponse?(recipientPubkey: string, response: PaymentRequestResponsePayload): Promise<string>;
|
|
869
|
+
/**
|
|
870
|
+
* Subscribe to incoming payment request responses
|
|
871
|
+
* @returns Unsubscribe function
|
|
872
|
+
*/
|
|
873
|
+
onPaymentRequestResponse?(handler: PaymentRequestResponseHandler): () => void;
|
|
874
|
+
/**
|
|
875
|
+
* Get list of configured relay URLs
|
|
876
|
+
*/
|
|
877
|
+
getRelays?(): string[];
|
|
878
|
+
/**
|
|
879
|
+
* Get list of currently connected relay URLs
|
|
880
|
+
*/
|
|
881
|
+
getConnectedRelays?(): string[];
|
|
882
|
+
/**
|
|
883
|
+
* Add a relay dynamically
|
|
884
|
+
* @returns true if added successfully
|
|
885
|
+
*/
|
|
886
|
+
addRelay?(relayUrl: string): Promise<boolean>;
|
|
887
|
+
/**
|
|
888
|
+
* Remove a relay dynamically
|
|
889
|
+
* @returns true if removed successfully
|
|
890
|
+
*/
|
|
891
|
+
removeRelay?(relayUrl: string): Promise<boolean>;
|
|
892
|
+
/**
|
|
893
|
+
* Check if a relay is configured
|
|
894
|
+
*/
|
|
895
|
+
hasRelay?(relayUrl: string): boolean;
|
|
896
|
+
/**
|
|
897
|
+
* Check if a relay is currently connected
|
|
898
|
+
*/
|
|
899
|
+
isRelayConnected?(relayUrl: string): boolean;
|
|
900
|
+
}
|
|
901
|
+
interface IncomingMessage {
|
|
902
|
+
id: string;
|
|
903
|
+
senderPubkey: string;
|
|
904
|
+
content: string;
|
|
905
|
+
timestamp: number;
|
|
906
|
+
encrypted: boolean;
|
|
907
|
+
}
|
|
908
|
+
type MessageHandler = (message: IncomingMessage) => void;
|
|
909
|
+
interface TokenTransferPayload {
|
|
910
|
+
/** Serialized token data */
|
|
911
|
+
token: string;
|
|
912
|
+
/** Inclusion proof */
|
|
913
|
+
proof: unknown;
|
|
914
|
+
/** Optional memo */
|
|
915
|
+
memo?: string;
|
|
916
|
+
/** Sender info */
|
|
917
|
+
sender?: {
|
|
918
|
+
pubkey: string;
|
|
919
|
+
nametag?: string;
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
interface IncomingTokenTransfer {
|
|
923
|
+
id: string;
|
|
924
|
+
senderPubkey: string;
|
|
925
|
+
payload: TokenTransferPayload;
|
|
926
|
+
timestamp: number;
|
|
927
|
+
}
|
|
928
|
+
type TokenTransferHandler = (transfer: IncomingTokenTransfer) => void;
|
|
929
|
+
interface PaymentRequestPayload {
|
|
930
|
+
/** Amount requested (in smallest units) */
|
|
931
|
+
amount: string | bigint;
|
|
932
|
+
/** Coin/token type ID */
|
|
933
|
+
coinId: string;
|
|
934
|
+
/** Message/memo for recipient */
|
|
935
|
+
message?: string;
|
|
936
|
+
/** Recipient's nametag (who should pay) */
|
|
937
|
+
recipientNametag?: string;
|
|
938
|
+
/** Custom metadata */
|
|
939
|
+
metadata?: Record<string, unknown>;
|
|
940
|
+
}
|
|
941
|
+
interface IncomingPaymentRequest {
|
|
942
|
+
/** Event ID */
|
|
943
|
+
id: string;
|
|
944
|
+
/** Sender's public key */
|
|
945
|
+
senderPubkey: string;
|
|
946
|
+
/** Parsed request data */
|
|
947
|
+
request: {
|
|
948
|
+
requestId: string;
|
|
949
|
+
amount: string;
|
|
950
|
+
coinId: string;
|
|
951
|
+
message?: string;
|
|
952
|
+
recipientNametag?: string;
|
|
953
|
+
metadata?: Record<string, unknown>;
|
|
954
|
+
};
|
|
955
|
+
/** Timestamp */
|
|
956
|
+
timestamp: number;
|
|
957
|
+
}
|
|
958
|
+
type PaymentRequestHandler = (request: IncomingPaymentRequest) => void;
|
|
959
|
+
type PaymentRequestResponseType = 'accepted' | 'rejected' | 'paid';
|
|
960
|
+
interface PaymentRequestResponsePayload {
|
|
961
|
+
/** Original request ID */
|
|
962
|
+
requestId: string;
|
|
963
|
+
/** Response type */
|
|
964
|
+
responseType: PaymentRequestResponseType;
|
|
965
|
+
/** Optional message */
|
|
966
|
+
message?: string;
|
|
967
|
+
/** Transfer ID (if paid) */
|
|
968
|
+
transferId?: string;
|
|
969
|
+
}
|
|
970
|
+
interface IncomingPaymentRequestResponse {
|
|
971
|
+
/** Event ID */
|
|
972
|
+
id: string;
|
|
973
|
+
/** Responder's public key */
|
|
974
|
+
responderPubkey: string;
|
|
975
|
+
/** Parsed response data */
|
|
976
|
+
response: {
|
|
977
|
+
requestId: string;
|
|
978
|
+
responseType: PaymentRequestResponseType;
|
|
979
|
+
message?: string;
|
|
980
|
+
transferId?: string;
|
|
981
|
+
};
|
|
982
|
+
/** Timestamp */
|
|
983
|
+
timestamp: number;
|
|
984
|
+
}
|
|
985
|
+
type PaymentRequestResponseHandler = (response: IncomingPaymentRequestResponse) => void;
|
|
986
|
+
interface IncomingBroadcast {
|
|
987
|
+
id: string;
|
|
988
|
+
authorPubkey: string;
|
|
989
|
+
content: string;
|
|
990
|
+
tags: string[];
|
|
991
|
+
timestamp: number;
|
|
992
|
+
}
|
|
993
|
+
type BroadcastHandler = (broadcast: IncomingBroadcast) => void;
|
|
994
|
+
type TransportEventType = 'transport:connected' | 'transport:disconnected' | 'transport:reconnecting' | 'transport:error' | 'transport:relay_added' | 'transport:relay_removed' | 'message:received' | 'message:sent' | 'transfer:received' | 'transfer:sent';
|
|
995
|
+
interface TransportEvent {
|
|
996
|
+
type: TransportEventType;
|
|
997
|
+
timestamp: number;
|
|
998
|
+
data?: unknown;
|
|
999
|
+
error?: string;
|
|
1000
|
+
}
|
|
1001
|
+
type TransportEventCallback = (event: TransportEvent) => void;
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
* WebSocket Abstraction
|
|
1005
|
+
* Platform-independent WebSocket interface for cross-platform support
|
|
1006
|
+
*/
|
|
1007
|
+
/**
|
|
1008
|
+
* Minimal WebSocket interface compatible with browser and Node.js
|
|
1009
|
+
*/
|
|
1010
|
+
interface IWebSocket {
|
|
1011
|
+
readonly readyState: number;
|
|
1012
|
+
send(data: string): void;
|
|
1013
|
+
close(code?: number, reason?: string): void;
|
|
1014
|
+
onopen: ((event: unknown) => void) | null;
|
|
1015
|
+
onclose: ((event: unknown) => void) | null;
|
|
1016
|
+
onerror: ((event: unknown) => void) | null;
|
|
1017
|
+
onmessage: ((event: IMessageEvent) => void) | null;
|
|
1018
|
+
}
|
|
1019
|
+
interface IMessageEvent {
|
|
1020
|
+
data: string;
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* WebSocket ready states (same as native WebSocket)
|
|
1024
|
+
*/
|
|
1025
|
+
declare const WebSocketReadyState: {
|
|
1026
|
+
readonly CONNECTING: 0;
|
|
1027
|
+
readonly OPEN: 1;
|
|
1028
|
+
readonly CLOSING: 2;
|
|
1029
|
+
readonly CLOSED: 3;
|
|
1030
|
+
};
|
|
1031
|
+
/**
|
|
1032
|
+
* Factory function to create WebSocket instances
|
|
1033
|
+
* Different implementations for browser (native) vs Node.js (ws package)
|
|
1034
|
+
*/
|
|
1035
|
+
type WebSocketFactory = (url: string) => IWebSocket;
|
|
1036
|
+
/**
|
|
1037
|
+
* Generate a unique ID (platform-independent)
|
|
1038
|
+
* Browser: crypto.randomUUID()
|
|
1039
|
+
* Node: crypto.randomUUID() or uuid package
|
|
1040
|
+
*/
|
|
1041
|
+
type UUIDGenerator = () => string;
|
|
1042
|
+
/**
|
|
1043
|
+
* Default UUID generator using crypto.randomUUID
|
|
1044
|
+
* Works in modern browsers and Node 19+
|
|
1045
|
+
*/
|
|
1046
|
+
declare function defaultUUIDGenerator(): string;
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Nostr Transport Provider
|
|
1050
|
+
* Platform-independent implementation using Nostr protocol for P2P messaging
|
|
1051
|
+
*
|
|
1052
|
+
* Uses @unicitylabs/nostr-js-sdk for:
|
|
1053
|
+
* - Real secp256k1 event signing
|
|
1054
|
+
* - NIP-04 encryption/decryption
|
|
1055
|
+
* - Event ID calculation
|
|
1056
|
+
*
|
|
1057
|
+
* WebSocket is injected via factory for cross-platform support
|
|
1058
|
+
*/
|
|
1059
|
+
|
|
1060
|
+
interface NostrTransportProviderConfig {
|
|
1061
|
+
/** Nostr relay URLs */
|
|
1062
|
+
relays?: string[];
|
|
1063
|
+
/** Connection timeout (ms) */
|
|
1064
|
+
timeout?: number;
|
|
1065
|
+
/** Auto-reconnect on disconnect */
|
|
1066
|
+
autoReconnect?: boolean;
|
|
1067
|
+
/** Reconnect delay (ms) */
|
|
1068
|
+
reconnectDelay?: number;
|
|
1069
|
+
/** Max reconnect attempts */
|
|
1070
|
+
maxReconnectAttempts?: number;
|
|
1071
|
+
/** Enable debug logging */
|
|
1072
|
+
debug?: boolean;
|
|
1073
|
+
/** WebSocket factory (required for platform support) */
|
|
1074
|
+
createWebSocket: WebSocketFactory;
|
|
1075
|
+
/** UUID generator (optional, defaults to crypto.randomUUID) */
|
|
1076
|
+
generateUUID?: UUIDGenerator;
|
|
1077
|
+
}
|
|
1078
|
+
declare class NostrTransportProvider implements TransportProvider {
|
|
1079
|
+
readonly id = "nostr";
|
|
1080
|
+
readonly name = "Nostr Transport";
|
|
1081
|
+
readonly type: "p2p";
|
|
1082
|
+
readonly description = "P2P messaging via Nostr protocol";
|
|
1083
|
+
private config;
|
|
1084
|
+
private identity;
|
|
1085
|
+
private keyManager;
|
|
1086
|
+
private status;
|
|
1087
|
+
private connections;
|
|
1088
|
+
private reconnectAttempts;
|
|
1089
|
+
private messageHandlers;
|
|
1090
|
+
private transferHandlers;
|
|
1091
|
+
private paymentRequestHandlers;
|
|
1092
|
+
private paymentRequestResponseHandlers;
|
|
1093
|
+
private broadcastHandlers;
|
|
1094
|
+
private eventCallbacks;
|
|
1095
|
+
private subscriptions;
|
|
1096
|
+
constructor(config: NostrTransportProviderConfig);
|
|
1097
|
+
connect(): Promise<void>;
|
|
1098
|
+
disconnect(): Promise<void>;
|
|
1099
|
+
isConnected(): boolean;
|
|
1100
|
+
getStatus(): ProviderStatus;
|
|
1101
|
+
/**
|
|
1102
|
+
* Get list of configured relay URLs
|
|
1103
|
+
*/
|
|
1104
|
+
getRelays(): string[];
|
|
1105
|
+
/**
|
|
1106
|
+
* Get list of currently connected relay URLs
|
|
1107
|
+
*/
|
|
1108
|
+
getConnectedRelays(): string[];
|
|
1109
|
+
/**
|
|
1110
|
+
* Add a new relay dynamically
|
|
1111
|
+
* Will connect immediately if provider is already connected
|
|
1112
|
+
*/
|
|
1113
|
+
addRelay(relayUrl: string): Promise<boolean>;
|
|
1114
|
+
/**
|
|
1115
|
+
* Remove a relay dynamically
|
|
1116
|
+
* Will disconnect from the relay if connected
|
|
1117
|
+
*/
|
|
1118
|
+
removeRelay(relayUrl: string): Promise<boolean>;
|
|
1119
|
+
/**
|
|
1120
|
+
* Check if a relay is configured
|
|
1121
|
+
*/
|
|
1122
|
+
hasRelay(relayUrl: string): boolean;
|
|
1123
|
+
/**
|
|
1124
|
+
* Check if a relay is currently connected
|
|
1125
|
+
*/
|
|
1126
|
+
isRelayConnected(relayUrl: string): boolean;
|
|
1127
|
+
setIdentity(identity: FullIdentity): void;
|
|
1128
|
+
/**
|
|
1129
|
+
* Get the Nostr-format public key (32 bytes / 64 hex chars)
|
|
1130
|
+
* This is the x-coordinate only, without the 02/03 prefix.
|
|
1131
|
+
*/
|
|
1132
|
+
getNostrPubkey(): string;
|
|
1133
|
+
sendMessage(recipientPubkey: string, content: string): Promise<string>;
|
|
1134
|
+
onMessage(handler: MessageHandler): () => void;
|
|
1135
|
+
sendTokenTransfer(recipientPubkey: string, payload: TokenTransferPayload): Promise<string>;
|
|
1136
|
+
onTokenTransfer(handler: TokenTransferHandler): () => void;
|
|
1137
|
+
sendPaymentRequest(recipientPubkey: string, payload: PaymentRequestPayload): Promise<string>;
|
|
1138
|
+
onPaymentRequest(handler: PaymentRequestHandler): () => void;
|
|
1139
|
+
sendPaymentRequestResponse(recipientPubkey: string, payload: PaymentRequestResponsePayload): Promise<string>;
|
|
1140
|
+
onPaymentRequestResponse(handler: PaymentRequestResponseHandler): () => void;
|
|
1141
|
+
resolveNametag(nametag: string): Promise<string | null>;
|
|
1142
|
+
publishNametag(nametag: string, address: string): Promise<void>;
|
|
1143
|
+
registerNametag(nametag: string, _publicKey: string): Promise<boolean>;
|
|
1144
|
+
subscribeToBroadcast(tags: string[], handler: BroadcastHandler): () => void;
|
|
1145
|
+
publishBroadcast(content: string, tags?: string[]): Promise<string>;
|
|
1146
|
+
onEvent(callback: TransportEventCallback): () => void;
|
|
1147
|
+
private connectToRelay;
|
|
1148
|
+
private scheduleReconnect;
|
|
1149
|
+
private handleRelayMessage;
|
|
1150
|
+
private handleEvent;
|
|
1151
|
+
private handleDirectMessage;
|
|
1152
|
+
private handleTokenTransfer;
|
|
1153
|
+
private handlePaymentRequest;
|
|
1154
|
+
private handlePaymentRequestResponse;
|
|
1155
|
+
private handleBroadcast;
|
|
1156
|
+
private createEvent;
|
|
1157
|
+
private createEncryptedEvent;
|
|
1158
|
+
private publishEvent;
|
|
1159
|
+
private queryEvents;
|
|
1160
|
+
private queryEventsFromRelay;
|
|
1161
|
+
private unsubscribeFromRelay;
|
|
1162
|
+
private subscribeToEvents;
|
|
1163
|
+
private subscribeToTags;
|
|
1164
|
+
private decryptContent;
|
|
1165
|
+
/**
|
|
1166
|
+
* Strip known content prefixes (nostr-js-sdk compatibility)
|
|
1167
|
+
* Handles: payment_request:, token_transfer:, etc.
|
|
1168
|
+
*/
|
|
1169
|
+
private stripContentPrefix;
|
|
1170
|
+
private ensureReady;
|
|
1171
|
+
private emitEvent;
|
|
1172
|
+
private log;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/**
|
|
1176
|
+
* Browser Transport Exports
|
|
1177
|
+
* Re-exports shared transport with browser-specific WebSocket factory
|
|
1178
|
+
*/
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* Browser WebSocket factory using native WebSocket
|
|
1182
|
+
* Cast to IWebSocket since native WebSocket is a superset
|
|
1183
|
+
*/
|
|
1184
|
+
declare function createBrowserWebSocket(url: string): IWebSocket;
|
|
1185
|
+
/**
|
|
1186
|
+
* Create NostrTransportProvider with browser WebSocket
|
|
1187
|
+
* Convenience factory that injects browser-native WebSocket
|
|
1188
|
+
*/
|
|
1189
|
+
declare function createNostrTransportProvider(config?: Omit<NostrTransportProviderConfig, 'createWebSocket'>): NostrTransportProvider;
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Oracle Provider Interface
|
|
1193
|
+
* Platform-independent Unicity oracle abstraction
|
|
1194
|
+
*
|
|
1195
|
+
* The oracle is a trusted third-party service that provides verifiable truth
|
|
1196
|
+
* about the state of tokens in the Unicity network. It aggregates state
|
|
1197
|
+
* transitions into rounds and provides inclusion proofs that cryptographically
|
|
1198
|
+
* verify token ownership and transfers.
|
|
1199
|
+
*/
|
|
1200
|
+
|
|
1201
|
+
/**
|
|
1202
|
+
* Unicity state transition oracle provider
|
|
1203
|
+
*
|
|
1204
|
+
* The oracle serves as the source of truth for:
|
|
1205
|
+
* - Token state validation (spent/unspent)
|
|
1206
|
+
* - State transition inclusion proofs
|
|
1207
|
+
* - Round-based commitment aggregation
|
|
1208
|
+
*/
|
|
1209
|
+
interface OracleProvider extends BaseProvider {
|
|
1210
|
+
/**
|
|
1211
|
+
* Initialize with trust base
|
|
1212
|
+
*/
|
|
1213
|
+
initialize(trustBase?: unknown): Promise<void>;
|
|
1214
|
+
/**
|
|
1215
|
+
* Submit transfer commitment
|
|
1216
|
+
*/
|
|
1217
|
+
submitCommitment(commitment: TransferCommitment): Promise<SubmitResult>;
|
|
1218
|
+
/**
|
|
1219
|
+
* Get inclusion proof for a request
|
|
1220
|
+
*/
|
|
1221
|
+
getProof(requestId: string): Promise<InclusionProof | null>;
|
|
1222
|
+
/**
|
|
1223
|
+
* Wait for inclusion proof with polling
|
|
1224
|
+
*/
|
|
1225
|
+
waitForProof(requestId: string, options?: WaitOptions): Promise<InclusionProof>;
|
|
1226
|
+
/**
|
|
1227
|
+
* Validate token against aggregator
|
|
1228
|
+
*/
|
|
1229
|
+
validateToken(tokenData: unknown): Promise<ValidationResult>;
|
|
1230
|
+
/**
|
|
1231
|
+
* Check if token state is spent
|
|
1232
|
+
*/
|
|
1233
|
+
isSpent(stateHash: string): Promise<boolean>;
|
|
1234
|
+
/**
|
|
1235
|
+
* Get token state
|
|
1236
|
+
*/
|
|
1237
|
+
getTokenState(tokenId: string): Promise<TokenState | null>;
|
|
1238
|
+
/**
|
|
1239
|
+
* Get current round number
|
|
1240
|
+
*/
|
|
1241
|
+
getCurrentRound(): Promise<number>;
|
|
1242
|
+
/**
|
|
1243
|
+
* Mint new tokens (for faucet/testing)
|
|
1244
|
+
*/
|
|
1245
|
+
mint?(params: MintParams): Promise<MintResult>;
|
|
1246
|
+
/**
|
|
1247
|
+
* Get underlying StateTransitionClient (if available)
|
|
1248
|
+
* Used for advanced SDK operations like commitment creation
|
|
1249
|
+
*/
|
|
1250
|
+
getStateTransitionClient?(): unknown;
|
|
1251
|
+
/**
|
|
1252
|
+
* Get underlying AggregatorClient (if available)
|
|
1253
|
+
* Used for direct aggregator API access
|
|
1254
|
+
*/
|
|
1255
|
+
getAggregatorClient?(): unknown;
|
|
1256
|
+
/**
|
|
1257
|
+
* Wait for inclusion proof using SDK commitment (if available)
|
|
1258
|
+
* Used for transfer flows with SDK TransferCommitment
|
|
1259
|
+
*/
|
|
1260
|
+
waitForProofSdk?(commitment: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
1261
|
+
}
|
|
1262
|
+
interface TransferCommitment {
|
|
1263
|
+
/** Source token (SDK format) */
|
|
1264
|
+
sourceToken: unknown;
|
|
1265
|
+
/** Recipient address/predicate */
|
|
1266
|
+
recipient: string;
|
|
1267
|
+
/** Random salt (non-reproducible) */
|
|
1268
|
+
salt: Uint8Array;
|
|
1269
|
+
/** Optional additional data */
|
|
1270
|
+
data?: unknown;
|
|
1271
|
+
}
|
|
1272
|
+
interface SubmitResult {
|
|
1273
|
+
success: boolean;
|
|
1274
|
+
requestId?: string;
|
|
1275
|
+
error?: string;
|
|
1276
|
+
timestamp: number;
|
|
1277
|
+
}
|
|
1278
|
+
interface InclusionProof {
|
|
1279
|
+
requestId: string;
|
|
1280
|
+
roundNumber: number;
|
|
1281
|
+
proof: unknown;
|
|
1282
|
+
timestamp: number;
|
|
1283
|
+
}
|
|
1284
|
+
interface WaitOptions {
|
|
1285
|
+
/** Timeout in ms (default: 30000) */
|
|
1286
|
+
timeout?: number;
|
|
1287
|
+
/** Poll interval in ms (default: 1000) */
|
|
1288
|
+
pollInterval?: number;
|
|
1289
|
+
/** Callback on each poll attempt */
|
|
1290
|
+
onPoll?: (attempt: number) => void;
|
|
1291
|
+
}
|
|
1292
|
+
interface ValidationResult {
|
|
1293
|
+
valid: boolean;
|
|
1294
|
+
spent: boolean;
|
|
1295
|
+
error?: string;
|
|
1296
|
+
stateHash?: string;
|
|
1297
|
+
}
|
|
1298
|
+
interface TokenState {
|
|
1299
|
+
tokenId: string;
|
|
1300
|
+
stateHash: string;
|
|
1301
|
+
spent: boolean;
|
|
1302
|
+
roundNumber?: number;
|
|
1303
|
+
lastUpdated: number;
|
|
1304
|
+
}
|
|
1305
|
+
interface MintParams {
|
|
1306
|
+
coinId: string;
|
|
1307
|
+
amount: string;
|
|
1308
|
+
recipientAddress: string;
|
|
1309
|
+
recipientPubkey?: string;
|
|
1310
|
+
}
|
|
1311
|
+
interface MintResult {
|
|
1312
|
+
success: boolean;
|
|
1313
|
+
requestId?: string;
|
|
1314
|
+
tokenId?: string;
|
|
1315
|
+
error?: string;
|
|
1316
|
+
}
|
|
1317
|
+
type OracleEventType = 'oracle:connected' | 'oracle:disconnected' | 'oracle:error' | 'commitment:submitted' | 'proof:received' | 'validation:completed';
|
|
1318
|
+
interface OracleEvent {
|
|
1319
|
+
type: OracleEventType;
|
|
1320
|
+
timestamp: number;
|
|
1321
|
+
data?: unknown;
|
|
1322
|
+
error?: string;
|
|
1323
|
+
}
|
|
1324
|
+
type OracleEventCallback = (event: OracleEvent) => void;
|
|
1325
|
+
/**
|
|
1326
|
+
* Trust base loader interface for platform-specific loading
|
|
1327
|
+
* Browser: fetch from URL
|
|
1328
|
+
* Node.js: read from file
|
|
1329
|
+
*/
|
|
1330
|
+
interface TrustBaseLoader {
|
|
1331
|
+
/**
|
|
1332
|
+
* Load trust base JSON data
|
|
1333
|
+
* @returns Trust base data or null if not available
|
|
1334
|
+
*/
|
|
1335
|
+
load(): Promise<unknown | null>;
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
/**
|
|
1339
|
+
* Unicity Aggregator Provider
|
|
1340
|
+
* Platform-independent implementation using @unicitylabs/state-transition-sdk
|
|
1341
|
+
*
|
|
1342
|
+
* The oracle is a trusted service that provides verifiable truth
|
|
1343
|
+
* about token state through cryptographic inclusion proofs.
|
|
1344
|
+
*
|
|
1345
|
+
* TrustBaseLoader is injected for platform-specific loading:
|
|
1346
|
+
* - Browser: fetch from URL
|
|
1347
|
+
* - Node.js: read from file
|
|
1348
|
+
*/
|
|
1349
|
+
|
|
1350
|
+
interface SdkMintCommitment {
|
|
1351
|
+
requestId?: {
|
|
1352
|
+
toString(): string;
|
|
1353
|
+
};
|
|
1354
|
+
[key: string]: unknown;
|
|
1355
|
+
}
|
|
1356
|
+
interface UnicityAggregatorProviderConfig {
|
|
1357
|
+
/** Aggregator URL */
|
|
1358
|
+
url: string;
|
|
1359
|
+
/** API key for authentication */
|
|
1360
|
+
apiKey?: string;
|
|
1361
|
+
/** Request timeout (ms) */
|
|
1362
|
+
timeout?: number;
|
|
1363
|
+
/** Skip trust base verification (dev only) */
|
|
1364
|
+
skipVerification?: boolean;
|
|
1365
|
+
/** Enable debug logging */
|
|
1366
|
+
debug?: boolean;
|
|
1367
|
+
/** Trust base loader (platform-specific) */
|
|
1368
|
+
trustBaseLoader?: TrustBaseLoader;
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Unicity Aggregator Provider
|
|
1372
|
+
* Concrete implementation of OracleProvider using Unicity's aggregator service
|
|
1373
|
+
*/
|
|
1374
|
+
declare class UnicityAggregatorProvider implements OracleProvider {
|
|
1375
|
+
readonly id = "unicity-aggregator";
|
|
1376
|
+
readonly name = "Unicity Aggregator";
|
|
1377
|
+
readonly type: "network";
|
|
1378
|
+
readonly description = "Unicity state transition aggregator (oracle implementation)";
|
|
1379
|
+
private config;
|
|
1380
|
+
private status;
|
|
1381
|
+
private eventCallbacks;
|
|
1382
|
+
private aggregatorClient;
|
|
1383
|
+
private stateTransitionClient;
|
|
1384
|
+
private trustBase;
|
|
1385
|
+
/** Get the current trust base */
|
|
1386
|
+
getTrustBase(): RootTrustBase | null;
|
|
1387
|
+
/** Get the state transition client */
|
|
1388
|
+
getStateTransitionClient(): StateTransitionClient | null;
|
|
1389
|
+
/** Get the aggregator client */
|
|
1390
|
+
getAggregatorClient(): AggregatorClient | null;
|
|
1391
|
+
private spentCache;
|
|
1392
|
+
constructor(config: UnicityAggregatorProviderConfig);
|
|
1393
|
+
connect(): Promise<void>;
|
|
1394
|
+
disconnect(): Promise<void>;
|
|
1395
|
+
isConnected(): boolean;
|
|
1396
|
+
getStatus(): ProviderStatus;
|
|
1397
|
+
initialize(trustBase?: RootTrustBase): Promise<void>;
|
|
1398
|
+
/**
|
|
1399
|
+
* Submit a transfer commitment to the aggregator.
|
|
1400
|
+
* Accepts either an SDK TransferCommitment or a simple commitment object.
|
|
1401
|
+
*/
|
|
1402
|
+
submitCommitment(commitment: TransferCommitment | TransferCommitment$1): Promise<SubmitResult>;
|
|
1403
|
+
/**
|
|
1404
|
+
* Submit a mint commitment to the aggregator (SDK only)
|
|
1405
|
+
* @param commitment - SDK MintCommitment instance
|
|
1406
|
+
*/
|
|
1407
|
+
submitMintCommitment(commitment: SdkMintCommitment): Promise<SubmitResult>;
|
|
1408
|
+
private isSdkTransferCommitment;
|
|
1409
|
+
getProof(requestId: string): Promise<InclusionProof | null>;
|
|
1410
|
+
waitForProof(requestId: string, options?: WaitOptions): Promise<InclusionProof>;
|
|
1411
|
+
validateToken(tokenData: unknown): Promise<ValidationResult>;
|
|
1412
|
+
/**
|
|
1413
|
+
* Wait for inclusion proof using SDK (for SDK commitments)
|
|
1414
|
+
*/
|
|
1415
|
+
waitForProofSdk(commitment: TransferCommitment$1 | SdkMintCommitment, signal?: AbortSignal): Promise<unknown>;
|
|
1416
|
+
isSpent(stateHash: string): Promise<boolean>;
|
|
1417
|
+
getTokenState(tokenId: string): Promise<TokenState | null>;
|
|
1418
|
+
getCurrentRound(): Promise<number>;
|
|
1419
|
+
mint(params: MintParams): Promise<MintResult>;
|
|
1420
|
+
onEvent(callback: OracleEventCallback): () => void;
|
|
1421
|
+
private rpcCall;
|
|
1422
|
+
private ensureConnected;
|
|
1423
|
+
private emitEvent;
|
|
1424
|
+
private log;
|
|
1425
|
+
}
|
|
1426
|
+
/** @deprecated Use UnicityAggregatorProvider instead */
|
|
1427
|
+
declare const UnicityOracleProvider: typeof UnicityAggregatorProvider;
|
|
1428
|
+
/** @deprecated Use UnicityAggregatorProviderConfig instead */
|
|
1429
|
+
type UnicityOracleProviderConfig = UnicityAggregatorProviderConfig;
|
|
1430
|
+
|
|
1431
|
+
/**
|
|
1432
|
+
* Browser Oracle Exports
|
|
1433
|
+
* Re-exports shared oracle with browser-specific TrustBaseLoader
|
|
1434
|
+
*/
|
|
1435
|
+
|
|
1436
|
+
/**
|
|
1437
|
+
* Browser TrustBase loader using fetch
|
|
1438
|
+
*/
|
|
1439
|
+
declare class BrowserTrustBaseLoader implements TrustBaseLoader {
|
|
1440
|
+
private url;
|
|
1441
|
+
constructor(url?: string);
|
|
1442
|
+
load(): Promise<unknown | null>;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Create browser TrustBase loader
|
|
1446
|
+
*/
|
|
1447
|
+
declare function createBrowserTrustBaseLoader(url?: string): TrustBaseLoader;
|
|
1448
|
+
/**
|
|
1449
|
+
* Create UnicityAggregatorProvider with browser TrustBase loader
|
|
1450
|
+
* Convenience factory that injects browser-specific loader
|
|
1451
|
+
*/
|
|
1452
|
+
declare function createUnicityAggregatorProvider(config: Omit<UnicityAggregatorProviderConfig, 'trustBaseLoader'> & {
|
|
1453
|
+
trustBaseUrl?: string;
|
|
1454
|
+
}): UnicityAggregatorProvider;
|
|
1455
|
+
/** @deprecated Use createUnicityAggregatorProvider instead */
|
|
1456
|
+
declare const createUnicityOracleProvider: typeof createUnicityAggregatorProvider;
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* L1 Payments Sub-Module
|
|
1460
|
+
*
|
|
1461
|
+
* Handles Layer 1 (ALPHA blockchain) transactions including:
|
|
1462
|
+
* - Balance queries
|
|
1463
|
+
* - UTXO management
|
|
1464
|
+
* - Transaction sending
|
|
1465
|
+
* - Vesting classification
|
|
1466
|
+
* - Transaction history
|
|
1467
|
+
*/
|
|
1468
|
+
|
|
1469
|
+
interface L1SendRequest {
|
|
1470
|
+
/** Recipient address */
|
|
1471
|
+
to: string;
|
|
1472
|
+
/** Amount in satoshis */
|
|
1473
|
+
amount: string;
|
|
1474
|
+
/** Fee rate in sat/byte */
|
|
1475
|
+
feeRate?: number;
|
|
1476
|
+
/** Use vested coins only */
|
|
1477
|
+
useVested?: boolean;
|
|
1478
|
+
/** Memo/OP_RETURN data */
|
|
1479
|
+
memo?: string;
|
|
1480
|
+
}
|
|
1481
|
+
interface L1SendResult {
|
|
1482
|
+
success: boolean;
|
|
1483
|
+
txHash?: string;
|
|
1484
|
+
fee?: string;
|
|
1485
|
+
error?: string;
|
|
1486
|
+
}
|
|
1487
|
+
interface L1Balance {
|
|
1488
|
+
confirmed: string;
|
|
1489
|
+
unconfirmed: string;
|
|
1490
|
+
vested: string;
|
|
1491
|
+
unvested: string;
|
|
1492
|
+
total: string;
|
|
1493
|
+
}
|
|
1494
|
+
interface L1Utxo {
|
|
1495
|
+
txid: string;
|
|
1496
|
+
vout: number;
|
|
1497
|
+
amount: string;
|
|
1498
|
+
address: string;
|
|
1499
|
+
isVested: boolean;
|
|
1500
|
+
confirmations: number;
|
|
1501
|
+
coinbaseHeight?: number;
|
|
1502
|
+
}
|
|
1503
|
+
interface L1Transaction {
|
|
1504
|
+
txid: string;
|
|
1505
|
+
type: 'send' | 'receive';
|
|
1506
|
+
amount: string;
|
|
1507
|
+
fee?: string;
|
|
1508
|
+
address: string;
|
|
1509
|
+
confirmations: number;
|
|
1510
|
+
timestamp: number;
|
|
1511
|
+
blockHeight?: number;
|
|
1512
|
+
}
|
|
1513
|
+
interface L1PaymentsModuleConfig {
|
|
1514
|
+
/** Fulcrum server URL */
|
|
1515
|
+
electrumUrl?: string;
|
|
1516
|
+
/** Network: mainnet or testnet */
|
|
1517
|
+
network?: 'mainnet' | 'testnet';
|
|
1518
|
+
/** Default fee rate */
|
|
1519
|
+
defaultFeeRate?: number;
|
|
1520
|
+
/** Enable vesting classification */
|
|
1521
|
+
enableVesting?: boolean;
|
|
1522
|
+
}
|
|
1523
|
+
interface L1PaymentsModuleDependencies {
|
|
1524
|
+
identity: FullIdentity;
|
|
1525
|
+
chainCode?: string;
|
|
1526
|
+
addresses?: string[];
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* L1 Payments Module - Full Implementation
|
|
1530
|
+
*
|
|
1531
|
+
* Handles all L1 (ALPHA blockchain) operations including balance queries,
|
|
1532
|
+
* transaction sending, UTXO management, and vesting classification.
|
|
1533
|
+
*/
|
|
1534
|
+
declare class L1PaymentsModule {
|
|
1535
|
+
private _initialized;
|
|
1536
|
+
private _config;
|
|
1537
|
+
private _identity?;
|
|
1538
|
+
private _chainCode?;
|
|
1539
|
+
private _addresses;
|
|
1540
|
+
private _wallet?;
|
|
1541
|
+
constructor(config?: L1PaymentsModuleConfig);
|
|
1542
|
+
initialize(deps: L1PaymentsModuleDependencies): Promise<void>;
|
|
1543
|
+
destroy(): void;
|
|
1544
|
+
send(request: L1SendRequest): Promise<L1SendResult>;
|
|
1545
|
+
getBalance(): Promise<L1Balance>;
|
|
1546
|
+
getUtxos(): Promise<L1Utxo[]>;
|
|
1547
|
+
getHistory(limit?: number): Promise<L1Transaction[]>;
|
|
1548
|
+
getTransaction(txid: string): Promise<L1Transaction | null>;
|
|
1549
|
+
estimateFee(to: string, amount: string): Promise<{
|
|
1550
|
+
fee: string;
|
|
1551
|
+
feeRate: number;
|
|
1552
|
+
}>;
|
|
1553
|
+
getAddresses(): string[];
|
|
1554
|
+
addAddress(address: string): void;
|
|
1555
|
+
getVestingThreshold(): number;
|
|
1556
|
+
isConnected(): boolean;
|
|
1557
|
+
private ensureInitialized;
|
|
1558
|
+
private _getWatchedAddresses;
|
|
1559
|
+
private _getAllUtxos;
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* Nametag Minter
|
|
1564
|
+
* Mints nametag tokens on-chain for PROXY address support
|
|
1565
|
+
*
|
|
1566
|
+
* Flow (same as Sphere wallet and lottery):
|
|
1567
|
+
* 1. Generate salt
|
|
1568
|
+
* 2. Create MintTransactionData from nametag
|
|
1569
|
+
* 3. Create MintCommitment
|
|
1570
|
+
* 4. Submit to aggregator
|
|
1571
|
+
* 5. Wait for inclusion proof
|
|
1572
|
+
* 6. Create Token with proof
|
|
1573
|
+
* 7. Return token for storage
|
|
1574
|
+
*/
|
|
1575
|
+
|
|
1576
|
+
interface MintNametagResult {
|
|
1577
|
+
success: boolean;
|
|
1578
|
+
token?: Token$1<any>;
|
|
1579
|
+
nametagData?: NametagData;
|
|
1580
|
+
error?: string;
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* Payments Module
|
|
1585
|
+
* Platform-independent token operations with full wallet repository functionality
|
|
1586
|
+
*
|
|
1587
|
+
* Includes:
|
|
1588
|
+
* - Token CRUD operations
|
|
1589
|
+
* - Tombstones for sync
|
|
1590
|
+
* - Archived tokens (spent history)
|
|
1591
|
+
* - Forked tokens (alternative histories)
|
|
1592
|
+
* - Transaction history
|
|
1593
|
+
* - Nametag storage
|
|
1594
|
+
*/
|
|
1595
|
+
|
|
1596
|
+
interface TransactionHistoryEntry {
|
|
1597
|
+
id: string;
|
|
1598
|
+
type: 'SENT' | 'RECEIVED' | 'SPLIT' | 'MINT';
|
|
1599
|
+
amount: string;
|
|
1600
|
+
coinId: string;
|
|
1601
|
+
symbol: string;
|
|
1602
|
+
timestamp: number;
|
|
1603
|
+
recipientNametag?: string;
|
|
1604
|
+
senderPubkey?: string;
|
|
1605
|
+
txHash?: string;
|
|
1606
|
+
}
|
|
1607
|
+
interface PaymentsModuleConfig {
|
|
1608
|
+
/** Auto-sync after operations */
|
|
1609
|
+
autoSync?: boolean;
|
|
1610
|
+
/** Auto-validate with aggregator */
|
|
1611
|
+
autoValidate?: boolean;
|
|
1612
|
+
/** Retry failed transfers */
|
|
1613
|
+
retryFailed?: boolean;
|
|
1614
|
+
/** Max retry attempts */
|
|
1615
|
+
maxRetries?: number;
|
|
1616
|
+
/** Enable debug logging */
|
|
1617
|
+
debug?: boolean;
|
|
1618
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
1619
|
+
l1?: L1PaymentsModuleConfig;
|
|
1620
|
+
}
|
|
1621
|
+
interface PaymentsModuleDependencies {
|
|
1622
|
+
identity: FullIdentity;
|
|
1623
|
+
storage: StorageProvider;
|
|
1624
|
+
/** @deprecated Use tokenStorageProviders instead */
|
|
1625
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
1626
|
+
/** Multiple token storage providers (e.g., IPFS, MongoDB, file) */
|
|
1627
|
+
tokenStorageProviders?: Map<string, TokenStorageProvider<TxfStorageDataBase>>;
|
|
1628
|
+
transport: TransportProvider;
|
|
1629
|
+
oracle: OracleProvider;
|
|
1630
|
+
emitEvent: <T extends SphereEventType>(type: T, data: SphereEventMap[T]) => void;
|
|
1631
|
+
/** Chain code for BIP32 HD derivation (for L1 multi-address support) */
|
|
1632
|
+
chainCode?: string;
|
|
1633
|
+
/** Additional L1 addresses to watch */
|
|
1634
|
+
l1Addresses?: string[];
|
|
1635
|
+
}
|
|
1636
|
+
declare class PaymentsModule {
|
|
1637
|
+
private readonly moduleConfig;
|
|
1638
|
+
private deps;
|
|
1639
|
+
/** L1 (ALPHA blockchain) payments sub-module (null if disabled) */
|
|
1640
|
+
readonly l1: L1PaymentsModule | null;
|
|
1641
|
+
private tokens;
|
|
1642
|
+
private pendingTransfers;
|
|
1643
|
+
private tombstones;
|
|
1644
|
+
private archivedTokens;
|
|
1645
|
+
private forkedTokens;
|
|
1646
|
+
private transactionHistory;
|
|
1647
|
+
private nametag;
|
|
1648
|
+
private paymentRequests;
|
|
1649
|
+
private paymentRequestHandlers;
|
|
1650
|
+
private outgoingPaymentRequests;
|
|
1651
|
+
private paymentRequestResponseHandlers;
|
|
1652
|
+
private pendingResponseResolvers;
|
|
1653
|
+
private unsubscribeTransfers;
|
|
1654
|
+
private unsubscribePaymentRequests;
|
|
1655
|
+
private unsubscribePaymentRequestResponses;
|
|
1656
|
+
constructor(config?: PaymentsModuleConfig);
|
|
1657
|
+
/** Get module configuration */
|
|
1658
|
+
getConfig(): Omit<Required<PaymentsModuleConfig>, 'l1'>;
|
|
1659
|
+
private log;
|
|
1660
|
+
/**
|
|
1661
|
+
* Initialize module with dependencies
|
|
1662
|
+
*/
|
|
1663
|
+
initialize(deps: PaymentsModuleDependencies): void;
|
|
1664
|
+
/**
|
|
1665
|
+
* Load tokens from storage
|
|
1666
|
+
*/
|
|
1667
|
+
load(): Promise<void>;
|
|
1668
|
+
/**
|
|
1669
|
+
* Cleanup resources
|
|
1670
|
+
*/
|
|
1671
|
+
destroy(): void;
|
|
1672
|
+
/**
|
|
1673
|
+
* Send tokens to recipient
|
|
1674
|
+
* Supports automatic token splitting when exact amount is needed
|
|
1675
|
+
*/
|
|
1676
|
+
send(request: TransferRequest): Promise<TransferResult>;
|
|
1677
|
+
/**
|
|
1678
|
+
* Get coin symbol from coinId
|
|
1679
|
+
*/
|
|
1680
|
+
private getCoinSymbol;
|
|
1681
|
+
/**
|
|
1682
|
+
* Get coin name from coinId
|
|
1683
|
+
*/
|
|
1684
|
+
private getCoinName;
|
|
1685
|
+
/**
|
|
1686
|
+
* Send a payment request to someone
|
|
1687
|
+
* @param recipientPubkeyOrNametag - Recipient's pubkey or @nametag
|
|
1688
|
+
* @param request - Payment request details
|
|
1689
|
+
* @returns Result with event ID
|
|
1690
|
+
*/
|
|
1691
|
+
sendPaymentRequest(recipientPubkeyOrNametag: string, request: Omit<PaymentRequest, 'id' | 'createdAt'>): Promise<PaymentRequestResult>;
|
|
1692
|
+
/**
|
|
1693
|
+
* Subscribe to incoming payment requests
|
|
1694
|
+
* @param handler - Handler function for incoming requests
|
|
1695
|
+
* @returns Unsubscribe function
|
|
1696
|
+
*/
|
|
1697
|
+
onPaymentRequest(handler: PaymentRequestHandler$1): () => void;
|
|
1698
|
+
/**
|
|
1699
|
+
* Get all payment requests
|
|
1700
|
+
* @param filter - Optional status filter
|
|
1701
|
+
*/
|
|
1702
|
+
getPaymentRequests(filter?: {
|
|
1703
|
+
status?: PaymentRequestStatus;
|
|
1704
|
+
}): IncomingPaymentRequest$1[];
|
|
1705
|
+
/**
|
|
1706
|
+
* Get pending payment requests count
|
|
1707
|
+
*/
|
|
1708
|
+
getPendingPaymentRequestsCount(): number;
|
|
1709
|
+
/**
|
|
1710
|
+
* Accept a payment request (marks it as accepted, user should then call send())
|
|
1711
|
+
*/
|
|
1712
|
+
acceptPaymentRequest(requestId: string): Promise<void>;
|
|
1713
|
+
/**
|
|
1714
|
+
* Reject a payment request
|
|
1715
|
+
*/
|
|
1716
|
+
rejectPaymentRequest(requestId: string): Promise<void>;
|
|
1717
|
+
/**
|
|
1718
|
+
* Mark a payment request as paid (after successful transfer)
|
|
1719
|
+
*/
|
|
1720
|
+
markPaymentRequestPaid(requestId: string): void;
|
|
1721
|
+
/**
|
|
1722
|
+
* Clear processed (non-pending) payment requests
|
|
1723
|
+
*/
|
|
1724
|
+
clearProcessedPaymentRequests(): void;
|
|
1725
|
+
/**
|
|
1726
|
+
* Remove a specific payment request
|
|
1727
|
+
*/
|
|
1728
|
+
removePaymentRequest(requestId: string): void;
|
|
1729
|
+
/**
|
|
1730
|
+
* Pay a payment request directly
|
|
1731
|
+
* Convenience method that accepts, sends, and marks as paid
|
|
1732
|
+
*/
|
|
1733
|
+
payPaymentRequest(requestId: string, memo?: string): Promise<TransferResult>;
|
|
1734
|
+
private updatePaymentRequestStatus;
|
|
1735
|
+
private handleIncomingPaymentRequest;
|
|
1736
|
+
/**
|
|
1737
|
+
* Get outgoing payment requests
|
|
1738
|
+
* @param filter - Optional status filter
|
|
1739
|
+
*/
|
|
1740
|
+
getOutgoingPaymentRequests(filter?: {
|
|
1741
|
+
status?: PaymentRequestStatus;
|
|
1742
|
+
}): OutgoingPaymentRequest[];
|
|
1743
|
+
/**
|
|
1744
|
+
* Subscribe to payment request responses (for outgoing requests)
|
|
1745
|
+
* @param handler - Handler function for incoming responses
|
|
1746
|
+
* @returns Unsubscribe function
|
|
1747
|
+
*/
|
|
1748
|
+
onPaymentRequestResponse(handler: PaymentRequestResponseHandler$1): () => void;
|
|
1749
|
+
/**
|
|
1750
|
+
* Wait for a response to a payment request
|
|
1751
|
+
* @param requestId - The outgoing request ID to wait for
|
|
1752
|
+
* @param timeoutMs - Timeout in milliseconds (default: 60000)
|
|
1753
|
+
* @returns Promise that resolves with the response or rejects on timeout
|
|
1754
|
+
*/
|
|
1755
|
+
waitForPaymentResponse(requestId: string, timeoutMs?: number): Promise<PaymentRequestResponse>;
|
|
1756
|
+
/**
|
|
1757
|
+
* Cancel waiting for a payment response
|
|
1758
|
+
*/
|
|
1759
|
+
cancelWaitForPaymentResponse(requestId: string): void;
|
|
1760
|
+
/**
|
|
1761
|
+
* Remove an outgoing payment request
|
|
1762
|
+
*/
|
|
1763
|
+
removeOutgoingPaymentRequest(requestId: string): void;
|
|
1764
|
+
/**
|
|
1765
|
+
* Clear completed/expired outgoing payment requests
|
|
1766
|
+
*/
|
|
1767
|
+
clearCompletedOutgoingPaymentRequests(): void;
|
|
1768
|
+
private handlePaymentRequestResponse;
|
|
1769
|
+
/**
|
|
1770
|
+
* Send a response to a payment request (used internally by accept/reject/pay methods)
|
|
1771
|
+
*/
|
|
1772
|
+
private sendPaymentRequestResponse;
|
|
1773
|
+
/**
|
|
1774
|
+
* Get balance for coin type
|
|
1775
|
+
*/
|
|
1776
|
+
getBalance(coinId?: string): TokenBalance[];
|
|
1777
|
+
/**
|
|
1778
|
+
* Get all tokens
|
|
1779
|
+
*/
|
|
1780
|
+
getTokens(filter?: {
|
|
1781
|
+
coinId?: string;
|
|
1782
|
+
status?: TokenStatus;
|
|
1783
|
+
}): Token[];
|
|
1784
|
+
/**
|
|
1785
|
+
* Get single token
|
|
1786
|
+
*/
|
|
1787
|
+
getToken(id: string): Token | undefined;
|
|
1788
|
+
/**
|
|
1789
|
+
* Add a token
|
|
1790
|
+
* @returns false if duplicate
|
|
1791
|
+
*/
|
|
1792
|
+
addToken(token: Token, skipHistory?: boolean): Promise<boolean>;
|
|
1793
|
+
/**
|
|
1794
|
+
* Save token as individual file to token storage providers
|
|
1795
|
+
* Similar to lottery's saveReceivedToken() pattern
|
|
1796
|
+
*/
|
|
1797
|
+
private saveTokenToFileStorage;
|
|
1798
|
+
/**
|
|
1799
|
+
* Load tokens from file storage providers (lottery compatibility)
|
|
1800
|
+
* This loads tokens from file-based storage that may have been saved
|
|
1801
|
+
* by other applications using the same storage directory.
|
|
1802
|
+
*/
|
|
1803
|
+
private loadTokensFromFileStorage;
|
|
1804
|
+
/**
|
|
1805
|
+
* Update an existing token
|
|
1806
|
+
*/
|
|
1807
|
+
updateToken(token: Token): Promise<void>;
|
|
1808
|
+
/**
|
|
1809
|
+
* Remove a token by ID
|
|
1810
|
+
*/
|
|
1811
|
+
removeToken(tokenId: string, recipientNametag?: string, skipHistory?: boolean): Promise<void>;
|
|
1812
|
+
/**
|
|
1813
|
+
* Get all tombstones
|
|
1814
|
+
*/
|
|
1815
|
+
getTombstones(): TombstoneEntry[];
|
|
1816
|
+
/**
|
|
1817
|
+
* Check if token state is tombstoned
|
|
1818
|
+
*/
|
|
1819
|
+
isStateTombstoned(tokenId: string, stateHash: string): boolean;
|
|
1820
|
+
/**
|
|
1821
|
+
* Merge remote tombstones
|
|
1822
|
+
* @returns number of local tokens removed
|
|
1823
|
+
*/
|
|
1824
|
+
mergeTombstones(remoteTombstones: TombstoneEntry[]): Promise<number>;
|
|
1825
|
+
/**
|
|
1826
|
+
* Prune old tombstones
|
|
1827
|
+
*/
|
|
1828
|
+
pruneTombstones(maxAge?: number): Promise<void>;
|
|
1829
|
+
/**
|
|
1830
|
+
* Get archived tokens
|
|
1831
|
+
*/
|
|
1832
|
+
getArchivedTokens(): Map<string, TxfToken>;
|
|
1833
|
+
/**
|
|
1834
|
+
* Get best archived version of a token
|
|
1835
|
+
*/
|
|
1836
|
+
getBestArchivedVersion(tokenId: string): TxfToken | null;
|
|
1837
|
+
/**
|
|
1838
|
+
* Merge remote archived tokens
|
|
1839
|
+
* @returns number of tokens updated/added
|
|
1840
|
+
*/
|
|
1841
|
+
mergeArchivedTokens(remoteArchived: Map<string, TxfToken>): Promise<number>;
|
|
1842
|
+
/**
|
|
1843
|
+
* Prune archived tokens
|
|
1844
|
+
*/
|
|
1845
|
+
pruneArchivedTokens(maxCount?: number): Promise<void>;
|
|
1846
|
+
/**
|
|
1847
|
+
* Get forked tokens
|
|
1848
|
+
*/
|
|
1849
|
+
getForkedTokens(): Map<string, TxfToken>;
|
|
1850
|
+
/**
|
|
1851
|
+
* Store a forked token
|
|
1852
|
+
*/
|
|
1853
|
+
storeForkedToken(tokenId: string, stateHash: string, txfToken: TxfToken): Promise<void>;
|
|
1854
|
+
/**
|
|
1855
|
+
* Merge remote forked tokens
|
|
1856
|
+
* @returns number of tokens added
|
|
1857
|
+
*/
|
|
1858
|
+
mergeForkedTokens(remoteForked: Map<string, TxfToken>): Promise<number>;
|
|
1859
|
+
/**
|
|
1860
|
+
* Prune forked tokens
|
|
1861
|
+
*/
|
|
1862
|
+
pruneForkedTokens(maxCount?: number): Promise<void>;
|
|
1863
|
+
/**
|
|
1864
|
+
* Get transaction history
|
|
1865
|
+
*/
|
|
1866
|
+
getHistory(): TransactionHistoryEntry[];
|
|
1867
|
+
/**
|
|
1868
|
+
* Add to transaction history
|
|
1869
|
+
*/
|
|
1870
|
+
addToHistory(entry: Omit<TransactionHistoryEntry, 'id'>): Promise<void>;
|
|
1871
|
+
/**
|
|
1872
|
+
* Set nametag for current identity
|
|
1873
|
+
*/
|
|
1874
|
+
setNametag(nametag: NametagData): Promise<void>;
|
|
1875
|
+
/**
|
|
1876
|
+
* Get nametag
|
|
1877
|
+
*/
|
|
1878
|
+
getNametag(): NametagData | null;
|
|
1879
|
+
/**
|
|
1880
|
+
* Check if has nametag
|
|
1881
|
+
*/
|
|
1882
|
+
hasNametag(): boolean;
|
|
1883
|
+
/**
|
|
1884
|
+
* Clear nametag
|
|
1885
|
+
*/
|
|
1886
|
+
clearNametag(): Promise<void>;
|
|
1887
|
+
/**
|
|
1888
|
+
* Save nametag to file storage for lottery compatibility
|
|
1889
|
+
* Creates file: nametag-{name}.json
|
|
1890
|
+
*/
|
|
1891
|
+
private saveNametagToFileStorage;
|
|
1892
|
+
/**
|
|
1893
|
+
* Load nametag from file storage (lottery compatibility)
|
|
1894
|
+
* Looks for file: nametag-{name}.json
|
|
1895
|
+
*/
|
|
1896
|
+
private loadNametagFromFileStorage;
|
|
1897
|
+
/**
|
|
1898
|
+
* Mint a nametag token on-chain (like Sphere wallet and lottery)
|
|
1899
|
+
* This creates the nametag token required for receiving tokens via PROXY addresses
|
|
1900
|
+
*
|
|
1901
|
+
* @param nametag - The nametag to mint (e.g., "alice" or "@alice")
|
|
1902
|
+
* @returns MintNametagResult with success status and token if successful
|
|
1903
|
+
*/
|
|
1904
|
+
mintNametag(nametag: string): Promise<MintNametagResult>;
|
|
1905
|
+
/**
|
|
1906
|
+
* Check if a nametag is available for minting
|
|
1907
|
+
* @param nametag - The nametag to check (e.g., "alice" or "@alice")
|
|
1908
|
+
*/
|
|
1909
|
+
isNametagAvailable(nametag: string): Promise<boolean>;
|
|
1910
|
+
/**
|
|
1911
|
+
* Sync with all token storage providers (IPFS, MongoDB, etc.)
|
|
1912
|
+
* Syncs with each provider and merges results
|
|
1913
|
+
*/
|
|
1914
|
+
sync(): Promise<{
|
|
1915
|
+
added: number;
|
|
1916
|
+
removed: number;
|
|
1917
|
+
}>;
|
|
1918
|
+
/**
|
|
1919
|
+
* Get all active token storage providers
|
|
1920
|
+
*/
|
|
1921
|
+
private getTokenStorageProviders;
|
|
1922
|
+
/**
|
|
1923
|
+
* Update token storage providers (called when providers are added/removed dynamically)
|
|
1924
|
+
*/
|
|
1925
|
+
updateTokenStorageProviders(providers: Map<string, TokenStorageProvider<TxfStorageDataBase>>): void;
|
|
1926
|
+
/**
|
|
1927
|
+
* Validate tokens with aggregator
|
|
1928
|
+
*/
|
|
1929
|
+
validate(): Promise<{
|
|
1930
|
+
valid: Token[];
|
|
1931
|
+
invalid: Token[];
|
|
1932
|
+
}>;
|
|
1933
|
+
/**
|
|
1934
|
+
* Get pending transfers
|
|
1935
|
+
*/
|
|
1936
|
+
getPendingTransfers(): TransferResult[];
|
|
1937
|
+
private resolveRecipient;
|
|
1938
|
+
/**
|
|
1939
|
+
* Create SDK TransferCommitment for a token transfer
|
|
1940
|
+
*/
|
|
1941
|
+
private createSdkCommitment;
|
|
1942
|
+
/**
|
|
1943
|
+
* Create SigningService from identity private key
|
|
1944
|
+
*/
|
|
1945
|
+
private createSigningService;
|
|
1946
|
+
/**
|
|
1947
|
+
* Resolve recipient to IAddress
|
|
1948
|
+
*/
|
|
1949
|
+
private resolveRecipientAddress;
|
|
1950
|
+
private handleIncomingTransfer;
|
|
1951
|
+
private archiveToken;
|
|
1952
|
+
private save;
|
|
1953
|
+
private saveToOutbox;
|
|
1954
|
+
private removeFromOutbox;
|
|
1955
|
+
private loadOutbox;
|
|
1956
|
+
private createStorageData;
|
|
1957
|
+
private loadFromStorageData;
|
|
1958
|
+
private ensureInitialized;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
/**
|
|
1962
|
+
* Communications Module
|
|
1963
|
+
* Platform-independent messaging operations
|
|
1964
|
+
*/
|
|
1965
|
+
|
|
1966
|
+
interface CommunicationsModuleConfig {
|
|
1967
|
+
/** Auto-save messages */
|
|
1968
|
+
autoSave?: boolean;
|
|
1969
|
+
/** Max messages in memory */
|
|
1970
|
+
maxMessages?: number;
|
|
1971
|
+
/** Enable read receipts */
|
|
1972
|
+
readReceipts?: boolean;
|
|
1973
|
+
}
|
|
1974
|
+
interface CommunicationsModuleDependencies {
|
|
1975
|
+
identity: FullIdentity;
|
|
1976
|
+
storage: StorageProvider;
|
|
1977
|
+
transport: TransportProvider;
|
|
1978
|
+
emitEvent: <T extends SphereEventType>(type: T, data: SphereEventMap[T]) => void;
|
|
1979
|
+
}
|
|
1980
|
+
declare class CommunicationsModule {
|
|
1981
|
+
private config;
|
|
1982
|
+
private deps;
|
|
1983
|
+
private messages;
|
|
1984
|
+
private broadcasts;
|
|
1985
|
+
private unsubscribeMessages;
|
|
1986
|
+
private broadcastSubscriptions;
|
|
1987
|
+
private dmHandlers;
|
|
1988
|
+
private broadcastHandlers;
|
|
1989
|
+
constructor(config?: CommunicationsModuleConfig);
|
|
1990
|
+
/**
|
|
1991
|
+
* Initialize module with dependencies
|
|
1992
|
+
*/
|
|
1993
|
+
initialize(deps: CommunicationsModuleDependencies): void;
|
|
1994
|
+
/**
|
|
1995
|
+
* Load messages from storage
|
|
1996
|
+
*/
|
|
1997
|
+
load(): Promise<void>;
|
|
1998
|
+
/**
|
|
1999
|
+
* Cleanup resources
|
|
2000
|
+
*/
|
|
2001
|
+
destroy(): void;
|
|
2002
|
+
/**
|
|
2003
|
+
* Send direct message
|
|
2004
|
+
*/
|
|
2005
|
+
sendDM(recipient: string, content: string): Promise<DirectMessage>;
|
|
2006
|
+
/**
|
|
2007
|
+
* Get conversation with peer
|
|
2008
|
+
*/
|
|
2009
|
+
getConversation(peerPubkey: string): DirectMessage[];
|
|
2010
|
+
/**
|
|
2011
|
+
* Get all conversations grouped by peer
|
|
2012
|
+
*/
|
|
2013
|
+
getConversations(): Map<string, DirectMessage[]>;
|
|
2014
|
+
/**
|
|
2015
|
+
* Mark messages as read
|
|
2016
|
+
*/
|
|
2017
|
+
markAsRead(messageIds: string[]): Promise<void>;
|
|
2018
|
+
/**
|
|
2019
|
+
* Get unread count
|
|
2020
|
+
*/
|
|
2021
|
+
getUnreadCount(peerPubkey?: string): number;
|
|
2022
|
+
/**
|
|
2023
|
+
* Subscribe to incoming DMs
|
|
2024
|
+
*/
|
|
2025
|
+
onDirectMessage(handler: (message: DirectMessage) => void): () => void;
|
|
2026
|
+
/**
|
|
2027
|
+
* Publish broadcast message
|
|
2028
|
+
*/
|
|
2029
|
+
broadcast(content: string, tags?: string[]): Promise<BroadcastMessage>;
|
|
2030
|
+
/**
|
|
2031
|
+
* Subscribe to broadcasts with tags
|
|
2032
|
+
*/
|
|
2033
|
+
subscribeToBroadcasts(tags: string[]): () => void;
|
|
2034
|
+
/**
|
|
2035
|
+
* Get broadcasts
|
|
2036
|
+
*/
|
|
2037
|
+
getBroadcasts(limit?: number): BroadcastMessage[];
|
|
2038
|
+
/**
|
|
2039
|
+
* Subscribe to incoming broadcasts
|
|
2040
|
+
*/
|
|
2041
|
+
onBroadcast(handler: (message: BroadcastMessage) => void): () => void;
|
|
2042
|
+
private handleIncomingMessage;
|
|
2043
|
+
private handleIncomingBroadcast;
|
|
2044
|
+
private save;
|
|
2045
|
+
private pruneIfNeeded;
|
|
2046
|
+
private resolveRecipient;
|
|
2047
|
+
private ensureInitialized;
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
/** Network configurations */
|
|
2051
|
+
declare const NETWORKS: {
|
|
2052
|
+
readonly mainnet: {
|
|
2053
|
+
readonly name: "Mainnet";
|
|
2054
|
+
readonly aggregatorUrl: "https://aggregator.unicity.network/rpc";
|
|
2055
|
+
readonly nostrRelays: readonly ["wss://relay.unicity.network", "wss://relay.damus.io", "wss://nos.lol", "wss://relay.nostr.band"];
|
|
2056
|
+
readonly ipfsGateways: readonly ["https://ipfs.unicity.network", "https://dweb.link", "https://ipfs.io"];
|
|
2057
|
+
readonly electrumUrl: "wss://fulcrum.alpha.unicity.network:50004";
|
|
2058
|
+
};
|
|
2059
|
+
readonly testnet: {
|
|
2060
|
+
readonly name: "Testnet";
|
|
2061
|
+
readonly aggregatorUrl: "https://goggregator-test.unicity.network";
|
|
2062
|
+
readonly nostrRelays: readonly ["wss://nostr-relay.testnet.unicity.network"];
|
|
2063
|
+
readonly ipfsGateways: readonly ["https://ipfs.unicity.network", "https://dweb.link", "https://ipfs.io"];
|
|
2064
|
+
readonly electrumUrl: "wss://fulcrum.alpha.testnet.unicity.network:50004";
|
|
2065
|
+
};
|
|
2066
|
+
readonly dev: {
|
|
2067
|
+
readonly name: "Development";
|
|
2068
|
+
readonly aggregatorUrl: "https://dev-aggregator.dyndns.org/rpc";
|
|
2069
|
+
readonly nostrRelays: readonly ["wss://nostr-relay.testnet.unicity.network"];
|
|
2070
|
+
readonly ipfsGateways: readonly ["https://ipfs.unicity.network", "https://dweb.link", "https://ipfs.io"];
|
|
2071
|
+
readonly electrumUrl: "wss://fulcrum.alpha.testnet.unicity.network:50004";
|
|
2072
|
+
};
|
|
2073
|
+
};
|
|
2074
|
+
type NetworkType = keyof typeof NETWORKS;
|
|
2075
|
+
|
|
2076
|
+
/**
|
|
2077
|
+
* Legacy File Serialization Types
|
|
2078
|
+
*/
|
|
2079
|
+
|
|
2080
|
+
type LegacyFileType = 'dat' | 'txt' | 'json' | 'mnemonic' | 'unknown';
|
|
2081
|
+
/**
|
|
2082
|
+
* Progress callback for decryption operations
|
|
2083
|
+
*/
|
|
2084
|
+
type DecryptionProgressCallback = (iteration: number, total: number) => Promise<void> | void;
|
|
2085
|
+
|
|
2086
|
+
/** Options for creating a new wallet */
|
|
2087
|
+
interface SphereCreateOptions {
|
|
2088
|
+
/** BIP39 mnemonic (12 or 24 words) */
|
|
2089
|
+
mnemonic: string;
|
|
2090
|
+
/** Custom derivation path (default: m/44'/0'/0') */
|
|
2091
|
+
derivationPath?: string;
|
|
2092
|
+
/** Optional nametag to register for this wallet (e.g., 'alice' for @alice). Token is auto-minted. */
|
|
2093
|
+
nametag?: string;
|
|
2094
|
+
/** Storage provider instance */
|
|
2095
|
+
storage: StorageProvider;
|
|
2096
|
+
/** Optional token storage provider (for IPFS sync) */
|
|
2097
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2098
|
+
/** Transport provider instance */
|
|
2099
|
+
transport: TransportProvider;
|
|
2100
|
+
/** Oracle provider instance */
|
|
2101
|
+
oracle: OracleProvider;
|
|
2102
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2103
|
+
l1?: L1Config$1;
|
|
2104
|
+
/**
|
|
2105
|
+
* Network type (mainnet, testnet, dev) - informational only.
|
|
2106
|
+
* Actual network configuration comes from provider URLs.
|
|
2107
|
+
* Use createBrowserProviders({ network: 'testnet' }) to set up testnet providers.
|
|
2108
|
+
*/
|
|
2109
|
+
network?: NetworkType;
|
|
2110
|
+
}
|
|
2111
|
+
/** Options for loading existing wallet */
|
|
2112
|
+
interface SphereLoadOptions {
|
|
2113
|
+
/** Storage provider instance */
|
|
2114
|
+
storage: StorageProvider;
|
|
2115
|
+
/** Optional token storage provider (for IPFS sync) */
|
|
2116
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2117
|
+
/** Transport provider instance */
|
|
2118
|
+
transport: TransportProvider;
|
|
2119
|
+
/** Oracle provider instance */
|
|
2120
|
+
oracle: OracleProvider;
|
|
2121
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2122
|
+
l1?: L1Config$1;
|
|
2123
|
+
/**
|
|
2124
|
+
* Network type (mainnet, testnet, dev) - informational only.
|
|
2125
|
+
* Actual network configuration comes from provider URLs.
|
|
2126
|
+
* Use createBrowserProviders({ network: 'testnet' }) to set up testnet providers.
|
|
2127
|
+
*/
|
|
2128
|
+
network?: NetworkType;
|
|
2129
|
+
}
|
|
2130
|
+
/** Options for importing a wallet */
|
|
2131
|
+
interface SphereImportOptions {
|
|
2132
|
+
/** BIP39 mnemonic to import */
|
|
2133
|
+
mnemonic?: string;
|
|
2134
|
+
/** Or master private key (hex) */
|
|
2135
|
+
masterKey?: string;
|
|
2136
|
+
/** Chain code for BIP32 (optional) */
|
|
2137
|
+
chainCode?: string;
|
|
2138
|
+
/** Custom derivation path */
|
|
2139
|
+
derivationPath?: string;
|
|
2140
|
+
/** Base path for BIP32 derivation (e.g., "m/84'/1'/0'" from wallet.dat) */
|
|
2141
|
+
basePath?: string;
|
|
2142
|
+
/** Derivation mode: bip32, wif_hmac, legacy_hmac */
|
|
2143
|
+
derivationMode?: DerivationMode;
|
|
2144
|
+
/** Optional nametag to register for this wallet (e.g., 'alice' for @alice). Token is auto-minted. */
|
|
2145
|
+
nametag?: string;
|
|
2146
|
+
/** Storage provider instance */
|
|
2147
|
+
storage: StorageProvider;
|
|
2148
|
+
/** Optional token storage provider */
|
|
2149
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2150
|
+
/** Transport provider instance */
|
|
2151
|
+
transport: TransportProvider;
|
|
2152
|
+
/** Oracle provider instance */
|
|
2153
|
+
oracle: OracleProvider;
|
|
2154
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2155
|
+
l1?: L1Config$1;
|
|
2156
|
+
}
|
|
2157
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2158
|
+
interface L1Config$1 {
|
|
2159
|
+
/** Fulcrum WebSocket URL (default: wss://fulcrum.alpha.unicity.network:50004) */
|
|
2160
|
+
electrumUrl?: string;
|
|
2161
|
+
/** Default fee rate in sat/byte (default: 10) */
|
|
2162
|
+
defaultFeeRate?: number;
|
|
2163
|
+
/** Enable vesting classification (default: true) */
|
|
2164
|
+
enableVesting?: boolean;
|
|
2165
|
+
}
|
|
2166
|
+
/** Options for unified init (auto-create or load) */
|
|
2167
|
+
interface SphereInitOptions {
|
|
2168
|
+
/** Storage provider instance */
|
|
2169
|
+
storage: StorageProvider;
|
|
2170
|
+
/** Transport provider instance */
|
|
2171
|
+
transport: TransportProvider;
|
|
2172
|
+
/** Oracle provider instance */
|
|
2173
|
+
oracle: OracleProvider;
|
|
2174
|
+
/** Optional token storage provider (for IPFS sync) */
|
|
2175
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2176
|
+
/** BIP39 mnemonic - if wallet doesn't exist, use this to create */
|
|
2177
|
+
mnemonic?: string;
|
|
2178
|
+
/** Auto-generate mnemonic if wallet doesn't exist and no mnemonic provided */
|
|
2179
|
+
autoGenerate?: boolean;
|
|
2180
|
+
/** Custom derivation path (default: m/44'/0'/0') */
|
|
2181
|
+
derivationPath?: string;
|
|
2182
|
+
/** Optional nametag to register (only on create). Token is auto-minted. */
|
|
2183
|
+
nametag?: string;
|
|
2184
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2185
|
+
l1?: L1Config$1;
|
|
2186
|
+
/**
|
|
2187
|
+
* Network type (mainnet, testnet, dev) - informational only.
|
|
2188
|
+
* Actual network configuration comes from provider URLs.
|
|
2189
|
+
* Use createBrowserProviders({ network: 'testnet' }) to set up testnet providers.
|
|
2190
|
+
*/
|
|
2191
|
+
network?: NetworkType;
|
|
2192
|
+
}
|
|
2193
|
+
/** Result of init operation */
|
|
2194
|
+
interface SphereInitResult {
|
|
2195
|
+
/** The initialized Sphere instance */
|
|
2196
|
+
sphere: Sphere;
|
|
2197
|
+
/** Whether wallet was newly created */
|
|
2198
|
+
created: boolean;
|
|
2199
|
+
/** Generated mnemonic (only if autoGenerate was used) */
|
|
2200
|
+
generatedMnemonic?: string;
|
|
2201
|
+
}
|
|
2202
|
+
declare class Sphere {
|
|
2203
|
+
private static instance;
|
|
2204
|
+
private _initialized;
|
|
2205
|
+
private _identity;
|
|
2206
|
+
private _masterKey;
|
|
2207
|
+
private _mnemonic;
|
|
2208
|
+
private _source;
|
|
2209
|
+
private _derivationMode;
|
|
2210
|
+
private _basePath;
|
|
2211
|
+
private _currentAddressIndex;
|
|
2212
|
+
private _addressNametags;
|
|
2213
|
+
private _storage;
|
|
2214
|
+
private _tokenStorageProviders;
|
|
2215
|
+
private _transport;
|
|
2216
|
+
private _oracle;
|
|
2217
|
+
private _payments;
|
|
2218
|
+
private _communications;
|
|
2219
|
+
private eventHandlers;
|
|
2220
|
+
private constructor();
|
|
2221
|
+
/**
|
|
2222
|
+
* Check if wallet exists in storage
|
|
2223
|
+
*/
|
|
2224
|
+
static exists(storage: StorageProvider): Promise<boolean>;
|
|
2225
|
+
/**
|
|
2226
|
+
* Initialize wallet - auto-loads existing or creates new
|
|
2227
|
+
*
|
|
2228
|
+
* @example
|
|
2229
|
+
* ```ts
|
|
2230
|
+
* // Load existing or create with provided mnemonic
|
|
2231
|
+
* const { sphere, created } = await Sphere.init({
|
|
2232
|
+
* storage,
|
|
2233
|
+
* transport,
|
|
2234
|
+
* oracle,
|
|
2235
|
+
* mnemonic: 'your twelve words...',
|
|
2236
|
+
* });
|
|
2237
|
+
*
|
|
2238
|
+
* // Load existing or auto-generate new mnemonic
|
|
2239
|
+
* const { sphere, created, generatedMnemonic } = await Sphere.init({
|
|
2240
|
+
* storage,
|
|
2241
|
+
* transport,
|
|
2242
|
+
* oracle,
|
|
2243
|
+
* autoGenerate: true,
|
|
2244
|
+
* });
|
|
2245
|
+
* if (generatedMnemonic) {
|
|
2246
|
+
* console.log('Save this mnemonic:', generatedMnemonic);
|
|
2247
|
+
* }
|
|
2248
|
+
* ```
|
|
2249
|
+
*/
|
|
2250
|
+
static init(options: SphereInitOptions): Promise<SphereInitResult>;
|
|
2251
|
+
/**
|
|
2252
|
+
* Create new wallet with mnemonic
|
|
2253
|
+
*/
|
|
2254
|
+
static create(options: SphereCreateOptions): Promise<Sphere>;
|
|
2255
|
+
/**
|
|
2256
|
+
* Load existing wallet from storage
|
|
2257
|
+
*/
|
|
2258
|
+
static load(options: SphereLoadOptions): Promise<Sphere>;
|
|
2259
|
+
/**
|
|
2260
|
+
* Import wallet from mnemonic or master key
|
|
2261
|
+
*/
|
|
2262
|
+
static import(options: SphereImportOptions): Promise<Sphere>;
|
|
2263
|
+
/**
|
|
2264
|
+
* Clear wallet data from storage
|
|
2265
|
+
*/
|
|
2266
|
+
static clear(storage: StorageProvider): Promise<void>;
|
|
2267
|
+
/**
|
|
2268
|
+
* Get current instance
|
|
2269
|
+
*/
|
|
2270
|
+
static getInstance(): Sphere | null;
|
|
2271
|
+
/**
|
|
2272
|
+
* Check if initialized
|
|
2273
|
+
*/
|
|
2274
|
+
static isInitialized(): boolean;
|
|
2275
|
+
/**
|
|
2276
|
+
* Validate mnemonic using BIP39
|
|
2277
|
+
*/
|
|
2278
|
+
static validateMnemonic(mnemonic: string): boolean;
|
|
2279
|
+
/**
|
|
2280
|
+
* Generate new BIP39 mnemonic
|
|
2281
|
+
* @param strength - 128 for 12 words, 256 for 24 words
|
|
2282
|
+
*/
|
|
2283
|
+
static generateMnemonic(strength?: 128 | 256): string;
|
|
2284
|
+
/** Payments module (L3 + L1) */
|
|
2285
|
+
get payments(): PaymentsModule;
|
|
2286
|
+
/** Communications module */
|
|
2287
|
+
get communications(): CommunicationsModule;
|
|
2288
|
+
/** Current identity (public info only) */
|
|
2289
|
+
get identity(): Identity | null;
|
|
2290
|
+
/** Is ready */
|
|
2291
|
+
get isReady(): boolean;
|
|
2292
|
+
getStorage(): StorageProvider;
|
|
2293
|
+
/**
|
|
2294
|
+
* Get first token storage provider (for backward compatibility)
|
|
2295
|
+
* @deprecated Use getTokenStorageProviders() for multiple providers
|
|
2296
|
+
*/
|
|
2297
|
+
getTokenStorage(): TokenStorageProvider<TxfStorageDataBase> | undefined;
|
|
2298
|
+
/**
|
|
2299
|
+
* Get all token storage providers
|
|
2300
|
+
*/
|
|
2301
|
+
getTokenStorageProviders(): Map<string, TokenStorageProvider<TxfStorageDataBase>>;
|
|
2302
|
+
/**
|
|
2303
|
+
* Add a token storage provider dynamically (e.g., from UI)
|
|
2304
|
+
* Provider will be initialized and connected automatically
|
|
2305
|
+
*/
|
|
2306
|
+
addTokenStorageProvider(provider: TokenStorageProvider<TxfStorageDataBase>): Promise<void>;
|
|
2307
|
+
/**
|
|
2308
|
+
* Remove a token storage provider dynamically
|
|
2309
|
+
*/
|
|
2310
|
+
removeTokenStorageProvider(providerId: string): Promise<boolean>;
|
|
2311
|
+
/**
|
|
2312
|
+
* Check if a token storage provider is registered
|
|
2313
|
+
*/
|
|
2314
|
+
hasTokenStorageProvider(providerId: string): boolean;
|
|
2315
|
+
getTransport(): TransportProvider;
|
|
2316
|
+
getAggregator(): OracleProvider;
|
|
2317
|
+
/**
|
|
2318
|
+
* Check if wallet has BIP32 master key for HD derivation
|
|
2319
|
+
*/
|
|
2320
|
+
hasMasterKey(): boolean;
|
|
2321
|
+
/**
|
|
2322
|
+
* Get the base derivation path used by this wallet (e.g., "m/44'/0'/0'")
|
|
2323
|
+
*/
|
|
2324
|
+
getBasePath(): string;
|
|
2325
|
+
/**
|
|
2326
|
+
* Get the default address path (first external address)
|
|
2327
|
+
* Returns path like "m/44'/0'/0'/0/0"
|
|
2328
|
+
*/
|
|
2329
|
+
getDefaultAddressPath(): string;
|
|
2330
|
+
/**
|
|
2331
|
+
* Get current derivation mode
|
|
2332
|
+
*/
|
|
2333
|
+
getDerivationMode(): DerivationMode;
|
|
2334
|
+
/**
|
|
2335
|
+
* Get the mnemonic phrase (for backup purposes)
|
|
2336
|
+
* Returns null if wallet was imported from file (masterKey only)
|
|
2337
|
+
*/
|
|
2338
|
+
getMnemonic(): string | null;
|
|
2339
|
+
/**
|
|
2340
|
+
* Get wallet info for backup/export purposes
|
|
2341
|
+
*/
|
|
2342
|
+
getWalletInfo(): WalletInfo;
|
|
2343
|
+
/**
|
|
2344
|
+
* Export wallet to JSON format for backup
|
|
2345
|
+
*
|
|
2346
|
+
* @example
|
|
2347
|
+
* ```ts
|
|
2348
|
+
* // Export with mnemonic (if available)
|
|
2349
|
+
* const json = sphere.exportToJSON();
|
|
2350
|
+
*
|
|
2351
|
+
* // Export with encryption
|
|
2352
|
+
* const encrypted = sphere.exportToJSON({ password: 'secret' });
|
|
2353
|
+
*
|
|
2354
|
+
* // Export multiple addresses
|
|
2355
|
+
* const multi = sphere.exportToJSON({ addressCount: 5 });
|
|
2356
|
+
* ```
|
|
2357
|
+
*/
|
|
2358
|
+
exportToJSON(options?: WalletJSONExportOptions): WalletJSON;
|
|
2359
|
+
/**
|
|
2360
|
+
* Export wallet to text format for backup
|
|
2361
|
+
*
|
|
2362
|
+
* @example
|
|
2363
|
+
* ```ts
|
|
2364
|
+
* // Export unencrypted
|
|
2365
|
+
* const text = sphere.exportToTxt();
|
|
2366
|
+
*
|
|
2367
|
+
* // Export with encryption
|
|
2368
|
+
* const encrypted = sphere.exportToTxt({ password: 'secret' });
|
|
2369
|
+
*
|
|
2370
|
+
* // Export multiple addresses
|
|
2371
|
+
* const multi = sphere.exportToTxt({ addressCount: 5 });
|
|
2372
|
+
* ```
|
|
2373
|
+
*/
|
|
2374
|
+
exportToTxt(options?: {
|
|
2375
|
+
password?: string;
|
|
2376
|
+
addressCount?: number;
|
|
2377
|
+
}): string;
|
|
2378
|
+
/**
|
|
2379
|
+
* Import wallet from JSON backup
|
|
2380
|
+
*
|
|
2381
|
+
* @returns Object with success status and optionally recovered mnemonic
|
|
2382
|
+
*
|
|
2383
|
+
* @example
|
|
2384
|
+
* ```ts
|
|
2385
|
+
* const json = '{"version":"1.0",...}';
|
|
2386
|
+
* const { success, mnemonic } = await Sphere.importFromJSON({
|
|
2387
|
+
* jsonContent: json,
|
|
2388
|
+
* password: 'secret', // if encrypted
|
|
2389
|
+
* storage, transport, oracle,
|
|
2390
|
+
* });
|
|
2391
|
+
* ```
|
|
2392
|
+
*/
|
|
2393
|
+
static importFromJSON(options: {
|
|
2394
|
+
jsonContent: string;
|
|
2395
|
+
password?: string;
|
|
2396
|
+
storage: StorageProvider;
|
|
2397
|
+
transport: TransportProvider;
|
|
2398
|
+
oracle: OracleProvider;
|
|
2399
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2400
|
+
}): Promise<{
|
|
2401
|
+
success: boolean;
|
|
2402
|
+
mnemonic?: string;
|
|
2403
|
+
error?: string;
|
|
2404
|
+
}>;
|
|
2405
|
+
/**
|
|
2406
|
+
* Import wallet from legacy file (.dat, .txt, or mnemonic text)
|
|
2407
|
+
*
|
|
2408
|
+
* Supports:
|
|
2409
|
+
* - Bitcoin Core wallet.dat files (SQLite format, encrypted or unencrypted)
|
|
2410
|
+
* - Text backup files (UNICITY WALLET DETAILS format)
|
|
2411
|
+
* - Plain mnemonic text (12 or 24 words)
|
|
2412
|
+
*
|
|
2413
|
+
* @returns Object with success status, created Sphere instance, and optionally recovered mnemonic
|
|
2414
|
+
*
|
|
2415
|
+
* @example
|
|
2416
|
+
* ```ts
|
|
2417
|
+
* // Import from .dat file
|
|
2418
|
+
* const fileBuffer = await file.arrayBuffer();
|
|
2419
|
+
* const result = await Sphere.importFromLegacyFile({
|
|
2420
|
+
* fileContent: new Uint8Array(fileBuffer),
|
|
2421
|
+
* fileName: 'wallet.dat',
|
|
2422
|
+
* password: 'wallet-password', // if encrypted
|
|
2423
|
+
* storage, transport, oracle,
|
|
2424
|
+
* });
|
|
2425
|
+
*
|
|
2426
|
+
* // Import from .txt file
|
|
2427
|
+
* const textContent = await file.text();
|
|
2428
|
+
* const result = await Sphere.importFromLegacyFile({
|
|
2429
|
+
* fileContent: textContent,
|
|
2430
|
+
* fileName: 'backup.txt',
|
|
2431
|
+
* storage, transport, oracle,
|
|
2432
|
+
* });
|
|
2433
|
+
* ```
|
|
2434
|
+
*/
|
|
2435
|
+
static importFromLegacyFile(options: {
|
|
2436
|
+
/** File content - Uint8Array for .dat, string for .txt */
|
|
2437
|
+
fileContent: string | Uint8Array;
|
|
2438
|
+
/** File name (used for type detection) */
|
|
2439
|
+
fileName: string;
|
|
2440
|
+
/** Password for encrypted files */
|
|
2441
|
+
password?: string;
|
|
2442
|
+
/** Progress callback for long decryption operations */
|
|
2443
|
+
onDecryptProgress?: DecryptionProgressCallback;
|
|
2444
|
+
/** Storage provider instance */
|
|
2445
|
+
storage: StorageProvider;
|
|
2446
|
+
/** Transport provider instance */
|
|
2447
|
+
transport: TransportProvider;
|
|
2448
|
+
/** Oracle provider instance */
|
|
2449
|
+
oracle: OracleProvider;
|
|
2450
|
+
/** Optional token storage provider */
|
|
2451
|
+
tokenStorage?: TokenStorageProvider<TxfStorageDataBase>;
|
|
2452
|
+
/** Optional nametag to register */
|
|
2453
|
+
nametag?: string;
|
|
2454
|
+
}): Promise<{
|
|
2455
|
+
success: boolean;
|
|
2456
|
+
sphere?: Sphere;
|
|
2457
|
+
mnemonic?: string;
|
|
2458
|
+
needsPassword?: boolean;
|
|
2459
|
+
error?: string;
|
|
2460
|
+
}>;
|
|
2461
|
+
/**
|
|
2462
|
+
* Detect legacy file type from filename and content
|
|
2463
|
+
*/
|
|
2464
|
+
static detectLegacyFileType(fileName: string, content: string | Uint8Array): LegacyFileType;
|
|
2465
|
+
/**
|
|
2466
|
+
* Check if a legacy file is encrypted
|
|
2467
|
+
*/
|
|
2468
|
+
static isLegacyFileEncrypted(fileName: string, content: string | Uint8Array): boolean;
|
|
2469
|
+
/**
|
|
2470
|
+
* Get the current active address index
|
|
2471
|
+
*
|
|
2472
|
+
* @example
|
|
2473
|
+
* ```ts
|
|
2474
|
+
* const currentIndex = sphere.getCurrentAddressIndex();
|
|
2475
|
+
* console.log(currentIndex); // 0
|
|
2476
|
+
*
|
|
2477
|
+
* await sphere.switchToAddress(2);
|
|
2478
|
+
* console.log(sphere.getCurrentAddressIndex()); // 2
|
|
2479
|
+
* ```
|
|
2480
|
+
*/
|
|
2481
|
+
getCurrentAddressIndex(): number;
|
|
2482
|
+
/**
|
|
2483
|
+
* Get nametag for a specific address index
|
|
2484
|
+
*
|
|
2485
|
+
* @param index - Address index (default: current address)
|
|
2486
|
+
* @returns Nametag or undefined if not registered
|
|
2487
|
+
*/
|
|
2488
|
+
getNametagForAddress(index?: number): string | undefined;
|
|
2489
|
+
/**
|
|
2490
|
+
* Get all registered address nametags
|
|
2491
|
+
*
|
|
2492
|
+
* @returns Map of address index to nametag
|
|
2493
|
+
*/
|
|
2494
|
+
getAllAddressNametags(): Map<number, string>;
|
|
2495
|
+
/**
|
|
2496
|
+
* Switch to a different address by index
|
|
2497
|
+
* This changes the active identity to the derived address at the specified index.
|
|
2498
|
+
*
|
|
2499
|
+
* @param index - Address index to switch to (0, 1, 2, ...)
|
|
2500
|
+
*
|
|
2501
|
+
* @example
|
|
2502
|
+
* ```ts
|
|
2503
|
+
* // Switch to second address
|
|
2504
|
+
* await sphere.switchToAddress(1);
|
|
2505
|
+
* console.log(sphere.identity?.address); // alpha1... (address at index 1)
|
|
2506
|
+
*
|
|
2507
|
+
* // Register nametag for this address
|
|
2508
|
+
* await sphere.registerNametag('bob');
|
|
2509
|
+
*
|
|
2510
|
+
* // Switch back to first address
|
|
2511
|
+
* await sphere.switchToAddress(0);
|
|
2512
|
+
* ```
|
|
2513
|
+
*/
|
|
2514
|
+
switchToAddress(index: number): Promise<void>;
|
|
2515
|
+
/**
|
|
2516
|
+
* Re-initialize modules after address switch
|
|
2517
|
+
*/
|
|
2518
|
+
private reinitializeModulesForNewAddress;
|
|
2519
|
+
/**
|
|
2520
|
+
* Derive address at a specific index
|
|
2521
|
+
*
|
|
2522
|
+
* @param index - Address index (0, 1, 2, ...)
|
|
2523
|
+
* @param isChange - Whether this is a change address (default: false)
|
|
2524
|
+
* @returns Address info with privateKey, publicKey, address, path, index
|
|
2525
|
+
*
|
|
2526
|
+
* @example
|
|
2527
|
+
* ```ts
|
|
2528
|
+
* // Derive first receiving address
|
|
2529
|
+
* const addr0 = sphere.deriveAddress(0);
|
|
2530
|
+
* console.log(addr0.address); // alpha1...
|
|
2531
|
+
*
|
|
2532
|
+
* // Derive second receiving address
|
|
2533
|
+
* const addr1 = sphere.deriveAddress(1);
|
|
2534
|
+
*
|
|
2535
|
+
* // Derive change address
|
|
2536
|
+
* const change = sphere.deriveAddress(0, true);
|
|
2537
|
+
* ```
|
|
2538
|
+
*/
|
|
2539
|
+
deriveAddress(index: number, isChange?: boolean): AddressInfo;
|
|
2540
|
+
/**
|
|
2541
|
+
* Derive address at a full BIP32 path
|
|
2542
|
+
*
|
|
2543
|
+
* @param path - Full BIP32 path like "m/44'/0'/0'/0/5"
|
|
2544
|
+
* @returns Address info
|
|
2545
|
+
*
|
|
2546
|
+
* @example
|
|
2547
|
+
* ```ts
|
|
2548
|
+
* const addr = sphere.deriveAddressAtPath("m/44'/0'/0'/0/5");
|
|
2549
|
+
* ```
|
|
2550
|
+
*/
|
|
2551
|
+
deriveAddressAtPath(path: string): AddressInfo;
|
|
2552
|
+
/**
|
|
2553
|
+
* Derive multiple addresses starting from index 0
|
|
2554
|
+
*
|
|
2555
|
+
* @param count - Number of addresses to derive
|
|
2556
|
+
* @param includeChange - Include change addresses (default: false)
|
|
2557
|
+
* @returns Array of address info
|
|
2558
|
+
*
|
|
2559
|
+
* @example
|
|
2560
|
+
* ```ts
|
|
2561
|
+
* // Get first 5 receiving addresses
|
|
2562
|
+
* const addresses = sphere.deriveAddresses(5);
|
|
2563
|
+
*
|
|
2564
|
+
* // Get 5 receiving + 5 change addresses
|
|
2565
|
+
* const allAddresses = sphere.deriveAddresses(5, true);
|
|
2566
|
+
* ```
|
|
2567
|
+
*/
|
|
2568
|
+
deriveAddresses(count: number, includeChange?: boolean): AddressInfo[];
|
|
2569
|
+
getStatus(): {
|
|
2570
|
+
storage: {
|
|
2571
|
+
connected: boolean;
|
|
2572
|
+
};
|
|
2573
|
+
transport: {
|
|
2574
|
+
connected: boolean;
|
|
2575
|
+
};
|
|
2576
|
+
oracle: {
|
|
2577
|
+
connected: boolean;
|
|
2578
|
+
};
|
|
2579
|
+
};
|
|
2580
|
+
reconnect(): Promise<void>;
|
|
2581
|
+
on<T extends SphereEventType>(type: T, handler: SphereEventHandler<T>): () => void;
|
|
2582
|
+
off<T extends SphereEventType>(type: T, handler: SphereEventHandler<T>): void;
|
|
2583
|
+
sync(): Promise<void>;
|
|
2584
|
+
/**
|
|
2585
|
+
* Get current nametag (if registered)
|
|
2586
|
+
*/
|
|
2587
|
+
getNametag(): string | undefined;
|
|
2588
|
+
/**
|
|
2589
|
+
* Check if nametag is registered
|
|
2590
|
+
*/
|
|
2591
|
+
hasNametag(): boolean;
|
|
2592
|
+
/**
|
|
2593
|
+
* Register a nametag for the current active address
|
|
2594
|
+
* Each address can have its own independent nametag
|
|
2595
|
+
*
|
|
2596
|
+
* @example
|
|
2597
|
+
* ```ts
|
|
2598
|
+
* // Register nametag for first address (index 0)
|
|
2599
|
+
* await sphere.registerNametag('alice');
|
|
2600
|
+
*
|
|
2601
|
+
* // Switch to second address and register different nametag
|
|
2602
|
+
* await sphere.switchToAddress(1);
|
|
2603
|
+
* await sphere.registerNametag('bob');
|
|
2604
|
+
*
|
|
2605
|
+
* // Now:
|
|
2606
|
+
* // - Address 0 has nametag @alice
|
|
2607
|
+
* // - Address 1 has nametag @bob
|
|
2608
|
+
* ```
|
|
2609
|
+
*/
|
|
2610
|
+
registerNametag(nametag: string): Promise<void>;
|
|
2611
|
+
/**
|
|
2612
|
+
* Persist address nametags to storage
|
|
2613
|
+
*/
|
|
2614
|
+
private persistAddressNametags;
|
|
2615
|
+
/**
|
|
2616
|
+
* Mint a nametag token on-chain (like Sphere wallet and lottery)
|
|
2617
|
+
* This creates the nametag token required for receiving tokens via PROXY addresses (@nametag)
|
|
2618
|
+
*
|
|
2619
|
+
* @param nametag - The nametag to mint (e.g., "alice" or "@alice")
|
|
2620
|
+
* @returns MintNametagResult with success status and token if successful
|
|
2621
|
+
*
|
|
2622
|
+
* @example
|
|
2623
|
+
* ```typescript
|
|
2624
|
+
* // Mint nametag token for receiving via @alice
|
|
2625
|
+
* const result = await sphere.mintNametag('alice');
|
|
2626
|
+
* if (result.success) {
|
|
2627
|
+
* console.log('Nametag minted:', result.nametagData?.name);
|
|
2628
|
+
* } else {
|
|
2629
|
+
* console.error('Mint failed:', result.error);
|
|
2630
|
+
* }
|
|
2631
|
+
* ```
|
|
2632
|
+
*/
|
|
2633
|
+
mintNametag(nametag: string): Promise<MintNametagResult>;
|
|
2634
|
+
/**
|
|
2635
|
+
* Check if a nametag is available for minting
|
|
2636
|
+
* @param nametag - The nametag to check (e.g., "alice" or "@alice")
|
|
2637
|
+
* @returns true if available, false if taken or error
|
|
2638
|
+
*/
|
|
2639
|
+
isNametagAvailable(nametag: string): Promise<boolean>;
|
|
2640
|
+
/**
|
|
2641
|
+
* Load address nametags from storage
|
|
2642
|
+
*/
|
|
2643
|
+
private loadAddressNametags;
|
|
2644
|
+
/**
|
|
2645
|
+
* Sync nametag with Nostr on wallet load
|
|
2646
|
+
* If local nametag exists but not registered on Nostr, re-register it
|
|
2647
|
+
*/
|
|
2648
|
+
private syncNametagWithNostr;
|
|
2649
|
+
/**
|
|
2650
|
+
* Validate nametag format
|
|
2651
|
+
*/
|
|
2652
|
+
private validateNametag;
|
|
2653
|
+
destroy(): Promise<void>;
|
|
2654
|
+
private storeMnemonic;
|
|
2655
|
+
private storeMasterKey;
|
|
2656
|
+
/**
|
|
2657
|
+
* Mark wallet as fully created (after successful initialization)
|
|
2658
|
+
* This is called at the end of create()/import() to ensure wallet is only
|
|
2659
|
+
* marked as existing after all initialization steps succeed.
|
|
2660
|
+
*/
|
|
2661
|
+
private finalizeWalletCreation;
|
|
2662
|
+
private loadIdentityFromStorage;
|
|
2663
|
+
private initializeIdentityFromMnemonic;
|
|
2664
|
+
private initializeIdentityFromMasterKey;
|
|
2665
|
+
private initializeProviders;
|
|
2666
|
+
private initializeModules;
|
|
2667
|
+
private ensureReady;
|
|
2668
|
+
private emitEvent;
|
|
2669
|
+
private encrypt;
|
|
2670
|
+
private decrypt;
|
|
2671
|
+
}
|
|
2672
|
+
|
|
2673
|
+
/**
|
|
2674
|
+
* Browser Download Utilities
|
|
2675
|
+
* Functions for downloading wallet backups as files
|
|
2676
|
+
*/
|
|
2677
|
+
|
|
2678
|
+
interface DownloadTextOptions {
|
|
2679
|
+
/** Password for encryption */
|
|
2680
|
+
password?: string;
|
|
2681
|
+
/** Number of addresses to include */
|
|
2682
|
+
addressCount?: number;
|
|
2683
|
+
/** Custom filename (without extension) */
|
|
2684
|
+
filename?: string;
|
|
2685
|
+
}
|
|
2686
|
+
interface DownloadJSONOptions extends WalletJSONExportOptions {
|
|
2687
|
+
/** Custom filename (without extension) */
|
|
2688
|
+
filename?: string;
|
|
2689
|
+
/** Pretty print JSON (default: true) */
|
|
2690
|
+
pretty?: boolean;
|
|
2691
|
+
}
|
|
2692
|
+
/**
|
|
2693
|
+
* Download content as a file in the browser
|
|
2694
|
+
*/
|
|
2695
|
+
declare function downloadFile(content: string | Blob, filename: string, mimeType?: string): void;
|
|
2696
|
+
/**
|
|
2697
|
+
* Download text content as a .txt file
|
|
2698
|
+
*/
|
|
2699
|
+
declare function downloadTextFile(content: string, filename: string): void;
|
|
2700
|
+
/**
|
|
2701
|
+
* Download JSON content as a .json file
|
|
2702
|
+
*/
|
|
2703
|
+
declare function downloadJSONFile(content: object | string, filename: string): void;
|
|
2704
|
+
/**
|
|
2705
|
+
* Download wallet backup as text file
|
|
2706
|
+
*
|
|
2707
|
+
* @example
|
|
2708
|
+
* ```ts
|
|
2709
|
+
* // Download unencrypted backup
|
|
2710
|
+
* downloadWalletText(sphere);
|
|
2711
|
+
*
|
|
2712
|
+
* // Download encrypted backup
|
|
2713
|
+
* downloadWalletText(sphere, { password: 'secret' });
|
|
2714
|
+
*
|
|
2715
|
+
* // Custom filename
|
|
2716
|
+
* downloadWalletText(sphere, { filename: 'my-backup' });
|
|
2717
|
+
* ```
|
|
2718
|
+
*/
|
|
2719
|
+
declare function downloadWalletText(sphere: Sphere, options?: DownloadTextOptions): void;
|
|
2720
|
+
/**
|
|
2721
|
+
* Download wallet backup as JSON file
|
|
2722
|
+
*
|
|
2723
|
+
* @example
|
|
2724
|
+
* ```ts
|
|
2725
|
+
* // Download unencrypted backup
|
|
2726
|
+
* downloadWalletJSON(sphere);
|
|
2727
|
+
*
|
|
2728
|
+
* // Download encrypted backup
|
|
2729
|
+
* downloadWalletJSON(sphere, { password: 'secret' });
|
|
2730
|
+
*
|
|
2731
|
+
* // Include multiple addresses
|
|
2732
|
+
* downloadWalletJSON(sphere, { addressCount: 5 });
|
|
2733
|
+
* ```
|
|
2734
|
+
*/
|
|
2735
|
+
declare function downloadWalletJSON(sphere: Sphere, options?: DownloadJSONOptions): void;
|
|
2736
|
+
/**
|
|
2737
|
+
* Download pre-built WalletJSON as file
|
|
2738
|
+
*/
|
|
2739
|
+
declare function downloadWalletJSONData(json: WalletJSON, filename?: string): void;
|
|
2740
|
+
/**
|
|
2741
|
+
* Read a file as text
|
|
2742
|
+
*/
|
|
2743
|
+
declare function readFileAsText(file: File): Promise<string>;
|
|
2744
|
+
/**
|
|
2745
|
+
* Read a file as ArrayBuffer (for binary files like .dat)
|
|
2746
|
+
*/
|
|
2747
|
+
declare function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer>;
|
|
2748
|
+
/**
|
|
2749
|
+
* Read a file as Uint8Array (for binary files like .dat)
|
|
2750
|
+
*/
|
|
2751
|
+
declare function readFileAsUint8Array(file: File): Promise<Uint8Array>;
|
|
2752
|
+
|
|
2753
|
+
/**
|
|
2754
|
+
* Shared Configuration Interfaces
|
|
2755
|
+
* Base types extended by platform-specific implementations
|
|
2756
|
+
*/
|
|
2757
|
+
|
|
2758
|
+
/**
|
|
2759
|
+
* Base transport (Nostr) configuration
|
|
2760
|
+
* Supports extend/override pattern for relays
|
|
2761
|
+
*/
|
|
2762
|
+
interface BaseTransportConfig {
|
|
2763
|
+
/** Replace default relays entirely */
|
|
2764
|
+
relays?: string[];
|
|
2765
|
+
/** Add relays to network defaults (use with network preset) */
|
|
2766
|
+
additionalRelays?: string[];
|
|
2767
|
+
/** Connection timeout (ms) */
|
|
2768
|
+
timeout?: number;
|
|
2769
|
+
/** Auto-reconnect on disconnect */
|
|
2770
|
+
autoReconnect?: boolean;
|
|
2771
|
+
/** Enable debug logging */
|
|
2772
|
+
debug?: boolean;
|
|
2773
|
+
}
|
|
2774
|
+
/**
|
|
2775
|
+
* Browser-specific transport extensions
|
|
2776
|
+
*/
|
|
2777
|
+
interface BrowserTransportExtensions {
|
|
2778
|
+
/** Reconnect delay (ms) */
|
|
2779
|
+
reconnectDelay?: number;
|
|
2780
|
+
/** Max reconnect attempts */
|
|
2781
|
+
maxReconnectAttempts?: number;
|
|
2782
|
+
}
|
|
2783
|
+
/**
|
|
2784
|
+
* Base oracle (Aggregator) configuration
|
|
2785
|
+
*/
|
|
2786
|
+
interface BaseOracleConfig {
|
|
2787
|
+
/** Replace default aggregator URL (if not set, uses network default) */
|
|
2788
|
+
url?: string;
|
|
2789
|
+
/** API key for authentication */
|
|
2790
|
+
apiKey?: string;
|
|
2791
|
+
/** Request timeout (ms) */
|
|
2792
|
+
timeout?: number;
|
|
2793
|
+
/** Skip trust base verification (dev only) */
|
|
2794
|
+
skipVerification?: boolean;
|
|
2795
|
+
/** Enable debug logging */
|
|
2796
|
+
debug?: boolean;
|
|
2797
|
+
}
|
|
2798
|
+
/**
|
|
2799
|
+
* L1 (ALPHA blockchain) configuration
|
|
2800
|
+
* Same for all platforms
|
|
2801
|
+
*/
|
|
2802
|
+
interface L1Config {
|
|
2803
|
+
/** Fulcrum WebSocket URL (if not set, uses network default) */
|
|
2804
|
+
electrumUrl?: string;
|
|
2805
|
+
/** Default fee rate in sat/byte */
|
|
2806
|
+
defaultFeeRate?: number;
|
|
2807
|
+
/** Enable vesting classification */
|
|
2808
|
+
enableVesting?: boolean;
|
|
2809
|
+
}
|
|
2810
|
+
/**
|
|
2811
|
+
* Base providers result
|
|
2812
|
+
* Common structure for all platforms
|
|
2813
|
+
*/
|
|
2814
|
+
interface BaseProviders {
|
|
2815
|
+
storage: StorageProvider;
|
|
2816
|
+
tokenStorage: TokenStorageProvider<TxfStorageDataBase>;
|
|
2817
|
+
transport: TransportProvider;
|
|
2818
|
+
oracle: OracleProvider;
|
|
2819
|
+
/** L1 configuration (for passing to Sphere.init) */
|
|
2820
|
+
l1?: L1Config;
|
|
2821
|
+
}
|
|
2822
|
+
|
|
2823
|
+
/**
|
|
2824
|
+
* Browser-specific implementations
|
|
2825
|
+
* All platform-dependent code lives here
|
|
2826
|
+
*/
|
|
2827
|
+
|
|
2828
|
+
/**
|
|
2829
|
+
* Browser transport configuration
|
|
2830
|
+
* Extends base with browser-specific options
|
|
2831
|
+
*/
|
|
2832
|
+
type TransportConfig = BaseTransportConfig & BrowserTransportExtensions;
|
|
2833
|
+
/**
|
|
2834
|
+
* Browser oracle configuration
|
|
2835
|
+
* Same as base (no browser-specific extensions)
|
|
2836
|
+
*/
|
|
2837
|
+
type OracleConfig = BaseOracleConfig;
|
|
2838
|
+
/**
|
|
2839
|
+
* IPFS sync backend configuration
|
|
2840
|
+
*/
|
|
2841
|
+
interface IpfsSyncConfig {
|
|
2842
|
+
/** Enable IPFS sync (default: false) */
|
|
2843
|
+
enabled?: boolean;
|
|
2844
|
+
/** Replace default gateways entirely */
|
|
2845
|
+
gateways?: string[];
|
|
2846
|
+
/** Add gateways to network defaults */
|
|
2847
|
+
additionalGateways?: string[];
|
|
2848
|
+
/** Replace default bootstrap peers */
|
|
2849
|
+
bootstrapPeers?: string[];
|
|
2850
|
+
/** Add bootstrap peers to defaults */
|
|
2851
|
+
additionalBootstrapPeers?: string[];
|
|
2852
|
+
/** Use browser DHT (Helia) vs HTTP-only mode */
|
|
2853
|
+
useDht?: boolean;
|
|
2854
|
+
}
|
|
2855
|
+
/**
|
|
2856
|
+
* File sync backend configuration (future)
|
|
2857
|
+
*/
|
|
2858
|
+
interface FileSyncConfig {
|
|
2859
|
+
/** Enable file sync (default: false) */
|
|
2860
|
+
enabled?: boolean;
|
|
2861
|
+
/** Directory path for token files */
|
|
2862
|
+
directory?: string;
|
|
2863
|
+
/** File format: 'json' | 'txf' */
|
|
2864
|
+
format?: 'json' | 'txf';
|
|
2865
|
+
}
|
|
2866
|
+
/**
|
|
2867
|
+
* Cloud sync backend configuration (future)
|
|
2868
|
+
*/
|
|
2869
|
+
interface CloudSyncConfig {
|
|
2870
|
+
/** Enable cloud sync (default: false) */
|
|
2871
|
+
enabled?: boolean;
|
|
2872
|
+
/** Cloud provider */
|
|
2873
|
+
provider?: 'aws' | 'gcp' | 'azure' | 'custom';
|
|
2874
|
+
/** Bucket/container name */
|
|
2875
|
+
bucket?: string;
|
|
2876
|
+
/** API endpoint (for custom provider) */
|
|
2877
|
+
endpoint?: string;
|
|
2878
|
+
/** API key or credentials */
|
|
2879
|
+
apiKey?: string;
|
|
2880
|
+
}
|
|
2881
|
+
/**
|
|
2882
|
+
* MongoDB sync backend configuration
|
|
2883
|
+
*/
|
|
2884
|
+
interface MongoDbSyncConfig {
|
|
2885
|
+
/** Enable MongoDB sync (default: false) */
|
|
2886
|
+
enabled?: boolean;
|
|
2887
|
+
/** MongoDB connection URI */
|
|
2888
|
+
uri?: string;
|
|
2889
|
+
/** Database name */
|
|
2890
|
+
database?: string;
|
|
2891
|
+
/** Collection name (default: 'tokens') */
|
|
2892
|
+
collection?: string;
|
|
2893
|
+
/** Enable authentication */
|
|
2894
|
+
authEnabled?: boolean;
|
|
2895
|
+
/** Username (if authEnabled) */
|
|
2896
|
+
username?: string;
|
|
2897
|
+
/** Password (if authEnabled) */
|
|
2898
|
+
password?: string;
|
|
2899
|
+
}
|
|
2900
|
+
/**
|
|
2901
|
+
* Token sync configuration - supports multiple backends
|
|
2902
|
+
*/
|
|
2903
|
+
interface TokenSyncConfig {
|
|
2904
|
+
/** IPFS sync backend */
|
|
2905
|
+
ipfs?: IpfsSyncConfig;
|
|
2906
|
+
/** File sync backend (future) */
|
|
2907
|
+
file?: FileSyncConfig;
|
|
2908
|
+
/** Cloud sync backend (future) */
|
|
2909
|
+
cloud?: CloudSyncConfig;
|
|
2910
|
+
/** MongoDB sync backend */
|
|
2911
|
+
mongodb?: MongoDbSyncConfig;
|
|
2912
|
+
}
|
|
2913
|
+
interface BrowserProvidersConfig {
|
|
2914
|
+
/** Network preset: mainnet, testnet, or dev. Sets default URLs for all services */
|
|
2915
|
+
network?: NetworkType;
|
|
2916
|
+
/** Storage configuration (localStorage) */
|
|
2917
|
+
storage?: LocalStorageProviderConfig;
|
|
2918
|
+
/** Transport (Nostr) configuration - supports extend/override pattern */
|
|
2919
|
+
transport?: TransportConfig;
|
|
2920
|
+
/** Oracle (Aggregator) configuration - supports extend/override pattern */
|
|
2921
|
+
oracle?: OracleConfig;
|
|
2922
|
+
/** L1 (ALPHA blockchain) configuration */
|
|
2923
|
+
l1?: L1Config;
|
|
2924
|
+
/**
|
|
2925
|
+
* Token sync backends configuration
|
|
2926
|
+
* Supports multiple backends: IPFS, file, cloud (future)
|
|
2927
|
+
* Each backend can be enabled/disabled independently
|
|
2928
|
+
*/
|
|
2929
|
+
tokenSync?: TokenSyncConfig;
|
|
2930
|
+
}
|
|
2931
|
+
interface BrowserProviders {
|
|
2932
|
+
storage: StorageProvider;
|
|
2933
|
+
transport: TransportProvider;
|
|
2934
|
+
oracle: OracleProvider;
|
|
2935
|
+
/** Token storage provider for local persistence (IndexedDB) */
|
|
2936
|
+
tokenStorage: TokenStorageProvider<TxfStorageDataBase>;
|
|
2937
|
+
/** L1 configuration (for passing to Sphere.init) */
|
|
2938
|
+
l1?: L1Config;
|
|
2939
|
+
/**
|
|
2940
|
+
* Token sync configuration (resolved from tokenSync options)
|
|
2941
|
+
* For advanced use cases when additional sync backends are needed
|
|
2942
|
+
* @deprecated Use tokenStorage provider instead. For custom sync backends,
|
|
2943
|
+
* use Sphere.addTokenStorageProvider() after initialization.
|
|
2944
|
+
*/
|
|
2945
|
+
tokenSyncConfig?: {
|
|
2946
|
+
ipfs?: {
|
|
2947
|
+
enabled: boolean;
|
|
2948
|
+
gateways: string[];
|
|
2949
|
+
bootstrapPeers?: string[];
|
|
2950
|
+
useDht?: boolean;
|
|
2951
|
+
};
|
|
2952
|
+
file?: {
|
|
2953
|
+
enabled: boolean;
|
|
2954
|
+
directory?: string;
|
|
2955
|
+
format?: 'json' | 'txf';
|
|
2956
|
+
};
|
|
2957
|
+
cloud?: {
|
|
2958
|
+
enabled: boolean;
|
|
2959
|
+
provider?: string;
|
|
2960
|
+
bucket?: string;
|
|
2961
|
+
endpoint?: string;
|
|
2962
|
+
apiKey?: string;
|
|
2963
|
+
};
|
|
2964
|
+
mongodb?: {
|
|
2965
|
+
enabled: boolean;
|
|
2966
|
+
uri?: string;
|
|
2967
|
+
database?: string;
|
|
2968
|
+
collection?: string;
|
|
2969
|
+
};
|
|
2970
|
+
};
|
|
2971
|
+
}
|
|
2972
|
+
/**
|
|
2973
|
+
* Create all browser providers with default configuration
|
|
2974
|
+
*
|
|
2975
|
+
* Supports extend/override pattern for flexible configuration:
|
|
2976
|
+
* - Use `network` preset for quick setup (mainnet/testnet/dev)
|
|
2977
|
+
* - Override specific values (e.g., `oracle.url` replaces default)
|
|
2978
|
+
* - Extend arrays with `additional*` (e.g., `additionalRelays` adds to defaults)
|
|
2979
|
+
*
|
|
2980
|
+
* @example
|
|
2981
|
+
* ```ts
|
|
2982
|
+
* // Simple - uses mainnet defaults
|
|
2983
|
+
* const providers = createBrowserProviders();
|
|
2984
|
+
*
|
|
2985
|
+
* // Testnet - all services use testnet URLs
|
|
2986
|
+
* const providers = createBrowserProviders({ network: 'testnet' });
|
|
2987
|
+
*
|
|
2988
|
+
* // Add extra relays to testnet defaults
|
|
2989
|
+
* const providers = createBrowserProviders({
|
|
2990
|
+
* network: 'testnet',
|
|
2991
|
+
* transport: {
|
|
2992
|
+
* additionalRelays: ['wss://my-relay.com', 'wss://backup-relay.com'],
|
|
2993
|
+
* },
|
|
2994
|
+
* });
|
|
2995
|
+
*
|
|
2996
|
+
* // Replace relays entirely (ignores network defaults)
|
|
2997
|
+
* const providers = createBrowserProviders({
|
|
2998
|
+
* network: 'testnet',
|
|
2999
|
+
* transport: {
|
|
3000
|
+
* relays: ['wss://only-this-relay.com'],
|
|
3001
|
+
* },
|
|
3002
|
+
* });
|
|
3003
|
+
*
|
|
3004
|
+
* // Use with Sphere.init (tokenStorage is automatically included)
|
|
3005
|
+
* const { sphere } = await Sphere.init({
|
|
3006
|
+
* ...providers,
|
|
3007
|
+
* autoGenerate: true,
|
|
3008
|
+
* });
|
|
3009
|
+
*
|
|
3010
|
+
* // Add additional sync backends dynamically after init
|
|
3011
|
+
* // await sphere.addTokenStorageProvider(myMongoDbProvider);
|
|
3012
|
+
* ```
|
|
3013
|
+
*/
|
|
3014
|
+
declare function createBrowserProviders(config?: BrowserProvidersConfig): BrowserProviders;
|
|
3015
|
+
|
|
3016
|
+
export { type BaseOracleConfig, type BaseProviders, type BaseTransportConfig, type BrowserProviders, type BrowserProvidersConfig, BrowserTrustBaseLoader, type CloudSyncConfig, type DownloadJSONOptions, type DownloadTextOptions, type FileSyncConfig, type IMessageEvent, type IWebSocket, type IndexedDBTokenStorageConfig, IndexedDBTokenStorageProvider, IpfsStorageProvider, type IpfsStorageProviderConfig, type IpfsSyncConfig, type L1Config, LocalStorageProvider, type LocalStorageProviderConfig, type MongoDbSyncConfig, NostrTransportProvider, type NostrTransportProviderConfig, type OracleConfig, type TokenSyncConfig, type TransportConfig, type TrustBaseLoader, type UUIDGenerator, UnicityAggregatorProvider, type UnicityAggregatorProviderConfig, UnicityOracleProvider, type UnicityOracleProviderConfig, type WebSocketFactory, WebSocketReadyState, createBrowserProviders, createBrowserTrustBaseLoader, createBrowserWebSocket, createIndexedDBTokenStorageProvider, createIpfsStorageProvider, createLocalStorageProvider, createNostrTransportProvider, createUnicityAggregatorProvider, createUnicityOracleProvider, defaultUUIDGenerator, downloadFile, downloadJSONFile, downloadTextFile, downloadWalletJSON, downloadWalletJSONData, downloadWalletText, readFileAsArrayBuffer, readFileAsText, readFileAsUint8Array };
|