@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/mjs/bee.js CHANGED
@@ -58,6 +58,7 @@ export class Bee {
58
58
  if (options?.signer) {
59
59
  this.signer = new PrivateKey(options.signer);
60
60
  }
61
+ this.network = options?.network ?? 'gnosis';
61
62
  this.requestOptions = {
62
63
  baseURL: this.url,
63
64
  timeout: options?.timeout ?? 0,
@@ -1147,7 +1148,7 @@ export class Bee {
1147
1148
  }
1148
1149
  async buyStorage(size, duration, options, requestOptions) {
1149
1150
  const chainState = await this.getChainState(requestOptions);
1150
- const amount = getAmountForDuration(duration, chainState.currentPrice);
1151
+ const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1151
1152
  const depth = getDepthForSize(size);
1152
1153
  if (options) {
1153
1154
  options = preparePostageBatchOptions(options);
@@ -1156,7 +1157,7 @@ export class Bee {
1156
1157
  }
1157
1158
  async getStorageCost(size, duration, options) {
1158
1159
  const chainState = await this.getChainState(options);
1159
- const amount = getAmountForDuration(duration, chainState.currentPrice);
1160
+ const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1160
1161
  const depth = getDepthForSize(size);
1161
1162
  return getStampCost(depth, amount);
1162
1163
  }
@@ -1173,13 +1174,13 @@ export class Bee {
1173
1174
  async extendStorageDuration(postageBatchId, duration, options) {
1174
1175
  const batch = await this.getPostageBatch(postageBatchId, options);
1175
1176
  const chainState = await this.getChainState(options);
1176
- const amount = getAmountForDuration(duration, chainState.currentPrice);
1177
+ const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1177
1178
  return this.topUpBatch(batch.batchID, amount, options);
1178
1179
  }
1179
1180
  async getExtensionCost(postageBatchId, size, duration, options) {
1180
1181
  const batch = await this.getPostageBatch(postageBatchId, options);
1181
1182
  const chainState = await this.getChainState(options);
1182
- const amount = getAmountForDuration(duration, chainState.currentPrice);
1183
+ const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1183
1184
  const depth = getDepthForSize(size);
1184
1185
  const currentValue = getStampCost(batch.depth, batch.amount);
1185
1186
  const newValue = getStampCost(depth, amount);
@@ -1199,7 +1200,7 @@ export class Bee {
1199
1200
  async getDurationExtensionCost(postageBatchId, duration, options) {
1200
1201
  const batch = await this.getPostageBatch(postageBatchId, options);
1201
1202
  const chainState = await this.getChainState(options);
1202
- const amount = getAmountForDuration(duration, chainState.currentPrice);
1203
+ const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1203
1204
  return getStampCost(batch.depth, amount);
1204
1205
  }
1205
1206
  /**
@@ -17,6 +17,22 @@ export class Fork {
17
17
  }
18
18
  static split(a, b) {
19
19
  const commonPart = Binary.commonPrefix(a.prefix, b.prefix);
20
+ if (commonPart.length === a.prefix.length) {
21
+ const remainingB = b.prefix.slice(commonPart.length);
22
+ b.node.path = b.prefix.slice(commonPart.length);
23
+ b.prefix = b.prefix.slice(commonPart.length);
24
+ b.node.parent = a.node;
25
+ a.node.forks.set(remainingB[0], b);
26
+ return a;
27
+ }
28
+ if (commonPart.length === b.prefix.length) {
29
+ const remainingA = a.prefix.slice(commonPart.length);
30
+ a.node.path = a.prefix.slice(commonPart.length);
31
+ a.prefix = a.prefix.slice(commonPart.length);
32
+ a.node.parent = b.node;
33
+ b.node.forks.set(remainingA[0], a);
34
+ return b;
35
+ }
20
36
  const node = new MantarayNode({
21
37
  path: commonPart
22
38
  });
@@ -57,14 +73,14 @@ export class Fork {
57
73
  const prefixLength = Binary.uint8ToNumber(reader.read(1));
58
74
  const prefix = reader.read(prefixLength);
59
75
  reader.read(30 - prefixLength);
60
- const targetAddress = reader.read(32);
76
+ const selfAddress = reader.read(32);
61
77
  let metadata = undefined;
62
78
  if (isType(type, TYPE_WITH_METADATA)) {
63
79
  const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE');
64
80
  metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
65
81
  }
66
82
  return new Fork(prefix, new MantarayNode({
67
- targetAddress,
83
+ selfAddress,
68
84
  metadata,
69
85
  path: prefix
70
86
  }));
@@ -176,15 +192,16 @@ export class MantarayNode {
176
192
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
177
193
  */
178
194
  static async unmarshal(bee, reference, options, requestOptions) {
195
+ reference = new Reference(reference);
179
196
  const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
180
- return this.unmarshalFromData(data);
197
+ return this.unmarshalFromData(data, reference.toUint8Array());
181
198
  }
182
199
  /**
183
200
  * Unmarshals a MantarayNode from the given data.
184
201
  *
185
202
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
186
203
  */
187
- static unmarshalFromData(data) {
204
+ static unmarshalFromData(data, selfAddress) {
188
205
  const obfuscationKey = data.subarray(0, 32);
189
206
  const decrypted = Binary.xorCypher(data.subarray(32), obfuscationKey);
190
207
  const reader = new Uint8ArrayReader(decrypted);
@@ -195,6 +212,7 @@ export class MantarayNode {
195
212
  const targetAddressLength = Binary.uint8ToNumber(reader.read(1));
196
213
  const targetAddress = targetAddressLength === 0 ? NULL_ADDRESS : reader.read(targetAddressLength);
197
214
  const node = new MantarayNode({
215
+ selfAddress,
198
216
  targetAddress,
199
217
  obfuscationKey
200
218
  });
@@ -293,7 +311,10 @@ export class MantarayNode {
293
311
  */
294
312
  async loadRecursively(bee, options, requestOptions) {
295
313
  for (const fork of this.forks.values()) {
296
- const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
314
+ if (!fork.node.selfAddress) {
315
+ throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
316
+ }
317
+ const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, options, requestOptions);
297
318
  fork.node.targetAddress = node.targetAddress;
298
319
  fork.node.forks = node.forks;
299
320
  fork.node.path = fork.prefix;
@@ -77,7 +77,7 @@ export function getStampCost(depth, amount) {
77
77
  *
78
78
  * @returns {number} The TTL of the postage batch.
79
79
  */
80
- export function getStampDuration(amount, pricePerBlock, blockTime = 5) {
80
+ export function getStampDuration(amount, pricePerBlock, blockTime) {
81
81
  const amountBigInt = BigInt(asNumberString(amount));
82
82
  return Duration.fromSeconds(Number(amountBigInt * BigInt(blockTime) / BigInt(pricePerBlock)));
83
83
  }
@@ -88,7 +88,7 @@ export function getStampDuration(amount, pricePerBlock, blockTime = 5) {
88
88
  * @param pricePerBlock The price per block in PLUR.
89
89
  * @param blockTime The block time in seconds.
90
90
  */
91
- export function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
91
+ export function getAmountForDuration(duration, pricePerBlock, blockTime) {
92
92
  return BigInt(duration.toSeconds()) / BigInt(blockTime) * BigInt(pricePerBlock);
93
93
  }
94
94
  /**
@@ -25,6 +25,10 @@ export declare class Bee {
25
25
  * Default Signer object used for signing operations, mainly Feeds.
26
26
  */
27
27
  readonly signer?: PrivateKey;
28
+ /**
29
+ * Network on which the Bee node is running
30
+ */
31
+ readonly network: 'gnosis' | 'sepolia';
28
32
  /**
29
33
  * Options for making requests
30
34
  * @private
@@ -60,7 +60,7 @@ export declare class MantarayNode {
60
60
  *
61
61
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
62
62
  */
63
- static unmarshalFromData(data: Uint8Array): MantarayNode;
63
+ static unmarshalFromData(data: Uint8Array, selfAddress: Uint8Array): MantarayNode;
64
64
  /**
65
65
  * Adds a fork to the node.
66
66
  */
@@ -37,6 +37,10 @@ export interface BeeOptions extends BeeRequestOptions {
37
37
  * Signer object or private key of the Signer in form of either hex string or Uint8Array that will be default signer for the instance.
38
38
  */
39
39
  signer?: PrivateKey | Uint8Array | string;
40
+ /**
41
+ * Default gnosis when unspecified.
42
+ */
43
+ network?: 'gnosis' | 'sepolia';
40
44
  }
41
45
  export interface GranteesResult {
42
46
  status: number;
@@ -40,7 +40,7 @@ export declare function getStampCost(depth: number, amount: NumberString | strin
40
40
  *
41
41
  * @returns {number} The TTL of the postage batch.
42
42
  */
43
- export declare function getStampDuration(amount: NumberString | string | bigint, pricePerBlock: number, blockTime?: number): Duration;
43
+ export declare function getStampDuration(amount: NumberString | string | bigint, pricePerBlock: number, blockTime: number): Duration;
44
44
  /**
45
45
  * Get the postage batch `amount` required for a given `duration`.
46
46
  *
@@ -48,7 +48,7 @@ export declare function getStampDuration(amount: NumberString | string | bigint,
48
48
  * @param pricePerBlock The price per block in PLUR.
49
49
  * @param blockTime The block time in seconds.
50
50
  */
51
- export declare function getAmountForDuration(duration: Duration, pricePerBlock: number, blockTime?: number): bigint;
51
+ export declare function getAmountForDuration(duration: Duration, pricePerBlock: number, blockTime: number): bigint;
52
52
  /**
53
53
  * Utility function that calculates the depth required for a postage batch to achieve the specified effective size
54
54
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "9.0.3",
3
+ "version": "9.1.1",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -56,13 +56,12 @@
56
56
  "build:types": "tsc --emitDeclarationOnly --declaration --outDir dist/types",
57
57
  "build:browser": "webpack --progress",
58
58
  "test": "jest --config=jest.config.ts --runInBand --verbose",
59
- "check:types": "tsc --project tsconfig.test.json",
60
- "lint": "eslint --fix \"src/**/*.ts\" \"test/**/*.ts\" && prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
61
- "lint:check": "eslint \"src/**/*.ts\" \"test/**/*.ts\" && prettier --check \"src/**/*.ts\" \"test/**/*.ts\"",
59
+ "check": "tsc --project tsconfig.test.json",
60
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\" && prettier --check \"src/**/*.ts\" \"test/**/*.ts\"",
62
61
  "depcheck": "depcheck ."
63
62
  },
64
63
  "dependencies": {
65
- "axios": "^0.28.1",
64
+ "axios": "^0.30.0",
66
65
  "cafe-utility": "^27.14.2",
67
66
  "isomorphic-ws": "^4.0.1",
68
67
  "semver": "^7.3.5",
@@ -95,7 +94,6 @@
95
94
  "eslint-plugin-unused-imports": "^2.0.0",
96
95
  "husky": "^8.0.1",
97
96
  "jest": "^29.7.0",
98
- "jest-puppeteer": "^11.0.0",
99
97
  "prettier": "^2.6.2",
100
98
  "rimraf": "^3.0.2",
101
99
  "terser-webpack-plugin": "^5.3.1",