@ethersphere/bee-js 9.0.3 → 9.1.1

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/cjs/bee.js CHANGED
@@ -84,6 +84,7 @@ class Bee {
84
84
  if (options?.signer) {
85
85
  this.signer = new typed_bytes_1.PrivateKey(options.signer);
86
86
  }
87
+ this.network = options?.network ?? 'gnosis';
87
88
  this.requestOptions = {
88
89
  baseURL: this.url,
89
90
  timeout: options?.timeout ?? 0,
@@ -1161,7 +1162,7 @@ class Bee {
1161
1162
  }
1162
1163
  async buyStorage(size, duration, options, requestOptions) {
1163
1164
  const chainState = await this.getChainState(requestOptions);
1164
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1165
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1165
1166
  const depth = (0, stamps_1.getDepthForSize)(size);
1166
1167
  if (options) {
1167
1168
  options = (0, type_1.preparePostageBatchOptions)(options);
@@ -1170,7 +1171,7 @@ class Bee {
1170
1171
  }
1171
1172
  async getStorageCost(size, duration, options) {
1172
1173
  const chainState = await this.getChainState(options);
1173
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1174
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1174
1175
  const depth = (0, stamps_1.getDepthForSize)(size);
1175
1176
  return (0, stamps_1.getStampCost)(depth, amount);
1176
1177
  }
@@ -1187,13 +1188,13 @@ class Bee {
1187
1188
  async extendStorageDuration(postageBatchId, duration, options) {
1188
1189
  const batch = await this.getPostageBatch(postageBatchId, options);
1189
1190
  const chainState = await this.getChainState(options);
1190
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1191
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1191
1192
  return this.topUpBatch(batch.batchID, amount, options);
1192
1193
  }
1193
1194
  async getExtensionCost(postageBatchId, size, duration, options) {
1194
1195
  const batch = await this.getPostageBatch(postageBatchId, options);
1195
1196
  const chainState = await this.getChainState(options);
1196
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1197
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1197
1198
  const depth = (0, stamps_1.getDepthForSize)(size);
1198
1199
  const currentValue = (0, stamps_1.getStampCost)(batch.depth, batch.amount);
1199
1200
  const newValue = (0, stamps_1.getStampCost)(depth, amount);
@@ -1213,7 +1214,7 @@ class Bee {
1213
1214
  async getDurationExtensionCost(postageBatchId, duration, options) {
1214
1215
  const batch = await this.getPostageBatch(postageBatchId, options);
1215
1216
  const chainState = await this.getChainState(options);
1216
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1217
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1217
1218
  return (0, stamps_1.getStampCost)(batch.depth, amount);
1218
1219
  }
1219
1220
  /**
@@ -20,6 +20,22 @@ class Fork {
20
20
  }
21
21
  static split(a, b) {
22
22
  const commonPart = cafe_utility_1.Binary.commonPrefix(a.prefix, b.prefix);
23
+ if (commonPart.length === a.prefix.length) {
24
+ const remainingB = b.prefix.slice(commonPart.length);
25
+ b.node.path = b.prefix.slice(commonPart.length);
26
+ b.prefix = b.prefix.slice(commonPart.length);
27
+ b.node.parent = a.node;
28
+ a.node.forks.set(remainingB[0], b);
29
+ return a;
30
+ }
31
+ if (commonPart.length === b.prefix.length) {
32
+ const remainingA = a.prefix.slice(commonPart.length);
33
+ a.node.path = a.prefix.slice(commonPart.length);
34
+ a.prefix = a.prefix.slice(commonPart.length);
35
+ a.node.parent = b.node;
36
+ b.node.forks.set(remainingA[0], a);
37
+ return b;
38
+ }
23
39
  const node = new MantarayNode({ path: commonPart });
24
40
  const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node);
25
41
  const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node);
@@ -58,13 +74,13 @@ class Fork {
58
74
  const prefixLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
59
75
  const prefix = reader.read(prefixLength);
60
76
  reader.read(30 - prefixLength);
61
- const targetAddress = reader.read(32);
77
+ const selfAddress = reader.read(32);
62
78
  let metadata = undefined;
63
79
  if (isType(type, TYPE_WITH_METADATA)) {
64
80
  const metadataLength = cafe_utility_1.Binary.uint16ToNumber(reader.read(2), 'BE');
65
81
  metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
66
82
  }
67
- return new Fork(prefix, new MantarayNode({ targetAddress, metadata, path: prefix }));
83
+ return new Fork(prefix, new MantarayNode({ selfAddress, metadata, path: prefix }));
68
84
  }
69
85
  }
70
86
  exports.Fork = Fork;
@@ -175,15 +191,16 @@ class MantarayNode {
175
191
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
176
192
  */
177
193
  static async unmarshal(bee, reference, options, requestOptions) {
194
+ reference = new typed_bytes_1.Reference(reference);
178
195
  const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
179
- return this.unmarshalFromData(data);
196
+ return this.unmarshalFromData(data, reference.toUint8Array());
180
197
  }
181
198
  /**
182
199
  * Unmarshals a MantarayNode from the given data.
183
200
  *
184
201
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
185
202
  */
186
- static unmarshalFromData(data) {
203
+ static unmarshalFromData(data, selfAddress) {
187
204
  const obfuscationKey = data.subarray(0, 32);
188
205
  const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
189
206
  const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
@@ -193,7 +210,7 @@ class MantarayNode {
193
210
  }
194
211
  const targetAddressLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
195
212
  const targetAddress = targetAddressLength === 0 ? __1.NULL_ADDRESS : reader.read(targetAddressLength);
196
- const node = new MantarayNode({ targetAddress, obfuscationKey });
213
+ const node = new MantarayNode({ selfAddress, targetAddress, obfuscationKey });
197
214
  const forkBitmap = reader.read(32);
198
215
  for (let i = 0; i < 256; i++) {
199
216
  if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
@@ -290,7 +307,10 @@ class MantarayNode {
290
307
  */
291
308
  async loadRecursively(bee, options, requestOptions) {
292
309
  for (const fork of this.forks.values()) {
293
- const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
310
+ if (!fork.node.selfAddress) {
311
+ throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
312
+ }
313
+ const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, options, requestOptions);
294
314
  fork.node.targetAddress = node.targetAddress;
295
315
  fork.node.forks = node.forks;
296
316
  fork.node.path = fork.prefix;
@@ -98,7 +98,7 @@ exports.getStampCost = getStampCost;
98
98
  *
99
99
  * @returns {number} The TTL of the postage batch.
100
100
  */
101
- function getStampDuration(amount, pricePerBlock, blockTime = 5) {
101
+ function getStampDuration(amount, pricePerBlock, blockTime) {
102
102
  const amountBigInt = BigInt((0, type_1.asNumberString)(amount));
103
103
  return duration_1.Duration.fromSeconds(Number((amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock)));
104
104
  }
@@ -110,7 +110,7 @@ exports.getStampDuration = getStampDuration;
110
110
  * @param pricePerBlock The price per block in PLUR.
111
111
  * @param blockTime The block time in seconds.
112
112
  */
113
- function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
113
+ function getAmountForDuration(duration, pricePerBlock, blockTime) {
114
114
  return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock);
115
115
  }
116
116
  exports.getAmountForDuration = getAmountForDuration;