@ethersphere/bee-js 8.3.1 → 9.0.2
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/README.md +192 -50
- package/dist/cjs/bee-dev.js +78 -0
- package/dist/cjs/bee.js +341 -417
- package/dist/cjs/chunk/bmt.js +15 -32
- package/dist/cjs/chunk/cac.js +26 -36
- package/dist/cjs/chunk/soc.js +56 -51
- package/dist/cjs/feed/identifier.js +3 -28
- package/dist/cjs/feed/index.js +95 -44
- package/dist/cjs/feed/retrievable.js +11 -22
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/manifest/manifest.js +369 -0
- package/dist/cjs/modules/bytes.js +24 -12
- package/dist/cjs/modules/bzz.js +24 -34
- package/dist/cjs/modules/chunk.js +13 -7
- package/dist/cjs/modules/debug/balance.js +29 -4
- package/dist/cjs/modules/debug/chequebook.js +60 -16
- package/dist/cjs/modules/debug/connectivity.js +82 -4
- package/dist/cjs/modules/debug/settlements.js +22 -2
- package/dist/cjs/modules/debug/stake.js +29 -11
- package/dist/cjs/modules/debug/stamps.js +88 -7
- package/dist/cjs/modules/debug/states.js +25 -3
- package/dist/cjs/modules/debug/status.js +45 -47
- package/dist/cjs/modules/debug/transactions.js +27 -5
- package/dist/cjs/modules/envelope.js +8 -6
- package/dist/cjs/modules/feed.js +25 -10
- package/dist/cjs/modules/grantee.js +18 -12
- package/dist/cjs/modules/gsoc.js +24 -0
- package/dist/cjs/modules/pinning.js +13 -2
- package/dist/cjs/modules/pss.js +9 -3
- package/dist/cjs/modules/soc.js +9 -4
- package/dist/cjs/modules/stewardship.js +7 -3
- package/dist/cjs/modules/tag.js +35 -3
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/stamper/stamper.js +43 -0
- package/dist/cjs/types/debug.js +16 -1
- package/dist/cjs/types/index.js +2 -24
- package/dist/cjs/utils/bytes.js +67 -115
- package/dist/cjs/utils/chunk-size.js +17 -0
- package/dist/cjs/utils/chunk-stream.browser.js +85 -0
- package/dist/cjs/utils/chunk-stream.js +84 -0
- package/dist/cjs/utils/cid.js +5 -6
- package/dist/cjs/utils/collection.browser.js +2 -2
- package/dist/cjs/utils/collection.js +3 -2
- package/dist/cjs/utils/collection.node.js +0 -2
- package/dist/cjs/utils/constants.js +7 -3
- package/dist/cjs/utils/data.browser.js +6 -3
- package/dist/cjs/utils/data.js +8 -4
- package/dist/cjs/utils/duration.js +51 -0
- package/dist/cjs/utils/expose.js +11 -40
- package/dist/cjs/utils/headers.js +79 -52
- package/dist/cjs/utils/http.js +34 -10
- package/dist/cjs/utils/mime.js +78 -0
- package/dist/cjs/utils/pss.js +3 -4
- package/dist/cjs/utils/redundancy.js +18 -14
- package/dist/cjs/utils/resource-locator.js +17 -0
- package/dist/cjs/utils/size.js +35 -0
- package/dist/cjs/utils/stamps.js +67 -51
- package/dist/cjs/utils/tar-uploader.browser.js +2 -2
- package/dist/cjs/utils/tar-uploader.js +2 -2
- package/dist/cjs/utils/tokens.js +144 -0
- package/dist/cjs/utils/type.js +142 -350
- package/dist/cjs/utils/typed-bytes.js +179 -0
- package/dist/cjs/utils/upload-progress.js +2 -0
- package/dist/cjs/utils/url.js +0 -4
- package/dist/cjs/utils/workaround.js +27 -0
- package/dist/index.browser.min.js +1 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/bee-dev.js +98 -0
- package/dist/mjs/bee.js +367 -411
- package/dist/mjs/chunk/bmt.js +13 -29
- package/dist/mjs/chunk/cac.js +26 -34
- package/dist/mjs/chunk/soc.js +57 -53
- package/dist/mjs/feed/identifier.js +3 -26
- package/dist/mjs/feed/index.js +98 -51
- package/dist/mjs/feed/retrievable.js +12 -23
- package/dist/mjs/index.js +10 -1
- package/dist/mjs/manifest/manifest.js +371 -0
- package/dist/mjs/modules/bytes.js +27 -15
- package/dist/mjs/modules/bzz.js +25 -32
- package/dist/mjs/modules/chunk.js +16 -8
- package/dist/mjs/modules/debug/balance.js +61 -4
- package/dist/mjs/modules/debug/chequebook.js +116 -16
- package/dist/mjs/modules/debug/connectivity.js +144 -3
- package/dist/mjs/modules/debug/settlements.js +46 -2
- package/dist/mjs/modules/debug/stake.js +69 -11
- package/dist/mjs/modules/debug/stamps.js +184 -7
- package/dist/mjs/modules/debug/states.js +55 -3
- package/dist/mjs/modules/debug/status.js +97 -45
- package/dist/mjs/modules/debug/transactions.js +61 -5
- package/dist/mjs/modules/envelope.js +11 -9
- package/dist/mjs/modules/feed.js +26 -10
- package/dist/mjs/modules/grantee.js +35 -13
- package/dist/mjs/modules/gsoc.js +16 -0
- package/dist/mjs/modules/pinning.js +23 -2
- package/dist/mjs/modules/pss.js +10 -4
- package/dist/mjs/modules/soc.js +10 -5
- package/dist/mjs/modules/stewardship.js +14 -4
- package/dist/mjs/modules/tag.js +93 -3
- package/dist/mjs/package.json +2 -1
- package/dist/mjs/stamper/stamper.js +39 -0
- package/dist/mjs/types/debug.js +15 -1
- package/dist/mjs/types/index.js +2 -24
- package/dist/mjs/utils/bytes.js +63 -104
- package/dist/mjs/utils/chunk-size.js +13 -0
- package/dist/mjs/utils/chunk-stream.browser.js +85 -0
- package/dist/mjs/utils/chunk-stream.js +87 -0
- package/dist/mjs/utils/cid.js +5 -6
- package/dist/mjs/utils/collection.browser.js +2 -2
- package/dist/mjs/utils/collection.js +2 -2
- package/dist/mjs/utils/collection.node.js +0 -2
- package/dist/mjs/utils/constants.js +6 -2
- package/dist/mjs/utils/data.browser.js +9 -3
- package/dist/mjs/utils/data.js +12 -4
- package/dist/mjs/utils/duration.js +47 -0
- package/dist/mjs/utils/expose.js +2 -7
- package/dist/mjs/utils/headers.js +73 -48
- package/dist/mjs/utils/http.js +33 -11
- package/dist/mjs/utils/mime.js +75 -0
- package/dist/mjs/utils/pss.js +3 -4
- package/dist/mjs/utils/redundancy.js +18 -8
- package/dist/mjs/utils/resource-locator.js +13 -0
- package/dist/mjs/utils/size.js +31 -0
- package/dist/mjs/utils/stamps.js +47 -44
- package/dist/mjs/utils/tar-uploader.browser.js +2 -2
- package/dist/mjs/utils/tar-uploader.js +2 -2
- package/dist/mjs/utils/tokens.js +139 -0
- package/dist/mjs/utils/type.js +216 -310
- package/dist/mjs/utils/typed-bytes.js +160 -0
- package/dist/mjs/utils/upload-progress.js +1 -0
- package/dist/mjs/utils/url.js +0 -4
- package/dist/mjs/utils/workaround.js +22 -0
- package/dist/types/bee-dev.d.ts +5 -0
- package/dist/types/bee.d.ts +88 -165
- package/dist/types/chunk/bmt.d.ts +2 -2
- package/dist/types/chunk/cac.d.ts +7 -24
- package/dist/types/chunk/soc.d.ts +15 -15
- package/dist/types/feed/identifier.d.ts +2 -4
- package/dist/types/feed/index.d.ts +14 -18
- package/dist/types/feed/retrievable.d.ts +3 -4
- package/dist/types/index.d.ts +28 -1
- package/dist/types/manifest/manifest.d.ts +106 -0
- package/dist/types/modules/bytes.d.ts +8 -5
- package/dist/types/modules/bzz.d.ts +8 -10
- package/dist/types/modules/chunk.d.ts +4 -3
- package/dist/types/modules/debug/balance.d.ts +3 -2
- package/dist/types/modules/debug/chequebook.d.ts +7 -6
- package/dist/types/modules/debug/connectivity.d.ts +5 -3
- package/dist/types/modules/debug/settlements.d.ts +2 -1
- package/dist/types/modules/debug/stake.d.ts +4 -2
- package/dist/types/modules/debug/stamps.d.ts +5 -4
- package/dist/types/modules/debug/status.d.ts +6 -25
- package/dist/types/modules/debug/transactions.d.ts +5 -4
- package/dist/types/modules/envelope.d.ts +3 -2
- package/dist/types/modules/feed.d.ts +20 -16
- package/dist/types/modules/grantee.d.ts +7 -6
- package/dist/types/modules/gsoc.d.ts +7 -0
- package/dist/types/modules/pinning.d.ts +2 -4
- package/dist/types/modules/pss.d.ts +4 -3
- package/dist/types/modules/soc.d.ts +3 -2
- package/dist/types/modules/stewardship.d.ts +4 -4
- package/dist/types/modules/tag.d.ts +2 -1
- package/dist/types/stamper/stamper.d.ts +15 -0
- package/dist/types/types/debug.d.ts +63 -115
- package/dist/types/types/index.d.ts +103 -204
- package/dist/types/utils/bytes.d.ts +16 -90
- package/dist/types/utils/chunk-size.d.ts +1 -0
- package/dist/types/utils/chunk-stream.browser.d.ts +6 -0
- package/dist/types/utils/chunk-stream.d.ts +6 -0
- package/dist/types/utils/cid.d.ts +3 -2
- package/dist/types/utils/collection.browser.d.ts +2 -2
- package/dist/types/utils/collection.d.ts +2 -1
- package/dist/types/utils/collection.node.d.ts +0 -1
- package/dist/types/utils/constants.d.ts +4 -1
- package/dist/types/utils/duration.d.ts +17 -0
- package/dist/types/utils/error.d.ts +2 -2
- package/dist/types/utils/expose.d.ts +2 -7
- package/dist/types/utils/headers.d.ts +3 -4
- package/dist/types/utils/mime.d.ts +1 -0
- package/dist/types/utils/pss.d.ts +2 -2
- package/dist/types/utils/resource-locator.d.ts +6 -0
- package/dist/types/utils/size.d.ts +16 -0
- package/dist/types/utils/stamps.d.ts +27 -33
- package/dist/types/utils/tar-uploader.browser.d.ts +3 -4
- package/dist/types/utils/tar-uploader.d.ts +3 -4
- package/dist/types/utils/tokens.d.ts +77 -0
- package/dist/types/utils/type.d.ts +20 -52
- package/dist/types/utils/typed-bytes.d.ts +68 -0
- package/dist/types/utils/upload-progress.d.ts +4 -0
- package/dist/types/utils/workaround.d.ts +2 -0
- package/package.json +11 -17
- package/dist/cjs/chunk/signer.js +0 -126
- package/dist/cjs/chunk/span.js +0 -25
- package/dist/cjs/feed/json.js +0 -28
- package/dist/cjs/feed/topic.js +0 -25
- package/dist/cjs/feed/type.js +0 -15
- package/dist/cjs/modules/debug/chunk.js +0 -21
- package/dist/cjs/modules/debug/tag.js +0 -19
- package/dist/cjs/utils/eth.js +0 -216
- package/dist/cjs/utils/hash.js +0 -21
- package/dist/cjs/utils/hex.js +0 -150
- package/dist/cjs/utils/reference.js +0 -36
- package/dist/index.browser.min.js.LICENSE.txt +0 -8
- package/dist/mjs/chunk/signer.js +0 -114
- package/dist/mjs/chunk/span.js +0 -21
- package/dist/mjs/feed/json.js +0 -26
- package/dist/mjs/feed/topic.js +0 -19
- package/dist/mjs/feed/type.js +0 -10
- package/dist/mjs/modules/debug/chunk.js +0 -17
- package/dist/mjs/modules/debug/tag.js +0 -15
- package/dist/mjs/utils/eth.js +0 -192
- package/dist/mjs/utils/hash.js +0 -16
- package/dist/mjs/utils/hex.js +0 -135
- package/dist/mjs/utils/reference.js +0 -29
- package/dist/types/chunk/signer.d.ts +0 -31
- package/dist/types/chunk/span.d.ts +0 -10
- package/dist/types/feed/json.d.ts +0 -4
- package/dist/types/feed/topic.d.ts +0 -3
- package/dist/types/feed/type.d.ts +0 -6
- package/dist/types/modules/debug/chunk.d.ts +0 -10
- package/dist/types/modules/debug/tag.d.ts +0 -8
- package/dist/types/utils/eth.d.ts +0 -67
- package/dist/types/utils/hash.d.ts +0 -9
- package/dist/types/utils/hex.d.ts +0 -86
- package/dist/types/utils/reference.d.ts +0 -2
package/dist/cjs/chunk/bmt.js
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
9
|
-
const { keccak256 } = js_sha3_1.default;
|
|
3
|
+
exports.calculateChunkAddress = void 0;
|
|
4
|
+
const cafe_utility_1 = require("cafe-utility");
|
|
10
5
|
const error_1 = require("../utils/error");
|
|
11
|
-
const
|
|
6
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
12
7
|
const MAX_CHUNK_PAYLOAD_SIZE = 4096;
|
|
13
8
|
const SEGMENT_SIZE = 32;
|
|
14
|
-
const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE;
|
|
15
|
-
const HASH_SIZE = 32;
|
|
16
9
|
/**
|
|
17
10
|
* Calculate a Binary Merkle Tree hash for a chunk
|
|
18
11
|
*
|
|
@@ -27,29 +20,19 @@ const HASH_SIZE = 32;
|
|
|
27
20
|
*
|
|
28
21
|
* @returns the keccak256 hash in a byte array
|
|
29
22
|
*/
|
|
30
|
-
function
|
|
31
|
-
const span = chunkContent.slice(0,
|
|
32
|
-
const payload = chunkContent.slice(
|
|
33
|
-
const rootHash =
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
return chunkHash;
|
|
23
|
+
function calculateChunkAddress(chunkContent) {
|
|
24
|
+
const span = chunkContent.slice(0, typed_bytes_1.Span.LENGTH);
|
|
25
|
+
const payload = chunkContent.slice(typed_bytes_1.Span.LENGTH);
|
|
26
|
+
const rootHash = calculateBmtRootHash(payload);
|
|
27
|
+
const chunkHash = cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(span, rootHash));
|
|
28
|
+
return new typed_bytes_1.Reference(chunkHash);
|
|
37
29
|
}
|
|
38
|
-
exports.
|
|
39
|
-
function
|
|
30
|
+
exports.calculateChunkAddress = calculateChunkAddress;
|
|
31
|
+
function calculateBmtRootHash(payload) {
|
|
40
32
|
if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
|
|
41
|
-
throw new error_1.BeeArgumentError(
|
|
33
|
+
throw new error_1.BeeArgumentError(`payload size ${payload.length} exceeds maximum chunk payload size ${MAX_CHUNK_PAYLOAD_SIZE}`, payload);
|
|
42
34
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const output = new Uint8Array(input.length / 2);
|
|
47
|
-
// in each round we hash the segment pairs together
|
|
48
|
-
for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
|
|
49
|
-
const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
|
|
50
|
-
output.set(hashNumbers, offset / 2);
|
|
51
|
-
}
|
|
52
|
-
input = output;
|
|
53
|
-
}
|
|
54
|
-
return input;
|
|
35
|
+
const input = new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE);
|
|
36
|
+
input.set(payload);
|
|
37
|
+
return cafe_utility_1.Binary.log2Reduce(cafe_utility_1.Binary.partition(input, SEGMENT_SIZE), (a, b) => cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(a, b)));
|
|
55
38
|
}
|
package/dist/cjs/chunk/cac.js
CHANGED
|
@@ -1,56 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.asContentAddressedChunk = exports.makeContentAddressedChunk = exports.MAX_PAYLOAD_SIZE = exports.MIN_PAYLOAD_SIZE = void 0;
|
|
4
4
|
const cafe_utility_1 = require("cafe-utility");
|
|
5
5
|
const bytes_1 = require("../utils/bytes");
|
|
6
|
-
const
|
|
6
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
7
7
|
const bmt_1 = require("./bmt");
|
|
8
|
-
const span_1 = require("./span");
|
|
9
8
|
exports.MIN_PAYLOAD_SIZE = 1;
|
|
10
9
|
exports.MAX_PAYLOAD_SIZE = 4096;
|
|
11
|
-
const
|
|
12
|
-
const CAC_PAYLOAD_OFFSET = CAC_SPAN_OFFSET + span_1.SPAN_SIZE;
|
|
10
|
+
const ENCODER = new TextEncoder();
|
|
13
11
|
/**
|
|
14
12
|
* Creates a content addressed chunk and verifies the payload size.
|
|
15
13
|
*
|
|
16
14
|
* @param payloadBytes the data to be stored in the chunk
|
|
17
15
|
*/
|
|
18
16
|
function makeContentAddressedChunk(payloadBytes) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
if (!(payloadBytes instanceof Uint8Array)) {
|
|
18
|
+
payloadBytes = ENCODER.encode(payloadBytes);
|
|
19
|
+
}
|
|
20
|
+
if (payloadBytes.length < exports.MIN_PAYLOAD_SIZE || payloadBytes.length > exports.MAX_PAYLOAD_SIZE) {
|
|
21
|
+
throw new RangeError(`payload size ${payloadBytes.length} exceeds limits [${exports.MIN_PAYLOAD_SIZE}, ${exports.MAX_PAYLOAD_SIZE}]`);
|
|
22
|
+
}
|
|
23
|
+
const span = typed_bytes_1.Span.fromBigInt(BigInt(payloadBytes.length));
|
|
24
|
+
const data = cafe_utility_1.Binary.concatBytes(span.toUint8Array(), payloadBytes);
|
|
22
25
|
return {
|
|
23
26
|
data,
|
|
24
|
-
span
|
|
25
|
-
payload:
|
|
26
|
-
address: (
|
|
27
|
+
span,
|
|
28
|
+
payload: bytes_1.Bytes.fromSlice(data, typed_bytes_1.Span.LENGTH),
|
|
29
|
+
address: (0, bmt_1.calculateChunkAddress)(data),
|
|
27
30
|
};
|
|
28
31
|
}
|
|
29
32
|
exports.makeContentAddressedChunk = makeContentAddressedChunk;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* @param data The chunk data
|
|
34
|
-
* @param chunkAddress The address of the chunk
|
|
35
|
-
*/
|
|
36
|
-
function isValidChunkData(data, chunkAddress) {
|
|
37
|
-
if (!(data instanceof Uint8Array))
|
|
38
|
-
return false;
|
|
39
|
-
const address = (0, bmt_1.bmtHash)(data);
|
|
40
|
-
return (0, bytes_1.bytesEqual)(address, chunkAddress);
|
|
41
|
-
}
|
|
42
|
-
exports.isValidChunkData = isValidChunkData;
|
|
43
|
-
/**
|
|
44
|
-
* Asserts if data are representing given address of its chunk.
|
|
45
|
-
*
|
|
46
|
-
* @param data The chunk data
|
|
47
|
-
* @param chunkAddress The address of the chunk
|
|
48
|
-
*
|
|
49
|
-
* @returns a valid content addressed chunk or throws error
|
|
50
|
-
*/
|
|
51
|
-
function assertValidChunkData(data, chunkAddress) {
|
|
52
|
-
if (!isValidChunkData(data, chunkAddress)) {
|
|
53
|
-
throw new error_1.BeeError('Address of content address chunk does not match given data!');
|
|
33
|
+
function asContentAddressedChunk(chunkBytes) {
|
|
34
|
+
if (chunkBytes.length < exports.MIN_PAYLOAD_SIZE + typed_bytes_1.Span.LENGTH || chunkBytes.length > exports.MAX_PAYLOAD_SIZE + typed_bytes_1.Span.LENGTH) {
|
|
35
|
+
throw new RangeError(`chunk size ${chunkBytes.length} exceeds limits [${exports.MIN_PAYLOAD_SIZE + typed_bytes_1.Span.LENGTH}, ${typed_bytes_1.Span.LENGTH}]`);
|
|
54
36
|
}
|
|
37
|
+
const span = typed_bytes_1.Span.fromSlice(chunkBytes, 0);
|
|
38
|
+
const data = cafe_utility_1.Binary.concatBytes(span.toUint8Array(), chunkBytes.slice(typed_bytes_1.Span.LENGTH));
|
|
39
|
+
return {
|
|
40
|
+
data,
|
|
41
|
+
span,
|
|
42
|
+
payload: bytes_1.Bytes.fromSlice(data, typed_bytes_1.Span.LENGTH),
|
|
43
|
+
address: (0, bmt_1.calculateChunkAddress)(data),
|
|
44
|
+
};
|
|
55
45
|
}
|
|
56
|
-
exports.
|
|
46
|
+
exports.asContentAddressedChunk = asContentAddressedChunk;
|
package/dist/cjs/chunk/soc.js
CHANGED
|
@@ -23,31 +23,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.downloadSingleOwnerChunk = exports.uploadSingleOwnerChunkData = exports.uploadSingleOwnerChunk = exports.makeSingleOwnerChunk = exports.makeSOCAddress = exports.makeSingleOwnerChunkFromData = void 0;
|
|
26
|
+
exports.downloadSingleOwnerChunk = exports.uploadSingleOwnerChunkWithWrappedChunk = exports.uploadSingleOwnerChunkData = exports.uploadSingleOwnerChunk = exports.makeSingleOwnerChunk = exports.makeSOCAddress = exports.makeSingleOwnerChunkFromData = void 0;
|
|
27
27
|
const cafe_utility_1 = require("cafe-utility");
|
|
28
28
|
const chunkAPI = __importStar(require("../modules/chunk"));
|
|
29
29
|
const socAPI = __importStar(require("../modules/soc"));
|
|
30
30
|
const bytes_1 = require("../utils/bytes");
|
|
31
31
|
const error_1 = require("../utils/error");
|
|
32
|
-
const
|
|
33
|
-
const hex_1 = require("../utils/hex");
|
|
32
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
34
33
|
const bmt_1 = require("./bmt");
|
|
35
34
|
const cac_1 = require("./cac");
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
const SIGNATURE_SIZE = 65;
|
|
40
|
-
const SOC_IDENTIFIER_OFFSET = 0;
|
|
41
|
-
const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
|
|
42
|
-
const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
|
|
43
|
-
const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + span_1.SPAN_SIZE;
|
|
35
|
+
const SOC_SIGNATURE_OFFSET = typed_bytes_1.Identifier.LENGTH;
|
|
36
|
+
const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + typed_bytes_1.Signature.LENGTH;
|
|
37
|
+
const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + typed_bytes_1.Span.LENGTH;
|
|
44
38
|
function recoverChunkOwner(data) {
|
|
45
39
|
const cacData = data.slice(SOC_SPAN_OFFSET);
|
|
46
|
-
const chunkAddress = (0, bmt_1.
|
|
47
|
-
const signature =
|
|
48
|
-
const identifier =
|
|
49
|
-
const digest = (
|
|
50
|
-
const ownerAddress =
|
|
40
|
+
const chunkAddress = (0, bmt_1.calculateChunkAddress)(cacData);
|
|
41
|
+
const signature = typed_bytes_1.Signature.fromSlice(data, SOC_SIGNATURE_OFFSET);
|
|
42
|
+
const identifier = bytes_1.Bytes.fromSlice(data, 0, typed_bytes_1.Identifier.LENGTH);
|
|
43
|
+
const digest = cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), chunkAddress.toUint8Array());
|
|
44
|
+
const ownerAddress = signature.recoverPublicKey(digest).address();
|
|
51
45
|
return ownerAddress;
|
|
52
46
|
}
|
|
53
47
|
/**
|
|
@@ -59,28 +53,30 @@ function recoverChunkOwner(data) {
|
|
|
59
53
|
* @returns a single owner chunk or throws error
|
|
60
54
|
*/
|
|
61
55
|
function makeSingleOwnerChunkFromData(data, address) {
|
|
56
|
+
data = data instanceof bytes_1.Bytes ? data.toUint8Array() : data;
|
|
57
|
+
address = new typed_bytes_1.Reference(address);
|
|
62
58
|
const ownerAddress = recoverChunkOwner(data);
|
|
63
|
-
const identifier =
|
|
64
|
-
const socAddress =
|
|
65
|
-
if (!(
|
|
59
|
+
const identifier = bytes_1.Bytes.fromSlice(data, 0, typed_bytes_1.Identifier.LENGTH);
|
|
60
|
+
const socAddress = new typed_bytes_1.Reference(cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), ownerAddress.toUint8Array())));
|
|
61
|
+
if (!cafe_utility_1.Binary.equals(address.toUint8Array(), socAddress.toUint8Array())) {
|
|
66
62
|
throw new error_1.BeeError('SOC Data does not match given address!');
|
|
67
63
|
}
|
|
68
|
-
const signature =
|
|
69
|
-
const span =
|
|
70
|
-
const payload =
|
|
64
|
+
const signature = typed_bytes_1.Signature.fromSlice(data, SOC_SIGNATURE_OFFSET);
|
|
65
|
+
const span = typed_bytes_1.Span.fromSlice(data, SOC_SPAN_OFFSET);
|
|
66
|
+
const payload = bytes_1.Bytes.fromSlice(data, SOC_PAYLOAD_OFFSET);
|
|
71
67
|
return {
|
|
72
68
|
data,
|
|
73
|
-
identifier
|
|
69
|
+
identifier,
|
|
74
70
|
signature,
|
|
75
71
|
span,
|
|
76
72
|
payload,
|
|
77
|
-
address:
|
|
78
|
-
owner:
|
|
73
|
+
address: socAddress,
|
|
74
|
+
owner: ownerAddress,
|
|
79
75
|
};
|
|
80
76
|
}
|
|
81
77
|
exports.makeSingleOwnerChunkFromData = makeSingleOwnerChunkFromData;
|
|
82
78
|
function makeSOCAddress(identifier, address) {
|
|
83
|
-
return
|
|
79
|
+
return new typed_bytes_1.Reference(cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), address.toUint8Array())));
|
|
84
80
|
}
|
|
85
81
|
exports.makeSOCAddress = makeSOCAddress;
|
|
86
82
|
/**
|
|
@@ -88,23 +84,24 @@ exports.makeSOCAddress = makeSOCAddress;
|
|
|
88
84
|
*
|
|
89
85
|
* @param chunk A chunk object used for the span and payload
|
|
90
86
|
* @param identifier The identifier of the chunk
|
|
91
|
-
* @param signer The
|
|
87
|
+
* @param signer The signer interface for signing the chunk
|
|
92
88
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
const signature =
|
|
98
|
-
const data = cafe_utility_1.Binary.concatBytes(identifier, signature
|
|
99
|
-
const
|
|
89
|
+
function makeSingleOwnerChunk(chunk, identifier, signer) {
|
|
90
|
+
identifier = new typed_bytes_1.Identifier(identifier);
|
|
91
|
+
signer = new typed_bytes_1.PrivateKey(signer);
|
|
92
|
+
const address = makeSOCAddress(identifier, signer.publicKey().address());
|
|
93
|
+
const signature = signer.sign(cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), chunk.address.toUint8Array()));
|
|
94
|
+
const data = cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), signature.toUint8Array(), chunk.data);
|
|
95
|
+
const span = typed_bytes_1.Span.fromSlice(chunk.data, 0);
|
|
96
|
+
const payload = bytes_1.Bytes.fromSlice(chunk.data, typed_bytes_1.Span.LENGTH);
|
|
100
97
|
return {
|
|
101
98
|
data,
|
|
102
|
-
identifier
|
|
103
|
-
signature
|
|
104
|
-
span
|
|
105
|
-
payload
|
|
106
|
-
address
|
|
107
|
-
owner: ()
|
|
99
|
+
identifier,
|
|
100
|
+
signature,
|
|
101
|
+
span,
|
|
102
|
+
payload,
|
|
103
|
+
address,
|
|
104
|
+
owner: signer.publicKey().address(),
|
|
108
105
|
};
|
|
109
106
|
}
|
|
110
107
|
exports.makeSingleOwnerChunk = makeSingleOwnerChunk;
|
|
@@ -115,43 +112,51 @@ exports.makeSingleOwnerChunk = makeSingleOwnerChunk;
|
|
|
115
112
|
*
|
|
116
113
|
* @param requestOptions Options for making requests
|
|
117
114
|
* @param chunk A chunk object
|
|
118
|
-
* @param
|
|
115
|
+
* @param stamp Postage BatchId that will be assigned to uploaded data
|
|
119
116
|
* @param options Upload options
|
|
120
117
|
*/
|
|
121
118
|
async function uploadSingleOwnerChunk(requestOptions, chunk, stamp, options) {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
const signature = (0, hex_1.bytesToHex)(chunk.signature());
|
|
125
|
-
const data = cafe_utility_1.Binary.concatBytes(chunk.span(), chunk.payload());
|
|
126
|
-
return socAPI.upload(requestOptions, owner, identifier, signature, data, stamp, options);
|
|
119
|
+
const data = cafe_utility_1.Binary.concatBytes(chunk.span.toUint8Array(), chunk.payload.toUint8Array());
|
|
120
|
+
return socAPI.upload(requestOptions, chunk.owner, chunk.identifier, chunk.signature, data, stamp, options);
|
|
127
121
|
}
|
|
128
122
|
exports.uploadSingleOwnerChunk = uploadSingleOwnerChunk;
|
|
129
123
|
/**
|
|
130
124
|
* Helper function to create and upload SOC.
|
|
131
125
|
*
|
|
132
126
|
* @param requestOptions Options for making requests
|
|
133
|
-
* @param signer The
|
|
127
|
+
* @param signer The signer interface for signing the chunk
|
|
134
128
|
* @param postageBatchId
|
|
135
129
|
* @param identifier The identifier of the chunk
|
|
136
130
|
* @param data The chunk data
|
|
137
131
|
* @param options
|
|
138
132
|
*/
|
|
139
133
|
async function uploadSingleOwnerChunkData(requestOptions, signer, stamp, identifier, data, options) {
|
|
134
|
+
signer = new typed_bytes_1.PrivateKey(signer);
|
|
135
|
+
identifier = new typed_bytes_1.Identifier(identifier);
|
|
140
136
|
const cac = (0, cac_1.makeContentAddressedChunk)(data);
|
|
141
|
-
const soc =
|
|
137
|
+
const soc = makeSingleOwnerChunk(cac, identifier, signer);
|
|
142
138
|
return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
|
|
143
139
|
}
|
|
144
140
|
exports.uploadSingleOwnerChunkData = uploadSingleOwnerChunkData;
|
|
141
|
+
async function uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, stamp, identifier, rootChunk, options) {
|
|
142
|
+
signer = new typed_bytes_1.PrivateKey(signer);
|
|
143
|
+
identifier = new typed_bytes_1.Identifier(identifier);
|
|
144
|
+
const soc = makeSingleOwnerChunk((0, cac_1.asContentAddressedChunk)(rootChunk), identifier, signer);
|
|
145
|
+
return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
|
|
146
|
+
}
|
|
147
|
+
exports.uploadSingleOwnerChunkWithWrappedChunk = uploadSingleOwnerChunkWithWrappedChunk;
|
|
145
148
|
/**
|
|
146
149
|
* Helper function to download SOC.
|
|
147
150
|
*
|
|
148
151
|
* @param url The url of the Bee service
|
|
149
|
-
* @param ownerAddress The
|
|
152
|
+
* @param ownerAddress The signer interface for signing the chunk
|
|
150
153
|
* @param identifier The identifier of the chunk
|
|
151
154
|
*/
|
|
152
155
|
async function downloadSingleOwnerChunk(requestOptions, ownerAddress, identifier) {
|
|
156
|
+
identifier = new typed_bytes_1.Identifier(identifier);
|
|
157
|
+
ownerAddress = new typed_bytes_1.EthAddress(ownerAddress);
|
|
153
158
|
const address = makeSOCAddress(identifier, ownerAddress);
|
|
154
|
-
const
|
|
155
|
-
return makeSingleOwnerChunkFromData(
|
|
159
|
+
const cac = await chunkAPI.download(requestOptions, address.toHex());
|
|
160
|
+
return makeSingleOwnerChunkFromData(cac, address);
|
|
156
161
|
}
|
|
157
162
|
exports.downloadSingleOwnerChunk = downloadSingleOwnerChunk;
|
|
@@ -2,34 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.makeFeedIdentifier = void 0;
|
|
4
4
|
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
-
const
|
|
6
|
-
const hash_1 = require("../utils/hash");
|
|
7
|
-
const hex_1 = require("../utils/hex");
|
|
8
|
-
function isEpoch(epoch) {
|
|
9
|
-
return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
|
|
10
|
-
}
|
|
11
|
-
function hashFeedIdentifier(topic, index) {
|
|
12
|
-
return (0, hash_1.keccak256Hash)((0, hex_1.hexToBytes)(topic), index);
|
|
13
|
-
}
|
|
14
|
-
function makeSequentialFeedIdentifier(topic, index) {
|
|
15
|
-
const indexBytes = cafe_utility_1.Binary.numberToUint64BE(index);
|
|
16
|
-
return hashFeedIdentifier(topic, indexBytes);
|
|
17
|
-
}
|
|
18
|
-
function makeFeedIndexBytes(s) {
|
|
19
|
-
const hex = (0, hex_1.makeHexString)(s, types_1.FEED_INDEX_HEX_LENGTH);
|
|
20
|
-
return (0, hex_1.hexToBytes)(hex);
|
|
21
|
-
}
|
|
5
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
22
6
|
function makeFeedIdentifier(topic, index) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
else if (typeof index === 'string') {
|
|
27
|
-
const indexBytes = makeFeedIndexBytes(index);
|
|
28
|
-
return hashFeedIdentifier(topic, indexBytes);
|
|
29
|
-
}
|
|
30
|
-
else if (isEpoch(index)) {
|
|
31
|
-
throw new TypeError('epoch is not yet implemented');
|
|
32
|
-
}
|
|
33
|
-
return hashFeedIdentifier(topic, index);
|
|
7
|
+
index = typeof index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(index)) : index;
|
|
8
|
+
return new typed_bytes_1.Identifier(cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(topic.toUint8Array(), index.toUint8Array())));
|
|
34
9
|
}
|
|
35
10
|
exports.makeFeedIdentifier = makeFeedIdentifier;
|
package/dist/cjs/feed/index.js
CHANGED
|
@@ -23,94 +23,145 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdate = exports.getFeedUpdateChunkReference = exports.
|
|
26
|
+
exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdateAsCAC = exports.downloadFeedUpdate = exports.getFeedUpdateChunkReference = exports.updateFeedWithPayload = exports.updateFeedWithReference = exports.findNextIndex = void 0;
|
|
27
27
|
const cafe_utility_1 = require("cafe-utility");
|
|
28
|
+
const cac_1 = require("../chunk/cac");
|
|
28
29
|
const soc_1 = require("../chunk/soc");
|
|
30
|
+
const bytes = __importStar(require("../modules/bytes"));
|
|
29
31
|
const chunkAPI = __importStar(require("../modules/chunk"));
|
|
30
32
|
const feed_1 = require("../modules/feed");
|
|
31
|
-
const types_1 = require("../types");
|
|
32
33
|
const bytes_1 = require("../utils/bytes");
|
|
33
34
|
const error_1 = require("../utils/error");
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const hex_1 = require("../utils/hex");
|
|
37
|
-
const reference_1 = require("../utils/reference");
|
|
38
|
-
const type_1 = require("../utils/type");
|
|
35
|
+
const resource_locator_1 = require("../utils/resource-locator");
|
|
36
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
39
37
|
const identifier_1 = require("./identifier");
|
|
40
38
|
const TIMESTAMP_PAYLOAD_OFFSET = 0;
|
|
41
39
|
const TIMESTAMP_PAYLOAD_SIZE = 8;
|
|
42
40
|
const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE;
|
|
43
|
-
async function findNextIndex(requestOptions, owner, topic
|
|
41
|
+
async function findNextIndex(requestOptions, owner, topic) {
|
|
44
42
|
try {
|
|
45
|
-
const feedUpdate = await (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic
|
|
46
|
-
|
|
43
|
+
const feedUpdate = await (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
|
|
44
|
+
if (!feedUpdate.feedIndexNext) {
|
|
45
|
+
throw Error('Feed index next is not defined. This should happen when fetching an exact index.');
|
|
46
|
+
}
|
|
47
|
+
return feedUpdate.feedIndexNext;
|
|
47
48
|
}
|
|
48
49
|
catch (e) {
|
|
49
50
|
if (e instanceof error_1.BeeResponseError) {
|
|
50
|
-
return
|
|
51
|
+
return typed_bytes_1.FeedIndex.fromBigInt(0n);
|
|
51
52
|
}
|
|
52
53
|
throw e;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
exports.findNextIndex = findNextIndex;
|
|
56
|
-
async function
|
|
57
|
-
|
|
58
|
-
const nextIndex = options?.index ?? (await findNextIndex(requestOptions,
|
|
57
|
+
async function updateFeedWithReference(requestOptions, signer, topic, reference, postageBatchId, options) {
|
|
58
|
+
reference = new typed_bytes_1.Reference(reference);
|
|
59
|
+
const nextIndex = options?.index ?? (await findNextIndex(requestOptions, signer.publicKey().address(), topic));
|
|
59
60
|
const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
|
|
60
61
|
const at = options?.at ?? Date.now() / 1000.0;
|
|
61
|
-
const timestamp = cafe_utility_1.Binary.
|
|
62
|
-
const payloadBytes = cafe_utility_1.Binary.concatBytes(timestamp, reference);
|
|
62
|
+
const timestamp = cafe_utility_1.Binary.numberToUint64(BigInt(Math.floor(at)), 'BE');
|
|
63
|
+
const payloadBytes = cafe_utility_1.Binary.concatBytes(timestamp, reference.toUint8Array());
|
|
63
64
|
return (0, soc_1.uploadSingleOwnerChunkData)(requestOptions, signer, postageBatchId, identifier, payloadBytes, options);
|
|
64
65
|
}
|
|
65
|
-
exports.
|
|
66
|
+
exports.updateFeedWithReference = updateFeedWithReference;
|
|
67
|
+
async function updateFeedWithPayload(requestOptions, signer, topic, data, postageBatchId, options) {
|
|
68
|
+
const nextIndex = options?.index ?? (await findNextIndex(requestOptions, signer.publicKey().address(), topic));
|
|
69
|
+
const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
|
|
70
|
+
if (data.length > 4096) {
|
|
71
|
+
const uploadResult = await bytes.upload(requestOptions, data, postageBatchId, options);
|
|
72
|
+
const rootChunk = await chunkAPI.download(requestOptions, uploadResult.reference);
|
|
73
|
+
return (0, soc_1.uploadSingleOwnerChunkWithWrappedChunk)(requestOptions, signer, postageBatchId, identifier, rootChunk, options);
|
|
74
|
+
}
|
|
75
|
+
return (0, soc_1.uploadSingleOwnerChunkData)(requestOptions, signer, postageBatchId, identifier, cafe_utility_1.Types.isString(data) ? bytes_1.Bytes.fromUtf8(data).toUint8Array() : data, options);
|
|
76
|
+
}
|
|
77
|
+
exports.updateFeedWithPayload = updateFeedWithPayload;
|
|
66
78
|
function getFeedUpdateChunkReference(owner, topic, index) {
|
|
67
79
|
const identifier = (0, identifier_1.makeFeedIdentifier)(topic, index);
|
|
68
|
-
return
|
|
80
|
+
return new typed_bytes_1.Reference(cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), owner.toUint8Array())));
|
|
69
81
|
}
|
|
70
82
|
exports.getFeedUpdateChunkReference = getFeedUpdateChunkReference;
|
|
71
|
-
async function downloadFeedUpdate(requestOptions, owner, topic, index) {
|
|
83
|
+
async function downloadFeedUpdate(requestOptions, owner, topic, index, hasTimestamp = false) {
|
|
84
|
+
index = typeof index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(index)) : index;
|
|
72
85
|
const address = getFeedUpdateChunkReference(owner, topic, index);
|
|
73
|
-
const
|
|
74
|
-
const data = await chunkAPI.download(requestOptions, addressHex);
|
|
86
|
+
const data = await chunkAPI.download(requestOptions, address.toHex());
|
|
75
87
|
const soc = (0, soc_1.makeSingleOwnerChunkFromData)(data, address);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
let timestamp = cafe_utility_1.Optional.empty();
|
|
89
|
+
if (hasTimestamp) {
|
|
90
|
+
const timestampBytes = bytes_1.Bytes.fromSlice(soc.payload.toUint8Array(), TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
|
|
91
|
+
timestamp = cafe_utility_1.Optional.of(Number(cafe_utility_1.Binary.uint64ToNumber(timestampBytes.toUint8Array(), 'BE')));
|
|
92
|
+
}
|
|
80
93
|
return {
|
|
81
94
|
timestamp,
|
|
82
|
-
|
|
95
|
+
payload: new bytes_1.Bytes(soc.payload.offset(hasTimestamp ? REFERENCE_PAYLOAD_OFFSET : 0)),
|
|
83
96
|
};
|
|
84
97
|
}
|
|
85
98
|
exports.downloadFeedUpdate = downloadFeedUpdate;
|
|
86
|
-
function
|
|
99
|
+
async function downloadFeedUpdateAsCAC(requestOptions, owner, topic, index) {
|
|
100
|
+
index = typeof index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(index)) : index;
|
|
101
|
+
const address = getFeedUpdateChunkReference(owner, topic, index);
|
|
102
|
+
const data = await chunkAPI.download(requestOptions, address);
|
|
103
|
+
return (0, cac_1.asContentAddressedChunk)(data.slice(typed_bytes_1.Identifier.LENGTH + typed_bytes_1.Signature.LENGTH));
|
|
104
|
+
}
|
|
105
|
+
exports.downloadFeedUpdateAsCAC = downloadFeedUpdateAsCAC;
|
|
106
|
+
function makeFeedReader(requestOptions, topic, owner) {
|
|
107
|
+
// TODO: remove after enough time has passed in deprecated version
|
|
108
|
+
const download = async (options) => {
|
|
109
|
+
if (options?.index === undefined) {
|
|
110
|
+
return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
|
|
111
|
+
}
|
|
112
|
+
const update = await downloadFeedUpdate(requestOptions, owner, topic, options.index, options.hasTimestamp ?? true);
|
|
113
|
+
const feedIndex = typeof options.index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
|
|
114
|
+
return {
|
|
115
|
+
payload: update.payload,
|
|
116
|
+
feedIndex,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const downloadPayload = async (options) => {
|
|
120
|
+
if (options?.index === undefined) {
|
|
121
|
+
return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
|
|
122
|
+
}
|
|
123
|
+
const cac = await downloadFeedUpdateAsCAC(requestOptions, owner, topic, options.index);
|
|
124
|
+
const payload = cac.span.toBigInt() <= 4096n
|
|
125
|
+
? cac.payload
|
|
126
|
+
: await bytes.download(requestOptions, new resource_locator_1.ResourceLocator(cac.address));
|
|
127
|
+
const feedIndex = typeof options.index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
|
|
128
|
+
return {
|
|
129
|
+
payload,
|
|
130
|
+
feedIndex,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const downloadReference = async (options) => {
|
|
134
|
+
let index = options?.index;
|
|
135
|
+
if (index === undefined) {
|
|
136
|
+
index = (await (0, feed_1.probeFeed)(requestOptions, owner, topic)).feedIndex;
|
|
137
|
+
}
|
|
138
|
+
const payload = await download({ ...options, index: index });
|
|
139
|
+
return {
|
|
140
|
+
reference: new typed_bytes_1.Reference(payload.payload.toUint8Array()),
|
|
141
|
+
feedIndex: payload.feedIndex,
|
|
142
|
+
};
|
|
143
|
+
};
|
|
87
144
|
return {
|
|
88
|
-
|
|
145
|
+
download,
|
|
146
|
+
downloadPayload,
|
|
147
|
+
downloadReference,
|
|
89
148
|
owner,
|
|
90
149
|
topic,
|
|
91
|
-
async download(options) {
|
|
92
|
-
if (!options?.index && options?.index !== 0) {
|
|
93
|
-
return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
|
|
94
|
-
}
|
|
95
|
-
const update = await downloadFeedUpdate(requestOptions, (0, hex_1.hexToBytes)(owner), topic, options.index);
|
|
96
|
-
return {
|
|
97
|
-
reference: (0, hex_1.bytesToHex)(update.reference),
|
|
98
|
-
feedIndex: options.index,
|
|
99
|
-
feedIndexNext: '',
|
|
100
|
-
};
|
|
101
|
-
},
|
|
102
150
|
};
|
|
103
151
|
}
|
|
104
152
|
exports.makeFeedReader = makeFeedReader;
|
|
105
|
-
function makeFeedWriter(requestOptions,
|
|
153
|
+
function makeFeedWriter(requestOptions, topic, signer) {
|
|
106
154
|
const upload = async (postageBatchId, reference, options) => {
|
|
107
|
-
(
|
|
108
|
-
|
|
109
|
-
|
|
155
|
+
return updateFeedWithReference(requestOptions, signer, topic, reference, postageBatchId, options);
|
|
156
|
+
};
|
|
157
|
+
const uploadPayload = async (postageBatchId, data, options) => {
|
|
158
|
+
return updateFeedWithPayload(requestOptions, signer, topic, data, postageBatchId, options);
|
|
110
159
|
};
|
|
111
160
|
return {
|
|
112
|
-
...makeFeedReader(requestOptions,
|
|
161
|
+
...makeFeedReader(requestOptions, topic, signer.publicKey().address()),
|
|
113
162
|
upload,
|
|
163
|
+
uploadReference: upload,
|
|
164
|
+
uploadPayload,
|
|
114
165
|
};
|
|
115
166
|
}
|
|
116
167
|
exports.makeFeedWriter = makeFeedWriter;
|
|
@@ -2,20 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.areAllSequentialFeedsUpdateRetrievable = void 0;
|
|
4
4
|
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
-
const
|
|
5
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
6
6
|
const index_1 = require("./index");
|
|
7
|
-
function makeNumericIndex(index) {
|
|
8
|
-
if (index instanceof Uint8Array) {
|
|
9
|
-
return cafe_utility_1.Binary.uint64BEToNumber(index);
|
|
10
|
-
}
|
|
11
|
-
if (typeof index === 'string') {
|
|
12
|
-
return parseInt(index);
|
|
13
|
-
}
|
|
14
|
-
if (typeof index === 'number') {
|
|
15
|
-
return index;
|
|
16
|
-
}
|
|
17
|
-
throw new TypeError('Unknown type of index!');
|
|
18
|
-
}
|
|
19
7
|
/**
|
|
20
8
|
* Function that checks if a chunk is retrievable by actually downloading it.
|
|
21
9
|
* The /stewardship/{reference} endpoint does not support verification of chunks, but only manifest's references.
|
|
@@ -24,13 +12,14 @@ function makeNumericIndex(index) {
|
|
|
24
12
|
* @param ref
|
|
25
13
|
* @param options
|
|
26
14
|
*/
|
|
27
|
-
async function isChunkRetrievable(bee,
|
|
15
|
+
async function isChunkRetrievable(bee, reference, options, requestOptions) {
|
|
28
16
|
try {
|
|
29
|
-
await bee.downloadChunk(
|
|
17
|
+
await bee.downloadChunk(reference, options, requestOptions);
|
|
30
18
|
return true;
|
|
31
19
|
}
|
|
32
20
|
catch (e) {
|
|
33
|
-
|
|
21
|
+
const status = cafe_utility_1.Objects.getDeep(e, 'status');
|
|
22
|
+
if (status === 404 || status === 500) {
|
|
34
23
|
return false;
|
|
35
24
|
}
|
|
36
25
|
throw e;
|
|
@@ -44,15 +33,15 @@ async function isChunkRetrievable(bee, ref, requestOptions) {
|
|
|
44
33
|
* @param index
|
|
45
34
|
*/
|
|
46
35
|
function getAllSequenceUpdateReferences(owner, topic, index) {
|
|
47
|
-
const
|
|
48
|
-
const updateReferences =
|
|
49
|
-
for (let i =
|
|
50
|
-
updateReferences
|
|
36
|
+
const count = index.toBigInt();
|
|
37
|
+
const updateReferences = [];
|
|
38
|
+
for (let i = 0n; i <= count; i++) {
|
|
39
|
+
updateReferences.push((0, index_1.getFeedUpdateChunkReference)(owner, topic, typed_bytes_1.FeedIndex.fromBigInt(i)));
|
|
51
40
|
}
|
|
52
41
|
return updateReferences;
|
|
53
42
|
}
|
|
54
|
-
async function areAllSequentialFeedsUpdateRetrievable(bee, owner, topic, index, requestOptions) {
|
|
55
|
-
const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async (
|
|
43
|
+
async function areAllSequentialFeedsUpdateRetrievable(bee, owner, topic, index, options, requestOptions) {
|
|
44
|
+
const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async (reference) => isChunkRetrievable(bee, reference, options, requestOptions));
|
|
56
45
|
return (await Promise.all(chunkRetrievablePromises)).every(result => result);
|
|
57
46
|
}
|
|
58
47
|
exports.areAllSequentialFeedsUpdateRetrievable = areAllSequentialFeedsUpdateRetrievable;
|