@net-protocol/storage 0.1.0 → 0.1.2

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/dist/index.mjs CHANGED
@@ -740,10 +740,10 @@ function useStorage({
740
740
  const outputAsString = outputFormat === "string";
741
741
  const storageKeyBytes = key ? getStorageKeyBytes(key, keyFormat) : void 0;
742
742
  const formatData = (text, dataHex) => {
743
- if (outputAsString) {
744
- return [text, hexToString(dataHex)];
745
- }
746
- return [text, dataHex];
743
+ return {
744
+ text,
745
+ value: outputAsString ? hexToString(dataHex) : dataHex
746
+ };
747
747
  };
748
748
  const [routerData, setRouterData] = useState();
749
749
  const [routerChunkLoading, setRouterChunkLoading] = useState(false);
@@ -765,6 +765,10 @@ function useStorage({
765
765
  }
766
766
  const [isChunkedStorage, text, data] = routerHook.data;
767
767
  if (!isChunkedStorage) {
768
+ if (!data || typeof data !== "string") {
769
+ setRouterData(void 0);
770
+ return;
771
+ }
768
772
  const formatted = formatData(text, data);
769
773
  setRouterData(formatted);
770
774
  return;
@@ -797,10 +801,10 @@ function useStorage({
797
801
  setRouterData(void 0);
798
802
  } else {
799
803
  if (outputAsString) {
800
- setRouterData([text, assembledString]);
804
+ setRouterData({ text, value: assembledString });
801
805
  } else {
802
806
  const hexData = stringToHex(assembledString);
803
- setRouterData([text, hexData]);
807
+ setRouterData({ text, value: hexData });
804
808
  }
805
809
  }
806
810
  } catch (error) {
@@ -887,6 +891,11 @@ function useStorage({
887
891
  args: [storageKeyBytes2, operatorAddress, index]
888
892
  });
889
893
  const [text, data] = result;
894
+ if (!data || typeof data !== "string") {
895
+ setHistoricalData(void 0);
896
+ setHistoricalLoading(false);
897
+ return;
898
+ }
890
899
  setHistoricalData(formatData(text, data));
891
900
  } catch (error) {
892
901
  console.error(
@@ -899,7 +908,15 @@ function useStorage({
899
908
  }
900
909
  }
901
910
  fetchHistoricalVersion();
902
- }, [chainId, key, operatorAddress, index, enabled, isLatestVersion, outputAsString]);
911
+ }, [
912
+ chainId,
913
+ key,
914
+ operatorAddress,
915
+ index,
916
+ enabled,
917
+ isLatestVersion,
918
+ outputAsString
919
+ ]);
903
920
  if (!isLatestVersion) {
904
921
  return {
905
922
  data: historicalData,
@@ -914,7 +931,14 @@ function useStorage({
914
931
  error: routerHook.error || routerChunkError
915
932
  };
916
933
  }
917
- const formattedDirectData = latestData ? formatData(latestData[0], latestData[1]) : void 0;
934
+ const formattedDirectData = latestData ? (() => {
935
+ const result = latestData;
936
+ const [text, valueHex] = result;
937
+ if (!valueHex || typeof valueHex !== "string") {
938
+ return void 0;
939
+ }
940
+ return formatData(text, valueHex);
941
+ })() : void 0;
918
942
  return {
919
943
  data: formattedDirectData,
920
944
  isLoading: latestLoading,
@@ -956,9 +980,11 @@ function useStorageForOperatorAndKey({
956
980
  chainId,
957
981
  key,
958
982
  operatorAddress,
959
- keyFormat
983
+ keyFormat,
984
+ outputFormat = "hex"
960
985
  }) {
961
986
  const storageKeyBytes = key ? getStorageKeyBytes(key, keyFormat) : void 0;
987
+ const outputAsString = outputFormat === "string";
962
988
  const readContractArgs = {
963
989
  abi: STORAGE_CONTRACT.abi,
964
990
  address: STORAGE_CONTRACT.address,
@@ -971,7 +997,13 @@ function useStorageForOperatorAndKey({
971
997
  };
972
998
  const { data, isLoading, error } = useReadContract(readContractArgs);
973
999
  return {
974
- data,
1000
+ data: data ? (() => {
1001
+ const [text, valueHex] = data;
1002
+ return {
1003
+ text,
1004
+ value: outputAsString ? hexToString(valueHex) : valueHex
1005
+ };
1006
+ })() : void 0,
975
1007
  isLoading,
976
1008
  error
977
1009
  };
@@ -1181,11 +1213,9 @@ function useXmlStorage({
1181
1213
  index,
1182
1214
  keyFormat,
1183
1215
  useRouter,
1184
- returnFormat = "object",
1185
1216
  outputFormat = "hex"
1186
1217
  }) {
1187
1218
  const isPreviewMode = !!content;
1188
- const returnAsTuple = returnFormat === "tuple";
1189
1219
  const outputAsString = outputFormat === "string";
1190
1220
  const {
1191
1221
  data: metadata,
@@ -1208,8 +1238,8 @@ function useXmlStorage({
1208
1238
  const metadataString = useMemo(() => {
1209
1239
  if (skipXmlParsing) return "";
1210
1240
  if (isPreviewMode) return content || "";
1211
- if (!metadata?.[1]) return "";
1212
- return metadata[1];
1241
+ if (!metadata?.value) return "";
1242
+ return metadata.value;
1213
1243
  }, [skipXmlParsing, isPreviewMode, content, metadata]);
1214
1244
  useMemo(() => {
1215
1245
  if (!metadataString) return [];
@@ -1260,78 +1290,22 @@ function useXmlStorage({
1260
1290
  if (skipXmlParsing || !metadataString) return false;
1261
1291
  return containsXmlReferences(metadataString);
1262
1292
  }, [metadataString, skipXmlParsing]);
1263
- if (returnAsTuple) {
1264
- if (skipXmlParsing) {
1265
- if (isPreviewMode) {
1266
- const contentValue = content || "";
1267
- return {
1268
- data: contentValue ? outputAsString ? [metadata?.[0] || "", contentValue] : [
1269
- metadata?.[0] || "",
1270
- stringToHex(contentValue)
1271
- ] : void 0,
1272
- isLoading: metadataLoading,
1273
- error: metadataError
1274
- };
1275
- } else {
1276
- const dataValue = metadata?.[1];
1277
- return {
1278
- data: dataValue ? outputAsString ? [metadata?.[0] || "", dataValue] : [
1279
- metadata?.[0] || "",
1280
- stringToHex(dataValue)
1281
- ] : void 0,
1282
- isLoading: metadataLoading,
1283
- error: metadataError
1284
- };
1285
- }
1286
- }
1287
- if (isXml) {
1288
- if (!assembledData) {
1289
- return {
1290
- data: void 0,
1291
- isLoading: metadataLoading || chunksLoading,
1292
- error: metadataError || chunksError
1293
- };
1294
- }
1295
- const hexData = stringToHex(assembledData);
1296
- return {
1297
- data: outputAsString ? [metadata?.[0] || "", assembledData] : [metadata?.[0] || "", hexData],
1298
- isLoading: metadataLoading || chunksLoading,
1299
- error: metadataError || chunksError
1300
- };
1301
- } else {
1302
- if (!metadata) {
1303
- return {
1304
- data: void 0,
1305
- isLoading: metadataLoading,
1306
- error: metadataError
1307
- };
1308
- }
1309
- const dataValue = metadata[1];
1310
- const returnValue = dataValue ? outputAsString ? [metadata[0], dataValue] : [
1311
- metadata[0],
1312
- stringToHex(dataValue)
1313
- ] : void 0;
1314
- return {
1315
- data: returnValue,
1316
- isLoading: metadataLoading,
1317
- error: metadataError
1318
- };
1319
- }
1320
- }
1293
+ const formatValue = (value) => {
1294
+ if (!value) return "";
1295
+ return outputAsString ? value : stringToHex(value);
1296
+ };
1321
1297
  if (skipXmlParsing) {
1322
1298
  return {
1323
- data: isPreviewMode ? content || "" : metadata?.[1] || "",
1324
- // metadata[1] is already a plain string
1325
- filename: metadata?.[0] || "",
1299
+ text: metadata?.text || "",
1300
+ value: isPreviewMode ? content || "" : formatValue(metadata?.value),
1326
1301
  isLoading: metadataLoading,
1327
1302
  error: metadataError,
1328
1303
  isXml: false
1329
1304
  };
1330
1305
  }
1331
1306
  return {
1332
- data: isXml ? assembledData : isPreviewMode ? content || "" : metadata?.[1] || "",
1333
- // metadata[1] is already a plain string
1334
- filename: metadata?.[0] || "",
1307
+ text: metadata?.text || "",
1308
+ value: isXml ? formatValue(assembledData) : isPreviewMode ? content || "" : formatValue(metadata?.value),
1335
1309
  isLoading: metadataLoading || isXml && chunksLoading,
1336
1310
  error: metadataError || chunksError,
1337
1311
  isXml
@@ -1368,16 +1342,13 @@ function useStorageFromRouter({
1368
1342
  }
1369
1343
  const [isChunkedStorage, text, data] = routerResult;
1370
1344
  if (!isChunkedStorage) {
1371
- setAssembledData([text, data]);
1345
+ setAssembledData({ text, value: data });
1372
1346
  return;
1373
1347
  }
1374
1348
  setIsChunkLoading(true);
1375
1349
  setChunkError(void 0);
1376
1350
  try {
1377
- const [chunkCount] = decodeAbiParameters(
1378
- [{ type: "uint8" }],
1379
- data
1380
- );
1351
+ const [chunkCount] = decodeAbiParameters([{ type: "uint8" }], data);
1381
1352
  if (chunkCount === 0) {
1382
1353
  setAssembledData(void 0);
1383
1354
  return;
@@ -1393,7 +1364,7 @@ function useStorageFromRouter({
1393
1364
  setAssembledData(void 0);
1394
1365
  } else {
1395
1366
  const hexData = stringToHex(assembledString);
1396
- setAssembledData([text, hexData]);
1367
+ setAssembledData({ text, value: hexData });
1397
1368
  }
1398
1369
  } catch (error) {
1399
1370
  setChunkError(error);
@@ -1617,7 +1588,8 @@ var StorageClient = class {
1617
1588
  });
1618
1589
  try {
1619
1590
  const result = await readContract(this.client, config);
1620
- return result;
1591
+ const [text, value] = result;
1592
+ return { text, value };
1621
1593
  } catch {
1622
1594
  return null;
1623
1595
  }
@@ -1635,7 +1607,8 @@ var StorageClient = class {
1635
1607
  });
1636
1608
  try {
1637
1609
  const result = await readContract(this.client, config);
1638
- return result;
1610
+ const [text, value] = result;
1611
+ return { text, value };
1639
1612
  } catch {
1640
1613
  return null;
1641
1614
  }
@@ -1822,7 +1795,8 @@ var StorageClient = class {
1822
1795
  functionName: "getForOperatorAndKey",
1823
1796
  args: [params.operator, storageKeyBytes]
1824
1797
  });
1825
- return result;
1798
+ const [text, value] = result;
1799
+ return { text, value };
1826
1800
  } catch {
1827
1801
  return null;
1828
1802
  }
@@ -1862,8 +1836,9 @@ var StorageClient = class {
1862
1836
  if (!result) {
1863
1837
  throw new Error("StoredDataNotFound");
1864
1838
  }
1865
- [text, data] = result;
1866
- data = hexToString(data);
1839
+ const resultObj = result;
1840
+ text = resultObj.text;
1841
+ data = hexToString(resultObj.value);
1867
1842
  }
1868
1843
  } else {
1869
1844
  const result = await this.getViaRouter({
@@ -1933,7 +1908,9 @@ var StorageClient = class {
1933
1908
  throw new Error("Chunks array cannot be empty");
1934
1909
  }
1935
1910
  if (params.chunks && params.chunks.length > 255) {
1936
- throw new Error(`Too many chunks: ${params.chunks.length} exceeds maximum of 255`);
1911
+ throw new Error(
1912
+ `Too many chunks: ${params.chunks.length} exceeds maximum of 255`
1913
+ );
1937
1914
  }
1938
1915
  }
1939
1916
  /**
@@ -1945,7 +1922,9 @@ var StorageClient = class {
1945
1922
  const transactionConfigs = [];
1946
1923
  for (const xmlChunk of params.xmlChunks) {
1947
1924
  const chunks = chunkDataForStorage(xmlChunk);
1948
- const chunkedHash = keccak256HashString(xmlChunk + params.operatorAddress);
1925
+ const chunkedHash = keccak256HashString(
1926
+ xmlChunk + params.operatorAddress
1927
+ );
1949
1928
  chunkedStorageHashes.push(chunkedHash);
1950
1929
  const config = this.prepareChunkedPut({
1951
1930
  key: chunkedHash,
@@ -1970,7 +1949,10 @@ var StorageClient = class {
1970
1949
  */
1971
1950
  preparePut(params) {
1972
1951
  this.validateStorageParams({ key: params.key });
1973
- const keyBytes32 = getStorageKeyBytes(params.key, params.keyFormat);
1952
+ const keyBytes32 = getStorageKeyBytes(
1953
+ params.key,
1954
+ params.keyFormat
1955
+ );
1974
1956
  const valueHex = normalizeDataOrEmpty(params.value);
1975
1957
  return {
1976
1958
  to: STORAGE_CONTRACT.address,
@@ -1984,7 +1966,10 @@ var StorageClient = class {
1984
1966
  */
1985
1967
  prepareChunkedPut(params) {
1986
1968
  this.validateStorageParams({ key: params.key, chunks: params.chunks });
1987
- const keyBytes32 = getStorageKeyBytes(params.key, params.keyFormat);
1969
+ const keyBytes32 = getStorageKeyBytes(
1970
+ params.key,
1971
+ params.keyFormat
1972
+ );
1988
1973
  for (const chunk of params.chunks) {
1989
1974
  if (!chunk.startsWith("0x")) {
1990
1975
  throw new Error(`Invalid chunk format: ${chunk} must be a hex string`);
@@ -2006,7 +1991,10 @@ var StorageClient = class {
2006
1991
  }
2007
1992
  const bulkEntries = params.entries.map((entry) => {
2008
1993
  this.validateStorageParams({ key: entry.key });
2009
- const keyBytes32 = getStorageKeyBytes(entry.key, params.keyFormat);
1994
+ const keyBytes32 = getStorageKeyBytes(
1995
+ entry.key,
1996
+ params.keyFormat
1997
+ );
2010
1998
  const valueHex = normalizeDataOrEmpty(entry.value);
2011
1999
  return {
2012
2000
  key: keyBytes32,
@@ -2046,7 +2034,10 @@ var StorageClient = class {
2046
2034
  keyFormat: params.keyFormat
2047
2035
  });
2048
2036
  return {
2049
- transactionConfigs: [metadataConfig, ...chunkedResult.transactionConfigs],
2037
+ transactionConfigs: [
2038
+ metadataConfig,
2039
+ ...chunkedResult.transactionConfigs
2040
+ ],
2050
2041
  topLevelHash: result.topLevelHash,
2051
2042
  metadata: chunkedResult.xmlMetadata
2052
2043
  };