@ethersphere/bee-js 13.0.0-upcoming.gcfb9756 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +104 -103
  2. package/dist/cjs/api/bytes.js +3 -4
  3. package/dist/cjs/api/bzz.js +5 -6
  4. package/dist/cjs/api/chunk.js +3 -3
  5. package/dist/cjs/api/envelope.js +3 -2
  6. package/dist/cjs/api/feed.js +4 -5
  7. package/dist/cjs/api/pin.js +2 -2
  8. package/dist/cjs/api/soc.js +2 -2
  9. package/dist/cjs/bee.js +18 -19
  10. package/dist/cjs/chunk/soc.js +21 -63
  11. package/dist/cjs/feed/identifier.js +3 -4
  12. package/dist/cjs/feed/index.js +17 -19
  13. package/dist/cjs/feed/retrievable.js +2 -2
  14. package/dist/cjs/index.js +18 -8
  15. package/dist/cjs/manifest/manifest.js +97 -333
  16. package/dist/cjs/modules/balance.js +3 -3
  17. package/dist/cjs/modules/cheque.js +4 -4
  18. package/dist/cjs/modules/chunk.js +8 -8
  19. package/dist/cjs/modules/collection.js +6 -6
  20. package/dist/cjs/modules/connectivity.js +3 -3
  21. package/dist/cjs/modules/data.js +6 -6
  22. package/dist/cjs/modules/feed.js +10 -10
  23. package/dist/cjs/modules/file.js +3 -3
  24. package/dist/cjs/modules/grantee.js +9 -9
  25. package/dist/cjs/modules/messaging.js +15 -17
  26. package/dist/cjs/modules/pin.js +6 -6
  27. package/dist/cjs/modules/settlement.js +2 -2
  28. package/dist/cjs/modules/soc.js +3 -3
  29. package/dist/cjs/modules/stamp.js +7 -7
  30. package/dist/cjs/modules/tag.js +2 -2
  31. package/dist/cjs/modules/transaction.js +4 -4
  32. package/dist/cjs/modules/wallet.js +3 -3
  33. package/dist/cjs/types/schema/chequebook.js +5 -5
  34. package/dist/cjs/types/schema/connectivity.js +5 -5
  35. package/dist/cjs/types/schema/envelope.js +5 -5
  36. package/dist/cjs/types/schema/grantee.js +4 -4
  37. package/dist/cjs/types/schema/stake.js +2 -2
  38. package/dist/cjs/types/schema/stamps.js +4 -4
  39. package/dist/cjs/types/schema/states.js +2 -2
  40. package/dist/cjs/types/schema/transactions.js +3 -3
  41. package/dist/cjs/types/schema/upload.js +2 -2
  42. package/dist/cjs/utils/chunk-stream.browser.js +24 -16
  43. package/dist/cjs/utils/chunk-stream.js +17 -9
  44. package/dist/cjs/utils/constants.js +3 -3
  45. package/dist/cjs/utils/expose.js +4 -4
  46. package/dist/cjs/utils/headers.js +4 -4
  47. package/dist/cjs/utils/http.js +3 -1
  48. package/dist/cjs/utils/pss.js +2 -2
  49. package/dist/cjs/utils/resource-locator.js +2 -2
  50. package/dist/cjs/utils/schema.js +4 -4
  51. package/dist/cjs/utils/stamps.js +15 -130
  52. package/dist/cjs/utils/type.js +2 -2
  53. package/dist/codemod/index.js +141 -0
  54. package/dist/codemod/transforms/v13.js +524 -0
  55. package/dist/index.browser.min.js +2 -1
  56. package/dist/index.browser.min.js.LICENSE.txt +3 -0
  57. package/dist/index.browser.min.js.map +1 -1
  58. package/dist/mjs/api/bytes.js +1 -2
  59. package/dist/mjs/api/bzz.js +1 -2
  60. package/dist/mjs/api/chunk.js +1 -1
  61. package/dist/mjs/api/envelope.js +3 -2
  62. package/dist/mjs/api/feed.js +1 -2
  63. package/dist/mjs/api/pin.js +1 -1
  64. package/dist/mjs/api/soc.js +1 -1
  65. package/dist/mjs/bee.js +13 -14
  66. package/dist/mjs/chunk/soc.js +11 -49
  67. package/dist/mjs/feed/identifier.js +2 -3
  68. package/dist/mjs/feed/index.js +6 -8
  69. package/dist/mjs/feed/retrievable.js +1 -1
  70. package/dist/mjs/index.js +2 -5
  71. package/dist/mjs/manifest/manifest.js +95 -339
  72. package/dist/mjs/modules/balance.js +1 -1
  73. package/dist/mjs/modules/cheque.js +1 -1
  74. package/dist/mjs/modules/chunk.js +1 -1
  75. package/dist/mjs/modules/collection.js +1 -1
  76. package/dist/mjs/modules/connectivity.js +1 -1
  77. package/dist/mjs/modules/data.js +3 -3
  78. package/dist/mjs/modules/feed.js +1 -1
  79. package/dist/mjs/modules/file.js +1 -1
  80. package/dist/mjs/modules/grantee.js +1 -1
  81. package/dist/mjs/modules/messaging.js +3 -5
  82. package/dist/mjs/modules/pin.js +1 -1
  83. package/dist/mjs/modules/settlement.js +1 -1
  84. package/dist/mjs/modules/soc.js +1 -1
  85. package/dist/mjs/modules/stamp.js +1 -1
  86. package/dist/mjs/modules/tag.js +1 -1
  87. package/dist/mjs/modules/transaction.js +1 -1
  88. package/dist/mjs/modules/wallet.js +1 -1
  89. package/dist/mjs/types/schema/chequebook.js +1 -1
  90. package/dist/mjs/types/schema/connectivity.js +1 -1
  91. package/dist/mjs/types/schema/envelope.js +5 -5
  92. package/dist/mjs/types/schema/grantee.js +1 -1
  93. package/dist/mjs/types/schema/stake.js +1 -1
  94. package/dist/mjs/types/schema/stamps.js +1 -1
  95. package/dist/mjs/types/schema/states.js +1 -1
  96. package/dist/mjs/types/schema/transactions.js +1 -1
  97. package/dist/mjs/types/schema/upload.js +1 -1
  98. package/dist/mjs/utils/chunk-stream.browser.js +28 -18
  99. package/dist/mjs/utils/chunk-stream.js +21 -11
  100. package/dist/mjs/utils/constants.js +1 -1
  101. package/dist/mjs/utils/expose.js +1 -1
  102. package/dist/mjs/utils/headers.js +1 -1
  103. package/dist/mjs/utils/http.js +6 -1
  104. package/dist/mjs/utils/pss.js +1 -1
  105. package/dist/mjs/utils/resource-locator.js +1 -1
  106. package/dist/mjs/utils/schema.js +1 -1
  107. package/dist/mjs/utils/stamps.js +5 -99
  108. package/dist/mjs/utils/type.js +2 -2
  109. package/dist/types/api/balance.d.ts +1 -1
  110. package/dist/types/api/batches.d.ts +1 -1
  111. package/dist/types/api/bytes.d.ts +3 -3
  112. package/dist/types/api/bzz.d.ts +1 -2
  113. package/dist/types/api/chequebook.d.ts +1 -1
  114. package/dist/types/api/chunk.d.ts +1 -1
  115. package/dist/types/api/connectivity.d.ts +1 -1
  116. package/dist/types/api/consumed.d.ts +1 -1
  117. package/dist/types/api/envelope.d.ts +1 -1
  118. package/dist/types/api/feed.d.ts +1 -2
  119. package/dist/types/api/grantee.d.ts +1 -1
  120. package/dist/types/api/pin.d.ts +1 -1
  121. package/dist/types/api/pss.d.ts +1 -1
  122. package/dist/types/api/settlement.d.ts +1 -1
  123. package/dist/types/api/soc.d.ts +1 -1
  124. package/dist/types/api/stake.d.ts +1 -1
  125. package/dist/types/api/stamp.d.ts +1 -1
  126. package/dist/types/api/stewardship.d.ts +1 -1
  127. package/dist/types/api/tag.d.ts +1 -1
  128. package/dist/types/api/transaction.d.ts +1 -1
  129. package/dist/types/api/wallet.d.ts +1 -1
  130. package/dist/types/bee.d.ts +11 -13
  131. package/dist/types/chunk/soc.d.ts +3 -48
  132. package/dist/types/feed/identifier.d.ts +1 -1
  133. package/dist/types/feed/index.d.ts +1 -3
  134. package/dist/types/feed/retrievable.d.ts +1 -1
  135. package/dist/types/index.d.ts +17 -20
  136. package/dist/types/manifest/manifest.d.ts +24 -33
  137. package/dist/types/modules/balance.d.ts +1 -1
  138. package/dist/types/modules/cheque.d.ts +1 -1
  139. package/dist/types/modules/chequebook.d.ts +1 -1
  140. package/dist/types/modules/chunk.d.ts +1 -2
  141. package/dist/types/modules/collection.d.ts +2 -2
  142. package/dist/types/modules/connectivity.d.ts +1 -1
  143. package/dist/types/modules/context.d.ts +1 -1
  144. package/dist/types/modules/data.d.ts +3 -3
  145. package/dist/types/modules/feed.d.ts +1 -1
  146. package/dist/types/modules/file.d.ts +1 -2
  147. package/dist/types/modules/grantee.d.ts +1 -1
  148. package/dist/types/modules/messaging.d.ts +1 -2
  149. package/dist/types/modules/pin.d.ts +1 -1
  150. package/dist/types/modules/settlement.d.ts +1 -1
  151. package/dist/types/modules/soc.d.ts +1 -1
  152. package/dist/types/modules/stake.d.ts +1 -1
  153. package/dist/types/modules/stamp.d.ts +1 -1
  154. package/dist/types/modules/storage.d.ts +1 -1
  155. package/dist/types/modules/tag.d.ts +1 -1
  156. package/dist/types/modules/transaction.d.ts +1 -1
  157. package/dist/types/modules/wallet.d.ts +1 -1
  158. package/dist/types/types/debug.d.ts +1 -1
  159. package/dist/types/types/index.d.ts +2 -11
  160. package/dist/types/types/schema/chequebook.d.ts +1 -1
  161. package/dist/types/types/schema/connectivity.d.ts +1 -1
  162. package/dist/types/types/schema/grantee.d.ts +1 -1
  163. package/dist/types/types/schema/stake.d.ts +1 -1
  164. package/dist/types/types/schema/stamps.d.ts +1 -1
  165. package/dist/types/types/schema/states.d.ts +1 -1
  166. package/dist/types/types/schema/transactions.d.ts +1 -1
  167. package/dist/types/types/schema/upload.d.ts +1 -1
  168. package/dist/types/utils/chunk-stream.browser.d.ts +1 -1
  169. package/dist/types/utils/chunk-stream.d.ts +2 -2
  170. package/dist/types/utils/constants.d.ts +1 -1
  171. package/dist/types/utils/expose.d.ts +1 -1
  172. package/dist/types/utils/headers.d.ts +1 -1
  173. package/dist/types/utils/pss.d.ts +1 -1
  174. package/dist/types/utils/resource-locator.d.ts +1 -1
  175. package/dist/types/utils/schema.d.ts +1 -1
  176. package/dist/types/utils/stamps.d.ts +2 -27
  177. package/dist/types/utils/tar-uploader.browser.d.ts +1 -1
  178. package/dist/types/utils/tar-uploader.d.ts +1 -1
  179. package/dist/types/utils/type.d.ts +1 -1
  180. package/package.json +7 -2
  181. package/dist/cjs/chunk/bmt.js +0 -37
  182. package/dist/cjs/chunk/cac.js +0 -43
  183. package/dist/cjs/stamper/stamper.js +0 -44
  184. package/dist/cjs/utils/bytes.js +0 -116
  185. package/dist/cjs/utils/cid.js +0 -37
  186. package/dist/cjs/utils/redundancy.js +0 -133
  187. package/dist/cjs/utils/typed-bytes.js +0 -186
  188. package/dist/mjs/chunk/bmt.js +0 -34
  189. package/dist/mjs/chunk/cac.js +0 -34
  190. package/dist/mjs/stamper/stamper.js +0 -40
  191. package/dist/mjs/utils/bytes.js +0 -102
  192. package/dist/mjs/utils/cid.js +0 -32
  193. package/dist/mjs/utils/redundancy.js +0 -98
  194. package/dist/mjs/utils/typed-bytes.js +0 -167
  195. package/dist/types/chunk/bmt.d.ts +0 -16
  196. package/dist/types/chunk/cac.d.ts +0 -40
  197. package/dist/types/stamper/stamper.d.ts +0 -15
  198. package/dist/types/utils/bytes.d.ts +0 -19
  199. package/dist/types/utils/cid.d.ts +0 -10
  200. package/dist/types/utils/redundancy.d.ts +0 -21
  201. package/dist/types/utils/typed-bytes.d.ts +0 -69
