@ethersphere/bee-js 10.3.0 → 10.4.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.
@@ -1,11 +1,8 @@
1
1
  import { Types } from 'cafe-utility';
2
- import { Duration } from "../../utils/duration.js";
3
2
  import { http } from "../../utils/http.js";
4
- import { Size } from "../../utils/size.js";
5
- import { getStampEffectiveBytes, getStampTheoreticalBytes, getStampUsage } from "../../utils/stamps.js";
3
+ import { mapPostageBatch } from "../../utils/stamps.js";
6
4
  import { asNumberString } from "../../utils/type.js";
7
5
  import { BatchId, EthAddress } from "../../utils/typed-bytes.js";
8
- import { normalizeBatchTTL } from "../../utils/workaround.js";
9
6
  const STAMPS_ENDPOINT = 'stamps';
10
7
  const BATCHES_ENDPOINT = 'batches';
11
8
  export async function getGlobalPostageBatches(requestOptions) {
@@ -63,60 +60,7 @@ export async function getAllPostageBatches(requestOptions) {
63
60
  }).map(x => Types.asObject(x, {
64
61
  name: 'stamp'
65
62
  }));
66
- return stamps.map(x => {
67
- const utilization = Types.asNumber(x.utilization, {
68
- name: 'utilization'
69
- });
70
- const depth = Types.asNumber(x.depth, {
71
- name: 'depth'
72
- });
73
- const bucketDepth = Types.asNumber(x.bucketDepth, {
74
- name: 'bucketDepth'
75
- });
76
- const usage = getStampUsage(utilization, depth, bucketDepth);
77
- const batchTTL = normalizeBatchTTL(Types.asNumber(x.batchTTL, {
78
- name: 'batchTTL'
79
- }));
80
- const duration = Duration.fromSeconds(batchTTL);
81
- const effectiveBytes = getStampEffectiveBytes(depth);
82
- return {
83
- batchID: new BatchId(Types.asString(x.batchID, {
84
- name: 'batchID'
85
- })),
86
- utilization,
87
- usable: Types.asBoolean(x.usable, {
88
- name: 'usable'
89
- }),
90
- label: Types.asEmptiableString(x.label, {
91
- name: 'label'
92
- }),
93
- depth,
94
- amount: asNumberString(x.amount, {
95
- name: 'amount'
96
- }),
97
- bucketDepth,
98
- blockNumber: Types.asNumber(x.blockNumber, {
99
- name: 'blockNumber'
100
- }),
101
- immutableFlag: Types.asBoolean(x.immutableFlag, {
102
- name: 'immutableFlag'
103
- }),
104
- usage,
105
- usageText: `${Math.round(usage * 100)}%`,
106
- size: Size.fromBytes(effectiveBytes),
107
- remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
108
- theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
109
- duration,
110
- calculateSize(encryption, redundancyLevel) {
111
- const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel);
112
- return Size.fromBytes(effectiveBytes);
113
- },
114
- calculateRemainingSize(encryption, redundancyLevel) {
115
- const effectiveBytes = getStampEffectiveBytes(this.depth, encryption, redundancyLevel);
116
- return Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
117
- }
118
- };
119
- });
63
+ return stamps.map(x => mapPostageBatch(validateRawPostageBatch(x)));
120
64
  }
121
65
  export async function getPostageBatch(requestOptions, postageBatchId, encryption, erasureCodeLevel) {
122
66
  const response = await http(requestOptions, {
@@ -127,58 +71,7 @@ export async function getPostageBatch(requestOptions, postageBatchId, encryption
127
71
  const body = Types.asObject(response.data, {
128
72
  name: 'response.data'
129
73
  });
130
- const utilization = Types.asNumber(body.utilization, {
131
- name: 'utilization'
132
- });
133
- const depth = Types.asNumber(body.depth, {
134
- name: 'depth'
135
- });
136
- const bucketDepth = Types.asNumber(body.bucketDepth, {
137
- name: 'bucketDepth'
138
- });
139
- const usage = getStampUsage(utilization, depth, bucketDepth);
140
- const batchTTL = normalizeBatchTTL(Types.asNumber(body.batchTTL, {
141
- name: 'batchTTL'
142
- }));
143
- const duration = Duration.fromSeconds(batchTTL);
144
- const effectiveBytes = getStampEffectiveBytes(depth, encryption, erasureCodeLevel);
145
- return {
146
- batchID: new BatchId(Types.asString(body.batchID, {
147
- name: 'batchID'
148
- })),
149
- utilization,
150
- usable: Types.asBoolean(body.usable, {
151
- name: 'usable'
152
- }),
153
- label: Types.asEmptiableString(body.label, {
154
- name: 'label'
155
- }),
156
- depth,
157
- amount: asNumberString(body.amount, {
158
- name: 'amount'
159
- }),
160
- bucketDepth,
161
- blockNumber: Types.asNumber(body.blockNumber, {
162
- name: 'blockNumber'
163
- }),
164
- immutableFlag: Types.asBoolean(body.immutableFlag, {
165
- name: 'immutableFlag'
166
- }),
167
- usage,
168
- usageText: `${Math.round(usage * 100)}%`,
169
- size: Size.fromBytes(effectiveBytes),
170
- remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
171
- theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
172
- duration,
173
- calculateSize(encryption, redundancyLevel) {
174
- const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel);
175
- return Size.fromBytes(effectiveBytes);
176
- },
177
- calculateRemainingSize(encryption, redundancyLevel) {
178
- const effectiveBytes = getStampEffectiveBytes(depth, encryption, redundancyLevel);
179
- return Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage)));
180
- }
181
- };
74
+ return mapPostageBatch(validateRawPostageBatch(body), encryption, erasureCodeLevel);
182
75
  }
