@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,152 +1,98 @@
1
- import { Binary, MerkleTree, Optional, Uint8ArrayReader } from 'cafe-utility';
2
- import _debug from 'debug';
3
- import { NULL_ADDRESS } from "../index.js";
4
- import { Reference } from "../utils/typed-bytes.js";
5
- const debug = _debug('bee-js:manifest');
6
- const ENCODER = new TextEncoder();
7
- const DECODER = new TextDecoder();
8
- const TYPE_VALUE = 2;
9
- const TYPE_EDGE = 4;
10
- const TYPE_WITH_PATH_SEPARATOR = 8;
11
- const TYPE_WITH_METADATA = 16;
12
- const PATH_SEPARATOR = new Uint8Array([47]);
13
- const VERSION_02_HASH_HEX = '5768b3b6a7db56d21d1abff40d41cebfc83448fed8d7e9b06ec0d3b073f28f7b';
14
- const VERSION_02_HASH = Binary.hexToUint8Array(VERSION_02_HASH_HEX);
15
- export class Fork {
16
- constructor(prefix, node) {
17
- this.prefix = prefix;
18
- this.node = node;
19
- }
20
- static split(a, b) {
21
- const commonPart = Binary.commonPrefix(a.prefix, b.prefix);
22
- if (commonPart.length === a.prefix.length) {
23
- const remainingB = b.prefix.slice(commonPart.length);
24
- b.node.path = b.prefix.slice(commonPart.length);
25
- b.prefix = b.prefix.slice(commonPart.length);
26
- b.node.parent = a.node;
27
- a.node.forks.set(remainingB[0], b);
28
- return a;
29
- }
30
- if (commonPart.length === b.prefix.length) {
31
- const remainingA = a.prefix.slice(commonPart.length);
32
- a.node.path = a.prefix.slice(commonPart.length);
33
- a.prefix = a.prefix.slice(commonPart.length);
34
- a.node.parent = b.node;
35
- b.node.forks.set(remainingA[0], a);
36
- return b;
37
- }
38
- const node = new MantarayNode({
39
- path: commonPart
40
- });
41
- const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node);
42
- const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node);
43
- a.node.path = a.prefix.slice(commonPart.length);
44
- b.node.path = b.prefix.slice(commonPart.length);
45
- a.prefix = a.prefix.slice(commonPart.length);
46
- b.prefix = b.prefix.slice(commonPart.length);
47
- node.forks.set(newAFork.prefix[0], newAFork);
48
- node.forks.set(newBFork.prefix[0], newBFork);
49
- newAFork.node.parent = node;
50
- newBFork.node.parent = node;
51
- return new Fork(commonPart, node);
52
- }
53
- marshal() {
54
- if (!this.node.selfAddress) {
55
- throw Error('Fork#marshal node.selfAddress is not set');
56
- }
57
- const data = [];
58
- // Re-emit the type byte read from the chunk when the node is untouched, so an
59
- // unmarshal → marshal round-trip is byte-identical. determineType() can only
60
- // recompute it correctly once the node's children are loaded/built in memory.
61
- data.push(new Uint8Array([this.node.type ?? this.node.determineType()]));
62
- data.push(Binary.numberToUint8(this.prefix.length));
63
- data.push(this.prefix);
64
- if (this.prefix.length < 30) {
65
- data.push(new Uint8Array(30 - this.prefix.length));
66
- }
67
- data.push(this.node.selfAddress);
68
- debug('marshalling fork', {
69
- prefixLength: this.prefix.length,
70
- prefix: DECODER.decode(this.prefix),
71
- address: Binary.uint8ArrayToHex(this.node.selfAddress)
72
- });
73
- if (this.node.metadata) {
74
- const metadataBytes = Binary.padEndToMultiple(new Uint8Array([0x00, 0x00, ...ENCODER.encode(JSON.stringify(this.node.metadata))]), 32, 0x0a);
75
- const metadataLengthBytes = Binary.numberToUint16(metadataBytes.length - 2, 'BE');
76
- metadataBytes.set(metadataLengthBytes, 0);
77
- data.push(metadataBytes);
1
+ import { Optional } from 'cafe-utility';
2
+ import { Bytes, MantarayNode as CoreMantarayNode, Reference } from '@ethersphere/core-sdk';
3
+ /**
4
+ * Uploads `node` and every node beneath it, updating each `selfAddress` in
5
+ * place. Only the network/ACT-history side is bee-js specific - the trie
6
+ * structure and marshaling live entirely in core-sdk's MantarayNode.
7
+ */
8
+ async function saveRecursively(node, bee, postageBatchId, options, requestOptions) {
9
+ for (const fork of node.forks.values()) {
10
+ const uploadResult = await saveRecursively(fork.node, bee, postageBatchId, options, requestOptions);
11
+ if (options?.act) {
12
+ let historyAddress;
13
+ uploadResult.historyAddress.ifPresent(ref => historyAddress = ref);
14
+ if (historyAddress) {
15
+ if (!fork.node.metadata) {
16
+ fork.node.metadata = {};
17
+ }
18
+ fork.node.metadata['swarm-act-history-address'] = historyAddress.toHex();
19
+ }
78
20
  }
79
- return Binary.concatBytes(...data);
80
21
  }
81
- static unmarshal(reader, addressLength) {
82
- const type = Binary.uint8ToNumber(reader.read(1));
83
- const prefixLength = Binary.uint8ToNumber(reader.read(1));
84
- const prefix = reader.read(prefixLength);
85
- if (prefixLength < 30) {
86
- reader.read(30 - prefixLength);
87
- }
88
- const selfAddress = reader.read(addressLength);
89
- debug('unmarshalling fork', {
90
- type,
91
- prefixLength,
92
- prefix: DECODER.decode(prefix),
93
- addressLength,
94
- address: Binary.uint8ArrayToHex(selfAddress)
95
- });
96
- let metadata = undefined;
97
- if (isType(type, TYPE_WITH_METADATA)) {
98
- const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE');
99
- metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
22
+ const result = await bee.data.upload(postageBatchId, await node.marshal(), options, requestOptions);
23
+ node.selfAddress = result.reference.toUint8Array();
24
+ return result;
25
+ }
26
+ /**
27
+ * Downloads and unmarshals every node beneath `node`, following ACT history
28
+ * metadata left by {@link saveRecursively} where present.
29
+ */
30
+ async function loadRecursively(node, bee, options, requestOptions) {
31
+ for (const fork of node.forks.values()) {
32
+ if (!fork.node.selfAddress) {
33
+ throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
34
+ }
35
+ let downloadOptions = options;
36
+ if (fork.node.metadata && fork.node.metadata['swarm-act-history-address']) {
37
+ downloadOptions = {
38
+ ...options,
39
+ actHistoryAddress: fork.node.metadata['swarm-act-history-address']
40
+ };
100
41
  }
101
- return new Fork(prefix, new MantarayNode({
102
- selfAddress,
103
- metadata,
104
- path: prefix,
105
- type
106
- }));
42
+ const data = (await bee.data.download(fork.node.selfAddress, downloadOptions, requestOptions)).toUint8Array();
43
+ const loaded = CoreMantarayNode.unmarshalFromData(data, fork.node.selfAddress);
44
+ fork.node.targetAddress = loaded.targetAddress;
45
+ fork.node.forks = loaded.forks;
46
+ fork.node.path = fork.prefix;
47
+ fork.node.parent = node;
48
+ await loadRecursively(fork.node, bee, options, requestOptions);
107
49
  }
108
50
  }
51
+ /**
52
+ * bee-js's Bee-client-coupled view of a Mantaray node: the trie structure,
53
+ * marshaling, and byte-level operations all delegate to core-sdk's
54
+ * `MantarayNode`; this class only adds what needs a live `Bee` instance
55
+ * (uploading/downloading, ACT history, feed resolution).
56
+ */
109
57
  export class MantarayNode {
110
- constructor(options) {
111
- this.obfuscationKey = new Uint8Array(32);
112
- this.selfAddress = null;
113
- this.targetAddress = new Uint8Array(32);
114
- this.metadata = null;
115
- this.path = new Uint8Array(0);
116
- this.forks = new Map();
117
- this.parent = null;
118
- this.type = null;
119
- if (options?.targetAddress) {
120
- this.targetAddress = options.targetAddress;
121
- }
122
- if (options?.selfAddress) {
123
- this.selfAddress = options.selfAddress;
124
- }
125
- if (options?.metadata) {
126
- this.metadata = options.metadata;
127
- }
128
- if (options?.obfuscationKey) {
129
- this.obfuscationKey = options.obfuscationKey;
130
- }
131
- if (options?.path) {
132
- this.path = options.path;
133
- }
134
- if (options?.parent) {
135
- this.parent = options.parent;
136
- }
137
- this.type = options?.type ?? null;
58
+ constructor(core = new CoreMantarayNode()) {
59
+ this.core = core;
60
+ }
61
+ get obfuscationKey() {
62
+ return this.core.obfuscationKey;
63
+ }
64
+ get selfAddress() {
65
+ return this.core.selfAddress;
66
+ }
67
+ get targetAddress() {
68
+ return this.core.targetAddress;
69
+ }
70
+ get metadata() {
71
+ return this.core.metadata;
72
+ }
73
+ get path() {
74
+ return this.core.path;
75
+ }
76
+ get forks() {
77
+ return this.core.forks;
78
+ }
79
+ get parent() {
80
+ return this.core.parent;
81
+ }
82
+ get type() {
83
+ return this.core.type;
138
84
  }
139
85
  get fullPath() {
140
- return Binary.concatBytes(this.parent?.fullPath ?? new Uint8Array(0), this.path);
86
+ return this.core.fullPath;
141
87
  }
142
88
  get fullPathString() {
143
- return DECODER.decode(this.fullPath);
89
+ return this.core.fullPathString;
144
90
  }
145
91
  /**
146
92
  * Returns the metadata at the `/` path to access idiomatic properties.
147
93
  */
148
94
  getRootMetadata() {
149
- const node = this.find('/');
95
+ const node = this.core.find('/');
150
96
  if (node && node.metadata) {
151
97
  return Optional.of(node.metadata);
152
98
  }
@@ -156,7 +102,7 @@ export class MantarayNode {
156
102
  * Returns the `swarm-index-document` and `swarm-error-document` metadata values.
157
103
  */
158
104
  getDocsMetadata() {
159
- const node = this.find('/');
105
+ const node = this.core.find('/');
160
106
  if (!node || !node.metadata) {
161
107
  return {
162
108
  indexDocument: null,
@@ -172,7 +118,7 @@ export class MantarayNode {
172
118
  * Attempts to resolve the manifest as a feed, returning the latest update.
173
119
  */
174
120
  async resolveFeed(bee, requestOptions) {
175
- const node = this.find('/');
121
+ const node = this.core.find('/');
176
122
  if (!node || !node.metadata) {
177
123
  return Optional.empty();
178
124
  }
@@ -187,39 +133,7 @@ export class MantarayNode {
187
133
  * Gets the binary representation of the node.
188
134
  */
189
135
  async marshal() {
190
- for (const fork of this.forks.values()) {
191
- if (!fork.node.selfAddress) {
192
- fork.node.selfAddress = (await fork.node.calculateSelfAddress()).toUint8Array();
193
- }
194
- }
195
- const hasEntry = !Binary.equals(this.targetAddress, NULL_ADDRESS);
196
- let refBytesSize = 0;
197
- if (hasEntry) {
198
- refBytesSize = this.targetAddress.length;
199
- } else {
200
- for (const fork of this.forks.values()) {
201
- if (fork.node.selfAddress) {
202
- refBytesSize = fork.node.selfAddress.length;
203
- break;
204
- }
205
- }
206
- }
207
- const header = new Uint8Array(32);
208
- header.set(VERSION_02_HASH, 0);
209
- header.set(Binary.numberToUint8(refBytesSize), 31);
210
- const entry = hasEntry ? this.targetAddress : new Uint8Array(refBytesSize);
211
- const forkBitmap = new Uint8Array(32);
212
- for (const fork of this.forks.keys()) {
213
- Binary.setBit(forkBitmap, fork, 1, 'LE');
214
- }
215
- const forks = [];
216
- for (let i = 0; i < 256; i++) {
217
- if (Binary.getBit(forkBitmap, i, 'LE')) {
218
- forks.push(this.forks.get(i).marshal());
219
- }
220
- }
221
- const data = Binary.xorCypher(Binary.concatBytes(header, entry, forkBitmap, ...forks), this.obfuscationKey);
222
- return Binary.concatBytes(this.obfuscationKey, data);
136
+ return this.core.marshal();
223
137
  }
224
138
  /**
225
139
  * Downloads and unmarshals a MantarayNode from the given reference.
@@ -237,173 +151,49 @@ export class MantarayNode {
237
151
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
238
152
  */
239
153
  static unmarshalFromData(data, selfAddress) {
240
- const obfuscationKey = data.subarray(0, 32);
241
- const decrypted = Binary.xorCypher(data.subarray(32), obfuscationKey);
242
- const reader = new Uint8ArrayReader(decrypted);
243
- const versionHash = reader.read(31);
244
- if (!Binary.equals(versionHash, VERSION_02_HASH.slice(0, 31))) {
245
- throw new Error('MantarayNode#unmarshal invalid version hash');
246
- }
247
- const targetAddressLength = Binary.uint8ToNumber(reader.read(1));
248
- const targetAddress = targetAddressLength ? reader.read(targetAddressLength) : NULL_ADDRESS;
249
- const node = new MantarayNode({
250
- selfAddress,
251
- targetAddress,
252
- obfuscationKey
253
- });
254
- const forkBitmap = reader.read(32);
255
- if (targetAddressLength > 0) {
256
- for (let i = 0; i < 256; i++) {
257
- if (Binary.getBit(forkBitmap, i, 'LE')) {
258
- const newFork = Fork.unmarshal(reader, selfAddress.length);
259
- node.forks.set(i, newFork);
260
- newFork.node.parent = node;
261
- }
262
- }
263
- }
264
- return node;
154
+ return new MantarayNode(CoreMantarayNode.unmarshalFromData(data, selfAddress));
265
155
  }
266
156
  /**
267
157
  * Adds a fork to the node.
268
158
  */
269
159
  addFork(path, reference, metadata) {
270
- this.selfAddress = null;
271
- this.type = null;
272
- path = path instanceof Uint8Array ? path : ENCODER.encode(path);
273
- debug('adding fork', {
274
- path: DECODER.decode(path),
275
- reference: new Reference(reference).represent()
276
- });
277
- // TODO: this should not be ignored
278
- // eslint-disable-next-line @typescript-eslint/no-this-alias
279
- let tip = this;
280
- while (path.length) {
281
- const prefix = path.slice(0, 30);
282
- path = path.slice(30);
283
- const isLast = path.length === 0;
284
- const [bestMatch, matchedPath] = tip.findClosest(prefix);
285
- const remainingPath = prefix.slice(matchedPath.length);
286
- if (matchedPath.length) {
287
- tip = bestMatch;
288
- }
289
- if (!remainingPath.length) {
290
- continue;
291
- }
292
- const newFork = new Fork(remainingPath, new MantarayNode({
293
- targetAddress: isLast ? new Reference(reference).toUint8Array() : undefined,
294
- metadata: isLast ? metadata : undefined,
295
- path: remainingPath
296
- }));
297
- const existing = bestMatch.forks.get(remainingPath[0]);
298
- if (existing) {
299
- const fork = Fork.split(newFork, existing);
300
- tip.forks.set(remainingPath[0], fork);
301
- fork.node.parent = tip;
302
- tip.selfAddress = null;
303
- tip.type = null;
304
- tip = newFork.node;
305
- } else {
306
- tip.forks.set(remainingPath[0], newFork);
307
- newFork.node.parent = tip;
308
- tip.selfAddress = null;
309
- tip.type = null;
310
- tip = newFork.node;
311
- }
312
- }
160
+ this.core.addFork(path, reference instanceof Bytes ? reference.toUint8Array() : reference, metadata);
313
161
  }
314
162
  /**
315
163
  * Removes a fork from the node.
316
164
  */
317
165
  removeFork(path) {
318
- this.selfAddress = null;
319
- this.type = null;
320
- path = path instanceof Uint8Array ? path : ENCODER.encode(path);
321
- if (path.length === 0) {
322
- throw Error('MantarayNode#removeFork [path] parameter cannot be empty');
323
- }
324
- const match = this.find(path);
325
- if (!match) {
326
- throw Error('MantarayNode#removeFork fork not found');
327
- }
328
- const [parent, matchedPath] = this.findClosest(path.slice(0, path.length - 1));
329
- parent.forks.delete(path.slice(matchedPath.length)[0]);
330
- for (const fork of match.forks.values()) {
331
- parent.addFork(Binary.concatBytes(match.path, fork.prefix), fork.node.targetAddress, fork.node.metadata);
332
- }
166
+ this.core.removeFork(path);
333
167
  }
334
168
  /**
335
169
  * Calculates the self address of the node.
336
170
  */
337
171
  async calculateSelfAddress() {
338
- if (this.selfAddress) {
339
- return new Reference(this.selfAddress);
340
- }
341
- return new Reference((await MerkleTree.root(await this.marshal())).hash());
172
+ return this.core.calculateSelfAddress();
342
173
  }
343
174
  /**
344
175
  * Saves the node and its children recursively.
345
176
  */
346
177
  async saveRecursively(bee, postageBatchId, options, requestOptions) {
347
- for (const fork of this.forks.values()) {
348
- const uploadResult = await fork.node.saveRecursively(bee, postageBatchId, options, requestOptions);
349
- if (options?.act) {
350
- let historyAddress;
351
- uploadResult.historyAddress.ifPresent(ref => historyAddress = ref);
352
- if (historyAddress) {
353
- if (!fork.node.metadata) {
354
- fork.node.metadata = {};
355
- }
356
- fork.node.metadata['swarm-act-history-address'] = historyAddress.toHex();
357
- }
358
- }
359
- }
360
- const result = await bee.data.upload(postageBatchId, await this.marshal(), options, requestOptions);
361
- this.selfAddress = result.reference.toUint8Array();
362
- return result;
178
+ return saveRecursively(this.core, bee, postageBatchId, options, requestOptions);
363
179
  }
364
180
  /**
365
181
  * Loads the node and its children recursively.
366
182
  */
367
183
  async loadRecursively(bee, options, requestOptions) {
368
- for (const fork of this.forks.values()) {
369
- if (!fork.node.selfAddress) {
370
- throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
371
- }
372
- let downloadOptions = options;
373
- if (fork.node.metadata && fork.node.metadata['swarm-act-history-address']) {
374
- downloadOptions = {
375
- ...options,
376
- actHistoryAddress: fork.node.metadata['swarm-act-history-address']
377
- };
378
- }
379
- const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, downloadOptions, requestOptions);
380
- fork.node.targetAddress = node.targetAddress;
381
- fork.node.forks = node.forks;
382
- fork.node.path = fork.prefix;
383
- fork.node.parent = this;
384
- await fork.node.loadRecursively(bee, options, requestOptions);
385
- }
184
+ return loadRecursively(this.core, bee, options, requestOptions);
386
185
  }
387
186
  /**
388
187
  * Finds a node in the tree by its path.
389
188
  */
390
189
  find(path) {
391
- const [closest, match] = this.findClosest(path);
392
- return match.length === path.length ? closest : null;
190
+ return this.core.find(path);
393
191
  }
394
192
  /**
395
193
  * Finds the closest node in the tree to the given path.
396
194
  */
397
- findClosest(path, current = new Uint8Array()) {
398
- path = path instanceof Uint8Array ? path : ENCODER.encode(path);
399
- if (path.length === 0) {
400
- return [this, current];
401
- }
402
- const fork = this.forks.get(path[0]);
403
- if (fork && Binary.commonPrefix(fork.prefix, path).length === fork.prefix.length) {
404
- return fork.node.findClosest(path.slice(fork.prefix.length), Binary.concatBytes(current, fork.prefix));
405
- }
406
- return [this, current];
195
+ findClosest(path, current) {
196
+ return this.core.findClosest(path, current);
407
197
  }
408
198
  /**
409
199
  * Returns an array of all nodes in the tree which have a target address set.
@@ -411,13 +201,7 @@ export class MantarayNode {
411
201
  * Must be called after `loadRecursively`.
412
202
  */
413
203
  collect(nodes = []) {
414
- for (const fork of this.forks.values()) {
415
- if (!Binary.equals(fork.node.targetAddress, NULL_ADDRESS)) {
416
- nodes.push(fork.node);
417
- }
418
- fork.node.collect(nodes);
419
- }
420
- return nodes;
204
+ return this.core.collect(nodes);
421
205
  }
422
206
  /**
423
207
  * Returns a path:reference map of all nodes in the tree which have a target address set.
@@ -425,37 +209,9 @@ export class MantarayNode {
425
209
  * Must be called after `loadRecursively`.
426
210
  */
427
211
  collectAndMap() {
428
- const nodes = this.collect();
429
- const result = {};
430
- for (const node of nodes) {
431
- result[node.fullPathString] = new Reference(node.targetAddress).toHex();
432
- }
433
- return result;
212
+ return this.core.collectAndMap();
434
213
  }
435
214
  determineType() {
436
- let type = 0;
437
- // Mirror Bee (pkg/manifest/mantaray/node.go): Add() marks every explicitly
438
- // added leaf as a value (makeValue), even one with a null entry such as a
439
- // metadata-only "/" node. In final-state terms a leaf (no forks) is always an
440
- // added entry, so it is a value; a node with forks is a value only when it also
441
- // carries an entry. The path-separator flag is set only when a separator occurs
442
- // past the first byte (IndexRune > 0), so a prefix that merely starts with '/'
443
- // does not qualify.
444
- if (!Binary.equals(this.targetAddress, NULL_ADDRESS) || this.forks.size === 0) {
445
- type |= TYPE_VALUE;
446
- }
447
- if (this.forks.size > 0) {
448
- type |= TYPE_EDGE;
449
- }
450
- if (Binary.indexOf(this.path, PATH_SEPARATOR) > 0) {
451
- type |= TYPE_WITH_PATH_SEPARATOR;
452
- }
453
- if (this.metadata) {
454
- type |= TYPE_WITH_METADATA;
455
- }
456
- return type;
215
+ return this.core.determineType();
457
216
  }
458
- }
459
- function isType(value, type) {
460
- return (value & type) === type;
461
217
  }
@@ -1,6 +1,6 @@
1
+ import { PeerAddress } from '@ethersphere/core-sdk';
1
2
  import * as balanceApi from "../api/balance.js";
2
3
  import * as consumedApi from "../api/consumed.js";
3
- import { PeerAddress } from "../utils/typed-bytes.js";
4
4
  /**
5
5
  * SWAP balance operations. Related to the bandwidth incentives and the chequebook.
6
6
  *
@@ -1,5 +1,5 @@
1
+ import { PeerAddress } from '@ethersphere/core-sdk';
1
2
  import { TransactionOptionsSchema } from "../utils/schema.js";
2
- import { PeerAddress } from "../utils/typed-bytes.js";
3
3
  import * as api from "../api/chequebook.js";
4
4
  /**
5
5
  * Cheque operations (last cheques and cashouts).
@@ -1,7 +1,7 @@
1
+ import { Identifier, Reference, Signature, Span } from '@ethersphere/core-sdk';
1
2
  import { CHUNK_SIZE } from "../types/index.js";
2
3
  import { BeeArgumentError } from "../utils/error.js";
3
4
  import { DownloadOptionsSchema, UploadOptionsSchema } from "../utils/schema.js";
4
- import { Identifier, Reference, Signature, Span } from "../utils/typed-bytes.js";
5
5
  import * as api from "../api/chunk.js";
6
6
  /**
7
7
  * Chunk operations backed by the `/chunks` endpoint.
@@ -1,8 +1,8 @@
1
+ import { BatchId } from '@ethersphere/core-sdk';
1
2
  import { hashDirectory as hashDirectoryImpl, streamDirectory as streamDirectoryImpl, streamFiles as streamFilesImpl } from "../utils/chunk-stream.js";
2
3
  import { assertCollection, makeCollectionFromFileList } from "../utils/collection.js";
3
4
  import { makeCollectionFromFS } from "../utils/collection.node.js";
4
5
  import { CollectionUploadOptionsSchema } from "../utils/schema.js";
5
- import { BatchId } from "../utils/typed-bytes.js";
6
6
  import * as api from "../api/bzz.js";
7
7
  /**
8
8
  * Collection (multi-file) operations backed by the `/bzz` endpoint.
@@ -1,4 +1,4 @@
1
- import { PeerAddress } from "../utils/typed-bytes.js";
1
+ import { PeerAddress } from '@ethersphere/core-sdk';
2
2
  import * as api from "../api/connectivity.js";
3
3
  /**
4
4
  * Peer, topology and network connectivity operations.
@@ -1,9 +1,9 @@
1
+ import { BatchId, Reference } from '@ethersphere/core-sdk';
2
+ import * as bytes from "../api/bytes.js";
3
+ import * as stewardship from "../api/stewardship.js";
1
4
  import { ResourceLocator } from "../utils/resource-locator.js";
2
5
  import { DownloadOptionsSchema, RedundantUploadOptionsSchema } from "../utils/schema.js";
3
6
  import { assertData } from "../utils/type.js";
4
- import { BatchId, Reference } from "../utils/typed-bytes.js";
5
- import * as bytes from "../api/bytes.js";
6
- import * as stewardship from "../api/stewardship.js";
7
7
  /**
8
8
  * Raw data operations backed by the `/bytes` endpoint.
9
9
  *
@@ -1,9 +1,9 @@
1
+ import { BatchId, EthAddress, PrivateKey, Topic } from '@ethersphere/core-sdk';
1
2
  import { Objects } from 'cafe-utility';
2
3
  import { createFeedManifest, fetchLatestFeedUpdate } from "../api/feed.js";
3
4
  import { makeFeedReader, makeFeedWriter } from "../feed/index.js";
4
5
  import { areAllSequentialFeedsUpdateRetrievable } from "../feed/retrievable.js";
5
6
  import { DownloadOptionsSchema, UploadOptionsSchema } from "../utils/schema.js";
6
- import { BatchId, EthAddress, PrivateKey, Topic } from "../utils/typed-bytes.js";
7
7
  /**
8
8
  * Feed operations.
9
9
  *
@@ -1,8 +1,8 @@
1
+ import { BatchId, Reference } from '@ethersphere/core-sdk';
1
2
  import { fileArrayBuffer, isFile } from "../utils/file.js";
2
3
  import { ResourceLocator } from "../utils/resource-locator.js";
3
4
  import { DownloadOptionsSchema, FileUploadOptionsSchema } from "../utils/schema.js";
4
5
  import { assertFileData } from "../utils/type.js";
5
- import { BatchId, Reference } from "../utils/typed-bytes.js";
6
6
  import * as api from "../api/bzz.js";
7
7
  /**
8
8
  * Single-file operations backed by the `/bzz` endpoint.
@@ -1,5 +1,5 @@
1
+ import { BatchId, PublicKey, Reference } from '@ethersphere/core-sdk';
1
2
  import * as api from "../api/grantee.js";
2
- import { BatchId, PublicKey, Reference } from "../utils/typed-bytes.js";
3
3
  /**
4
4
  * Grantee (access control) operations.
5
5
  *
@@ -1,14 +1,12 @@
1
1
  import { Binary, System } from 'cafe-utility';
2
2
  import WebSocket from 'isomorphic-ws';
3
- import { makeContentAddressedChunk } from "../chunk/cac.js";
4
- import { makeSOCAddress, uploadSingleOwnerChunk } from "../chunk/soc.js";
3
+ import { BatchId, Bytes, EthAddress, Identifier, makeContentAddressedChunk, numberToUint256, PeerAddress, PrivateKey, PublicKey } from '@ethersphere/core-sdk';
5
4
  import * as pssApi from "../api/pss.js";
6
- import { Bytes } from "../utils/bytes.js";
5
+ import { makeSOCAddress, uploadSingleOwnerChunk } from "../chunk/soc.js";
7
6
  import { prepareWebsocketData } from "../utils/data.js";
8
7
  import { BeeError } from "../utils/error.js";
9
8
  import { GsocMessageHandlerSchema, PssMessageHandlerSchema } from "../utils/schema.js";
10
9
  import { assertData } from "../utils/type.js";
11
- import { BatchId, EthAddress, Identifier, PeerAddress, PrivateKey, PublicKey } from "../utils/typed-bytes.js";
12
10
  const PSS_ENDPOINT = 'pss';
13
11
  const GSOC_ENDPOINT = 'gsoc';
14
12
  /**
@@ -122,7 +120,7 @@ export class Messaging {
122
120
  const id = new Identifier(identifier);
123
121
  const start = 0xb33n;
124
122
  for (let i = 0n; i < 0xffffn; i++) {
125
- const signer = new PrivateKey(Binary.numberToUint256(start + i, 'BE'));
123
+ const signer = new PrivateKey(numberToUint256(start + i, 'BE'));
126
124
  const socAddress = makeSOCAddress(id, signer.publicKey().address());
127
125
  // TODO: test the significance of the hardcoded 256
128
126
  const actualProximity = 256 - Binary.proximity(socAddress.toUint8Array(), overlay.toUint8Array());
@@ -1,6 +1,6 @@
1
+ import { BatchId, Reference } from '@ethersphere/core-sdk';
1
2
  import * as pinApi from "../api/pin.js";
2
3
  import * as stewardshipApi from "../api/stewardship.js";
3
- import { BatchId, Reference } from "../utils/typed-bytes.js";
4
4
  /**
5
5
  * Local pinning operations.
6
6
  *
@@ -1,4 +1,4 @@
1
- import { PeerAddress } from "../utils/typed-bytes.js";
1
+ import { PeerAddress } from '@ethersphere/core-sdk';
2
2
  import * as api from "../api/settlement.js";
3
3
  /**
4
4
  * Settlement operations. Related to the bandwidth incentives and the chequebook.