@unicitylabs/sphere-sdk 0.2.3 → 0.2.5

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.
@@ -38,7 +38,9 @@ var STORAGE_KEYS_ADDRESS = {
38
38
  /** Messages for this address */
39
39
  MESSAGES: "messages",
40
40
  /** Transaction history for this address */
41
- TRANSACTION_HISTORY: "transaction_history"
41
+ TRANSACTION_HISTORY: "transaction_history",
42
+ /** Pending V5 finalization tokens (unconfirmed instant split tokens) */
43
+ PENDING_V5_TOKENS: "pending_v5_tokens"
42
44
  };
43
45
  var STORAGE_KEYS = {
44
46
  ...STORAGE_KEYS_GLOBAL,
@@ -85,6 +87,19 @@ var DEFAULT_IPFS_GATEWAYS = [
85
87
  "https://dweb.link",
86
88
  "https://ipfs.io"
87
89
  ];
90
+ var UNICITY_IPFS_NODES = [
91
+ {
92
+ host: "unicity-ipfs1.dyndns.org",
93
+ peerId: "12D3KooWDKJqEMAhH4nsSSiKtK1VLcas5coUqSPZAfbWbZpxtL4u",
94
+ httpPort: 9080,
95
+ httpsPort: 443
96
+ }
97
+ ];
98
+ function getIpfsGatewayUrls(isSecure) {
99
+ return UNICITY_IPFS_NODES.map(
100
+ (node) => isSecure !== false ? `https://${node.host}` : `http://${node.host}:${node.httpPort}`
101
+ );
102
+ }
88
103
  var DEFAULT_BASE_PATH = "m/44'/0'/0'";
89
104
  var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
90
105
  var DEFAULT_ELECTRUM_URL = "wss://fulcrum.alpha.unicity.network:50004";
@@ -1206,6 +1221,13 @@ function publicKeyToAddress(publicKey, prefix = "alpha", witnessVersion = 0) {
1206
1221
  const programBytes = hash160ToBytes(pubKeyHash);
1207
1222
  return encodeBech32(prefix, witnessVersion, programBytes);
1208
1223
  }
1224
+ function hexToBytes(hex) {
1225
+ const matches = hex.match(/../g);
1226
+ if (!matches) {
1227
+ return new Uint8Array(0);
1228
+ }
1229
+ return Uint8Array.from(matches.map((x) => parseInt(x, 16)));
1230
+ }
1209
1231
 
1210
1232
  // transport/websocket.ts
1211
1233
  var WebSocketReadyState = {
@@ -1301,6 +1323,7 @@ var NostrTransportProvider = class {
1301
1323
  nostrClient = null;
1302
1324
  mainSubscriptionId = null;
1303
1325
  // Event handlers
1326
+ processedEventIds = /* @__PURE__ */ new Set();
1304
1327
  messageHandlers = /* @__PURE__ */ new Set();
1305
1328
  transferHandlers = /* @__PURE__ */ new Set();
1306
1329
  paymentRequestHandlers = /* @__PURE__ */ new Set();
@@ -2041,6 +2064,12 @@ var NostrTransportProvider = class {
2041
2064
  // Private: Message Handling
2042
2065
  // ===========================================================================
2043
2066
  async handleEvent(event) {
2067
+ if (event.id && this.processedEventIds.has(event.id)) {
2068
+ return;
2069
+ }
2070
+ if (event.id) {
2071
+ this.processedEventIds.add(event.id);
2072
+ }
2044
2073
  this.log("Processing event kind:", event.kind, "id:", event.id?.slice(0, 12));
2045
2074
  try {
2046
2075
  switch (event.kind) {
@@ -2153,7 +2182,7 @@ var NostrTransportProvider = class {
2153
2182
  this.emitEvent({ type: "transfer:received", timestamp: Date.now() });
2154
2183
  for (const handler of this.transferHandlers) {
2155
2184
  try {
2156
- handler(transfer);
2185
+ await handler(transfer);
2157
2186
  } catch (error) {
2158
2187
  this.log("Transfer handler error:", error);
2159
2188
  }
@@ -2283,6 +2312,49 @@ var NostrTransportProvider = class {
2283
2312
  const sdkEvent = NostrEventClass.fromJSON(event);
2284
2313
  await this.nostrClient.publishEvent(sdkEvent);
2285
2314
  }
2315
+ async fetchPendingEvents() {
2316
+ if (!this.nostrClient?.isConnected() || !this.keyManager) {
2317
+ throw new Error("Transport not connected");
2318
+ }
2319
+ const nostrPubkey = this.keyManager.getPublicKeyHex();
2320
+ const walletFilter = new Filter();
2321
+ walletFilter.kinds = [
2322
+ EVENT_KINDS.DIRECT_MESSAGE,
2323
+ EVENT_KINDS.TOKEN_TRANSFER,
2324
+ EVENT_KINDS.PAYMENT_REQUEST,
2325
+ EVENT_KINDS.PAYMENT_REQUEST_RESPONSE
2326
+ ];
2327
+ walletFilter["#p"] = [nostrPubkey];
2328
+ walletFilter.since = Math.floor(Date.now() / 1e3) - 86400;
2329
+ const events = [];
2330
+ await new Promise((resolve) => {
2331
+ const timeout = setTimeout(() => {
2332
+ if (subId) this.nostrClient?.unsubscribe(subId);
2333
+ resolve();
2334
+ }, 5e3);
2335
+ const subId = this.nostrClient.subscribe(walletFilter, {
2336
+ onEvent: (event) => {
2337
+ events.push({
2338
+ id: event.id,
2339
+ kind: event.kind,
2340
+ content: event.content,
2341
+ tags: event.tags,
2342
+ pubkey: event.pubkey,
2343
+ created_at: event.created_at,
2344
+ sig: event.sig
2345
+ });
2346
+ },
2347
+ onEndOfStoredEvents: () => {
2348
+ clearTimeout(timeout);
2349
+ this.nostrClient?.unsubscribe(subId);
2350
+ resolve();
2351
+ }
2352
+ });
2353
+ });
2354
+ for (const event of events) {
2355
+ await this.handleEvent(event);
2356
+ }
2357
+ }
2286
2358
  async queryEvents(filterObj) {
2287
2359
  if (!this.nostrClient || !this.nostrClient.isConnected()) {
2288
2360
  throw new Error("No connected relays");
@@ -3045,6 +3117,1913 @@ async function readFileAsUint8Array(file) {
3045
3117
  return new Uint8Array(buffer);
3046
3118
  }
3047
3119
 
3120
+ // impl/shared/ipfs/ipfs-error-types.ts
3121
+ var IpfsError = class extends Error {
3122
+ category;
3123
+ gateway;
3124
+ cause;
3125
+ constructor(message, category, gateway, cause) {
3126
+ super(message);
3127
+ this.name = "IpfsError";
3128
+ this.category = category;
3129
+ this.gateway = gateway;
3130
+ this.cause = cause;
3131
+ }
3132
+ /** Whether this error should trigger the circuit breaker */
3133
+ get shouldTriggerCircuitBreaker() {
3134
+ return this.category !== "NOT_FOUND" && this.category !== "SEQUENCE_DOWNGRADE";
3135
+ }
3136
+ };
3137
+ function classifyFetchError(error) {
3138
+ if (error instanceof DOMException && error.name === "AbortError") {
3139
+ return "TIMEOUT";
3140
+ }
3141
+ if (error instanceof TypeError) {
3142
+ return "NETWORK_ERROR";
3143
+ }
3144
+ if (error instanceof Error && error.name === "TimeoutError") {
3145
+ return "TIMEOUT";
3146
+ }
3147
+ return "NETWORK_ERROR";
3148
+ }
3149
+ function classifyHttpStatus(status, responseBody) {
3150
+ if (status === 404) {
3151
+ return "NOT_FOUND";
3152
+ }
3153
+ if (status === 500 && responseBody) {
3154
+ if (/routing:\s*not\s*found/i.test(responseBody)) {
3155
+ return "NOT_FOUND";
3156
+ }
3157
+ }
3158
+ if (status >= 500) {
3159
+ return "GATEWAY_ERROR";
3160
+ }
3161
+ if (status >= 400) {
3162
+ return "GATEWAY_ERROR";
3163
+ }
3164
+ return "GATEWAY_ERROR";
3165
+ }
3166
+
3167
+ // impl/shared/ipfs/ipfs-state-persistence.ts
3168
+ var InMemoryIpfsStatePersistence = class {
3169
+ states = /* @__PURE__ */ new Map();
3170
+ async load(ipnsName) {
3171
+ return this.states.get(ipnsName) ?? null;
3172
+ }
3173
+ async save(ipnsName, state) {
3174
+ this.states.set(ipnsName, { ...state });
3175
+ }
3176
+ async clear(ipnsName) {
3177
+ this.states.delete(ipnsName);
3178
+ }
3179
+ };
3180
+
3181
+ // impl/shared/ipfs/ipns-key-derivation.ts
3182
+ var IPNS_HKDF_INFO = "ipfs-storage-ed25519-v1";
3183
+ var libp2pCryptoModule = null;
3184
+ var libp2pPeerIdModule = null;
3185
+ async function loadLibp2pModules() {
3186
+ if (!libp2pCryptoModule) {
3187
+ [libp2pCryptoModule, libp2pPeerIdModule] = await Promise.all([
3188
+ import("@libp2p/crypto/keys"),
3189
+ import("@libp2p/peer-id")
3190
+ ]);
3191
+ }
3192
+ return {
3193
+ generateKeyPairFromSeed: libp2pCryptoModule.generateKeyPairFromSeed,
3194
+ peerIdFromPrivateKey: libp2pPeerIdModule.peerIdFromPrivateKey
3195
+ };
3196
+ }
3197
+ function deriveEd25519KeyMaterial(privateKeyHex, info = IPNS_HKDF_INFO) {
3198
+ const walletSecret = hexToBytes(privateKeyHex);
3199
+ const infoBytes = new TextEncoder().encode(info);
3200
+ return hkdf(sha256, walletSecret, void 0, infoBytes, 32);
3201
+ }
3202
+ async function deriveIpnsIdentity(privateKeyHex) {
3203
+ const { generateKeyPairFromSeed, peerIdFromPrivateKey } = await loadLibp2pModules();
3204
+ const derivedKey = deriveEd25519KeyMaterial(privateKeyHex);
3205
+ const keyPair = await generateKeyPairFromSeed("Ed25519", derivedKey);
3206
+ const peerId = peerIdFromPrivateKey(keyPair);
3207
+ return {
3208
+ keyPair,
3209
+ ipnsName: peerId.toString()
3210
+ };
3211
+ }
3212
+
3213
+ // impl/shared/ipfs/ipns-record-manager.ts
3214
+ var DEFAULT_LIFETIME_MS = 99 * 365 * 24 * 60 * 60 * 1e3;
3215
+ var ipnsModule = null;
3216
+ async function loadIpnsModule() {
3217
+ if (!ipnsModule) {
3218
+ const mod = await import("ipns");
3219
+ ipnsModule = {
3220
+ createIPNSRecord: mod.createIPNSRecord,
3221
+ marshalIPNSRecord: mod.marshalIPNSRecord,
3222
+ unmarshalIPNSRecord: mod.unmarshalIPNSRecord
3223
+ };
3224
+ }
3225
+ return ipnsModule;
3226
+ }
3227
+ async function createSignedRecord(keyPair, cid, sequenceNumber, lifetimeMs = DEFAULT_LIFETIME_MS) {
3228
+ const { createIPNSRecord, marshalIPNSRecord } = await loadIpnsModule();
3229
+ const record = await createIPNSRecord(
3230
+ keyPair,
3231
+ `/ipfs/${cid}`,
3232
+ sequenceNumber,
3233
+ lifetimeMs
3234
+ );
3235
+ return marshalIPNSRecord(record);
3236
+ }
3237
+ async function parseRoutingApiResponse(responseText) {
3238
+ const { unmarshalIPNSRecord } = await loadIpnsModule();
3239
+ const lines = responseText.trim().split("\n");
3240
+ for (const line of lines) {
3241
+ if (!line.trim()) continue;
3242
+ try {
3243
+ const obj = JSON.parse(line);
3244
+ if (obj.Extra) {
3245
+ const recordData = base64ToUint8Array(obj.Extra);
3246
+ const record = unmarshalIPNSRecord(recordData);
3247
+ const valueBytes = typeof record.value === "string" ? new TextEncoder().encode(record.value) : record.value;
3248
+ const valueStr = new TextDecoder().decode(valueBytes);
3249
+ const cidMatch = valueStr.match(/\/ipfs\/([a-zA-Z0-9]+)/);
3250
+ if (cidMatch) {
3251
+ return {
3252
+ cid: cidMatch[1],
3253
+ sequence: record.sequence,
3254
+ recordData
3255
+ };
3256
+ }
3257
+ }
3258
+ } catch {
3259
+ continue;
3260
+ }
3261
+ }
3262
+ return null;
3263
+ }
3264
+ function base64ToUint8Array(base64) {
3265
+ const binary = atob(base64);
3266
+ const bytes = new Uint8Array(binary.length);
3267
+ for (let i = 0; i < binary.length; i++) {
3268
+ bytes[i] = binary.charCodeAt(i);
3269
+ }
3270
+ return bytes;
3271
+ }
3272
+
3273
+ // impl/shared/ipfs/ipfs-cache.ts
3274
+ var DEFAULT_IPNS_TTL_MS = 6e4;
3275
+ var DEFAULT_FAILURE_COOLDOWN_MS = 6e4;
3276
+ var DEFAULT_FAILURE_THRESHOLD = 3;
3277
+ var DEFAULT_KNOWN_FRESH_WINDOW_MS = 3e4;
3278
+ var IpfsCache = class {
3279
+ ipnsRecords = /* @__PURE__ */ new Map();
3280
+ content = /* @__PURE__ */ new Map();
3281
+ gatewayFailures = /* @__PURE__ */ new Map();
3282
+ knownFreshTimestamps = /* @__PURE__ */ new Map();
3283
+ ipnsTtlMs;
3284
+ failureCooldownMs;
3285
+ failureThreshold;
3286
+ knownFreshWindowMs;
3287
+ constructor(config) {
3288
+ this.ipnsTtlMs = config?.ipnsTtlMs ?? DEFAULT_IPNS_TTL_MS;
3289
+ this.failureCooldownMs = config?.failureCooldownMs ?? DEFAULT_FAILURE_COOLDOWN_MS;
3290
+ this.failureThreshold = config?.failureThreshold ?? DEFAULT_FAILURE_THRESHOLD;
3291
+ this.knownFreshWindowMs = config?.knownFreshWindowMs ?? DEFAULT_KNOWN_FRESH_WINDOW_MS;
3292
+ }
3293
+ // ---------------------------------------------------------------------------
3294
+ // IPNS Record Cache (60s TTL)
3295
+ // ---------------------------------------------------------------------------
3296
+ getIpnsRecord(ipnsName) {
3297
+ const entry = this.ipnsRecords.get(ipnsName);
3298
+ if (!entry) return null;
3299
+ if (Date.now() - entry.timestamp > this.ipnsTtlMs) {
3300
+ this.ipnsRecords.delete(ipnsName);
3301
+ return null;
3302
+ }
3303
+ return entry.data;
3304
+ }
3305
+ /**
3306
+ * Get cached IPNS record ignoring TTL (for known-fresh optimization).
3307
+ */
3308
+ getIpnsRecordIgnoreTtl(ipnsName) {
3309
+ const entry = this.ipnsRecords.get(ipnsName);
3310
+ return entry?.data ?? null;
3311
+ }
3312
+ setIpnsRecord(ipnsName, result) {
3313
+ this.ipnsRecords.set(ipnsName, {
3314
+ data: result,
3315
+ timestamp: Date.now()
3316
+ });
3317
+ }
3318
+ invalidateIpns(ipnsName) {
3319
+ this.ipnsRecords.delete(ipnsName);
3320
+ }
3321
+ // ---------------------------------------------------------------------------
3322
+ // Content Cache (infinite TTL - content is immutable by CID)
3323
+ // ---------------------------------------------------------------------------
3324
+ getContent(cid) {
3325
+ const entry = this.content.get(cid);
3326
+ return entry?.data ?? null;
3327
+ }
3328
+ setContent(cid, data) {
3329
+ this.content.set(cid, {
3330
+ data,
3331
+ timestamp: Date.now()
3332
+ });
3333
+ }
3334
+ // ---------------------------------------------------------------------------
3335
+ // Gateway Failure Tracking (Circuit Breaker)
3336
+ // ---------------------------------------------------------------------------
3337
+ /**
3338
+ * Record a gateway failure. After threshold consecutive failures,
3339
+ * the gateway enters cooldown and is considered unhealthy.
3340
+ */
3341
+ recordGatewayFailure(gateway) {
3342
+ const existing = this.gatewayFailures.get(gateway);
3343
+ this.gatewayFailures.set(gateway, {
3344
+ count: (existing?.count ?? 0) + 1,
3345
+ lastFailure: Date.now()
3346
+ });
3347
+ }
3348
+ /** Reset failure count for a gateway (on successful request) */
3349
+ recordGatewaySuccess(gateway) {
3350
+ this.gatewayFailures.delete(gateway);
3351
+ }
3352
+ /**
3353
+ * Check if a gateway is currently in circuit breaker cooldown.
3354
+ * A gateway is considered unhealthy if it has had >= threshold
3355
+ * consecutive failures and the cooldown period hasn't elapsed.
3356
+ */
3357
+ isGatewayInCooldown(gateway) {
3358
+ const failure = this.gatewayFailures.get(gateway);
3359
+ if (!failure) return false;
3360
+ if (failure.count < this.failureThreshold) return false;
3361
+ const elapsed = Date.now() - failure.lastFailure;
3362
+ if (elapsed >= this.failureCooldownMs) {
3363
+ this.gatewayFailures.delete(gateway);
3364
+ return false;
3365
+ }
3366
+ return true;
3367
+ }
3368
+ // ---------------------------------------------------------------------------
3369
+ // Known-Fresh Flag (FAST mode optimization)
3370
+ // ---------------------------------------------------------------------------
3371
+ /**
3372
+ * Mark IPNS cache as "known-fresh" (after local publish or push notification).
3373
+ * Within the fresh window, we can skip network resolution.
3374
+ */
3375
+ markIpnsFresh(ipnsName) {
3376
+ this.knownFreshTimestamps.set(ipnsName, Date.now());
3377
+ }
3378
+ /**
3379
+ * Check if the cache is known-fresh (within the fresh window).
3380
+ */
3381
+ isIpnsKnownFresh(ipnsName) {
3382
+ const timestamp = this.knownFreshTimestamps.get(ipnsName);
3383
+ if (!timestamp) return false;
3384
+ if (Date.now() - timestamp > this.knownFreshWindowMs) {
3385
+ this.knownFreshTimestamps.delete(ipnsName);
3386
+ return false;
3387
+ }
3388
+ return true;
3389
+ }
3390
+ // ---------------------------------------------------------------------------
3391
+ // Cache Management
3392
+ // ---------------------------------------------------------------------------
3393
+ clear() {
3394
+ this.ipnsRecords.clear();
3395
+ this.content.clear();
3396
+ this.gatewayFailures.clear();
3397
+ this.knownFreshTimestamps.clear();
3398
+ }
3399
+ };
3400
+
3401
+ // impl/shared/ipfs/ipfs-http-client.ts
3402
+ var DEFAULT_CONNECTIVITY_TIMEOUT_MS = 5e3;
3403
+ var DEFAULT_FETCH_TIMEOUT_MS = 15e3;
3404
+ var DEFAULT_RESOLVE_TIMEOUT_MS = 1e4;
3405
+ var DEFAULT_PUBLISH_TIMEOUT_MS = 3e4;
3406
+ var DEFAULT_GATEWAY_PATH_TIMEOUT_MS = 3e3;
3407
+ var DEFAULT_ROUTING_API_TIMEOUT_MS = 2e3;
3408
+ var IpfsHttpClient = class {
3409
+ gateways;
3410
+ fetchTimeoutMs;
3411
+ resolveTimeoutMs;
3412
+ publishTimeoutMs;
3413
+ connectivityTimeoutMs;
3414
+ debug;
3415
+ cache;
3416
+ constructor(config, cache) {
3417
+ this.gateways = config.gateways;
3418
+ this.fetchTimeoutMs = config.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;
3419
+ this.resolveTimeoutMs = config.resolveTimeoutMs ?? DEFAULT_RESOLVE_TIMEOUT_MS;
3420
+ this.publishTimeoutMs = config.publishTimeoutMs ?? DEFAULT_PUBLISH_TIMEOUT_MS;
3421
+ this.connectivityTimeoutMs = config.connectivityTimeoutMs ?? DEFAULT_CONNECTIVITY_TIMEOUT_MS;
3422
+ this.debug = config.debug ?? false;
3423
+ this.cache = cache;
3424
+ }
3425
+ // ---------------------------------------------------------------------------
3426
+ // Public Accessors
3427
+ // ---------------------------------------------------------------------------
3428
+ /**
3429
+ * Get configured gateway URLs.
3430
+ */
3431
+ getGateways() {
3432
+ return [...this.gateways];
3433
+ }
3434
+ // ---------------------------------------------------------------------------
3435
+ // Gateway Health
3436
+ // ---------------------------------------------------------------------------
3437
+ /**
3438
+ * Test connectivity to a single gateway.
3439
+ */
3440
+ async testConnectivity(gateway) {
3441
+ const start = Date.now();
3442
+ try {
3443
+ const response = await this.fetchWithTimeout(
3444
+ `${gateway}/api/v0/version`,
3445
+ this.connectivityTimeoutMs,
3446
+ { method: "POST" }
3447
+ );
3448
+ if (!response.ok) {
3449
+ return { gateway, healthy: false, error: `HTTP ${response.status}` };
3450
+ }
3451
+ return {
3452
+ gateway,
3453
+ healthy: true,
3454
+ responseTimeMs: Date.now() - start
3455
+ };
3456
+ } catch (error) {
3457
+ return {
3458
+ gateway,
3459
+ healthy: false,
3460
+ error: error instanceof Error ? error.message : String(error)
3461
+ };
3462
+ }
3463
+ }
3464
+ /**
3465
+ * Find healthy gateways from the configured list.
3466
+ */
3467
+ async findHealthyGateways() {
3468
+ const results = await Promise.allSettled(
3469
+ this.gateways.map((gw) => this.testConnectivity(gw))
3470
+ );
3471
+ return results.filter((r) => r.status === "fulfilled" && r.value.healthy).map((r) => r.value.gateway);
3472
+ }
3473
+ /**
3474
+ * Get gateways that are not in circuit breaker cooldown.
3475
+ */
3476
+ getAvailableGateways() {
3477
+ return this.gateways.filter((gw) => !this.cache.isGatewayInCooldown(gw));
3478
+ }
3479
+ // ---------------------------------------------------------------------------
3480
+ // Content Upload
3481
+ // ---------------------------------------------------------------------------
3482
+ /**
3483
+ * Upload JSON content to IPFS.
3484
+ * Tries all gateways in parallel, returns first success.
3485
+ */
3486
+ async upload(data, gateways) {
3487
+ const targets = gateways ?? this.getAvailableGateways();
3488
+ if (targets.length === 0) {
3489
+ throw new IpfsError("No gateways available for upload", "NETWORK_ERROR");
3490
+ }
3491
+ const jsonBytes = new TextEncoder().encode(JSON.stringify(data));
3492
+ const promises = targets.map(async (gateway) => {
3493
+ try {
3494
+ const formData = new FormData();
3495
+ formData.append("file", new Blob([jsonBytes], { type: "application/json" }), "data.json");
3496
+ const response = await this.fetchWithTimeout(
3497
+ `${gateway}/api/v0/add?pin=true&cid-version=1`,
3498
+ this.publishTimeoutMs,
3499
+ { method: "POST", body: formData }
3500
+ );
3501
+ if (!response.ok) {
3502
+ throw new IpfsError(
3503
+ `Upload failed: HTTP ${response.status}`,
3504
+ classifyHttpStatus(response.status),
3505
+ gateway
3506
+ );
3507
+ }
3508
+ const result = await response.json();
3509
+ this.cache.recordGatewaySuccess(gateway);
3510
+ this.log(`Uploaded to ${gateway}: CID=${result.Hash}`);
3511
+ return { cid: result.Hash, gateway };
3512
+ } catch (error) {
3513
+ if (error instanceof IpfsError && error.shouldTriggerCircuitBreaker) {
3514
+ this.cache.recordGatewayFailure(gateway);
3515
+ }
3516
+ throw error;
3517
+ }
3518
+ });
3519
+ try {
3520
+ const result = await Promise.any(promises);
3521
+ return { cid: result.cid };
3522
+ } catch (error) {
3523
+ if (error instanceof AggregateError) {
3524
+ throw new IpfsError(
3525
+ `Upload failed on all gateways: ${error.errors.map((e) => e.message).join("; ")}`,
3526
+ "NETWORK_ERROR"
3527
+ );
3528
+ }
3529
+ throw error;
3530
+ }
3531
+ }
3532
+ // ---------------------------------------------------------------------------
3533
+ // Content Fetch
3534
+ // ---------------------------------------------------------------------------
3535
+ /**
3536
+ * Fetch content by CID from IPFS gateways.
3537
+ * Checks content cache first. Races all gateways for fastest response.
3538
+ */
3539
+ async fetchContent(cid, gateways) {
3540
+ const cached = this.cache.getContent(cid);
3541
+ if (cached) {
3542
+ this.log(`Content cache hit for CID=${cid}`);
3543
+ return cached;
3544
+ }
3545
+ const targets = gateways ?? this.getAvailableGateways();
3546
+ if (targets.length === 0) {
3547
+ throw new IpfsError("No gateways available for fetch", "NETWORK_ERROR");
3548
+ }
3549
+ const promises = targets.map(async (gateway) => {
3550
+ try {
3551
+ const response = await this.fetchWithTimeout(
3552
+ `${gateway}/ipfs/${cid}`,
3553
+ this.fetchTimeoutMs,
3554
+ { headers: { Accept: "application/octet-stream" } }
3555
+ );
3556
+ if (!response.ok) {
3557
+ const body = await response.text().catch(() => "");
3558
+ throw new IpfsError(
3559
+ `Fetch failed: HTTP ${response.status}`,
3560
+ classifyHttpStatus(response.status, body),
3561
+ gateway
3562
+ );
3563
+ }
3564
+ const data = await response.json();
3565
+ this.cache.recordGatewaySuccess(gateway);
3566
+ this.cache.setContent(cid, data);
3567
+ this.log(`Fetched from ${gateway}: CID=${cid}`);
3568
+ return data;
3569
+ } catch (error) {
3570
+ if (error instanceof IpfsError && error.shouldTriggerCircuitBreaker) {
3571
+ this.cache.recordGatewayFailure(gateway);
3572
+ }
3573
+ throw error;
3574
+ }
3575
+ });
3576
+ try {
3577
+ return await Promise.any(promises);
3578
+ } catch (error) {
3579
+ if (error instanceof AggregateError) {
3580
+ throw new IpfsError(
3581
+ `Fetch failed on all gateways for CID=${cid}`,
3582
+ "NETWORK_ERROR"
3583
+ );
3584
+ }
3585
+ throw error;
3586
+ }
3587
+ }
3588
+ // ---------------------------------------------------------------------------
3589
+ // IPNS Resolution
3590
+ // ---------------------------------------------------------------------------
3591
+ /**
3592
+ * Resolve IPNS via Routing API (returns record with sequence number).
3593
+ * POST /api/v0/routing/get?arg=/ipns/{name}
3594
+ */
3595
+ async resolveIpnsViaRoutingApi(gateway, ipnsName, timeoutMs = DEFAULT_ROUTING_API_TIMEOUT_MS) {
3596
+ try {
3597
+ const response = await this.fetchWithTimeout(
3598
+ `${gateway}/api/v0/routing/get?arg=/ipns/${ipnsName}`,
3599
+ timeoutMs,
3600
+ { method: "POST" }
3601
+ );
3602
+ if (!response.ok) {
3603
+ const body = await response.text().catch(() => "");
3604
+ const category = classifyHttpStatus(response.status, body);
3605
+ if (category === "NOT_FOUND") return null;
3606
+ throw new IpfsError(`Routing API: HTTP ${response.status}`, category, gateway);
3607
+ }
3608
+ const text = await response.text();
3609
+ const parsed = await parseRoutingApiResponse(text);
3610
+ if (!parsed) return null;
3611
+ this.cache.recordGatewaySuccess(gateway);
3612
+ return {
3613
+ cid: parsed.cid,
3614
+ sequence: parsed.sequence,
3615
+ gateway,
3616
+ recordData: parsed.recordData
3617
+ };
3618
+ } catch (error) {
3619
+ if (error instanceof IpfsError) throw error;
3620
+ const category = classifyFetchError(error);
3621
+ if (category !== "NOT_FOUND") {
3622
+ this.cache.recordGatewayFailure(gateway);
3623
+ }
3624
+ return null;
3625
+ }
3626
+ }
3627
+ /**
3628
+ * Resolve IPNS via gateway path (simpler, no sequence number).
3629
+ * GET /ipns/{name}?format=dag-json
3630
+ */
3631
+ async resolveIpnsViaGatewayPath(gateway, ipnsName, timeoutMs = DEFAULT_GATEWAY_PATH_TIMEOUT_MS) {
3632
+ try {
3633
+ const response = await this.fetchWithTimeout(
3634
+ `${gateway}/ipns/${ipnsName}`,
3635
+ timeoutMs,
3636
+ { headers: { Accept: "application/json" } }
3637
+ );
3638
+ if (!response.ok) return null;
3639
+ const content = await response.json();
3640
+ const cidHeader = response.headers.get("X-Ipfs-Path");
3641
+ if (cidHeader) {
3642
+ const match = cidHeader.match(/\/ipfs\/([a-zA-Z0-9]+)/);
3643
+ if (match) {
3644
+ this.cache.recordGatewaySuccess(gateway);
3645
+ return { cid: match[1], content };
3646
+ }
3647
+ }
3648
+ return { cid: "", content };
3649
+ } catch {
3650
+ return null;
3651
+ }
3652
+ }
3653
+ /**
3654
+ * Progressive IPNS resolution across all gateways.
3655
+ * Queries all gateways in parallel, returns highest sequence number.
3656
+ */
3657
+ async resolveIpns(ipnsName, gateways) {
3658
+ const targets = gateways ?? this.getAvailableGateways();
3659
+ if (targets.length === 0) {
3660
+ return { best: null, allResults: [], respondedCount: 0, totalGateways: 0 };
3661
+ }
3662
+ const results = [];
3663
+ let respondedCount = 0;
3664
+ const promises = targets.map(async (gateway) => {
3665
+ const result = await this.resolveIpnsViaRoutingApi(
3666
+ gateway,
3667
+ ipnsName,
3668
+ this.resolveTimeoutMs
3669
+ );
3670
+ if (result) results.push(result);
3671
+ respondedCount++;
3672
+ return result;
3673
+ });
3674
+ await Promise.race([
3675
+ Promise.allSettled(promises),
3676
+ new Promise((resolve) => setTimeout(resolve, this.resolveTimeoutMs + 1e3))
3677
+ ]);
3678
+ let best = null;
3679
+ for (const result of results) {
3680
+ if (!best || result.sequence > best.sequence) {
3681
+ best = result;
3682
+ }
3683
+ }
3684
+ if (best) {
3685
+ this.cache.setIpnsRecord(ipnsName, best);
3686
+ }
3687
+ return {
3688
+ best,
3689
+ allResults: results,
3690
+ respondedCount,
3691
+ totalGateways: targets.length
3692
+ };
3693
+ }
3694
+ // ---------------------------------------------------------------------------
3695
+ // IPNS Publishing
3696
+ // ---------------------------------------------------------------------------
3697
+ /**
3698
+ * Publish IPNS record to a single gateway via routing API.
3699
+ */
3700
+ async publishIpnsViaRoutingApi(gateway, ipnsName, marshalledRecord, timeoutMs = DEFAULT_PUBLISH_TIMEOUT_MS) {
3701
+ try {
3702
+ const formData = new FormData();
3703
+ formData.append(
3704
+ "file",
3705
+ new Blob([new Uint8Array(marshalledRecord)]),
3706
+ "record"
3707
+ );
3708
+ const response = await this.fetchWithTimeout(
3709
+ `${gateway}/api/v0/routing/put?arg=/ipns/${ipnsName}&allow-offline=true`,
3710
+ timeoutMs,
3711
+ { method: "POST", body: formData }
3712
+ );
3713
+ if (!response.ok) {
3714
+ const errorText = await response.text().catch(() => "");
3715
+ throw new IpfsError(
3716
+ `IPNS publish: HTTP ${response.status}: ${errorText.slice(0, 100)}`,
3717
+ classifyHttpStatus(response.status, errorText),
3718
+ gateway
3719
+ );
3720
+ }
3721
+ this.cache.recordGatewaySuccess(gateway);
3722
+ this.log(`IPNS published to ${gateway}: ${ipnsName}`);
3723
+ return true;
3724
+ } catch (error) {
3725
+ if (error instanceof IpfsError && error.shouldTriggerCircuitBreaker) {
3726
+ this.cache.recordGatewayFailure(gateway);
3727
+ }
3728
+ this.log(`IPNS publish to ${gateway} failed: ${error}`);
3729
+ return false;
3730
+ }
3731
+ }
3732
+ /**
3733
+ * Publish IPNS record to all gateways in parallel.
3734
+ */
3735
+ async publishIpns(ipnsName, marshalledRecord, gateways) {
3736
+ const targets = gateways ?? this.getAvailableGateways();
3737
+ if (targets.length === 0) {
3738
+ return { success: false, error: "No gateways available" };
3739
+ }
3740
+ const results = await Promise.allSettled(
3741
+ targets.map((gw) => this.publishIpnsViaRoutingApi(gw, ipnsName, marshalledRecord, this.publishTimeoutMs))
3742
+ );
3743
+ const successfulGateways = [];
3744
+ results.forEach((result, index) => {
3745
+ if (result.status === "fulfilled" && result.value) {
3746
+ successfulGateways.push(targets[index]);
3747
+ }
3748
+ });
3749
+ return {
3750
+ success: successfulGateways.length > 0,
3751
+ ipnsName,
3752
+ successfulGateways,
3753
+ error: successfulGateways.length === 0 ? "All gateways failed" : void 0
3754
+ };
3755
+ }
3756
+ // ---------------------------------------------------------------------------
3757
+ // IPNS Verification
3758
+ // ---------------------------------------------------------------------------
3759
+ /**
3760
+ * Verify IPNS record persistence after publishing.
3761
+ * Retries resolution to confirm the record was accepted.
3762
+ */
3763
+ async verifyIpnsRecord(ipnsName, expectedSeq, expectedCid, retries = 3, delayMs = 1e3) {
3764
+ for (let i = 0; i < retries; i++) {
3765
+ if (i > 0) {
3766
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
3767
+ }
3768
+ const { best } = await this.resolveIpns(ipnsName);
3769
+ if (best && best.sequence >= expectedSeq && best.cid === expectedCid) {
3770
+ return true;
3771
+ }
3772
+ }
3773
+ return false;
3774
+ }
3775
+ // ---------------------------------------------------------------------------
3776
+ // Helpers
3777
+ // ---------------------------------------------------------------------------
3778
+ async fetchWithTimeout(url, timeoutMs, options) {
3779
+ const controller = new AbortController();
3780
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
3781
+ try {
3782
+ return await fetch(url, {
3783
+ ...options,
3784
+ signal: controller.signal
3785
+ });
3786
+ } finally {
3787
+ clearTimeout(timer);
3788
+ }
3789
+ }
3790
+ log(message) {
3791
+ if (this.debug) {
3792
+ console.log(`[IPFS-HTTP] ${message}`);
3793
+ }
3794
+ }
3795
+ };
3796
+
3797
+ // impl/shared/ipfs/txf-merge.ts
3798
+ function mergeTxfData(local, remote) {
3799
+ let added = 0;
3800
+ let removed = 0;
3801
+ let conflicts = 0;
3802
+ const localVersion = local._meta?.version ?? 0;
3803
+ const remoteVersion = remote._meta?.version ?? 0;
3804
+ const baseMeta = localVersion >= remoteVersion ? local._meta : remote._meta;
3805
+ const mergedMeta = {
3806
+ ...baseMeta,
3807
+ version: Math.max(localVersion, remoteVersion) + 1,
3808
+ updatedAt: Date.now()
3809
+ };
3810
+ const mergedTombstones = mergeTombstones(
3811
+ local._tombstones ?? [],
3812
+ remote._tombstones ?? []
3813
+ );
3814
+ const tombstoneKeys = new Set(
3815
+ mergedTombstones.map((t) => `${t.tokenId}:${t.stateHash}`)
3816
+ );
3817
+ const localTokenKeys = getTokenKeys(local);
3818
+ const remoteTokenKeys = getTokenKeys(remote);
3819
+ const allTokenKeys = /* @__PURE__ */ new Set([...localTokenKeys, ...remoteTokenKeys]);
3820
+ const mergedTokens = {};
3821
+ for (const key of allTokenKeys) {
3822
+ const tokenId = key.startsWith("_") ? key.slice(1) : key;
3823
+ const localToken = local[key];
3824
+ const remoteToken = remote[key];
3825
+ if (isTokenTombstoned(tokenId, localToken, remoteToken, tombstoneKeys)) {
3826
+ if (localTokenKeys.has(key)) removed++;
3827
+ continue;
3828
+ }
3829
+ if (localToken && !remoteToken) {
3830
+ mergedTokens[key] = localToken;
3831
+ } else if (!localToken && remoteToken) {
3832
+ mergedTokens[key] = remoteToken;
3833
+ added++;
3834
+ } else if (localToken && remoteToken) {
3835
+ mergedTokens[key] = localToken;
3836
+ conflicts++;
3837
+ }
3838
+ }
3839
+ const mergedOutbox = mergeArrayById(
3840
+ local._outbox ?? [],
3841
+ remote._outbox ?? [],
3842
+ "id"
3843
+ );
3844
+ const mergedSent = mergeArrayById(
3845
+ local._sent ?? [],
3846
+ remote._sent ?? [],
3847
+ "tokenId"
3848
+ );
3849
+ const mergedInvalid = mergeArrayById(
3850
+ local._invalid ?? [],
3851
+ remote._invalid ?? [],
3852
+ "tokenId"
3853
+ );
3854
+ const merged = {
3855
+ _meta: mergedMeta,
3856
+ _tombstones: mergedTombstones.length > 0 ? mergedTombstones : void 0,
3857
+ _outbox: mergedOutbox.length > 0 ? mergedOutbox : void 0,
3858
+ _sent: mergedSent.length > 0 ? mergedSent : void 0,
3859
+ _invalid: mergedInvalid.length > 0 ? mergedInvalid : void 0,
3860
+ ...mergedTokens
3861
+ };
3862
+ return { merged, added, removed, conflicts };
3863
+ }
3864
+ function mergeTombstones(local, remote) {
3865
+ const merged = /* @__PURE__ */ new Map();
3866
+ for (const tombstone of [...local, ...remote]) {
3867
+ const key = `${tombstone.tokenId}:${tombstone.stateHash}`;
3868
+ const existing = merged.get(key);
3869
+ if (!existing || tombstone.timestamp > existing.timestamp) {
3870
+ merged.set(key, tombstone);
3871
+ }
3872
+ }
3873
+ return Array.from(merged.values());
3874
+ }
3875
+ function getTokenKeys(data) {
3876
+ const reservedKeys = /* @__PURE__ */ new Set([
3877
+ "_meta",
3878
+ "_tombstones",
3879
+ "_outbox",
3880
+ "_sent",
3881
+ "_invalid",
3882
+ "_nametag",
3883
+ "_mintOutbox",
3884
+ "_invalidatedNametags",
3885
+ "_integrity"
3886
+ ]);
3887
+ const keys = /* @__PURE__ */ new Set();
3888
+ for (const key of Object.keys(data)) {
3889
+ if (reservedKeys.has(key)) continue;
3890
+ if (key.startsWith("archived-") || key.startsWith("_forked_") || key.startsWith("nametag-")) continue;
3891
+ keys.add(key);
3892
+ }
3893
+ return keys;
3894
+ }
3895
+ function isTokenTombstoned(tokenId, localToken, remoteToken, tombstoneKeys) {
3896
+ for (const key of tombstoneKeys) {
3897
+ if (key.startsWith(`${tokenId}:`)) {
3898
+ return true;
3899
+ }
3900
+ }
3901
+ void localToken;
3902
+ void remoteToken;
3903
+ return false;
3904
+ }
3905
+ function mergeArrayById(local, remote, idField) {
3906
+ const seen = /* @__PURE__ */ new Map();
3907
+ for (const item of local) {
3908
+ const id = item[idField];
3909
+ if (id !== void 0) {
3910
+ seen.set(id, item);
3911
+ }
3912
+ }
3913
+ for (const item of remote) {
3914
+ const id = item[idField];
3915
+ if (id !== void 0 && !seen.has(id)) {
3916
+ seen.set(id, item);
3917
+ }
3918
+ }
3919
+ return Array.from(seen.values());
3920
+ }
3921
+
3922
+ // impl/shared/ipfs/ipns-subscription-client.ts
3923
+ var IpnsSubscriptionClient = class {
3924
+ ws = null;
3925
+ subscriptions = /* @__PURE__ */ new Map();
3926
+ reconnectTimeout = null;
3927
+ pingInterval = null;
3928
+ fallbackPollInterval = null;
3929
+ wsUrl;
3930
+ createWebSocket;
3931
+ pingIntervalMs;
3932
+ initialReconnectDelayMs;
3933
+ maxReconnectDelayMs;
3934
+ debugEnabled;
3935
+ reconnectAttempts = 0;
3936
+ isConnecting = false;
3937
+ connectionOpenedAt = 0;
3938
+ destroyed = false;
3939
+ /** Minimum stable connection time before resetting backoff (30 seconds) */
3940
+ minStableConnectionMs = 3e4;
3941
+ fallbackPollFn = null;
3942
+ fallbackPollIntervalMs = 0;
3943
+ constructor(config) {
3944
+ this.wsUrl = config.wsUrl;
3945
+ this.createWebSocket = config.createWebSocket;
3946
+ this.pingIntervalMs = config.pingIntervalMs ?? 3e4;
3947
+ this.initialReconnectDelayMs = config.reconnectDelayMs ?? 5e3;
3948
+ this.maxReconnectDelayMs = config.maxReconnectDelayMs ?? 6e4;
3949
+ this.debugEnabled = config.debug ?? false;
3950
+ }
3951
+ // ---------------------------------------------------------------------------
3952
+ // Public API
3953
+ // ---------------------------------------------------------------------------
3954
+ /**
3955
+ * Subscribe to IPNS updates for a specific name.
3956
+ * Automatically connects the WebSocket if not already connected.
3957
+ * If WebSocket is connecting, the name will be subscribed once connected.
3958
+ */
3959
+ subscribe(ipnsName, callback) {
3960
+ if (!ipnsName || typeof ipnsName !== "string") {
3961
+ this.log("Invalid IPNS name for subscription");
3962
+ return () => {
3963
+ };
3964
+ }
3965
+ const isNewSubscription = !this.subscriptions.has(ipnsName);
3966
+ if (isNewSubscription) {
3967
+ this.subscriptions.set(ipnsName, /* @__PURE__ */ new Set());
3968
+ }
3969
+ this.subscriptions.get(ipnsName).add(callback);
3970
+ if (isNewSubscription && this.ws?.readyState === WebSocketReadyState.OPEN) {
3971
+ this.sendSubscribe([ipnsName]);
3972
+ }
3973
+ if (!this.ws || this.ws.readyState !== WebSocketReadyState.OPEN) {
3974
+ this.connect();
3975
+ }
3976
+ return () => {
3977
+ const callbacks = this.subscriptions.get(ipnsName);
3978
+ if (callbacks) {
3979
+ callbacks.delete(callback);
3980
+ if (callbacks.size === 0) {
3981
+ this.subscriptions.delete(ipnsName);
3982
+ if (this.ws?.readyState === WebSocketReadyState.OPEN) {
3983
+ this.sendUnsubscribe([ipnsName]);
3984
+ }
3985
+ if (this.subscriptions.size === 0) {
3986
+ this.disconnect();
3987
+ }
3988
+ }
3989
+ }
3990
+ };
3991
+ }
3992
+ /**
3993
+ * Register a convenience update callback for all subscriptions.
3994
+ * Returns an unsubscribe function.
3995
+ */
3996
+ onUpdate(callback) {
3997
+ if (!this.subscriptions.has("*")) {
3998
+ this.subscriptions.set("*", /* @__PURE__ */ new Set());
3999
+ }
4000
+ this.subscriptions.get("*").add(callback);
4001
+ return () => {
4002
+ const callbacks = this.subscriptions.get("*");
4003
+ if (callbacks) {
4004
+ callbacks.delete(callback);
4005
+ if (callbacks.size === 0) {
4006
+ this.subscriptions.delete("*");
4007
+ }
4008
+ }
4009
+ };
4010
+ }
4011
+ /**
4012
+ * Set a fallback poll function to use when WebSocket is disconnected.
4013
+ * The poll function will be called at the specified interval while WS is down.
4014
+ */
4015
+ setFallbackPoll(fn, intervalMs) {
4016
+ this.fallbackPollFn = fn;
4017
+ this.fallbackPollIntervalMs = intervalMs;
4018
+ if (!this.isConnected()) {
4019
+ this.startFallbackPolling();
4020
+ }
4021
+ }
4022
+ /**
4023
+ * Connect to the WebSocket server.
4024
+ */
4025
+ connect() {
4026
+ if (this.destroyed) return;
4027
+ if (this.ws?.readyState === WebSocketReadyState.OPEN || this.isConnecting) {
4028
+ return;
4029
+ }
4030
+ this.isConnecting = true;
4031
+ try {
4032
+ this.log(`Connecting to ${this.wsUrl}...`);
4033
+ this.ws = this.createWebSocket(this.wsUrl);
4034
+ this.ws.onopen = () => {
4035
+ this.log("WebSocket connected");
4036
+ this.isConnecting = false;
4037
+ this.connectionOpenedAt = Date.now();
4038
+ const names = Array.from(this.subscriptions.keys()).filter((n) => n !== "*");
4039
+ if (names.length > 0) {
4040
+ this.sendSubscribe(names);
4041
+ }
4042
+ this.startPingInterval();
4043
+ this.stopFallbackPolling();
4044
+ };
4045
+ this.ws.onmessage = (event) => {
4046
+ this.handleMessage(event.data);
4047
+ };
4048
+ this.ws.onclose = () => {
4049
+ const connectionDuration = this.connectionOpenedAt > 0 ? Date.now() - this.connectionOpenedAt : 0;
4050
+ const wasStable = connectionDuration >= this.minStableConnectionMs;
4051
+ this.log(`WebSocket closed (duration: ${Math.round(connectionDuration / 1e3)}s)`);
4052
+ this.isConnecting = false;
4053
+ this.connectionOpenedAt = 0;
4054
+ this.stopPingInterval();
4055
+ if (wasStable) {
4056
+ this.reconnectAttempts = 0;
4057
+ }
4058
+ this.startFallbackPolling();
4059
+ this.scheduleReconnect();
4060
+ };
4061
+ this.ws.onerror = () => {
4062
+ this.log("WebSocket error");
4063
+ this.isConnecting = false;
4064
+ };
4065
+ } catch (e) {
4066
+ this.log(`Failed to connect: ${e}`);
4067
+ this.isConnecting = false;
4068
+ this.startFallbackPolling();
4069
+ this.scheduleReconnect();
4070
+ }
4071
+ }
4072
+ /**
4073
+ * Disconnect from the WebSocket server and clean up.
4074
+ */
4075
+ disconnect() {
4076
+ this.destroyed = true;
4077
+ if (this.reconnectTimeout) {
4078
+ clearTimeout(this.reconnectTimeout);
4079
+ this.reconnectTimeout = null;
4080
+ }
4081
+ this.stopPingInterval();
4082
+ this.stopFallbackPolling();
4083
+ if (this.ws) {
4084
+ this.ws.onopen = null;
4085
+ this.ws.onclose = null;
4086
+ this.ws.onerror = null;
4087
+ this.ws.onmessage = null;
4088
+ this.ws.close();
4089
+ this.ws = null;
4090
+ }
4091
+ this.isConnecting = false;
4092
+ this.reconnectAttempts = 0;
4093
+ }
4094
+ /**
4095
+ * Check if connected to the WebSocket server.
4096
+ */
4097
+ isConnected() {
4098
+ return this.ws?.readyState === WebSocketReadyState.OPEN;
4099
+ }
4100
+ // ---------------------------------------------------------------------------
4101
+ // Internal: Message Handling
4102
+ // ---------------------------------------------------------------------------
4103
+ handleMessage(data) {
4104
+ try {
4105
+ const message = JSON.parse(data);
4106
+ switch (message.type) {
4107
+ case "update":
4108
+ if (message.name && message.sequence !== void 0) {
4109
+ this.notifySubscribers({
4110
+ type: "update",
4111
+ name: message.name,
4112
+ sequence: message.sequence,
4113
+ cid: message.cid ?? "",
4114
+ timestamp: message.timestamp || (/* @__PURE__ */ new Date()).toISOString()
4115
+ });
4116
+ }
4117
+ break;
4118
+ case "subscribed":
4119
+ this.log(`Subscribed to ${message.names?.length || 0} names`);
4120
+ break;
4121
+ case "unsubscribed":
4122
+ this.log(`Unsubscribed from ${message.names?.length || 0} names`);
4123
+ break;
4124
+ case "pong":
4125
+ break;
4126
+ case "error":
4127
+ this.log(`Server error: ${message.message}`);
4128
+ break;
4129
+ default:
4130
+ break;
4131
+ }
4132
+ } catch {
4133
+ this.log("Failed to parse message");
4134
+ }
4135
+ }
4136
+ notifySubscribers(update) {
4137
+ const callbacks = this.subscriptions.get(update.name);
4138
+ if (callbacks) {
4139
+ this.log(`Update: ${update.name.slice(0, 16)}... seq=${update.sequence}`);
4140
+ for (const callback of callbacks) {
4141
+ try {
4142
+ callback(update);
4143
+ } catch {
4144
+ }
4145
+ }
4146
+ }
4147
+ const globalCallbacks = this.subscriptions.get("*");
4148
+ if (globalCallbacks) {
4149
+ for (const callback of globalCallbacks) {
4150
+ try {
4151
+ callback(update);
4152
+ } catch {
4153
+ }
4154
+ }
4155
+ }
4156
+ }
4157
+ // ---------------------------------------------------------------------------
4158
+ // Internal: WebSocket Send
4159
+ // ---------------------------------------------------------------------------
4160
+ sendSubscribe(names) {
4161
+ if (this.ws?.readyState === WebSocketReadyState.OPEN) {
4162
+ this.ws.send(JSON.stringify({ action: "subscribe", names }));
4163
+ }
4164
+ }
4165
+ sendUnsubscribe(names) {
4166
+ if (this.ws?.readyState === WebSocketReadyState.OPEN) {
4167
+ this.ws.send(JSON.stringify({ action: "unsubscribe", names }));
4168
+ }
4169
+ }
4170
+ // ---------------------------------------------------------------------------
4171
+ // Internal: Reconnection
4172
+ // ---------------------------------------------------------------------------
4173
+ /**
4174
+ * Schedule reconnection with exponential backoff.
4175
+ * Sequence: 5s, 10s, 20s, 40s, 60s (capped)
4176
+ */
4177
+ scheduleReconnect() {
4178
+ if (this.destroyed || this.reconnectTimeout) return;
4179
+ const realSubscriptions = Array.from(this.subscriptions.keys()).filter((n) => n !== "*");
4180
+ if (realSubscriptions.length === 0) return;
4181
+ this.reconnectAttempts++;
4182
+ const delay = Math.min(
4183
+ this.initialReconnectDelayMs * Math.pow(2, this.reconnectAttempts - 1),
4184
+ this.maxReconnectDelayMs
4185
+ );
4186
+ this.log(`Reconnecting in ${(delay / 1e3).toFixed(1)}s (attempt ${this.reconnectAttempts})...`);
4187
+ this.reconnectTimeout = setTimeout(() => {
4188
+ this.reconnectTimeout = null;
4189
+ this.connect();
4190
+ }, delay);
4191
+ }
4192
+ // ---------------------------------------------------------------------------
4193
+ // Internal: Keepalive
4194
+ // ---------------------------------------------------------------------------
4195
+ startPingInterval() {
4196
+ this.stopPingInterval();
4197
+ this.pingInterval = setInterval(() => {
4198
+ if (this.ws?.readyState === WebSocketReadyState.OPEN) {
4199
+ this.ws.send(JSON.stringify({ action: "ping" }));
4200
+ }
4201
+ }, this.pingIntervalMs);
4202
+ }
4203
+ stopPingInterval() {
4204
+ if (this.pingInterval) {
4205
+ clearInterval(this.pingInterval);
4206
+ this.pingInterval = null;
4207
+ }
4208
+ }
4209
+ // ---------------------------------------------------------------------------
4210
+ // Internal: Fallback Polling
4211
+ // ---------------------------------------------------------------------------
4212
+ startFallbackPolling() {
4213
+ if (this.fallbackPollInterval || !this.fallbackPollFn || this.destroyed) return;
4214
+ this.log(`Starting fallback polling (${this.fallbackPollIntervalMs / 1e3}s interval)`);
4215
+ this.fallbackPollFn().catch(() => {
4216
+ });
4217
+ this.fallbackPollInterval = setInterval(() => {
4218
+ this.fallbackPollFn?.().catch(() => {
4219
+ });
4220
+ }, this.fallbackPollIntervalMs);
4221
+ }
4222
+ stopFallbackPolling() {
4223
+ if (this.fallbackPollInterval) {
4224
+ clearInterval(this.fallbackPollInterval);
4225
+ this.fallbackPollInterval = null;
4226
+ }
4227
+ }
4228
+ // ---------------------------------------------------------------------------
4229
+ // Internal: Logging
4230
+ // ---------------------------------------------------------------------------
4231
+ log(message) {
4232
+ if (this.debugEnabled) {
4233
+ console.log(`[IPNS-WS] ${message}`);
4234
+ }
4235
+ }
4236
+ };
4237
+
4238
+ // impl/shared/ipfs/write-behind-buffer.ts
4239
+ var AsyncSerialQueue = class {
4240
+ tail = Promise.resolve();
4241
+ /** Enqueue an async operation. Returns when it completes. */
4242
+ enqueue(fn) {
4243
+ let resolve;
4244
+ let reject;
4245
+ const promise = new Promise((res, rej) => {
4246
+ resolve = res;
4247
+ reject = rej;
4248
+ });
4249
+ this.tail = this.tail.then(
4250
+ () => fn().then(resolve, reject),
4251
+ () => fn().then(resolve, reject)
4252
+ );
4253
+ return promise;
4254
+ }
4255
+ };
4256
+ var WriteBuffer = class {
4257
+ /** Full TXF data from save() calls — latest wins */
4258
+ txfData = null;
4259
+ /** Individual token mutations: key -> { op: 'save'|'delete', data? } */
4260
+ tokenMutations = /* @__PURE__ */ new Map();
4261
+ get isEmpty() {
4262
+ return this.txfData === null && this.tokenMutations.size === 0;
4263
+ }
4264
+ clear() {
4265
+ this.txfData = null;
4266
+ this.tokenMutations.clear();
4267
+ }
4268
+ /**
4269
+ * Merge another buffer's contents into this one (for rollback).
4270
+ * Existing (newer) mutations in `this` take precedence over `other`.
4271
+ */
4272
+ mergeFrom(other) {
4273
+ if (other.txfData && !this.txfData) {
4274
+ this.txfData = other.txfData;
4275
+ }
4276
+ for (const [id, mutation] of other.tokenMutations) {
4277
+ if (!this.tokenMutations.has(id)) {
4278
+ this.tokenMutations.set(id, mutation);
4279
+ }
4280
+ }
4281
+ }
4282
+ };
4283
+
4284
+ // impl/shared/ipfs/ipfs-storage-provider.ts
4285
+ var IpfsStorageProvider = class {
4286
+ id = "ipfs";
4287
+ name = "IPFS Storage";
4288
+ type = "p2p";
4289
+ status = "disconnected";
4290
+ identity = null;
4291
+ ipnsKeyPair = null;
4292
+ ipnsName = null;
4293
+ ipnsSequenceNumber = 0n;
4294
+ lastCid = null;
4295
+ lastKnownRemoteSequence = 0n;
4296
+ dataVersion = 0;
4297
+ /**
4298
+ * The CID currently stored on the sidecar for this IPNS name.
4299
+ * Used as `_meta.lastCid` in the next save to satisfy chain validation.
4300
+ * - null for bootstrap (first-ever save)
4301
+ * - set after every successful save() or load()
4302
+ */
4303
+ remoteCid = null;
4304
+ cache;
4305
+ httpClient;
4306
+ statePersistence;
4307
+ eventCallbacks = /* @__PURE__ */ new Set();
4308
+ debug;
4309
+ ipnsLifetimeMs;
4310
+ /** WebSocket factory for push subscriptions */
4311
+ createWebSocket;
4312
+ /** Override WS URL */
4313
+ wsUrl;
4314
+ /** Fallback poll interval (default: 90000) */
4315
+ fallbackPollIntervalMs;
4316
+ /** IPNS subscription client for push notifications */
4317
+ subscriptionClient = null;
4318
+ /** Unsubscribe function from subscription client */
4319
+ subscriptionUnsubscribe = null;
4320
+ /** In-memory buffer for individual token save/delete calls */
4321
+ tokenBuffer = /* @__PURE__ */ new Map();
4322
+ deletedTokenIds = /* @__PURE__ */ new Set();
4323
+ /** Write-behind buffer: serializes flush / sync / shutdown */
4324
+ flushQueue = new AsyncSerialQueue();
4325
+ /** Pending mutations not yet flushed to IPFS */
4326
+ pendingBuffer = new WriteBuffer();
4327
+ /** Debounce timer for background flush */
4328
+ flushTimer = null;
4329
+ /** Debounce interval in ms */
4330
+ flushDebounceMs;
4331
+ /** Set to true during shutdown to prevent new flushes */
4332
+ isShuttingDown = false;
4333
+ constructor(config, statePersistence) {
4334
+ const gateways = config?.gateways ?? getIpfsGatewayUrls();
4335
+ this.debug = config?.debug ?? false;
4336
+ this.ipnsLifetimeMs = config?.ipnsLifetimeMs ?? 99 * 365 * 24 * 60 * 60 * 1e3;
4337
+ this.flushDebounceMs = config?.flushDebounceMs ?? 2e3;
4338
+ this.cache = new IpfsCache({
4339
+ ipnsTtlMs: config?.ipnsCacheTtlMs,
4340
+ failureCooldownMs: config?.circuitBreakerCooldownMs,
4341
+ failureThreshold: config?.circuitBreakerThreshold,
4342
+ knownFreshWindowMs: config?.knownFreshWindowMs
4343
+ });
4344
+ this.httpClient = new IpfsHttpClient({
4345
+ gateways,
4346
+ fetchTimeoutMs: config?.fetchTimeoutMs,
4347
+ resolveTimeoutMs: config?.resolveTimeoutMs,
4348
+ publishTimeoutMs: config?.publishTimeoutMs,
4349
+ connectivityTimeoutMs: config?.connectivityTimeoutMs,
4350
+ debug: this.debug
4351
+ }, this.cache);
4352
+ this.statePersistence = statePersistence ?? new InMemoryIpfsStatePersistence();
4353
+ this.createWebSocket = config?.createWebSocket;
4354
+ this.wsUrl = config?.wsUrl;
4355
+ this.fallbackPollIntervalMs = config?.fallbackPollIntervalMs ?? 9e4;
4356
+ }
4357
+ // ---------------------------------------------------------------------------
4358
+ // BaseProvider interface
4359
+ // ---------------------------------------------------------------------------
4360
+ async connect() {
4361
+ await this.initialize();
4362
+ }
4363
+ async disconnect() {
4364
+ await this.shutdown();
4365
+ }
4366
+ isConnected() {
4367
+ return this.status === "connected";
4368
+ }
4369
+ getStatus() {
4370
+ return this.status;
4371
+ }
4372
+ // ---------------------------------------------------------------------------
4373
+ // Identity & Initialization
4374
+ // ---------------------------------------------------------------------------
4375
+ setIdentity(identity) {
4376
+ this.identity = identity;
4377
+ }
4378
+ async initialize() {
4379
+ if (!this.identity) {
4380
+ this.log("Cannot initialize: no identity set");
4381
+ return false;
4382
+ }
4383
+ this.status = "connecting";
4384
+ this.emitEvent({ type: "storage:loading", timestamp: Date.now() });
4385
+ try {
4386
+ const { keyPair, ipnsName } = await deriveIpnsIdentity(this.identity.privateKey);
4387
+ this.ipnsKeyPair = keyPair;
4388
+ this.ipnsName = ipnsName;
4389
+ this.log(`IPNS name derived: ${ipnsName}`);
4390
+ const persisted = await this.statePersistence.load(ipnsName);
4391
+ if (persisted) {
4392
+ this.ipnsSequenceNumber = BigInt(persisted.sequenceNumber);
4393
+ this.lastCid = persisted.lastCid;
4394
+ this.remoteCid = persisted.lastCid;
4395
+ this.dataVersion = persisted.version;
4396
+ this.log(`Loaded persisted state: seq=${this.ipnsSequenceNumber}, cid=${this.lastCid}`);
4397
+ }
4398
+ if (this.createWebSocket) {
4399
+ try {
4400
+ const wsUrlFinal = this.wsUrl ?? this.deriveWsUrl();
4401
+ if (wsUrlFinal) {
4402
+ this.subscriptionClient = new IpnsSubscriptionClient({
4403
+ wsUrl: wsUrlFinal,
4404
+ createWebSocket: this.createWebSocket,
4405
+ debug: this.debug
4406
+ });
4407
+ this.subscriptionUnsubscribe = this.subscriptionClient.subscribe(
4408
+ ipnsName,
4409
+ (update) => {
4410
+ this.log(`Push update: seq=${update.sequence}, cid=${update.cid}`);
4411
+ this.emitEvent({
4412
+ type: "storage:remote-updated",
4413
+ timestamp: Date.now(),
4414
+ data: { name: update.name, sequence: update.sequence, cid: update.cid }
4415
+ });
4416
+ }
4417
+ );
4418
+ this.subscriptionClient.setFallbackPoll(
4419
+ () => this.pollForRemoteChanges(),
4420
+ this.fallbackPollIntervalMs
4421
+ );
4422
+ this.subscriptionClient.connect();
4423
+ }
4424
+ } catch (wsError) {
4425
+ this.log(`Failed to set up IPNS subscription: ${wsError}`);
4426
+ }
4427
+ }
4428
+ this.httpClient.findHealthyGateways().then((healthy) => {
4429
+ if (healthy.length > 0) {
4430
+ this.log(`${healthy.length} healthy gateway(s) found`);
4431
+ } else {
4432
+ this.log("Warning: no healthy gateways found");
4433
+ }
4434
+ }).catch(() => {
4435
+ });
4436
+ this.isShuttingDown = false;
4437
+ this.status = "connected";
4438
+ this.emitEvent({ type: "storage:loaded", timestamp: Date.now() });
4439
+ return true;
4440
+ } catch (error) {
4441
+ this.status = "error";
4442
+ this.emitEvent({
4443
+ type: "storage:error",
4444
+ timestamp: Date.now(),
4445
+ error: error instanceof Error ? error.message : String(error)
4446
+ });
4447
+ return false;
4448
+ }
4449
+ }
4450
+ async shutdown() {
4451
+ this.isShuttingDown = true;
4452
+ if (this.flushTimer) {
4453
+ clearTimeout(this.flushTimer);
4454
+ this.flushTimer = null;
4455
+ }
4456
+ await this.flushQueue.enqueue(async () => {
4457
+ if (!this.pendingBuffer.isEmpty) {
4458
+ try {
4459
+ await this.executeFlush();
4460
+ } catch {
4461
+ this.log("Final flush on shutdown failed (data may be lost)");
4462
+ }
4463
+ }
4464
+ });
4465
+ if (this.subscriptionUnsubscribe) {
4466
+ this.subscriptionUnsubscribe();
4467
+ this.subscriptionUnsubscribe = null;
4468
+ }
4469
+ if (this.subscriptionClient) {
4470
+ this.subscriptionClient.disconnect();
4471
+ this.subscriptionClient = null;
4472
+ }
4473
+ this.cache.clear();
4474
+ this.status = "disconnected";
4475
+ }
4476
+ // ---------------------------------------------------------------------------
4477
+ // Save (non-blocking — buffers data for async flush)
4478
+ // ---------------------------------------------------------------------------
4479
+ async save(data) {
4480
+ if (!this.ipnsKeyPair || !this.ipnsName) {
4481
+ return { success: false, error: "Not initialized", timestamp: Date.now() };
4482
+ }
4483
+ this.pendingBuffer.txfData = data;
4484
+ this.scheduleFlush();
4485
+ return { success: true, timestamp: Date.now() };
4486
+ }
4487
+ // ---------------------------------------------------------------------------
4488
+ // Internal: Blocking save (used by sync and executeFlush)
4489
+ // ---------------------------------------------------------------------------
4490
+ /**
4491
+ * Perform the actual upload + IPNS publish synchronously.
4492
+ * Called by executeFlush() and sync() — never by public save().
4493
+ */
4494
+ async _doSave(data) {
4495
+ if (!this.ipnsKeyPair || !this.ipnsName) {
4496
+ return { success: false, error: "Not initialized", timestamp: Date.now() };
4497
+ }
4498
+ this.emitEvent({ type: "storage:saving", timestamp: Date.now() });
4499
+ try {
4500
+ this.dataVersion++;
4501
+ const metaUpdate = {
4502
+ ...data._meta,
4503
+ version: this.dataVersion,
4504
+ ipnsName: this.ipnsName,
4505
+ updatedAt: Date.now()
4506
+ };
4507
+ if (this.remoteCid) {
4508
+ metaUpdate.lastCid = this.remoteCid;
4509
+ }
4510
+ const updatedData = { ...data, _meta: metaUpdate };
4511
+ for (const [tokenId, tokenData] of this.tokenBuffer) {
4512
+ if (!this.deletedTokenIds.has(tokenId)) {
4513
+ updatedData[tokenId] = tokenData;
4514
+ }
4515
+ }
4516
+ for (const tokenId of this.deletedTokenIds) {
4517
+ delete updatedData[tokenId];
4518
+ }
4519
+ const { cid } = await this.httpClient.upload(updatedData);
4520
+ this.log(`Content uploaded: CID=${cid}`);
4521
+ const baseSeq = this.ipnsSequenceNumber > this.lastKnownRemoteSequence ? this.ipnsSequenceNumber : this.lastKnownRemoteSequence;
4522
+ const newSeq = baseSeq + 1n;
4523
+ const marshalledRecord = await createSignedRecord(
4524
+ this.ipnsKeyPair,
4525
+ cid,
4526
+ newSeq,
4527
+ this.ipnsLifetimeMs
4528
+ );
4529
+ const publishResult = await this.httpClient.publishIpns(
4530
+ this.ipnsName,
4531
+ marshalledRecord
4532
+ );
4533
+ if (!publishResult.success) {
4534
+ this.dataVersion--;
4535
+ this.log(`IPNS publish failed: ${publishResult.error}`);
4536
+ return {
4537
+ success: false,
4538
+ error: publishResult.error ?? "IPNS publish failed",
4539
+ timestamp: Date.now()
4540
+ };
4541
+ }
4542
+ this.ipnsSequenceNumber = newSeq;
4543
+ this.lastCid = cid;
4544
+ this.remoteCid = cid;
4545
+ this.cache.setIpnsRecord(this.ipnsName, {
4546
+ cid,
4547
+ sequence: newSeq,
4548
+ gateway: "local"
4549
+ });
4550
+ this.cache.setContent(cid, updatedData);
4551
+ this.cache.markIpnsFresh(this.ipnsName);
4552
+ await this.statePersistence.save(this.ipnsName, {
4553
+ sequenceNumber: newSeq.toString(),
4554
+ lastCid: cid,
4555
+ version: this.dataVersion
4556
+ });
4557
+ this.deletedTokenIds.clear();
4558
+ this.emitEvent({
4559
+ type: "storage:saved",
4560
+ timestamp: Date.now(),
4561
+ data: { cid, sequence: newSeq.toString() }
4562
+ });
4563
+ this.log(`Saved: CID=${cid}, seq=${newSeq}`);
4564
+ return { success: true, cid, timestamp: Date.now() };
4565
+ } catch (error) {
4566
+ this.dataVersion--;
4567
+ const errorMessage = error instanceof Error ? error.message : String(error);
4568
+ this.emitEvent({
4569
+ type: "storage:error",
4570
+ timestamp: Date.now(),
4571
+ error: errorMessage
4572
+ });
4573
+ return { success: false, error: errorMessage, timestamp: Date.now() };
4574
+ }
4575
+ }
4576
+ // ---------------------------------------------------------------------------
4577
+ // Write-behind buffer: scheduling and flushing
4578
+ // ---------------------------------------------------------------------------
4579
+ /**
4580
+ * Schedule a debounced background flush.
4581
+ * Resets the timer on each call so rapid mutations coalesce.
4582
+ */
4583
+ scheduleFlush() {
4584
+ if (this.isShuttingDown) return;
4585
+ if (this.flushTimer) clearTimeout(this.flushTimer);
4586
+ this.flushTimer = setTimeout(() => {
4587
+ this.flushTimer = null;
4588
+ this.flushQueue.enqueue(() => this.executeFlush()).catch((err) => {
4589
+ this.log(`Background flush failed: ${err}`);
4590
+ });
4591
+ }, this.flushDebounceMs);
4592
+ }
4593
+ /**
4594
+ * Execute a flush of the pending buffer to IPFS.
4595
+ * Runs inside AsyncSerialQueue for concurrency safety.
4596
+ */
4597
+ async executeFlush() {
4598
+ if (this.pendingBuffer.isEmpty) return;
4599
+ const active = this.pendingBuffer;
4600
+ this.pendingBuffer = new WriteBuffer();
4601
+ try {
4602
+ const baseData = active.txfData ?? {
4603
+ _meta: { version: 0, address: this.identity?.directAddress ?? "", formatVersion: "2.0", updatedAt: 0 }
4604
+ };
4605
+ const result = await this._doSave(baseData);
4606
+ if (!result.success) {
4607
+ throw new Error(result.error ?? "Save failed");
4608
+ }
4609
+ this.log(`Flushed successfully: CID=${result.cid}`);
4610
+ } catch (error) {
4611
+ this.pendingBuffer.mergeFrom(active);
4612
+ const msg = error instanceof Error ? error.message : String(error);
4613
+ this.log(`Flush failed (will retry): ${msg}`);
4614
+ this.scheduleFlush();
4615
+ throw error;
4616
+ }
4617
+ }
4618
+ // ---------------------------------------------------------------------------
4619
+ // Load
4620
+ // ---------------------------------------------------------------------------
4621
+ async load(identifier) {
4622
+ if (!this.ipnsName && !identifier) {
4623
+ return { success: false, error: "Not initialized", source: "local", timestamp: Date.now() };
4624
+ }
4625
+ this.emitEvent({ type: "storage:loading", timestamp: Date.now() });
4626
+ try {
4627
+ if (identifier) {
4628
+ const data2 = await this.httpClient.fetchContent(identifier);
4629
+ return { success: true, data: data2, source: "remote", timestamp: Date.now() };
4630
+ }
4631
+ const ipnsName = this.ipnsName;
4632
+ if (this.cache.isIpnsKnownFresh(ipnsName)) {
4633
+ const cached = this.cache.getIpnsRecordIgnoreTtl(ipnsName);
4634
+ if (cached) {
4635
+ const content = this.cache.getContent(cached.cid);
4636
+ if (content) {
4637
+ this.log("Using known-fresh cached data");
4638
+ return { success: true, data: content, source: "cache", timestamp: Date.now() };
4639
+ }
4640
+ }
4641
+ }
4642
+ const cachedRecord = this.cache.getIpnsRecord(ipnsName);
4643
+ if (cachedRecord) {
4644
+ const content = this.cache.getContent(cachedRecord.cid);
4645
+ if (content) {
4646
+ this.log("IPNS cache hit");
4647
+ return { success: true, data: content, source: "cache", timestamp: Date.now() };
4648
+ }
4649
+ try {
4650
+ const data2 = await this.httpClient.fetchContent(cachedRecord.cid);
4651
+ return { success: true, data: data2, source: "remote", timestamp: Date.now() };
4652
+ } catch {
4653
+ }
4654
+ }
4655
+ const { best } = await this.httpClient.resolveIpns(ipnsName);
4656
+ if (!best) {
4657
+ this.log("IPNS record not found (new wallet?)");
4658
+ return { success: false, error: "IPNS record not found", source: "remote", timestamp: Date.now() };
4659
+ }
4660
+ if (best.sequence > this.lastKnownRemoteSequence) {
4661
+ this.lastKnownRemoteSequence = best.sequence;
4662
+ }
4663
+ this.remoteCid = best.cid;
4664
+ const data = await this.httpClient.fetchContent(best.cid);
4665
+ const remoteVersion = data?._meta?.version;
4666
+ if (typeof remoteVersion === "number" && remoteVersion > this.dataVersion) {
4667
+ this.dataVersion = remoteVersion;
4668
+ }
4669
+ this.populateTokenBuffer(data);
4670
+ this.emitEvent({
4671
+ type: "storage:loaded",
4672
+ timestamp: Date.now(),
4673
+ data: { cid: best.cid, sequence: best.sequence.toString() }
4674
+ });
4675
+ return { success: true, data, source: "remote", timestamp: Date.now() };
4676
+ } catch (error) {
4677
+ if (this.ipnsName) {
4678
+ const cached = this.cache.getIpnsRecordIgnoreTtl(this.ipnsName);
4679
+ if (cached) {
4680
+ const content = this.cache.getContent(cached.cid);
4681
+ if (content) {
4682
+ this.log("Network error, returning stale cache");
4683
+ return { success: true, data: content, source: "cache", timestamp: Date.now() };
4684
+ }
4685
+ }
4686
+ }
4687
+ const errorMessage = error instanceof Error ? error.message : String(error);
4688
+ this.emitEvent({
4689
+ type: "storage:error",
4690
+ timestamp: Date.now(),
4691
+ error: errorMessage
4692
+ });
4693
+ return { success: false, error: errorMessage, source: "remote", timestamp: Date.now() };
4694
+ }
4695
+ }
4696
+ // ---------------------------------------------------------------------------
4697
+ // Sync (enters serial queue to avoid concurrent IPNS conflicts)
4698
+ // ---------------------------------------------------------------------------
4699
+ async sync(localData) {
4700
+ return this.flushQueue.enqueue(async () => {
4701
+ if (this.flushTimer) {
4702
+ clearTimeout(this.flushTimer);
4703
+ this.flushTimer = null;
4704
+ }
4705
+ this.emitEvent({ type: "sync:started", timestamp: Date.now() });
4706
+ try {
4707
+ this.pendingBuffer.clear();
4708
+ const remoteResult = await this.load();
4709
+ if (!remoteResult.success || !remoteResult.data) {
4710
+ this.log("No remote data found, uploading local data");
4711
+ const saveResult2 = await this._doSave(localData);
4712
+ this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
4713
+ return {
4714
+ success: saveResult2.success,
4715
+ merged: this.enrichWithTokenBuffer(localData),
4716
+ added: 0,
4717
+ removed: 0,
4718
+ conflicts: 0,
4719
+ error: saveResult2.error
4720
+ };
4721
+ }
4722
+ const remoteData = remoteResult.data;
4723
+ const localVersion = localData._meta?.version ?? 0;
4724
+ const remoteVersion = remoteData._meta?.version ?? 0;
4725
+ if (localVersion === remoteVersion && this.lastCid) {
4726
+ this.log("Data is in sync (same version)");
4727
+ this.emitEvent({ type: "sync:completed", timestamp: Date.now() });
4728
+ return {
4729
+ success: true,
4730
+ merged: this.enrichWithTokenBuffer(localData),
4731
+ added: 0,
4732
+ removed: 0,
4733
+ conflicts: 0
4734
+ };
4735
+ }
4736
+ this.log(`Merging: local v${localVersion} <-> remote v${remoteVersion}`);
4737
+ const { merged, added, removed, conflicts } = mergeTxfData(localData, remoteData);
4738
+ if (conflicts > 0) {
4739
+ this.emitEvent({
4740
+ type: "sync:conflict",
4741
+ timestamp: Date.now(),
4742
+ data: { conflicts }
4743
+ });
4744
+ }
4745
+ const saveResult = await this._doSave(merged);
4746
+ this.emitEvent({
4747
+ type: "sync:completed",
4748
+ timestamp: Date.now(),
4749
+ data: { added, removed, conflicts }
4750
+ });
4751
+ return {
4752
+ success: saveResult.success,
4753
+ merged: this.enrichWithTokenBuffer(merged),
4754
+ added,
4755
+ removed,
4756
+ conflicts,
4757
+ error: saveResult.error
4758
+ };
4759
+ } catch (error) {
4760
+ const errorMessage = error instanceof Error ? error.message : String(error);
4761
+ this.emitEvent({
4762
+ type: "sync:error",
4763
+ timestamp: Date.now(),
4764
+ error: errorMessage
4765
+ });
4766
+ return {
4767
+ success: false,
4768
+ added: 0,
4769
+ removed: 0,
4770
+ conflicts: 0,
4771
+ error: errorMessage
4772
+ };
4773
+ }
4774
+ });
4775
+ }
4776
+ // ---------------------------------------------------------------------------
4777
+ // Private Helpers
4778
+ // ---------------------------------------------------------------------------
4779
+ /**
4780
+ * Enrich TXF data with individually-buffered tokens before returning to caller.
4781
+ * PaymentsModule.createStorageData() passes empty tokens (they're stored via
4782
+ * saveToken()), but loadFromStorageData() needs them in the merged result.
4783
+ */
4784
+ enrichWithTokenBuffer(data) {
4785
+ if (this.tokenBuffer.size === 0) return data;
4786
+ const enriched = { ...data };
4787
+ for (const [tokenId, tokenData] of this.tokenBuffer) {
4788
+ if (!this.deletedTokenIds.has(tokenId)) {
4789
+ enriched[tokenId] = tokenData;
4790
+ }
4791
+ }
4792
+ return enriched;
4793
+ }
4794
+ // ---------------------------------------------------------------------------
4795
+ // Optional Methods
4796
+ // ---------------------------------------------------------------------------
4797
+ async exists() {
4798
+ if (!this.ipnsName) return false;
4799
+ const cached = this.cache.getIpnsRecord(this.ipnsName);
4800
+ if (cached) return true;
4801
+ const { best } = await this.httpClient.resolveIpns(this.ipnsName);
4802
+ return best !== null;
4803
+ }
4804
+ async clear() {
4805
+ if (!this.ipnsKeyPair || !this.ipnsName) return false;
4806
+ this.pendingBuffer.clear();
4807
+ if (this.flushTimer) {
4808
+ clearTimeout(this.flushTimer);
4809
+ this.flushTimer = null;
4810
+ }
4811
+ const emptyData = {
4812
+ _meta: {
4813
+ version: 0,
4814
+ address: this.identity?.directAddress ?? "",
4815
+ ipnsName: this.ipnsName,
4816
+ formatVersion: "2.0",
4817
+ updatedAt: Date.now()
4818
+ }
4819
+ };
4820
+ const result = await this._doSave(emptyData);
4821
+ if (result.success) {
4822
+ this.cache.clear();
4823
+ this.tokenBuffer.clear();
4824
+ this.deletedTokenIds.clear();
4825
+ await this.statePersistence.clear(this.ipnsName);
4826
+ }
4827
+ return result.success;
4828
+ }
4829
+ onEvent(callback) {
4830
+ this.eventCallbacks.add(callback);
4831
+ return () => {
4832
+ this.eventCallbacks.delete(callback);
4833
+ };
4834
+ }
4835
+ async saveToken(tokenId, tokenData) {
4836
+ this.pendingBuffer.tokenMutations.set(tokenId, { op: "save", data: tokenData });
4837
+ this.tokenBuffer.set(tokenId, tokenData);
4838
+ this.deletedTokenIds.delete(tokenId);
4839
+ this.scheduleFlush();
4840
+ }
4841
+ async getToken(tokenId) {
4842
+ if (this.deletedTokenIds.has(tokenId)) return null;
4843
+ return this.tokenBuffer.get(tokenId) ?? null;
4844
+ }
4845
+ async listTokenIds() {
4846
+ return Array.from(this.tokenBuffer.keys()).filter(
4847
+ (id) => !this.deletedTokenIds.has(id)
4848
+ );
4849
+ }
4850
+ async deleteToken(tokenId) {
4851
+ this.pendingBuffer.tokenMutations.set(tokenId, { op: "delete" });
4852
+ this.tokenBuffer.delete(tokenId);
4853
+ this.deletedTokenIds.add(tokenId);
4854
+ this.scheduleFlush();
4855
+ }
4856
+ // ---------------------------------------------------------------------------
4857
+ // Public Accessors
4858
+ // ---------------------------------------------------------------------------
4859
+ getIpnsName() {
4860
+ return this.ipnsName;
4861
+ }
4862
+ getLastCid() {
4863
+ return this.lastCid;
4864
+ }
4865
+ getSequenceNumber() {
4866
+ return this.ipnsSequenceNumber;
4867
+ }
4868
+ getDataVersion() {
4869
+ return this.dataVersion;
4870
+ }
4871
+ getRemoteCid() {
4872
+ return this.remoteCid;
4873
+ }
4874
+ // ---------------------------------------------------------------------------
4875
+ // Testing helper: wait for pending flush to complete
4876
+ // ---------------------------------------------------------------------------
4877
+ /**
4878
+ * Wait for the pending flush timer to fire and the flush operation to
4879
+ * complete. Useful in tests to await background writes.
4880
+ * Returns immediately if no flush is pending.
4881
+ */
4882
+ async waitForFlush() {
4883
+ if (this.flushTimer) {
4884
+ clearTimeout(this.flushTimer);
4885
+ this.flushTimer = null;
4886
+ await this.flushQueue.enqueue(() => this.executeFlush()).catch(() => {
4887
+ });
4888
+ } else if (!this.pendingBuffer.isEmpty) {
4889
+ await this.flushQueue.enqueue(() => this.executeFlush()).catch(() => {
4890
+ });
4891
+ } else {
4892
+ await this.flushQueue.enqueue(async () => {
4893
+ });
4894
+ }
4895
+ }
4896
+ // ---------------------------------------------------------------------------
4897
+ // Internal: Push Subscription Helpers
4898
+ // ---------------------------------------------------------------------------
4899
+ /**
4900
+ * Derive WebSocket URL from the first configured gateway.
4901
+ * Converts https://host → wss://host/ws/ipns
4902
+ */
4903
+ deriveWsUrl() {
4904
+ const gateways = this.httpClient.getGateways();
4905
+ if (gateways.length === 0) return null;
4906
+ const gateway = gateways[0];
4907
+ const wsProtocol = gateway.startsWith("https://") ? "wss://" : "ws://";
4908
+ const host = gateway.replace(/^https?:\/\//, "");
4909
+ return `${wsProtocol}${host}/ws/ipns`;
4910
+ }
4911
+ /**
4912
+ * Poll for remote IPNS changes (fallback when WS is unavailable).
4913
+ * Compares remote sequence number with last known and emits event if changed.
4914
+ */
4915
+ async pollForRemoteChanges() {
4916
+ if (!this.ipnsName) return;
4917
+ try {
4918
+ const { best } = await this.httpClient.resolveIpns(this.ipnsName);
4919
+ if (best && best.sequence > this.lastKnownRemoteSequence) {
4920
+ this.log(`Poll detected remote change: seq=${best.sequence} (was ${this.lastKnownRemoteSequence})`);
4921
+ this.lastKnownRemoteSequence = best.sequence;
4922
+ this.emitEvent({
4923
+ type: "storage:remote-updated",
4924
+ timestamp: Date.now(),
4925
+ data: { name: this.ipnsName, sequence: Number(best.sequence), cid: best.cid }
4926
+ });
4927
+ }
4928
+ } catch {
4929
+ }
4930
+ }
4931
+ // ---------------------------------------------------------------------------
4932
+ // Internal
4933
+ // ---------------------------------------------------------------------------
4934
+ emitEvent(event) {
4935
+ for (const callback of this.eventCallbacks) {
4936
+ try {
4937
+ callback(event);
4938
+ } catch {
4939
+ }
4940
+ }
4941
+ }
4942
+ log(message) {
4943
+ if (this.debug) {
4944
+ console.log(`[IPFS-Storage] ${message}`);
4945
+ }
4946
+ }
4947
+ META_KEYS = /* @__PURE__ */ new Set([
4948
+ "_meta",
4949
+ "_tombstones",
4950
+ "_outbox",
4951
+ "_sent",
4952
+ "_invalid",
4953
+ "_nametag",
4954
+ "_mintOutbox",
4955
+ "_invalidatedNametags",
4956
+ "_integrity"
4957
+ ]);
4958
+ populateTokenBuffer(data) {
4959
+ this.tokenBuffer.clear();
4960
+ this.deletedTokenIds.clear();
4961
+ for (const key of Object.keys(data)) {
4962
+ if (!this.META_KEYS.has(key)) {
4963
+ this.tokenBuffer.set(key, data[key]);
4964
+ }
4965
+ }
4966
+ }
4967
+ };
4968
+
4969
+ // impl/browser/ipfs/browser-ipfs-state-persistence.ts
4970
+ var KEY_PREFIX = "sphere_ipfs_";
4971
+ function seqKey(ipnsName) {
4972
+ return `${KEY_PREFIX}seq_${ipnsName}`;
4973
+ }
4974
+ function cidKey(ipnsName) {
4975
+ return `${KEY_PREFIX}cid_${ipnsName}`;
4976
+ }
4977
+ function verKey(ipnsName) {
4978
+ return `${KEY_PREFIX}ver_${ipnsName}`;
4979
+ }
4980
+ var BrowserIpfsStatePersistence = class {
4981
+ async load(ipnsName) {
4982
+ try {
4983
+ const seq = localStorage.getItem(seqKey(ipnsName));
4984
+ if (!seq) return null;
4985
+ return {
4986
+ sequenceNumber: seq,
4987
+ lastCid: localStorage.getItem(cidKey(ipnsName)),
4988
+ version: parseInt(localStorage.getItem(verKey(ipnsName)) ?? "0", 10)
4989
+ };
4990
+ } catch {
4991
+ return null;
4992
+ }
4993
+ }
4994
+ async save(ipnsName, state) {
4995
+ try {
4996
+ localStorage.setItem(seqKey(ipnsName), state.sequenceNumber);
4997
+ if (state.lastCid) {
4998
+ localStorage.setItem(cidKey(ipnsName), state.lastCid);
4999
+ } else {
5000
+ localStorage.removeItem(cidKey(ipnsName));
5001
+ }
5002
+ localStorage.setItem(verKey(ipnsName), String(state.version));
5003
+ } catch {
5004
+ }
5005
+ }
5006
+ async clear(ipnsName) {
5007
+ try {
5008
+ localStorage.removeItem(seqKey(ipnsName));
5009
+ localStorage.removeItem(cidKey(ipnsName));
5010
+ localStorage.removeItem(verKey(ipnsName));
5011
+ } catch {
5012
+ }
5013
+ }
5014
+ };
5015
+
5016
+ // impl/browser/ipfs/index.ts
5017
+ function createBrowserWebSocket2(url) {
5018
+ return new WebSocket(url);
5019
+ }
5020
+ function createBrowserIpfsStorageProvider(config) {
5021
+ return new IpfsStorageProvider(
5022
+ { ...config, createWebSocket: config?.createWebSocket ?? createBrowserWebSocket2 },
5023
+ new BrowserIpfsStatePersistence()
5024
+ );
5025
+ }
5026
+
3048
5027
  // price/CoinGeckoPriceProvider.ts
3049
5028
  var CoinGeckoPriceProvider = class {
3050
5029
  platform = "coingecko";
@@ -3282,6 +5261,12 @@ function createBrowserProviders(config) {
3282
5261
  const tokenSyncConfig = resolveTokenSyncConfig(network, config?.tokenSync);
3283
5262
  const priceConfig = resolvePriceConfig(config?.price);
3284
5263
  const storage = createLocalStorageProvider(config?.storage);
5264
+ const ipfsConfig = tokenSyncConfig?.ipfs;
5265
+ const ipfsTokenStorage = ipfsConfig?.enabled ? createBrowserIpfsStorageProvider({
5266
+ gateways: ipfsConfig.gateways,
5267
+ debug: config?.tokenSync?.ipfs?.useDht
5268
+ // reuse debug-like flag
5269
+ }) : void 0;
3285
5270
  return {
3286
5271
  storage,
3287
5272
  transport: createNostrTransportProvider({
@@ -3304,6 +5289,7 @@ function createBrowserProviders(config) {
3304
5289
  tokenStorage: createIndexedDBTokenStorageProvider(),
3305
5290
  l1: l1Config,
3306
5291
  price: priceConfig ? createPriceProvider(priceConfig) : void 0,
5292
+ ipfsTokenStorage,
3307
5293
  tokenSyncConfig
3308
5294
  };
3309
5295
  }