@upcoming/bee-js 0.10.0 → 0.11.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/cjs/index.js +3 -1
- package/dist/cjs/modules/debug/stamps.js +3 -2
- package/dist/cjs/modules/debug/states.js +2 -1
- package/dist/cjs/stamper/stamper.js +43 -0
- package/dist/cjs/utils/stamps.js +1 -2
- package/dist/cjs/utils/workaround.js +27 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/index.js +2 -1
- package/dist/mjs/modules/debug/stamps.js +5 -4
- package/dist/mjs/modules/debug/states.js +3 -2
- package/dist/mjs/stamper/stamper.js +39 -0
- package/dist/mjs/utils/stamps.js +1 -2
- package/dist/mjs/utils/workaround.js +22 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/stamper/stamper.d.ts +15 -0
- package/dist/types/utils/workaround.d.ts +2 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -26,11 +26,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.BeeDev = exports.Bee = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
|
|
29
|
+
exports.Stamper = exports.BeeDev = exports.Bee = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
|
|
30
30
|
const bee_1 = require("./bee");
|
|
31
31
|
Object.defineProperty(exports, "Bee", { enumerable: true, get: function () { return bee_1.Bee; } });
|
|
32
32
|
const bee_dev_1 = require("./bee-dev");
|
|
33
33
|
Object.defineProperty(exports, "BeeDev", { enumerable: true, get: function () { return bee_dev_1.BeeDev; } });
|
|
34
|
+
const stamper_1 = require("./stamper/stamper");
|
|
35
|
+
Object.defineProperty(exports, "Stamper", { enumerable: true, get: function () { return stamper_1.Stamper; } });
|
|
34
36
|
var cafe_utility_1 = require("cafe-utility");
|
|
35
37
|
Object.defineProperty(exports, "MerkleTree", { enumerable: true, get: function () { return cafe_utility_1.MerkleTree; } });
|
|
36
38
|
var manifest_1 = require("./manifest/manifest");
|
|
@@ -7,6 +7,7 @@ const http_1 = require("../../utils/http");
|
|
|
7
7
|
const stamps_1 = require("../../utils/stamps");
|
|
8
8
|
const type_1 = require("../../utils/type");
|
|
9
9
|
const typed_bytes_1 = require("../../utils/typed-bytes");
|
|
10
|
+
const workaround_1 = require("../../utils/workaround");
|
|
10
11
|
const STAMPS_ENDPOINT = 'stamps';
|
|
11
12
|
const BATCHES_ENDPOINT = 'batches';
|
|
12
13
|
async function getGlobalPostageBatches(requestOptions) {
|
|
@@ -42,7 +43,7 @@ async function getAllPostageBatches(requestOptions) {
|
|
|
42
43
|
const depth = cafe_utility_1.Types.asNumber(x.depth, { name: 'depth' });
|
|
43
44
|
const bucketDepth = cafe_utility_1.Types.asNumber(x.bucketDepth, { name: 'bucketDepth' });
|
|
44
45
|
const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
|
|
45
|
-
const batchTTL = cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' });
|
|
46
|
+
const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' }));
|
|
46
47
|
const duration = duration_1.Duration.fromSeconds(batchTTL);
|
|
47
48
|
return {
|
|
48
49
|
batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
|
|
@@ -74,7 +75,7 @@ async function getPostageBatch(requestOptions, postageBatchId) {
|
|
|
74
75
|
const depth = cafe_utility_1.Types.asNumber(body.depth, { name: 'depth' });
|
|
75
76
|
const bucketDepth = cafe_utility_1.Types.asNumber(body.bucketDepth, { name: 'bucketDepth' });
|
|
76
77
|
const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
|
|
77
|
-
const batchTTL = cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' });
|
|
78
|
+
const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' }));
|
|
78
79
|
const duration = duration_1.Duration.fromSeconds(batchTTL);
|
|
79
80
|
return {
|
|
80
81
|
batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' })),
|
|
@@ -5,6 +5,7 @@ const cafe_utility_1 = require("cafe-utility");
|
|
|
5
5
|
const http_1 = require("../../utils/http");
|
|
6
6
|
const tokens_1 = require("../../utils/tokens");
|
|
7
7
|
const type_1 = require("../../utils/type");
|
|
8
|
+
const workaround_1 = require("../../utils/workaround");
|
|
8
9
|
const RESERVE_STATE_ENDPOINT = 'reservestate';
|
|
9
10
|
const WALLET_ENDPOINT = 'wallet';
|
|
10
11
|
const CHAIN_STATE_ENDPOINT = 'chainstate';
|
|
@@ -43,7 +44,7 @@ async function getChainState(requestOptions) {
|
|
|
43
44
|
block: cafe_utility_1.Types.asNumber(body.block, { name: 'block' }),
|
|
44
45
|
chainTip: cafe_utility_1.Types.asNumber(body.chainTip, { name: 'chainTip' }),
|
|
45
46
|
totalAmount: (0, type_1.asNumberString)(body.totalAmount, { name: 'totalAmount' }),
|
|
46
|
-
currentPrice: cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' }),
|
|
47
|
+
currentPrice: (0, workaround_1.normalizeCurrentPrice)(cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' })),
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
50
|
exports.getChainState = getChainState;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stamper = void 0;
|
|
4
|
+
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
6
|
+
class Stamper {
|
|
7
|
+
constructor(signer, batchId, buckets, depth) {
|
|
8
|
+
this.signer = new typed_bytes_1.PrivateKey(signer);
|
|
9
|
+
this.batchId = new typed_bytes_1.BatchId(batchId);
|
|
10
|
+
this.buckets = buckets;
|
|
11
|
+
this.depth = depth;
|
|
12
|
+
this.maxSlot = 2 ** (this.depth - 16);
|
|
13
|
+
}
|
|
14
|
+
static fromBlank(signer, batchId, depth) {
|
|
15
|
+
return new Stamper(signer, batchId, new Uint32Array(65536), depth);
|
|
16
|
+
}
|
|
17
|
+
static fromState(signer, batchId, buckets, depth) {
|
|
18
|
+
return new Stamper(signer, batchId, buckets, depth);
|
|
19
|
+
}
|
|
20
|
+
stamp(chunk) {
|
|
21
|
+
const address = chunk.hash();
|
|
22
|
+
const bucket = cafe_utility_1.Binary.uint16ToNumber(address, 'BE');
|
|
23
|
+
const height = this.buckets[bucket];
|
|
24
|
+
if (height >= this.maxSlot) {
|
|
25
|
+
throw Error('Bucket is full');
|
|
26
|
+
}
|
|
27
|
+
this.buckets[bucket]++;
|
|
28
|
+
const index = cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint32(bucket, 'BE'), cafe_utility_1.Binary.numberToUint32(height, 'BE'));
|
|
29
|
+
const timestamp = cafe_utility_1.Binary.numberToUint64(BigInt(Date.now()), 'BE');
|
|
30
|
+
const signature = this.signer.sign(cafe_utility_1.Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
|
|
31
|
+
return {
|
|
32
|
+
batchId: this.batchId,
|
|
33
|
+
index,
|
|
34
|
+
issuer: this.signer.publicKey().address().toUint8Array(),
|
|
35
|
+
signature: signature.toUint8Array(),
|
|
36
|
+
timestamp,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
getState() {
|
|
40
|
+
return this.buckets;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Stamper = Stamper;
|
package/dist/cjs/utils/stamps.js
CHANGED
|
@@ -80,8 +80,7 @@ exports.getStampEffectiveBytes = getStampEffectiveBytes;
|
|
|
80
80
|
* Utility function that calculates the cost of a postage batch based on its depth and amount.
|
|
81
81
|
*/
|
|
82
82
|
function getStampCost(depth, amount) {
|
|
83
|
-
|
|
84
|
-
return tokens_1.BZZ.fromPLUR(2n ** BigInt(depth) * amountBigInt);
|
|
83
|
+
return tokens_1.BZZ.fromPLUR(2n ** BigInt(depth) * BigInt(amount));
|
|
85
84
|
}
|
|
86
85
|
exports.getStampCost = getStampCost;
|
|
87
86
|
/**
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// TODO: Remove this file after the issue is fixed
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.normalizeCurrentPrice = exports.normalizeBatchTTL = void 0;
|
|
5
|
+
function normalizeBatchTTL(batchTTL) {
|
|
6
|
+
if (!Number.isInteger(batchTTL)) {
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
if (batchTTL < 1) {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
if (batchTTL > 315569260) {
|
|
13
|
+
return 315569260;
|
|
14
|
+
}
|
|
15
|
+
return batchTTL;
|
|
16
|
+
}
|
|
17
|
+
exports.normalizeBatchTTL = normalizeBatchTTL;
|
|
18
|
+
function normalizeCurrentPrice(currentPrice) {
|
|
19
|
+
if (!Number.isInteger(currentPrice)) {
|
|
20
|
+
return 24000;
|
|
21
|
+
}
|
|
22
|
+
if (currentPrice < 24000) {
|
|
23
|
+
return 24000;
|
|
24
|
+
}
|
|
25
|
+
return currentPrice;
|
|
26
|
+
}
|
|
27
|
+
exports.normalizeCurrentPrice = normalizeCurrentPrice;
|