@upcoming/bee-js 9.4.1 → 9.7.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.
package/dist/mjs/bee.js CHANGED
@@ -35,7 +35,7 @@ import { BeeArgumentError, BeeError } from "./utils/error.js";
35
35
  import { fileArrayBuffer, isFile } from "./utils/file.js";
36
36
  import { ResourceLocator } from "./utils/resource-locator.js";
37
37
  import { getAmountForDuration, getDepthForSize, getStampCost } from "./utils/stamps.js";
38
- import { BZZ } from "./utils/tokens.js";
38
+ import { BZZ, DAI } from "./utils/tokens.js";
39
39
  import { asNumberString, assertData, assertFileData, makeTagUid, prepareAllTagsOptions, prepareBeeRequestOptions, prepareCollectionUploadOptions, prepareDownloadOptions, prepareFileUploadOptions, prepareGsocMessageHandler, preparePostageBatchOptions, preparePssMessageHandler, prepareRedundantUploadOptions, prepareTransactionOptions, prepareUploadOptions } from "./utils/type.js";
40
40
  import { BatchId, EthAddress, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Span, Topic, TransactionId } from "./utils/typed-bytes.js";
41
41
  import { assertBeeUrl, stripLastSlash } from "./utils/url.js";
@@ -695,7 +695,8 @@ export class Bee {
695
695
  for (let i = 0n; i < 0xffffn; i++) {
696
696
  const signer = new PrivateKey(Binary.numberToUint256(start + i, 'BE'));
697
697
  const socAddress = makeSOCAddress(identifier, signer.publicKey().address());
698
- const actualProximity = 256 - Binary.proximity(socAddress.toUint8Array(), targetOverlay.toUint8Array(), 256);
698
+ // TODO: test the significance of the hardcoded 256
699
+ const actualProximity = 256 - Binary.proximity(socAddress.toUint8Array(), targetOverlay.toUint8Array());
699
700
  if (actualProximity <= 256 - proximity) {
700
701
  return signer;
701
702
  }
@@ -867,6 +868,17 @@ export class Bee {
867
868
  }
868
869
  return true;
869
870
  }
871
+ /**
872
+ * Checks the `/gateway` endpoint to see if the remote API is a gateway.
873
+ *
874
+ * Do note that this is not a standard way to check for gateway nodes,
875
+ * but some of the gateway tooling expose this endpoint.
876
+ *
877
+ * @param options
878
+ */
879
+ async isGateway(options) {
880
+ return status.isGateway(this.getRequestOptionsForCall(options));
881
+ }
870
882
  // Legacy debug API
871
883
  async getNodeAddresses(options) {
872
884
  return connectivity.getNodeAddresses(this.getRequestOptionsForCall(options));
@@ -982,13 +994,24 @@ export class Bee {
982
994
  return chequebook.cashoutLastCheque(this.getRequestOptionsForCall(requestOptions), address, options);
983
995
  }
984
996
  /**
985
- * Deposit tokens from overlay address into chequebook
997
+ * Deposit tokens from node wallet into chequebook
986
998
  *
987
999
  * @param amount Amount of tokens to deposit (must be positive integer)
988
1000
  * @param gasPrice Gas Price in WEI for the transaction call
989
1001
  * @return string Hash of the transaction
1002
+ * @deprecated Use `depositBZZToChequebook` instead.
990
1003
  */
991
1004
  async depositTokens(amount, gasPrice, options) {
1005
+ return this.depositBZZToChequebook(amount, gasPrice, options);
1006
+ }
1007
+ /**
1008
+ * Deposit tokens from node wallet into chequebook
1009
+ *
1010
+ * @param amount Amount of tokens to deposit (must be positive integer)
1011
+ * @param gasPrice Gas Price in WEI for the transaction call
1012
+ * @return string Hash of the transaction
1013
+ */
1014
+ async depositBZZToChequebook(amount, gasPrice, options) {
992
1015
  const amountString = amount instanceof BZZ ? amount.toPLURString() : asNumberString(amount, {
993
1016
  min: 1n,
994
1017
  name: 'amount'
@@ -1003,13 +1026,24 @@ export class Bee {
1003
1026
  return chequebook.depositTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1004
1027
  }
1005
1028
  /**
1006
- * Withdraw tokens from the chequebook to the overlay address
1029
+ * Withdraw tokens from the chequebook to the node wallet
1007
1030
  *
1008
1031
  * @param amount Amount of tokens to withdraw (must be positive integer)
1009
1032
  * @param gasPrice Gas Price in WEI for the transaction call
1010
1033
  * @return string Hash of the transaction
1034
+ * @deprecated Use `withdrawBZZFromChequebook` instead.
1011
1035
  */
1012
1036
  async withdrawTokens(amount, gasPrice, options) {
1037
+ return this.withdrawBZZFromChequebook(amount, gasPrice, options);
1038
+ }
1039
+ /**
1040
+ * Withdraw tokens from the chequebook to the node wallet
1041
+ *
1042
+ * @param amount Amount of tokens to withdraw (must be positive integer)
1043
+ * @param gasPrice Gas Price in WEI for the transaction call
1044
+ * @return string Hash of the transaction
1045
+ */
1046
+ async withdrawBZZFromChequebook(amount, gasPrice, options) {
1013
1047
  // TODO: check BZZ in tests
1014
1048
  const amountString = amount instanceof BZZ ? amount.toPLURString() : asNumberString(amount, {
1015
1049
  min: 1n,
@@ -1024,6 +1058,16 @@ export class Bee {
1024
1058
  }
1025
1059
  return chequebook.withdrawTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1026
1060
  }
1061
+ async withdrawBZZToExternalWallet(amount, address, options) {
1062
+ amount = amount instanceof BZZ ? amount : BZZ.fromPLUR(amount);
1063
+ address = new EthAddress(address);
1064
+ return states.withdrawBZZ(this.getRequestOptionsForCall(options), amount, address);
1065
+ }
1066
+ async withdrawDAIToExternalWallet(amount, address, options) {
1067
+ amount = amount instanceof DAI ? amount : DAI.fromWei(amount);
1068
+ address = new EthAddress(address);
1069
+ return states.withdrawDAI(this.getRequestOptionsForCall(options), amount, address);
1070
+ }
1027
1071
  /*
1028
1072
  * Settlements endpoint
1029
1073
  */
@@ -1107,7 +1151,7 @@ export class Bee {
1107
1151
  return states.getChainState(this.getRequestOptionsForCall(options));
1108
1152
  }
1109
1153
  /**
1110
- * Get wallet balances for xDai and BZZ of the Bee node
1154
+ * Get wallet balances for DAI and BZZ of the Bee node
1111
1155
  *
1112
1156
  * @param options
1113
1157
  */
@@ -1153,24 +1197,24 @@ export class Bee {
1153
1197
  }
1154
1198
  return stamp;
1155
1199
  }
1156
- async buyStorage(size, duration, options, requestOptions) {
1200
+ async buyStorage(size, duration, options, requestOptions, encryption, erasureCodeLevel) {
1157
1201
  const chainState = await this.getChainState(requestOptions);
1158
1202
  const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1159
- const depth = getDepthForSize(size);
1203
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1160
1204
  if (options) {
1161
1205
  options = preparePostageBatchOptions(options);
1162
1206
  }
1163
1207
  return this.createPostageBatch(amount, depth, options, requestOptions);
1164
1208
  }
1165
- async getStorageCost(size, duration, options) {
1209
+ async getStorageCost(size, duration, options, encryption, erasureCodeLevel) {
1166
1210
  const chainState = await this.getChainState(options);
1167
1211
  const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1168
- const depth = getDepthForSize(size);
1212
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1169
1213
  return getStampCost(depth, amount);
1170
1214
  }
1171
- async extendStorageSize(postageBatchId, size, options) {
1215
+ async extendStorageSize(postageBatchId, size, options, encryption, erasureCodeLevel) {
1172
1216
  const batch = await this.getPostageBatch(postageBatchId, options);
1173
- const depth = getDepthForSize(size);
1217
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1174
1218
  const delta = depth - batch.depth;
1175
1219
  if (delta <= 0) {
1176
1220
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1184,18 +1228,18 @@ export class Bee {
1184
1228
  const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1185
1229
  return this.topUpBatch(batch.batchID, amount, options);
1186
1230
  }
1187
- async getExtensionCost(postageBatchId, size, duration, options) {
1231
+ async getExtensionCost(postageBatchId, size, duration, options, encryption, erasureCodeLevel) {
1188
1232
  const batch = await this.getPostageBatch(postageBatchId, options);
1189
1233
  const chainState = await this.getChainState(options);
1190
1234
  const amount = getAmountForDuration(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1191
- const depth = getDepthForSize(size);
1235
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1192
1236
  const currentValue = getStampCost(batch.depth, batch.amount);
1193
1237
  const newValue = getStampCost(depth, amount);
1194
1238
  return newValue.minus(currentValue);
1195
1239
  }
1196
- async getSizeExtensionCost(postageBatchId, size, options) {
1240
+ async getSizeExtensionCost(postageBatchId, size, options, encryption, erasureCodeLevel) {
1197
1241
  const batch = await this.getPostageBatch(postageBatchId, options);
1198
- const depth = getDepthForSize(size);
1242
+ const depth = getDepthForSize(size, encryption, erasureCodeLevel);
1199
1243
  const delta = depth - batch.depth;
1200
1244
  if (delta <= 0) {
1201
1245
  throw new BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1335,7 +1379,7 @@ export class Bee {
1335
1379
  return transactions.rebroadcastTransaction(this.getRequestOptionsForCall(options), transactionHash);
1336
1380
  }
1337
1381
  /**
1338
- * Cancel currently pending transaction
1382
+ * Cancels a currently pending transaction
1339
1383
  * @param transactionHash
1340
1384
  * @param gasPrice
1341
1385
  */
@@ -1351,20 +1395,48 @@ export class Bee {
1351
1395
  return transactions.cancelTransaction(this.getRequestOptionsForCall(options), transactionHash, gasPriceString);
1352
1396
  }
1353
1397
  /**
1354
- * Gets the staked amount of BZZ (in PLUR unit) as number string.
1398
+ * Gets the amount of staked BZZ
1355
1399
  *
1356
- * @param options
1400
+ * @param options HTTP request options, such as `headers` or `timeout`
1357
1401
  */
1358
1402
  async getStake(options) {
1359
1403
  return stake.getStake(this.getRequestOptionsForCall(options));
1360
1404
  }
1361
1405
  /**
1362
- * Deposits given amount of BZZ token (in PLUR unit).
1406
+ * Gets the amount of withdrawable staked BZZ.
1407
+ *
1408
+ * @param options HTTP request options, such as `headers` or `timeout`
1409
+ */
1410
+ async getWithdrawableStake(options) {
1411
+ return stake.getWithdrawableStake(this.getRequestOptionsForCall(options));
1412
+ }
1413
+ /**
1414
+ * Withdraws ALL surplus staked BZZ to the node wallet.
1415
+ *
1416
+ * Use the `getWithdrawableStake` method to check how much surplus stake is available.
1417
+ *
1418
+ * @param options HTTP request options, such as `headers` or `timeout`
1419
+ */
1420
+ async withdrawSurplusStake(options) {
1421
+ return stake.withdrawSurplusStake(this.getRequestOptionsForCall(options));
1422
+ }
1423
+ /**
1424
+ * Withdraws all staked BZZ to the node wallet.
1425
+ *
1426
+ * **Only available when the staking contract is paused and is in the process of being migrated to a new contract!**
1427
+ *
1428
+ * @param options HTTP request options, such as `headers` or `timeout`
1429
+ */
1430
+ async migrateStake(options) {
1431
+ return stake.migrateStake(this.getRequestOptionsForCall(options));
1432
+ }
1433
+ /**
1434
+ * Stakes the given amount of BZZ. Initial deposit must be at least 10 BZZ.
1363
1435
  *
1364
1436
  * Be aware that staked BZZ tokens can **not** be withdrawn.
1365
1437
  *
1366
- * @param amount Amount of BZZ token (in PLUR unit) to be staked. Minimum is 100_000_000_000_000_000 PLUR (10 BZZ).
1367
- * @param options
1438
+ * @param amount Amount of BZZ tokens to be staked. If not providing a `BZZ` instance, the amount is denoted in PLUR.
1439
+ * @param options HTTP request options, such as `headers` or `timeout`
1368
1440
  */
1369
1441
  async depositStake(amount, options, requestOptions) {
1370
1442
  const amountString = amount instanceof BZZ ? amount.toPLURString() : asNumberString(amount, {
@@ -1377,9 +1449,9 @@ export class Bee {
1377
1449
  return stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1378
1450
  }
1379
1451
  /**
1380
- * Get current status of node in redistribution game
1452
+ * Gets current status of node in redistribution game
1381
1453
  *
1382
- * @param options
1454
+ * @param options HTTP request options, such as `headers` or `timeout`
1383
1455
  */
1384
1456
  async getRedistributionState(options) {
1385
1457
  return stake.getRedistributionState(this.getRequestOptionsForCall(options));
@@ -68,12 +68,12 @@ export class Fork {
68
68
  }
69
69
  return Binary.concatBytes(...data);
70
70
  }
71
- static unmarshal(reader) {
71
+ static unmarshal(reader, addressLength) {
72
72
  const type = Binary.uint8ToNumber(reader.read(1));
73
73
  const prefixLength = Binary.uint8ToNumber(reader.read(1));
74
74
  const prefix = reader.read(prefixLength);
75
75
  reader.read(30 - prefixLength);
76
- const selfAddress = reader.read(32);
76
+ const selfAddress = reader.read(addressLength);
77
77
  let metadata = undefined;
78
78
  if (isType(type, TYPE_WITH_METADATA)) {
79
79
  const metadataLength = Binary.uint16ToNumber(reader.read(2), 'BE');
@@ -219,7 +219,7 @@ export class MantarayNode {
219
219
  const forkBitmap = reader.read(32);
220
220
  for (let i = 0; i < 256; i++) {
221
221
  if (Binary.getBit(forkBitmap, i, 'LE')) {
222
- const newFork = Fork.unmarshal(reader);
222
+ const newFork = Fork.unmarshal(reader, targetAddressLength);
223
223
  node.forks.set(i, newFork);
224
224
  newFork.node.parent = node;
225
225
  }
@@ -7,7 +7,7 @@ import { TransactionId } from "../../utils/typed-bytes.js";
7
7
  const STAKE_ENDPOINT = 'stake';
8
8
  const REDISTRIBUTION_ENDPOINT = 'redistributionstate';
9
9
  /**
10
- * Gets the staked amount
10
+ * Gets the amount of staked BZZ
11
11
  *
12
12
  * @param requestOptions Options for making requests
13
13
  */
@@ -24,12 +24,55 @@ export async function getStake(requestOptions) {
24
24
  name: 'stakedAmount'
25
25
  }));
26
26
  }
27
+ /**
28
+ * Gets the amount of withdrawable staked BZZ
29
+ *
30
+ * @param requestOptions Options for making requests
31
+ */
32
+ export async function getWithdrawableStake(requestOptions) {
33
+ const response = await http(requestOptions, {
34
+ method: 'get',
35
+ responseType: 'json',
36
+ url: `${STAKE_ENDPOINT}/withdrawable`
37
+ });
38
+ const body = Types.asObject(response.data, {
39
+ name: 'response.data'
40
+ });
41
+ return BZZ.fromPLUR(asNumberString(body.withdrawableAmount, {
42
+ name: 'withdrawableAmount'
43
+ }));
44
+ }
45
+ export async function withdrawSurplusStake(requestOptions) {
46
+ const response = await http(requestOptions, {
47
+ method: 'delete',
48
+ responseType: 'json',
49
+ url: `${STAKE_ENDPOINT}/withdrawable`
50
+ });
51
+ const body = Types.asObject(response.data, {
52
+ name: 'response.data'
53
+ });
54
+ return new TransactionId(Types.asHexString(body.txHash, {
55
+ name: 'txHash'
56
+ }));
57
+ }
58
+ export async function migrateStake(requestOptions) {
59
+ const response = await http(requestOptions, {
60
+ method: 'delete',
61
+ responseType: 'json',
62
+ url: STAKE_ENDPOINT
63
+ });
64
+ const body = Types.asObject(response.data, {
65
+ name: 'response.data'
66
+ });
67
+ return new TransactionId(Types.asHexString(body.txHash, {
68
+ name: 'txHash'
69
+ }));
70
+ }
27
71
  /**
28
72
  * Stake given amount of tokens.
29
73
  *
30
74
  * @param requestOptions Options for making requests
31
75
  * @param amount
32
- * @param options
33
76
  */
34
77
  export async function stake(requestOptions, amount, options) {
35
78
  const repsonse = await http(requestOptions, {
@@ -2,6 +2,7 @@ import { Types } from 'cafe-utility';
2
2
  import { http } from "../../utils/http.js";
3
3
  import { BZZ, DAI } from "../../utils/tokens.js";
4
4
  import { asNumberString } from "../../utils/type.js";
5
+ import { TransactionId } from "../../utils/typed-bytes.js";
5
6
  import { normalizeCurrentPrice } from "../../utils/workaround.js";
6
7
  const RESERVE_STATE_ENDPOINT = 'reservestate';
7
8
  const WALLET_ENDPOINT = 'wallet';
@@ -92,4 +93,38 @@ export async function getWalletBalance(requestOptions) {
92
93
  name: 'walletAddress'
93
94
  })
94
95
  };
96
+ }
97
+ export async function withdrawBZZ(requestOptions, amount, address) {
98
+ const response = await http(requestOptions, {
99
+ method: 'post',
100
+ url: `${WALLET_ENDPOINT}/withdraw/bzz`,
101
+ responseType: 'json',
102
+ params: {
103
+ amount: amount.toPLURString(),
104
+ address: address.toHex()
105
+ }
106
+ });
107
+ const body = Types.asObject(response.data, {
108
+ name: 'response.data'
109
+ });
110
+ return new TransactionId(Types.asString(body.transactionHash, {
111
+ name: 'transactionHash'
112
+ }));
113
+ }
114
+ export async function withdrawDAI(requestOptions, amount, address) {
115
+ const response = await http(requestOptions, {
116
+ method: 'post',
117
+ url: `${WALLET_ENDPOINT}/withdraw/nativetoken`,
118
+ responseType: 'json',
119
+ params: {
120
+ amount: amount.toWeiString(),
121
+ address: address.toHex()
122
+ }
123
+ });
124
+ const body = Types.asObject(response.data, {
125
+ name: 'response.data'
126
+ });
127
+ return new TransactionId(Types.asString(body.transactionHash, {
128
+ name: 'transactionHash'
129
+ }));
95
130
  }
@@ -2,9 +2,9 @@ import { Types } from 'cafe-utility';
2
2
  import getMajorSemver from 'semver/functions/major.js';
3
3
  import { toBeeMode } from "../../types/debug.js";
4
4
  import { http } from "../../utils/http.js";
5
- export const SUPPORTED_BEE_VERSION_EXACT = '2.4.0-390a402e';
5
+ export const SUPPORTED_BEE_VERSION_EXACT = '2.6.0-d0aa8b93';
6
6
  export const SUPPORTED_BEE_VERSION = SUPPORTED_BEE_VERSION_EXACT.split('-')[0];
7
- export const SUPPORTED_API_VERSION = '7.2.0';
7
+ export const SUPPORTED_API_VERSION = '7.3.0';
8
8
  const NODE_INFO_URL = 'node';
9
9
  const STATUS_URL = 'status';
10
10
  const HEALTH_URL = 'health';
@@ -57,6 +57,9 @@ export async function getDebugStatus(requestOptions) {
57
57
  }),
58
58
  committedDepth: Types.asNumber(body.committedDepth, {
59
59
  name: 'committedDepth'
60
+ }),
61
+ isWarmingUp: Types.asBoolean(body.isWarmingUp, {
62
+ name: 'isWarmingUp'
60
63
  })
61
64
  };
62
65
  }
@@ -1,3 +1,4 @@
1
+ import { Types } from 'cafe-utility';
1
2
  import { http } from "../utils/http.js";
2
3
  /**
3
4
  * Ping the base bee URL. If connection was not successful throw error
@@ -8,4 +9,15 @@ export async function checkConnection(requestOptions) {
8
9
  await http(requestOptions, {
9
10
  url: ''
10
11
  });
12
+ }
13
+ export async function isGateway(requestOptions) {
14
+ try {
15
+ const response = await http(requestOptions, {
16
+ url: '/gateway'
17
+ });
18
+ const data = Types.asObject(response.data);
19
+ return Types.asBoolean(data.gateway);
20
+ } catch (error) {
21
+ return false;
22
+ }
11
23
  }