@upcoming/bee-js 0.5.2 → 0.5.4

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
@@ -58,6 +58,7 @@ const collection_node_1 = require("./utils/collection.node");
58
58
  const data_1 = require("./utils/data");
59
59
  const error_1 = require("./utils/error");
60
60
  const file_1 = require("./utils/file");
61
+ const tokens_1 = require("./utils/tokens");
61
62
  const type_1 = require("./utils/type");
62
63
  const typed_bytes_1 = require("./utils/typed-bytes");
63
64
  const url_1 = require("./utils/url");
@@ -341,9 +342,9 @@ class Bee {
341
342
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
342
343
  return (0, chunk_stream_1.streamDirectory)(this, dir, postageBatchId, onUploadProgress, options, this.getRequestOptionsForCall(requestOptions));
343
344
  }
344
- async streamFiles(postageBatchId, files, onUploadProgress, options) {
345
+ async streamFiles(postageBatchId, files, onUploadProgress, options, requestOptions) {
345
346
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
346
- return (0, chunk_stream_1.streamFiles)(this, files, postageBatchId, onUploadProgress, options);
347
+ return (0, chunk_stream_1.streamFiles)(this, files, postageBatchId, onUploadProgress, options, this.getRequestOptionsForCall(requestOptions));
347
348
  }
348
349
  /**
349
350
  * Upload Collection that you can assembly yourself.
@@ -1005,7 +1006,7 @@ class Bee {
1005
1006
  * @return string Hash of the transaction
1006
1007
  */
1007
1008
  async depositTokens(amount, gasPrice, options) {
1008
- const amountString = (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1009
+ const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1009
1010
  let gasPriceString;
1010
1011
  if (gasPrice) {
1011
1012
  gasPriceString = (0, type_1.asNumberString)(amount, { min: 0n, name: 'gasPrice' });
@@ -1020,7 +1021,8 @@ class Bee {
1020
1021
  * @return string Hash of the transaction
1021
1022
  */
1022
1023
  async withdrawTokens(amount, gasPrice, options) {
1023
- const amountString = (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1024
+ // TODO: check BZZ in tests
1025
+ const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1024
1026
  let gasPriceString;
1025
1027
  if (gasPrice) {
1026
1028
  gasPriceString = (0, type_1.asNumberString)(amount, { min: 0n, name: 'gasPrice' });
@@ -1287,11 +1289,12 @@ class Bee {
1287
1289
  * @param options
1288
1290
  */
1289
1291
  async depositStake(amount, options, requestOptions) {
1290
- const amountString = (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1292
+ const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1291
1293
  if (options) {
1292
1294
  options = (0, type_1.prepareTransactionOptions)(options);
1293
1295
  }
1294
- await stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1296
+ // TODO: Add test for response
1297
+ return stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1295
1298
  }
1296
1299
  /**
1297
1300
  * Get current status of node in redistribution game
@@ -102,6 +102,13 @@ class MantarayNode {
102
102
  get fullPathString() {
103
103
  return DECODER.decode(this.fullPath);
104
104
  }
105
+ getRootMetadata() {
106
+ const node = this.find('/');
107
+ if (node && node.metadata) {
108
+ return cafe_utility_1.Optional.of(node.metadata);
109
+ }
110
+ return cafe_utility_1.Optional.empty();
111
+ }
105
112
  async marshal() {
106
113
  for (const fork of this.forks.values()) {
107
114
  if (!fork.node.selfAddress) {
@@ -128,8 +135,8 @@ class MantarayNode {
128
135
  : this.targetAddress, forkBitmap, ...forks), this.obfuscationKey);
129
136
  return cafe_utility_1.Binary.concatBytes(this.obfuscationKey, data);
130
137
  }
131
- static async unmarshal(bee, reference) {
132
- const data = (await bee.downloadData(reference)).toUint8Array();
138
+ static async unmarshal(bee, reference, options, requestOptions) {
139
+ const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
133
140
  const obfuscationKey = data.subarray(0, 32);
134
141
  const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
135
142
  const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
@@ -217,15 +224,16 @@ class MantarayNode {
217
224
  }
218
225
  const result = await bee.uploadData(postageBatchId, await this.marshal(), options, requestOptions);
219
226
  this.selfAddress = result.reference.toUint8Array();
220
- return new typed_bytes_1.Reference(this.selfAddress);
227
+ return result;
221
228
  }
222
- async loadRecursively(bee) {
229
+ async loadRecursively(bee, options, requestOptions) {
223
230
  for (const fork of this.forks.values()) {
224
- const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress);
231
+ const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
225
232
  fork.node.targetAddress = node.targetAddress;
226
233
  fork.node.forks = node.forks;
227
234
  fork.node.path = fork.prefix;
228
- await fork.node.loadRecursively(bee);
235
+ fork.node.parent = this;
236
+ await fork.node.loadRecursively(bee, options, requestOptions);
229
237
  }
230
238
  }
231
239
  find(path) {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPastDueConsumptionPeerBalance = exports.getPastDueConsumptionBalances = exports.getPeerBalance = exports.getAllBalances = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const http_1 = require("../../utils/http");
6
+ const tokens_1 = require("../../utils/tokens");
6
7
  const type_1 = require("../../utils/type");
7
8
  const balancesEndpoint = 'balances';
8
9
  const consumedEndpoint = 'consumed';
@@ -21,7 +22,7 @@ async function getAllBalances(requestOptions) {
21
22
  return {
22
23
  balances: balances.map(x => ({
23
24
  peer: cafe_utility_1.Types.asString(x.peer, { name: 'peer' }),
24
- balance: (0, type_1.asNumberString)(x.balance, { name: 'balance' }),
25
+ balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(x.balance, { name: 'balance' })),
25
26
  })),
26
27
  };
27
28
  }
@@ -40,7 +41,7 @@ async function getPeerBalance(requestOptions, address) {
40
41
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
41
42
  return {
42
43
  peer: cafe_utility_1.Types.asString(body.peer, { name: 'peer' }),
43
- balance: (0, type_1.asNumberString)(body.balance, { name: 'balance' }),
44
+ balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.balance, { name: 'balance' })),
44
45
  };
45
46
  }
46
47
  exports.getPeerBalance = getPeerBalance;
@@ -59,7 +60,7 @@ async function getPastDueConsumptionBalances(requestOptions) {
59
60
  return {
60
61
  balances: balances.map(x => ({
61
62
  peer: cafe_utility_1.Types.asString(x.peer, { name: 'peer' }),
62
- balance: (0, type_1.asNumberString)(x.balance, { name: 'balance' }),
63
+ balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(x.balance, { name: 'balance' })),
63
64
  })),
64
65
  };
65
66
  }
@@ -78,7 +79,7 @@ async function getPastDueConsumptionPeerBalance(requestOptions, address) {
78
79
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
79
80
  return {
80
81
  peer: cafe_utility_1.Types.asString(body.peer, { name: 'peer' }),
81
- balance: (0, type_1.asNumberString)(body.balance, { name: 'balance' }),
82
+ balance: tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.balance, { name: 'balance' })),
82
83
  };
83
84
  }
84
85
  exports.getPastDueConsumptionPeerBalance = getPastDueConsumptionPeerBalance;
@@ -6,6 +6,7 @@ const headers_1 = require("../../utils/headers");
6
6
  const http_1 = require("../../utils/http");
7
7
  const tokens_1 = require("../../utils/tokens");
8
8
  const type_1 = require("../../utils/type");
9
+ const typed_bytes_1 = require("../../utils/typed-bytes");
9
10
  const STAKE_ENDPOINT = 'stake';
10
11
  const REDISTRIBUTION_ENDPOINT = 'redistributionstate';
11
12
  /**
@@ -31,12 +32,14 @@ exports.getStake = getStake;
31
32
  * @param options
32
33
  */
33
34
  async function stake(requestOptions, amount, options) {
34
- await (0, http_1.http)(requestOptions, {
35
+ const repsonse = await (0, http_1.http)(requestOptions, {
35
36
  method: 'post',
36
37
  responseType: 'json',
37
38
  url: `${STAKE_ENDPOINT}/${amount}`,
38
39
  headers: (0, headers_1.prepareRequestHeaders)(null, options),
39
40
  });
41
+ const body = cafe_utility_1.Types.asObject(repsonse.data, { name: 'response.data' });
42
+ return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
40
43
  }
41
44
  exports.stake = stake;
42
45
  /**
@@ -16,7 +16,7 @@ async function streamDirectory(_bee, _dir, _postageBatchId, _onUploadProgress, _
16
16
  throw new Error('Streaming directories is not supported in browsers!');
17
17
  }
18
18
  exports.streamDirectory = streamDirectory;
19
- async function streamFiles(bee, files, postageBatchId, onUploadProgress, options) {
19
+ async function streamFiles(bee, files, postageBatchId, onUploadProgress, options, requestOptions) {
20
20
  const queue = new cafe_utility_1.AsyncQueue(64, 64);
21
21
  let total = 0;
22
22
  let processed = 0;
@@ -26,7 +26,7 @@ async function streamFiles(bee, files, postageBatchId, onUploadProgress, options
26
26
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
27
27
  async function onChunk(chunk) {
28
28
  await queue.enqueue(async () => {
29
- await bee.uploadChunk(postageBatchId, chunk.build(), options);
29
+ await bee.uploadChunk(postageBatchId, chunk.build(), options, requestOptions);
30
30
  onUploadProgress?.({ total, processed: ++processed });
31
31
  });
32
32
  }
@@ -74,7 +74,7 @@ async function streamFiles(bee, files, postageBatchId, onUploadProgress, options
74
74
  });
75
75
  }
76
76
  }
77
- return mantaray.saveRecursively(bee, postageBatchId);
77
+ return mantaray.saveRecursively(bee, postageBatchId, options, requestOptions);
78
78
  }
79
79
  exports.streamFiles = streamFiles;
80
80
  function maybeEnrichMime(mime) {
@@ -78,7 +78,7 @@ function maybeEnrichMime(mime) {
78
78
  }
79
79
  return mime;
80
80
  }
81
- async function streamFiles(_bee, _files, _postageBatchId, _onUploadProgress, _options) {
81
+ async function streamFiles(_bee, _files, _postageBatchId, _onUploadProgress, _options, _requestOptions) {
82
82
  throw new Error('Streaming files is not supported in Node.js');
83
83
  }
84
84
  exports.streamFiles = streamFiles;
@@ -21,6 +21,51 @@ class BZZ {
21
21
  toDecimalString() {
22
22
  return this.state.toDecimalString();
23
23
  }
24
+ toSignificantDigits(digits) {
25
+ return this.toDecimalString().slice(0, this.toDecimalString().indexOf('.') + digits + 1);
26
+ }
27
+ /**
28
+ * Does not mutate the current BZZ instance.
29
+ *
30
+ * @param other BZZ instance, or amount in PLUR
31
+ * @returns New BZZ instance
32
+ */
33
+ plus(other) {
34
+ return new BZZ(this.state.add(other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)));
35
+ }
36
+ /**
37
+ * Does not mutate the current BZZ instance.
38
+ *
39
+ * @param other BZZ instance, or amount in PLUR
40
+ * @returns New BZZ instance
41
+ */
42
+ minus(other) {
43
+ return new BZZ(this.state.subtract(other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)));
44
+ }
45
+ /**
46
+ * Does not mutate the current BZZ instance.
47
+ *
48
+ * @param other Divisor
49
+ * @returns New BZZ instance
50
+ */
51
+ divide(other) {
52
+ return new BZZ(this.state.divide(other)[0]);
53
+ }
54
+ gt(other) {
55
+ return this.state.value > (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
56
+ }
57
+ gte(other) {
58
+ return this.state.value >= (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
59
+ }
60
+ lt(other) {
61
+ return this.state.value < (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
62
+ }
63
+ lte(other) {
64
+ return this.state.value <= (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
65
+ }
66
+ eq(other) {
67
+ return this.state.value === (other instanceof BZZ ? other.state : new cafe_utility_1.FixedPointNumber(other, BZZ.DIGITS)).value;
68
+ }
24
69
  }
25
70
  exports.BZZ = BZZ;
26
71
  BZZ.DIGITS = 16;
@@ -43,6 +88,51 @@ class DAI {
43
88
  toDecimalString() {
44
89
  return this.state.toDecimalString();
45
90
  }
91
+ toSignificantDigits(digits) {
92
+ return this.toDecimalString().slice(0, this.toDecimalString().indexOf('.') + digits + 1);
93
+ }
94
+ /**
95
+ * Does not mutate the current DAI instance.
96
+ *
97
+ * @param other DAI instance, or amount in PLUR
98
+ * @returns New DAI instance
99
+ */
100
+ plus(other) {
101
+ return new DAI(this.state.add(other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)));
102
+ }
103
+ /**
104
+ * Does not mutate the current DAI instance.
105
+ *
106
+ * @param other DAI instance, or amount in PLUR
107
+ * @returns New DAI instance
108
+ */
109
+ minus(other) {
110
+ return new DAI(this.state.subtract(other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)));
111
+ }
112
+ /**
113
+ * Does not mutate the current DAI instance.
114
+ *
115
+ * @param other Divisor
116
+ * @returns New DAI instance
117
+ */
118
+ divide(other) {
119
+ return new DAI(this.state.divide(other)[0]);
120
+ }
121
+ gt(other) {
122
+ return this.state.value > (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
123
+ }
124
+ gte(other) {
125
+ return this.state.value >= (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
126
+ }
127
+ lt(other) {
128
+ return this.state.value < (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
129
+ }
130
+ lte(other) {
131
+ return this.state.value <= (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
132
+ }
133
+ eq(other) {
134
+ return this.state.value === (other instanceof DAI ? other.state : new cafe_utility_1.FixedPointNumber(other, DAI.DIGITS)).value;
135
+ }
46
136
  }
47
137
  exports.DAI = DAI;
48
138
  DAI.DIGITS = 18;
@@ -4,6 +4,7 @@ exports.FeedIndex = exports.Topic = exports.Signature = exports.BatchId = export
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const bytes_1 = require("./bytes");
6
6
  const cid_1 = require("./cid");
7
+ // TODO: add JSdocs for each class
7
8
  const ENCODER = new TextEncoder();
8
9
  class PrivateKey extends bytes_1.Bytes {
9
10
  constructor(bytes) {
@@ -78,6 +79,15 @@ class Reference extends bytes_1.Bytes {
78
79
  toCid(type) {
79
80
  return (0, cid_1.convertReferenceToCid)(this.bytes, type);
80
81
  }
82
+ static isValid(value) {
83
+ try {
84
+ new Reference(value);
85
+ return true;
86
+ }
87
+ catch {
88
+ return false;
89
+ }
90
+ }
81
91
  }
82
92
  exports.Reference = Reference;
83
93
  Reference.LENGTH = 32;