@unicitylabs/sphere-sdk 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1734,11 +1734,17 @@ var AsyncSerialQueue = class {
1734
1734
  var WriteBuffer = class {
1735
1735
  /** Full TXF data from save() calls — latest wins */
1736
1736
  txfData = null;
1737
+ /** IPNS context captured at save() time — ensures flush writes to the correct
1738
+ * IPNS record even if identity changes between save() and flush(). */
1739
+ capturedIpnsKeyPair = null;
1740
+ capturedIpnsName = null;
1737
1741
  get isEmpty() {
1738
1742
  return this.txfData === null;
1739
1743
  }
1740
1744
  clear() {
1741
1745
  this.txfData = null;
1746
+ this.capturedIpnsKeyPair = null;
1747
+ this.capturedIpnsName = null;
1742
1748
  }
1743
1749
  /**
1744
1750
  * Merge another buffer's contents into this one (for rollback).
@@ -1747,6 +1753,8 @@ var WriteBuffer = class {
1747
1753
  mergeFrom(other) {
1748
1754
  if (other.txfData && !this.txfData) {
1749
1755
  this.txfData = other.txfData;
1756
+ this.capturedIpnsKeyPair = other.capturedIpnsKeyPair;
1757
+ this.capturedIpnsName = other.capturedIpnsName;
1750
1758
  }
1751
1759
  }
1752
1760
  };
@@ -1835,7 +1843,7 @@ var DEFAULT_BASE_PATH = "m/44'/0'/0'";
1835
1843
  var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
1836
1844
 
1837
1845
  // impl/shared/ipfs/ipfs-storage-provider.ts
1838
- var IpfsStorageProvider = class {
1846
+ var IpfsStorageProvider = class _IpfsStorageProvider {
1839
1847
  id = "ipfs";
1840
1848
  name = "IPFS Storage";
1841
1849
  type = "p2p";
@@ -1880,7 +1888,12 @@ var IpfsStorageProvider = class {
1880
1888
  flushDebounceMs;
1881
1889
  /** Set to true during shutdown to prevent new flushes */
1882
1890
  isShuttingDown = false;
1891
+ /** Stored config for createForAddress() cloning */
1892
+ _config;
1893
+ _statePersistenceCtor;
1883
1894
  constructor(config, statePersistence) {
1895
+ this._config = config;
1896
+ this._statePersistenceCtor = statePersistence;
1884
1897
  const gateways = config?.gateways ?? getIpfsGatewayUrls();
1885
1898
  this.debug = config?.debug ?? false;
1886
1899
  this.ipnsLifetimeMs = config?.ipnsLifetimeMs ?? 99 * 365 * 24 * 60 * 60 * 1e3;
@@ -2000,6 +2013,7 @@ var IpfsStorageProvider = class {
2000
2013
  }
2001
2014
  async shutdown() {
2002
2015
  this.isShuttingDown = true;
2016
+ logger.debug("IPFS-Storage", `shutdown: ipnsName=${this.ipnsName?.slice(0, 20)}..., pendingEmpty=${this.pendingBuffer.isEmpty}, capturedIpns=${this.pendingBuffer.capturedIpnsName?.slice(0, 20) ?? "none"}`);
2003
2017
  if (this.flushTimer) {
2004
2018
  clearTimeout(this.flushTimer);
2005
2019
  this.flushTimer = null;
@@ -2032,6 +2046,8 @@ var IpfsStorageProvider = class {
2032
2046
  return { success: false, error: "Not initialized", timestamp: Date.now() };
2033
2047
  }
2034
2048
  this.pendingBuffer.txfData = data;
2049
+ this.pendingBuffer.capturedIpnsKeyPair = this.ipnsKeyPair;
2050
+ this.pendingBuffer.capturedIpnsName = this.ipnsName;
2035
2051
  this.scheduleFlush();
2036
2052
  return { success: true, timestamp: Date.now() };
2037
2053
  }
@@ -2042,8 +2058,12 @@ var IpfsStorageProvider = class {
2042
2058
  * Perform the actual upload + IPNS publish synchronously.
2043
2059
  * Called by executeFlush() and sync() — never by public save().
2044
2060
  */
2045
- async _doSave(data) {
2046
- if (!this.ipnsKeyPair || !this.ipnsName) {
2061
+ async _doSave(data, overrideIpns) {
2062
+ const ipnsKeyPair = overrideIpns?.keyPair ?? this.ipnsKeyPair;
2063
+ const ipnsName = overrideIpns?.name ?? this.ipnsName;
2064
+ const metaAddr = data?._meta?.address;
2065
+ logger.debug("IPFS-Storage", `_doSave: ipnsName=${ipnsName?.slice(0, 20)}..., override=${!!overrideIpns}, meta.address=${metaAddr?.slice(0, 20) ?? "none"}`);
2066
+ if (!ipnsKeyPair || !ipnsName) {
2047
2067
  return { success: false, error: "Not initialized", timestamp: Date.now() };
2048
2068
  }
2049
2069
  this.emitEvent({ type: "storage:saving", timestamp: Date.now() });
@@ -2052,7 +2072,7 @@ var IpfsStorageProvider = class {
2052
2072
  const metaUpdate = {
2053
2073
  ...data._meta,
2054
2074
  version: this.dataVersion,
2055
- ipnsName: this.ipnsName,
2075
+ ipnsName,
2056
2076
  updatedAt: Date.now()
2057
2077
  };
2058
2078
  if (this.remoteCid) {
@@ -2064,13 +2084,13 @@ var IpfsStorageProvider = class {
2064
2084
  const baseSeq = this.ipnsSequenceNumber > this.lastKnownRemoteSequence ? this.ipnsSequenceNumber : this.lastKnownRemoteSequence;
2065
2085
  const newSeq = baseSeq + 1n;
2066
2086
  const marshalledRecord = await createSignedRecord(
2067
- this.ipnsKeyPair,
2087
+ ipnsKeyPair,
2068
2088
  cid,
2069
2089
  newSeq,
2070
2090
  this.ipnsLifetimeMs
2071
2091
  );
2072
2092
  const publishResult = await this.httpClient.publishIpns(
2073
- this.ipnsName,
2093
+ ipnsName,
2074
2094
  marshalledRecord
2075
2095
  );
2076
2096
  if (!publishResult.success) {
@@ -2085,14 +2105,14 @@ var IpfsStorageProvider = class {
2085
2105
  this.ipnsSequenceNumber = newSeq;
2086
2106
  this.lastCid = cid;
2087
2107
  this.remoteCid = cid;
2088
- this.cache.setIpnsRecord(this.ipnsName, {
2108
+ this.cache.setIpnsRecord(ipnsName, {
2089
2109
  cid,
2090
2110
  sequence: newSeq,
2091
2111
  gateway: "local"
2092
2112
  });
2093
2113
  this.cache.setContent(cid, updatedData);
2094
- this.cache.markIpnsFresh(this.ipnsName);
2095
- await this.statePersistence.save(this.ipnsName, {
2114
+ this.cache.markIpnsFresh(ipnsName);
2115
+ await this.statePersistence.save(ipnsName, {
2096
2116
  sequenceNumber: newSeq.toString(),
2097
2117
  lastCid: cid,
2098
2118
  version: this.dataVersion
@@ -2144,7 +2164,8 @@ var IpfsStorageProvider = class {
2144
2164
  const baseData = active.txfData ?? {
2145
2165
  _meta: { version: 0, address: this.identity?.directAddress ?? "", formatVersion: "2.0", updatedAt: 0 }
2146
2166
  };
2147
- const result = await this._doSave(baseData);
2167
+ const overrideIpns = active.capturedIpnsKeyPair && active.capturedIpnsName ? { keyPair: active.capturedIpnsKeyPair, name: active.capturedIpnsName } : void 0;
2168
+ const result = await this._doSave(baseData, overrideIpns);
2148
2169
  if (!result.success) {
2149
2170
  throw new SphereError(result.error ?? "Save failed", "STORAGE_ERROR");
2150
2171
  }
@@ -2447,6 +2468,13 @@ var IpfsStorageProvider = class {
2447
2468
  log(message) {
2448
2469
  logger.debug("IPFS-Storage", message);
2449
2470
  }
2471
+ /**
2472
+ * Create an independent instance for a different address.
2473
+ * Shares the same gateway/timeout config but has fresh IPNS state.
2474
+ */
2475
+ createForAddress() {
2476
+ return new _IpfsStorageProvider(this._config, this._statePersistenceCtor);
2477
+ }
2450
2478
  };
2451
2479
 
2452
2480
  // impl/browser/ipfs/browser-ipfs-state-persistence.ts