@unicitylabs/sphere-sdk 0.6.2 → 0.6.4

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.
@@ -1773,11 +1773,17 @@ var AsyncSerialQueue = class {
1773
1773
  var WriteBuffer = class {
1774
1774
  /** Full TXF data from save() calls — latest wins */
1775
1775
  txfData = null;
1776
+ /** IPNS context captured at save() time — ensures flush writes to the correct
1777
+ * IPNS record even if identity changes between save() and flush(). */
1778
+ capturedIpnsKeyPair = null;
1779
+ capturedIpnsName = null;
1776
1780
  get isEmpty() {
1777
1781
  return this.txfData === null;
1778
1782
  }
1779
1783
  clear() {
1780
1784
  this.txfData = null;
1785
+ this.capturedIpnsKeyPair = null;
1786
+ this.capturedIpnsName = null;
1781
1787
  }
1782
1788
  /**
1783
1789
  * Merge another buffer's contents into this one (for rollback).
@@ -1786,6 +1792,8 @@ var WriteBuffer = class {
1786
1792
  mergeFrom(other) {
1787
1793
  if (other.txfData && !this.txfData) {
1788
1794
  this.txfData = other.txfData;
1795
+ this.capturedIpnsKeyPair = other.capturedIpnsKeyPair;
1796
+ this.capturedIpnsName = other.capturedIpnsName;
1789
1797
  }
1790
1798
  }
1791
1799
  };
@@ -1874,7 +1882,7 @@ var DEFAULT_BASE_PATH = "m/44'/0'/0'";
1874
1882
  var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
1875
1883
 
1876
1884
  // impl/shared/ipfs/ipfs-storage-provider.ts
1877
- var IpfsStorageProvider = class {
1885
+ var IpfsStorageProvider = class _IpfsStorageProvider {
1878
1886
  id = "ipfs";
1879
1887
  name = "IPFS Storage";
1880
1888
  type = "p2p";
@@ -1919,7 +1927,12 @@ var IpfsStorageProvider = class {
1919
1927
  flushDebounceMs;
1920
1928
  /** Set to true during shutdown to prevent new flushes */
1921
1929
  isShuttingDown = false;
1930
+ /** Stored config for createForAddress() cloning */
1931
+ _config;
1932
+ _statePersistenceCtor;
1922
1933
  constructor(config, statePersistence) {
1934
+ this._config = config;
1935
+ this._statePersistenceCtor = statePersistence;
1923
1936
  const gateways = config?.gateways ?? getIpfsGatewayUrls();
1924
1937
  this.debug = config?.debug ?? false;
1925
1938
  this.ipnsLifetimeMs = config?.ipnsLifetimeMs ?? 99 * 365 * 24 * 60 * 60 * 1e3;
@@ -2039,6 +2052,7 @@ var IpfsStorageProvider = class {
2039
2052
  }
2040
2053
  async shutdown() {
2041
2054
  this.isShuttingDown = true;
2055
+ logger.debug("IPFS-Storage", `shutdown: ipnsName=${this.ipnsName?.slice(0, 20)}..., pendingEmpty=${this.pendingBuffer.isEmpty}, capturedIpns=${this.pendingBuffer.capturedIpnsName?.slice(0, 20) ?? "none"}`);
2042
2056
  if (this.flushTimer) {
2043
2057
  clearTimeout(this.flushTimer);
2044
2058
  this.flushTimer = null;
@@ -2071,6 +2085,8 @@ var IpfsStorageProvider = class {
2071
2085
  return { success: false, error: "Not initialized", timestamp: Date.now() };
2072
2086
  }
2073
2087
  this.pendingBuffer.txfData = data;
2088
+ this.pendingBuffer.capturedIpnsKeyPair = this.ipnsKeyPair;
2089
+ this.pendingBuffer.capturedIpnsName = this.ipnsName;
2074
2090
  this.scheduleFlush();
2075
2091
  return { success: true, timestamp: Date.now() };
2076
2092
  }
@@ -2081,8 +2097,12 @@ var IpfsStorageProvider = class {
2081
2097
  * Perform the actual upload + IPNS publish synchronously.
2082
2098
  * Called by executeFlush() and sync() — never by public save().
2083
2099
  */
2084
- async _doSave(data) {
2085
- if (!this.ipnsKeyPair || !this.ipnsName) {
2100
+ async _doSave(data, overrideIpns) {
2101
+ const ipnsKeyPair = overrideIpns?.keyPair ?? this.ipnsKeyPair;
2102
+ const ipnsName = overrideIpns?.name ?? this.ipnsName;
2103
+ const metaAddr = data?._meta?.address;
2104
+ logger.debug("IPFS-Storage", `_doSave: ipnsName=${ipnsName?.slice(0, 20)}..., override=${!!overrideIpns}, meta.address=${metaAddr?.slice(0, 20) ?? "none"}`);
2105
+ if (!ipnsKeyPair || !ipnsName) {
2086
2106
  return { success: false, error: "Not initialized", timestamp: Date.now() };
2087
2107
  }
2088
2108
  this.emitEvent({ type: "storage:saving", timestamp: Date.now() });
@@ -2091,7 +2111,7 @@ var IpfsStorageProvider = class {
2091
2111
  const metaUpdate = {
2092
2112
  ...data._meta,
2093
2113
  version: this.dataVersion,
2094
- ipnsName: this.ipnsName,
2114
+ ipnsName,
2095
2115
  updatedAt: Date.now()
2096
2116
  };
2097
2117
  if (this.remoteCid) {
@@ -2103,13 +2123,13 @@ var IpfsStorageProvider = class {
2103
2123
  const baseSeq = this.ipnsSequenceNumber > this.lastKnownRemoteSequence ? this.ipnsSequenceNumber : this.lastKnownRemoteSequence;
2104
2124
  const newSeq = baseSeq + 1n;
2105
2125
  const marshalledRecord = await createSignedRecord(
2106
- this.ipnsKeyPair,
2126
+ ipnsKeyPair,
2107
2127
  cid,
2108
2128
  newSeq,
2109
2129
  this.ipnsLifetimeMs
2110
2130
  );
2111
2131
  const publishResult = await this.httpClient.publishIpns(
2112
- this.ipnsName,
2132
+ ipnsName,
2113
2133
  marshalledRecord
2114
2134
  );
2115
2135
  if (!publishResult.success) {
@@ -2124,14 +2144,14 @@ var IpfsStorageProvider = class {
2124
2144
  this.ipnsSequenceNumber = newSeq;
2125
2145
  this.lastCid = cid;
2126
2146
  this.remoteCid = cid;
2127
- this.cache.setIpnsRecord(this.ipnsName, {
2147
+ this.cache.setIpnsRecord(ipnsName, {
2128
2148
  cid,
2129
2149
  sequence: newSeq,
2130
2150
  gateway: "local"
2131
2151
  });
2132
2152
  this.cache.setContent(cid, updatedData);
2133
- this.cache.markIpnsFresh(this.ipnsName);
2134
- await this.statePersistence.save(this.ipnsName, {
2153
+ this.cache.markIpnsFresh(ipnsName);
2154
+ await this.statePersistence.save(ipnsName, {
2135
2155
  sequenceNumber: newSeq.toString(),
2136
2156
  lastCid: cid,
2137
2157
  version: this.dataVersion
@@ -2183,7 +2203,8 @@ var IpfsStorageProvider = class {
2183
2203
  const baseData = active.txfData ?? {
2184
2204
  _meta: { version: 0, address: this.identity?.directAddress ?? "", formatVersion: "2.0", updatedAt: 0 }
2185
2205
  };
2186
- const result = await this._doSave(baseData);
2206
+ const overrideIpns = active.capturedIpnsKeyPair && active.capturedIpnsName ? { keyPair: active.capturedIpnsKeyPair, name: active.capturedIpnsName } : void 0;
2207
+ const result = await this._doSave(baseData, overrideIpns);
2187
2208
  if (!result.success) {
2188
2209
  throw new SphereError(result.error ?? "Save failed", "STORAGE_ERROR");
2189
2210
  }
@@ -2486,6 +2507,13 @@ var IpfsStorageProvider = class {
2486
2507
  log(message) {
2487
2508
  logger.debug("IPFS-Storage", message);
2488
2509
  }
2510
+ /**
2511
+ * Create an independent instance for a different address.
2512
+ * Shares the same gateway/timeout config but has fresh IPNS state.
2513
+ */
2514
+ createForAddress() {
2515
+ return new _IpfsStorageProvider(this._config, this._statePersistenceCtor);
2516
+ }
2489
2517
  };
2490
2518
 
2491
2519
  // impl/browser/ipfs/browser-ipfs-state-persistence.ts