@@ -1,5 +1,5 @@
1
+ import { Reference } from '@ethersphere/core-sdk';
1
2
  import { z } from 'zod';
2
- import { Reference } from '../../utils/typed-bytes';
3
3
  export declare const UploadResultBody: z.ZodObject<{
4
4
  reference: z.ZodPipe<z.ZodString, z.ZodTransform<Reference, string>>;
5
5
  }, z.core.$strip>;
@@ -1,6 +1,6 @@
1
1
  import { Bee, BeeRequestOptions, UploadOptions, UploadResult } from '..';
2
- import { BatchId, Reference } from './typed-bytes';
3
2
  import { UploadProgress } from './upload-progress';
3
+ import { BatchId, Reference } from '@ethersphere/core-sdk';
4
4
  export declare function hashDirectory(_dir: string): Promise<void>;
5
5
  export declare function streamDirectory(_bee: Bee, _dir: string, _postageBatchId: BatchId | string | Uint8Array, _onUploadProgress?: (progress: UploadProgress) => void, _options?: UploadOptions, _requestOptions?: BeeRequestOptions): Promise<Reference>;
6
6
  export declare function streamFiles(bee: Bee, files: File[] | FileList, postageBatchId: BatchId, onUploadProgress?: (progress: UploadProgress) => void, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
@@ -1,6 +1,6 @@
1
1
  import { Bee, BeeRequestOptions, CollectionUploadOptions, UploadOptions, UploadResult } from '..';
2
- import { BatchId } from './typed-bytes';
3
2
  import { UploadProgress } from './upload-progress';
4
- export declare function hashDirectory(dir: string): Promise<import("./typed-bytes").Reference>;
3
+ import { BatchId } from '@ethersphere/core-sdk';
4
+ export declare function hashDirectory(dir: string): Promise<import("@ethersphere/core-sdk").Reference>;
5
5
  export declare function streamDirectory(bee: Bee, dir: string, postageBatchId: BatchId | string | Uint8Array, onUploadProgress?: (progress: UploadProgress) => void, options?: CollectionUploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
6
6
  export declare function streamFiles(_bee: Bee, _files: File[] | FileList, _postageBatchId: BatchId, _onUploadProgress?: (progress: UploadProgress) => void, _options?: UploadOptions, _requestOptions?: BeeRequestOptions): Promise<UploadResult>;
@@ -1,4 +1,4 @@
1
- import { BatchId, Topic } from './typed-bytes';
1
+ import { BatchId, Topic } from '@ethersphere/core-sdk';
2
2
  export declare const SWARM_GATEWAY_URL = "https://api.gateway.ethswarm.org";
3
3
  export declare const NULL_STAMP: BatchId;
4
4
  export declare const NULL_TOPIC: Topic;
@@ -1,5 +1,5 @@
1
1
  export { getCollectionSize, makeCollectionFromFileList } from './collection';
2
2
  export { getFolderSize } from './collection.node';
3
3
  export { makeMaxTarget } from './pss';
4
- export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from './redundancy';
4
+ export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from '@ethersphere/core-sdk';
5
5
  export { convertEnvelopeToMarshaledStamp, getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage, mapPostageBatch, unmapPostageBatch, } from './stamps';
@@ -1,4 +1,4 @@
1
+ import { BatchId } from '@ethersphere/core-sdk';
1
2
  import { EnvelopeWithBatchId, FileHeaders } from '../types';
2
- import { BatchId } from './typed-bytes';
3
3
  export declare function readFileHeaders(headers: Record<string, string>): FileHeaders;
4
4
  export declare function prepareRequestHeaders(stamp: BatchId | Uint8Array | string | EnvelopeWithBatchId | null, nullableOptions?: unknown): Record<string, string>;
@@ -1,4 +1,4 @@
1
- import { PeerAddress } from './typed-bytes';
1
+ import { PeerAddress } from '@ethersphere/core-sdk';
2
2
  /**
3
3
  * Utility function that for given strings/reference takes the most specific
4
4
  * target that Bee node will except.
@@ -1,4 +1,4 @@
1
- import { Reference } from './typed-bytes';
1
+ import { Reference } from '@ethersphere/core-sdk';
2
2
  export declare class ResourceLocator {
3
3
  private raw;
4
4
  constructor(raw: Reference | Uint8Array | string);
@@ -1,6 +1,6 @@
1
+ import { PublicKey, Reference } from '@ethersphere/core-sdk';
1
2
  import { z } from 'zod';
2
3
  import { type NumberString } from '../types';
3
- import { PublicKey, Reference } from './typed-bytes';
4
4
  export { HexStringSchema } from './hex-schema';
5
5
  export declare const BeeRequestOptionsSchema: z.ZodObject<{
6
6
  baseURL: z.ZodOptional<z.ZodString>;
@@ -1,34 +1,9 @@
1
1
  import { EnvelopeWithBatchId, NumberString, PostageBatch, RedundancyLevel } from '../types';
2
- import { Bytes } from './bytes';
3
2
  import { Duration } from './duration';
4
3
  import { Size } from './size';
5
4
  import { BZZ } from './tokens';
6
- /**
7
- * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
8
- *
9
- * For smaller depths (up to 20), this may provide less accurate results.
10
- *
11
- * @returns {number} A number between 0 and 1 representing the usage of the postage batch.
12
- */
13
- export declare function getStampUsage(utilization: number, depth: number, bucketDepth: number): number;
14
- /**
15
- * Utility function that calculates the theoritical maximum size of a postage batch based on its depth.
16
- *
17
- * For smaller depths (up to 22), this may provide less accurate results.
18
- *
19
- * @returns {number} The maximum theoretical size of the postage batch in bytes.
20
- */
21
- export declare function getStampTheoreticalBytes(depth: number): number;
22
- /**
23
- * Utility function that calculates the effective size of a postage batch based on its depth.
24
- *
25
- * Below 22 depth the effective size is 0
26
- * Above 34 it's always > 99%
27
- *
28
- * @returns {number} The effective size of the postage batch in bytes.
29
- */
30
- export declare function getStampEffectiveBytes(depth: number, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): number;
31
- export declare function getStampEffectiveBytesBreakpoints(encryption: boolean, erasureCodeLevel?: RedundancyLevel): Map<number, number>;
5
+ import { Bytes, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage } from '@ethersphere/core-sdk';
6
+ export { getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage };
32
7
  /**
33
8
  * Utility function that calculates the cost of a postage batch based on its depth and amount.
34
9
  */
@@ -1,3 +1,3 @@
1
+ import { BatchId } from '@ethersphere/core-sdk';
1
2
  import { BeeRequestOptions, Collection, CollectionUploadOptions } from '..';
2
- import { BatchId } from './typed-bytes';
3
3
  export declare function uploadTar(requestOptions: BeeRequestOptions, collection: Collection, postageBatchId: BatchId, options?: CollectionUploadOptions): Promise<import("./http").BeeResponse<unknown>>;
@@ -1,3 +1,3 @@
1
+ import { BatchId } from '@ethersphere/core-sdk';
1
2
  import { BeeRequestOptions, Collection, CollectionUploadOptions } from '..';
2
- import { BatchId } from './typed-bytes';
3
3
  export declare function uploadTar(requestOptions: BeeRequestOptions, collection: Collection, postageBatchId: BatchId, options?: CollectionUploadOptions): Promise<import("./http").BeeResponse<unknown>>;
@@ -12,7 +12,7 @@ export declare function isTag(value: unknown): value is Tag;
12
12
  * @param value
13
13
  * @throws TypeError if not valid
14
14
  */
15
- export declare function assertData(value: unknown): asserts value is string | Uint8Array;
15
+ export declare function assertData(value: unknown): asserts value is string | Uint8Array | Blob | stream.Readable;
16
16
  /**
17
17
  * Check whether the given parameter is a correct file representation to file upload.
18
18
  * @param value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "13.0.0-upcoming.gcfb9756",
3
+ "version": "13.0.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -31,6 +31,9 @@
31
31
  "types": "./dist/types/index.d.ts"
32
32
  }
33
33
  },
34
+ "bin": {
35
+ "bee-js-codemod": "dist/codemod/index.js"
36
+ },
34
37
  "engines": {
35
38
  "bee": "2.8.1-7cf53193",
36
39
  "beeApiVersion": "8.1.0"
@@ -55,10 +58,11 @@
55
58
  "prepublishOnly": "NODE_ENV=production npm run build",
56
59
  "prepare": "node scripts/generate-version.cjs",
57
60
  "generate:version": "node scripts/generate-version.cjs",
58
- "build": "npm run generate:version && rimraf dist && npm run build:node && npm run build:types && npm run build:browser",
61
+ "build": "npm run generate:version && rimraf dist && npm run build:node && npm run build:types && npm run build:browser && npm run build:codemod",
59
62
  "build:node": "tsc -p tsconfig.json && tsc -p tsconfig-mjs.json && ./build-fixup && babel --plugins \"babel-plugin-add-import-extension\" --out-dir dist/mjs/ dist/mjs/",
60
63
  "build:types": "tsc --emitDeclarationOnly --declaration --outDir dist/types",
61
64
  "build:browser": "webpack --progress",
65
+ "build:codemod": "tsc -p codemod/tsconfig.build.json && chmod +x dist/codemod/index.js",
62
66
  "test": "TS_NODE_PROJECT=tsconfig.test.json jest --runInBand --verbose --forceExit",
63
67
  "test:browser": "TS_NODE_PROJECT=tsconfig.test.json jest --config jest.browser.config.js --runInBand --verbose --forceExit",
64
68
  "check": "tsc --project tsconfig.test.json",
@@ -71,6 +75,7 @@
71
75
  "debug": "^4.4.3",
72
76
  "isomorphic-ws": "^5.0.0",
73
77
  "semver": "^7.3.5",
78
+ "@ethersphere/core-sdk": "^0.1.1",
74
79
  "ws": "^8.21.0",
75
80
  "zod": "^4.4.3"
76
81
  },
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateChunkAddress = calculateChunkAddress;
4
- const cafe_utility_1 = require("cafe-utility");
5
- const error_1 = require("../utils/error");
6
- const typed_bytes_1 = require("../utils/typed-bytes");
7
- const MAX_CHUNK_PAYLOAD_SIZE = 4096;
8
- const SEGMENT_SIZE = 32;
9
- /**
10
- * Calculate a Binary Merkle Tree hash for a chunk
11
- *
12
- * The BMT chunk address is the hash of the 8 byte span and the root
13
- * hash of a binary Merkle tree (BMT) built on the 32-byte segments
14
- * of the underlying data.
15
- *
16
- * If the chunk content is less than 4k, the hash is calculated as
17
- * if the chunk was padded with all zeros up to 4096 bytes.
18
- *
19
- * @param chunkContent Chunk data including span and payload as well
20
- *
21
- * @returns the keccak256 hash in a byte array
22
- */
23
- function calculateChunkAddress(chunkContent) {
24
- const span = chunkContent.slice(0, typed_bytes_1.Span.LENGTH);
25
- const payload = chunkContent.slice(typed_bytes_1.Span.LENGTH);
26
- const rootHash = calculateBmtRootHash(payload);
27
- const chunkHash = cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(span, rootHash));
28
- return new typed_bytes_1.Reference(chunkHash);
29
- }
30
- function calculateBmtRootHash(payload) {
31
- if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
32
- throw new error_1.BeeArgumentError(`payload size ${payload.length} exceeds maximum chunk payload size ${MAX_CHUNK_PAYLOAD_SIZE}`, payload);
33
- }
34
- const input = new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE);
35
- input.set(payload);
36
- return cafe_utility_1.Binary.log2Reduce(cafe_utility_1.Binary.partition(input, SEGMENT_SIZE), (a, b) => cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(a, b)));
37
- }
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_PAYLOAD_SIZE = exports.MIN_PAYLOAD_SIZE = void 0;
4
- exports.unmarshalContentAddressedChunk = unmarshalContentAddressedChunk;
5
- exports.makeContentAddressedChunk = makeContentAddressedChunk;
6
- const cafe_utility_1 = require("cafe-utility");
7
- const zod_1 = require("zod");
8
- const bytes_1 = require("../utils/bytes");
9
- const typed_bytes_1 = require("../utils/typed-bytes");
10
- const bmt_1 = require("./bmt");
11
- const soc_1 = require("./soc");
12
- exports.MIN_PAYLOAD_SIZE = 1;
13
- exports.MAX_PAYLOAD_SIZE = 4096;
14
- function unmarshalContentAddressedChunk(data) {
15
- data = new bytes_1.Bytes(data);
16
- return makeContentAddressedChunk(data.toUint8Array().slice(typed_bytes_1.Span.LENGTH), typed_bytes_1.Span.fromSlice(data.toUint8Array(), 0));
17
- }
18
- function makeContentAddressedChunk(rawPayload, span) {
19
- const asString = zod_1.z.string().safeParse(rawPayload);
20
- if (asString.success) {
21
- rawPayload = bytes_1.Bytes.fromUtf8(asString.data);
22
- }
23
- if (rawPayload.length < exports.MIN_PAYLOAD_SIZE || rawPayload.length > exports.MAX_PAYLOAD_SIZE) {
24
- throw new RangeError(`payload size ${rawPayload.length} exceeds limits [${exports.MIN_PAYLOAD_SIZE}, ${exports.MAX_PAYLOAD_SIZE}]`);
25
- }
26
- const typedSpan = span
27
- ? typeof span === 'bigint'
28
- ? typed_bytes_1.Span.fromBigInt(span)
29
- : span
30
- : typed_bytes_1.Span.fromBigInt(BigInt(rawPayload.length));
31
- const payload = new bytes_1.Bytes(rawPayload);
32
- const data = cafe_utility_1.Binary.concatBytes(typedSpan.toUint8Array(), payload.toUint8Array());
33
- const address = (0, bmt_1.calculateChunkAddress)(data);
34
- return {
35
- data,
36
- span: typedSpan,
37
- payload,
38
- address,
39
- toSingleOwnerChunk: (identifier, signer) => {
40
- return (0, soc_1.makeSingleOwnerChunk)(address, typedSpan, payload, identifier, signer);
41
- },
42
- };
43
- }
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Stamper = void 0;
4
- const cafe_utility_1 = require("cafe-utility");
5
- const typed_bytes_1 = require("../utils/typed-bytes");
6
- class Stamper {
7
- constructor(signer, batchId, buckets, depth) {
8
- this.signer = new typed_bytes_1.PrivateKey(signer);
9
- this.batchId = new typed_bytes_1.BatchId(batchId);
10
- this.buckets = buckets;
11
- this.depth = depth;
12
- this.maxSlot = 2 ** (this.depth - 16);
13
- }
14
- static fromBlank(signer, batchId, depth) {
15
- return new Stamper(signer, batchId, new Uint32Array(65536), depth);
16
- }
17
- static fromState(signer, batchId, buckets, depth) {
18
- return new Stamper(signer, batchId, buckets, depth);
19
- }
20
- stamp(chunk) {
21
- const address = chunk.hash();
22
- const bucket = cafe_utility_1.Binary.uint16ToNumber(address, 'BE');
23
- const height = this.buckets[bucket];
24
- if (height >= this.maxSlot) {
25
- throw Error('Bucket is full');
26
- }
27
- this.buckets[bucket]++;
28
- const index = cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint32(bucket, 'BE'), cafe_utility_1.Binary.numberToUint32(height, 'BE'));
29
- const currentTimeNs = BigInt(Date.now()) * 1000000n; // Bee uses unix timestamp in nanoseconds
30
- const timestamp = cafe_utility_1.Binary.numberToUint64(currentTimeNs, 'BE');
31
- const signature = this.signer.sign(cafe_utility_1.Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
32
- return {
33
- batchId: this.batchId,
34
- index,
35
- issuer: this.signer.publicKey().address().toUint8Array(),
36
- signature: signature.toUint8Array(),
37
- timestamp,
38
- };
39
- }
40
- getState() {
41
- return this.buckets;
42
- }
43
- }
44
- exports.Stamper = Stamper;
@@ -1,116 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Bytes = void 0;
7
- exports.parseSizeToBytes = parseSizeToBytes;
8
- const cafe_utility_1 = require("cafe-utility");
9
- const debug_1 = __importDefault(require("debug"));
10
- const zod_1 = require("zod");
11
- const hex_schema_1 = require("./hex-schema");
12
- const debug = (0, debug_1.default)('bee-js:bytes');
13
- const DECODER = new TextDecoder();
14
- const ENCODER = new TextEncoder();
15
- class Bytes {
16
- constructor(bytes, byteLength) {
17
- if (!bytes) {
18
- throw Error(`Bytes#constructor: constructor parameter is falsy: ${bytes}`);
19
- }
20
- if (bytes instanceof Bytes) {
21
- this.bytes = bytes.bytes;
22
- }
23
- else if (typeof bytes === 'string') {
24
- this.bytes = cafe_utility_1.Binary.hexToUint8Array(hex_schema_1.HexStringSchema.parse(bytes));
25
- }
26
- else if (bytes instanceof ArrayBuffer) {
27
- this.bytes = new Uint8Array(bytes);
28
- }
29
- else if (bytes instanceof Uint8Array) {
30
- this.bytes = bytes;
31
- }
32
- else {
33
- const unknownInput = bytes;
34
- const toHex = cafe_utility_1.Objects.getDeep(unknownInput, 'toHex');
35
- if (zod_1.z.function().safeParse(toHex).success) {
36
- const hex = toHex.call(unknownInput);
37
- this.bytes = cafe_utility_1.Binary.hexToUint8Array(hex_schema_1.HexStringSchema.parse(hex));
38
- }
39
- else {
40
- debug('bytes', bytes);
41
- throw new Error(`Bytes#constructor: unsupported type: ${typeof bytes}`);
42
- }
43
- }
44
- this.length = this.bytes.length;
45
- if (byteLength) {
46
- if (Array.isArray(byteLength)) {
47
- if (!byteLength.includes(this.length)) {
48
- throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength.join(' or ')}`);
49
- }
50
- }
51
- else if (this.length !== byteLength) {
52
- throw new Error(`Bytes#checkByteLength: bytes length is ${this.length} but expected ${byteLength}`);
53
- }
54
- }
55
- }
56
- static keccak256(bytes) {
57
- return new Bytes(cafe_utility_1.Binary.keccak256(new Bytes(bytes).toUint8Array()));
58
- }
59
- static fromUtf8(utf8) {
60
- return new Bytes(ENCODER.encode(utf8));
61
- }
62
- static fromSlice(bytes, start, length) {
63
- if (length === undefined) {
64
- return new Bytes(bytes.slice(start));
65
- }
66
- return new Bytes(bytes.slice(start, start + length));
67
- }
68
- offset(index) {
69
- return new Uint8Array(this.bytes.slice(index));
70
- }
71
- toUint8Array() {
72
- return new Uint8Array(this.bytes);
73
- }
74
- toHex() {
75
- return cafe_utility_1.Binary.uint8ArrayToHex(this.bytes);
76
- }
77
- toBase64() {
78
- return cafe_utility_1.Binary.uint8ArrayToBase64(this.bytes);
79
- }
80
- toBase32() {
81
- return cafe_utility_1.Binary.uint8ArrayToBase32(this.bytes);
82
- }
83
- toString() {
84
- return this.toHex();
85
- }
86
- toUtf8() {
87
- return DECODER.decode(this.bytes);
88
- }
89
- toJSON() {
90
- return JSON.parse(this.toUtf8());
91
- }
92
- equals(other) {
93
- return this.toHex() === new Bytes(other).toHex();
94
- }
95
- represent() {
96
- return this.toHex();
97
- }
98
- }
99
- exports.Bytes = Bytes;
100
- function parseSizeToBytes(sizeStr) {
101
- const units = {
102
- B: 1,
103
- kB: 1000,
104
- MB: 1000 ** 2,
105
- GB: 1000 ** 3,
106
- TB: 1000 ** 4,
107
- PB: 1000 ** 5,
108
- };
109
- const match = sizeStr.match(/^([\d.]+)\s*(B|kB|MB|GB|TB|PB)$/);
110
- if (!match) {
111
- throw new Error(`Invalid size format: ${sizeStr}`);
112
- }
113
- const value = parseFloat(match[1]);
114
- const unit = match[2];
115
- return Math.ceil(value * units[unit]);
116
- }
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SWARM_FEED_CODEC = exports.SWARM_MANIFEST_CODEC = void 0;
4
- exports.convertReferenceToCid = convertReferenceToCid;
5
- exports.convertCidToReference = convertCidToReference;
6
- const cafe_utility_1 = require("cafe-utility");
7
- const typed_bytes_1 = require("./typed-bytes");
8
- exports.SWARM_MANIFEST_CODEC = 0xfa;
9
- exports.SWARM_FEED_CODEC = 0xfb;
10
- const CODEC_TABLE = {
11
- [exports.SWARM_MANIFEST_CODEC]: 'manifest',
12
- [exports.SWARM_FEED_CODEC]: 'feed',
13
- };
14
- function convertReferenceToCid(reference, type) {
15
- reference = new typed_bytes_1.Reference(reference);
16
- const base32 = 'b';
17
- const version = new Uint8Array([1]);
18
- const codec = new Uint8Array([type === 'feed' ? exports.SWARM_FEED_CODEC : exports.SWARM_MANIFEST_CODEC]);
19
- const unknown = new Uint8Array([1]);
20
- const sha256 = new Uint8Array([27]);
21
- const size = new Uint8Array([32]);
22
- const header = cafe_utility_1.Binary.uint8ArrayToBase32(cafe_utility_1.Binary.concatBytes(version, codec, unknown, sha256, size)).replace(/\=+$/, '');
23
- const hash = reference.toBase32().replace(/\=+$/, '');
24
- return `${base32}${header}${hash}`.toLowerCase();
25
- }
26
- function convertCidToReference(cid) {
27
- const bytes = cafe_utility_1.Binary.base32ToUint8Array(cid.toUpperCase().slice(1));
28
- const codec = bytes[1];
29
- if (!CODEC_TABLE[codec]) {
30
- throw new Error('Unknown codec');
31
- }
32
- const reference = new typed_bytes_1.Reference(bytes.slice(-32));
33
- return {
34
- type: CODEC_TABLE[codec],
35
- reference,
36
- };
37
- }
@@ -1,133 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.approximateOverheadForRedundancyLevel = approximateOverheadForRedundancyLevel;
4
- exports.getRedundancyStats = getRedundancyStats;
5
- exports.getRedundancyStat = getRedundancyStat;
6
- const __1 = require("..");
7
- const mediumTable = [
8
- [94, 68, 46, 28, 14, 5, 1],
9
- [9, 8, 7, 6, 5, 4, 3],
10
- ];
11
- const encMediumTable = [
12
- [47, 34, 23, 14, 7, 2],
13
- [9, 8, 7, 6, 5, 4],
14
- ];
15
- const strongTable = [
16
- [104, 95, 86, 77, 69, 61, 53, 46, 39, 32, 26, 20, 15, 10, 6, 3, 1],
17
- [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5],
18
- ];
19
- const encStrongTable = [
20
- [52, 47, 43, 38, 34, 30, 26, 23, 19, 16, 13, 10, 7, 5, 3, 1],
21
- [21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
22
- ];
23
- const insaneTable = [
24
- [92, 87, 82, 77, 73, 68, 63, 59, 54, 50, 45, 41, 37, 33, 29, 26, 22, 19, 16, 13, 10, 8, 5, 3, 2, 1],
25
- [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6],
26
- ];
27
- const encInsaneTable = [
28
- [46, 43, 41, 38, 36, 34, 31, 29, 27, 25, 22, 20, 18, 16, 14, 13, 11, 9, 8, 6, 5, 4, 2, 1],
29
- [31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 7],
30
- ];
31
- const paranoidTable = [
32
- [
33
- 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9,
34
- 8, 7, 6, 5, 4, 3, 2, 1,
35
- ],
36
- [
37
- 90, 88, 87, 85, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 60, 59, 57, 55, 53, 51, 49, 48, 46, 44, 41,
38
- 39, 37, 35, 32, 30, 27, 24, 20,
39
- ],
40
- ];
41
- const encParanoidTable = [
42
- [18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
43
- [88, 85, 82, 79, 76, 72, 69, 66, 62, 59, 55, 51, 48, 44, 39, 35, 30, 24],
44
- ];
45
- /**
46
- * Returns an approximate multiplier for the overhead of a given redundancy level.
47
- * Redundancy level is a tradeoff between storage overhead and fault tolerance.
48
- * Use this number to estimate the amount of chunks that will be stored for a given
49
- * redundancy level.
50
- */
51
- function approximateOverheadForRedundancyLevel(chunks, level, encrypted) {
52
- if (level === __1.RedundancyLevel.OFF) {
53
- return 0;
54
- }
55
- const table = selectTable(level, encrypted);
56
- const [supportedChunks, parities] = table;
57
- for (let i = 0; i < supportedChunks.length; i++) {
58
- if (chunks >= supportedChunks[i]) {
59
- return parities[i] / supportedChunks[i];
60
- }
61
- }
62
- return parities[parities.length - 1] / supportedChunks[supportedChunks.length - 1];
63
- }
64
- function selectTable(level, encrypted) {
65
- switch (level) {
66
- case __1.RedundancyLevel.MEDIUM:
67
- return encrypted ? encMediumTable : mediumTable;
68
- case __1.RedundancyLevel.STRONG:
69
- return encrypted ? encStrongTable : strongTable;
70
- case __1.RedundancyLevel.INSANE:
71
- return encrypted ? encInsaneTable : insaneTable;
72
- case __1.RedundancyLevel.PARANOID:
73
- return encrypted ? encParanoidTable : paranoidTable;
74
- default:
75
- throw new Error(`Unknown redundancy level ${level}`);
76
- }
77
- }
78
- const medium = {
79
- label: 'medium',
80
- value: __1.RedundancyLevel.MEDIUM,
81
- errorTolerance: 0.01,
82
- };
83
- const strong = {
84
- label: 'strong',
85
- value: __1.RedundancyLevel.STRONG,
86
- errorTolerance: 0.05,
87
- };
88
- const insane = {
89
- label: 'insane',
90
- value: __1.RedundancyLevel.INSANE,
91
- errorTolerance: 0.1,
92
- };
93
- const paranoid = {
94
- label: 'paranoid',
95
- value: __1.RedundancyLevel.PARANOID,
96
- errorTolerance: 0.5,
97
- };
98
- function getRedundancyStats() {
99
- return {
100
- medium,
101
- strong,
102
- insane,
103
- paranoid,
104
- };
105
- }
106
- function getRedundancyStat(level) {
107
- if (typeof level === 'string') {
108
- switch (level.toLowerCase()) {
109
- case 'medium':
110
- return medium;
111
- case 'strong':
112
- return strong;
113
- case 'insane':
114
- return insane;
115
- case 'paranoid':
116
- return paranoid;
117
- default:
118
- throw new Error(`Unknown redundancy level '${level}'`);
119
- }
120
- }
121
- switch (level) {
122
- case __1.RedundancyLevel.MEDIUM:
123
- return medium;
124
- case __1.RedundancyLevel.STRONG:
125
- return strong;
126
- case __1.RedundancyLevel.INSANE:
127
- return insane;
128
- case __1.RedundancyLevel.PARANOID:
129
- return paranoid;
130
- default:
131
- throw new Error(`Unknown redundancy level '${level}'`);
132
- }
133
- }