183
76
  export async function getPostageBatchBuckets(requestOptions, postageBatchId) {
184
77
  const response = await http(requestOptions, {
@@ -262,4 +155,38 @@ export async function diluteBatch(requestOptions, id, depth) {
262
155
  return new BatchId(Types.asString(body.batchID, {
263
156
  name: 'batchID'
264
157
  }));
158
+ }
159
+ function validateRawPostageBatch(raw) {
160
+ return {
161
+ amount: asNumberString(raw.amount, {
162
+ name: 'amount'
163
+ }),
164
+ batchID: Types.asString(raw.batchID, {
165
+ name: 'batchID'
166
+ }),
167
+ batchTTL: Types.asNumber(raw.batchTTL, {
168
+ name: 'batchTTL'
169
+ }),
170
+ bucketDepth: Types.asNumber(raw.bucketDepth, {
171
+ name: 'bucketDepth'
172
+ }),
173
+ blockNumber: Types.asNumber(raw.blockNumber, {
174
+ name: 'blockNumber'
175
+ }),
176
+ depth: Types.asNumber(raw.depth, {
177
+ name: 'depth'
178
+ }),
179
+ immutableFlag: Types.asBoolean(raw.immutableFlag, {
180
+ name: 'immutableFlag'
181
+ }),
182
+ label: Types.asEmptiableString(raw.label, {
183
+ name: 'label'
184
+ }),
185
+ usable: Types.asBoolean(raw.usable, {
186
+ name: 'usable'
187
+ }),
188
+ utilization: Types.asNumber(raw.utilization, {
189
+ name: 'utilization'
190
+ })
191
+ };
265
192
  }
@@ -2,4 +2,4 @@ export { getCollectionSize, makeCollectionFromFileList } from "./collection.js";
2
2
  export { getFolderSize } from "./collection.node.js";
3
3
  export { makeMaxTarget } from "./pss.js";
4
4
  export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from "./redundancy.js";
5
- export { getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage } from "./stamps.js";
5
+ export { getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage, mapPostageBatch, unmapPostageBatch } from "./stamps.js";
@@ -2,8 +2,11 @@ import { Binary } from 'cafe-utility';
2
2
  import { capacityBreakpoints } from "../types/index.js";
3
3
  import { Bytes, parseSizeToBytes } from "./bytes.js";
4
4
  import { Duration } from "./duration.js";
5
+ import { Size } from "./size.js";
5
6
  import { BZZ } from "./tokens.js";
6
7
  import { asNumberString } from "./type.js";
8
+ import { BatchId } from "./typed-bytes.js";
9
+ import { normalizeBatchTTL } from "./workaround.js";
7
10
  const MAX_UTILIZATION = 0.9;
8
11
  /**
9
12
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
@@ -137,4 +140,49 @@ export function marshalStamp(signature, batchId, timestamp, index) {
137
140
  throw Error('invalid index length');
138
141
  }
139
142
  return new Bytes(Binary.concatBytes(batchId, index, timestamp, signature));
143
+ }
144
+ export function mapPostageBatch(raw, encryption, erasureCodeLevel) {
145
+ const usage = getStampUsage(raw.utilization, raw.depth, raw.bucketDepth);
146
+ const batchTTL = normalizeBatchTTL(raw.batchTTL);
147
+ const duration = Duration.fromSeconds(batchTTL);
148
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, erasureCodeLevel);
149
+ return {
150
+ batchID: new BatchId(raw.batchID),
151
+ utilization: raw.utilization,
152
+ usable: raw.usable,
153
+ label: raw.label,
154
+ depth: raw.depth,
155
+ amount: asNumberString(raw.amount),
156
+ bucketDepth: raw.bucketDepth,
157
+ blockNumber: raw.blockNumber,
158
+ immutableFlag: raw.immutableFlag,
159
+ usage,
160
+ usageText: `${Math.round(usage * 100)}%`,
161
+ size: Size.fromBytes(effectiveBytes),
162
+ remainingSize: Size.fromBytes(Math.ceil(effectiveBytes * (1 - usage))),
163
+ theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(raw.depth)),
164
+ duration,
165
+ calculateSize(encryption, redundancyLevel) {
166
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
167
+ return Size.fromBytes(effectiveBytes);
168
+ },
169
+ calculateRemainingSize(encryption, redundancyLevel) {
170
+ const effectiveBytes = getStampEffectiveBytes(raw.depth, encryption, redundancyLevel);
171
+ return Size.fromBytes(Math.ceil(effectiveBytes * (1 - this.usage)));
172
+ }
173
+ };
174
+ }
175
+ export function unmapPostageBatch(batch) {
176
+ return {
177
+ batchID: batch.batchID.toHex(),
178
+ utilization: batch.utilization,
179
+ usable: batch.usable,
180
+ label: batch.label,
181
+ depth: batch.depth,
182
+ amount: batch.amount,
183
+ bucketDepth: batch.bucketDepth,
184
+ blockNumber: batch.blockNumber,
185
+ immutableFlag: batch.immutableFlag,
186
+ batchTTL: batch.duration.toSeconds()
187
+ };
140
188
  }
@@ -6,8 +6,11 @@ export function normalizeBatchTTL(batchTTL) {
6
6
  if (batchTTL < 1) {
7
7
  return 1;
8
8
  }
9
- if (batchTTL > 315569260) {
10
- return 315569260;
9
+ // Cap `batchTTL` (represents seconds) to 100 years.
10
+ // We can assume `storagePrice` is invalid (e.g. 1).
11
+ // This is needed to prevent Date objects breaking.
12
+ if (batchTTL > 3155695200) {
13
+ return 3155695200;
11
14
  }
12
15
  return batchTTL;
13
16
  }
@@ -2,8 +2,8 @@ import { Bee } from './bee';
2
2
  import { BeeDev } from './bee-dev';
3
3
  import { Stamper } from './stamper/stamper';
4
4
  export { MerkleTree } from 'cafe-utility';
5
- export { Chunk } from './chunk/cac';
6
- export { SingleOwnerChunk } from './chunk/soc';
5
+ export type { Chunk } from './chunk/cac';
6
+ export type { SingleOwnerChunk } from './chunk/soc';
7
7
  export { MantarayNode } from './manifest/manifest';
8
8
  export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './modules/debug/status';
9
9
  export * from './types';
@@ -2,4 +2,4 @@ export { getCollectionSize, makeCollectionFromFileList } from './collection';
2
2
  export { getFolderSize } from './collection.node';
3
3
  export { makeMaxTarget } from './pss';
4
4
  export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from './redundancy';
5
- export { getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage, } from './stamps';
5
+ export { getAmountForDuration, getDepthForSize, getStampCost, getStampDuration, getStampEffectiveBytes, getStampEffectiveBytesBreakpoints, getStampTheoreticalBytes, getStampUsage, mapPostageBatch, unmapPostageBatch, } from './stamps';
@@ -1,4 +1,4 @@
1
- import { EnvelopeWithBatchId, NumberString, RedundancyLevel } from '../types';
1
+ import { EnvelopeWithBatchId, NumberString, PostageBatch, RedundancyLevel } from '../types';
2
2
  import { Bytes } from './bytes';
3
3
  import { Duration } from './duration';
4
4
  import { Size } from './size';
@@ -58,3 +58,17 @@ export declare function getAmountForDuration(duration: Duration, pricePerBlock:
58
58
  export declare function getDepthForSize(size: Size, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): number;
59
59
  export declare function convertEnvelopeToMarshaledStamp(envelope: EnvelopeWithBatchId): Bytes;
60
60
  export declare function marshalStamp(signature: Uint8Array, batchId: Uint8Array, timestamp: Uint8Array, index: Uint8Array): Bytes;
61
+ export interface RawPostageBatch {
62
+ batchID: string;
63
+ utilization: number;
64
+ usable: boolean;
65
+ label: string;
66
+ depth: number;
67
+ amount: string;
68
+ bucketDepth: number;
69
+ blockNumber: number;
70
+ immutableFlag: boolean;
71
+ batchTTL: number;
72
+ }
73
+ export declare function mapPostageBatch(raw: RawPostageBatch, encryption?: boolean, erasureCodeLevel?: RedundancyLevel): PostageBatch;
74
+ export declare function unmapPostageBatch(batch: PostageBatch): RawPostageBatch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "10.3.0",
3
+ "version": "10.4.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",