@ethersphere/bee-js 3.2.0 → 3.3.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/bee-debug.js +615 -0
- package/dist/cjs/bee.js +922 -0
- package/dist/cjs/chunk/bmt.js +50 -0
- package/dist/cjs/chunk/cac.js +56 -0
- package/dist/cjs/chunk/serialize.js +19 -0
- package/dist/cjs/chunk/signer.js +132 -0
- package/dist/cjs/chunk/soc.js +172 -0
- package/dist/cjs/chunk/span.js +29 -0
- package/dist/cjs/feed/index.js +184 -0
- package/dist/cjs/feed/json.js +41 -0
- package/dist/cjs/feed/topic.js +25 -0
- package/dist/cjs/feed/type.js +15 -0
- package/dist/cjs/index.js +35 -0
- package/dist/cjs/modules/bytes.js +74 -0
- package/dist/cjs/modules/bzz.js +131 -0
- package/dist/cjs/modules/chunk.js +58 -0
- package/dist/cjs/modules/debug/balance.js +77 -0
- package/dist/cjs/modules/debug/chequebook.js +167 -0
- package/dist/cjs/modules/debug/chunk.js +51 -0
- package/dist/cjs/modules/debug/connectivity.js +75 -0
- package/dist/cjs/modules/debug/settlements.js +45 -0
- package/dist/cjs/modules/debug/stamps.js +89 -0
- package/dist/cjs/modules/debug/states.js +47 -0
- package/dist/cjs/modules/debug/status.js +153 -0
- package/dist/cjs/modules/debug/tag.js +30 -0
- package/dist/cjs/modules/debug/transactions.js +81 -0
- package/dist/cjs/modules/feed.js +76 -0
- package/dist/cjs/modules/pinning.js +84 -0
- package/dist/cjs/modules/pss.js +55 -0
- package/dist/cjs/modules/soc.js +40 -0
- package/dist/cjs/modules/status.js +26 -0
- package/dist/cjs/modules/stewardship.js +41 -0
- package/dist/cjs/modules/tag.js +96 -0
- package/dist/cjs/package.json +7 -0
- package/dist/cjs/types/debug.js +9 -0
- package/dist/cjs/types/index.js +46 -0
- package/dist/cjs/utils/bytes.js +107 -0
- package/dist/cjs/utils/collection.browser.js +36 -0
- package/dist/cjs/utils/collection.js +70 -0
- package/dist/cjs/utils/collection.node.js +115 -0
- package/dist/cjs/utils/data.browser.js +78 -0
- package/dist/cjs/utils/data.js +60 -0
- package/dist/cjs/utils/error.js +50 -0
- package/dist/cjs/utils/eth.js +206 -0
- package/dist/cjs/utils/expose.js +44 -0
- package/dist/cjs/utils/file.js +49 -0
- package/dist/cjs/utils/hash.js +16 -0
- package/dist/cjs/utils/headers.js +59 -0
- package/dist/cjs/utils/hex.js +150 -0
- package/dist/cjs/utils/http.js +166 -0
- package/dist/cjs/utils/merge.js +34 -0
- package/dist/cjs/utils/pss.js +18 -0
- package/dist/cjs/utils/stamps.js +17 -0
- package/dist/cjs/utils/stream.js +137 -0
- package/dist/cjs/utils/tar.js +25 -0
- package/dist/cjs/utils/type.js +327 -0
- package/dist/cjs/utils/uint64.js +29 -0
- package/dist/cjs/utils/url.js +56 -0
- package/dist/index.js +28326 -0
- package/dist/index.js.map +1 -0
- package/dist/mjs/bee-debug.js +509 -0
- package/dist/mjs/bee.js +845 -0
- package/dist/mjs/chunk/bmt.js +46 -0
- package/dist/mjs/chunk/cac.js +50 -0
- package/dist/mjs/chunk/serialize.js +15 -0
- package/dist/mjs/chunk/signer.js +112 -0
- package/dist/mjs/chunk/soc.js +127 -0
- package/dist/mjs/chunk/span.js +25 -0
- package/dist/mjs/feed/index.js +136 -0
- package/dist/mjs/feed/json.js +23 -0
- package/dist/mjs/feed/topic.js +20 -0
- package/dist/mjs/feed/type.js +10 -0
- package/dist/mjs/index.js +7 -0
- package/dist/mjs/modules/bytes.js +56 -0
- package/dist/mjs/modules/bzz.js +119 -0
- package/dist/mjs/modules/chunk.js +43 -0
- package/dist/mjs/modules/debug/balance.js +53 -0
- package/dist/mjs/modules/debug/chequebook.js +131 -0
- package/dist/mjs/modules/debug/chunk.js +33 -0
- package/dist/mjs/modules/debug/connectivity.js +45 -0
- package/dist/mjs/modules/debug/settlements.js +27 -0
- package/dist/mjs/modules/debug/stamps.js +59 -0
- package/dist/mjs/modules/debug/states.js +29 -0
- package/dist/mjs/modules/debug/status.js +114 -0
- package/dist/mjs/modules/debug/tag.js +15 -0
- package/dist/mjs/modules/debug/transactions.js +57 -0
- package/dist/mjs/modules/feed.js +61 -0
- package/dist/mjs/modules/pinning.js +60 -0
- package/dist/mjs/modules/pss.js +36 -0
- package/dist/mjs/modules/soc.js +28 -0
- package/dist/mjs/modules/status.js +11 -0
- package/dist/mjs/modules/stewardship.js +23 -0
- package/dist/mjs/modules/tag.js +69 -0
- package/dist/mjs/package.json +7 -0
- package/dist/mjs/types/debug.js +6 -0
- package/dist/mjs/types/index.js +33 -0
- package/dist/mjs/utils/bytes.js +95 -0
- package/dist/mjs/utils/collection.browser.js +18 -0
- package/dist/mjs/utils/collection.js +52 -0
- package/dist/mjs/utils/collection.node.js +63 -0
- package/dist/mjs/utils/data.browser.js +57 -0
- package/dist/mjs/utils/data.js +39 -0
- package/dist/mjs/utils/error.js +48 -0
- package/dist/mjs/utils/eth.js +184 -0
- package/dist/mjs/utils/expose.js +9 -0
- package/dist/mjs/utils/file.js +33 -0
- package/dist/mjs/utils/hash.js +12 -0
- package/dist/mjs/utils/headers.js +54 -0
- package/dist/mjs/utils/hex.js +139 -0
- package/dist/mjs/utils/http.js +136 -0
- package/dist/mjs/utils/merge.js +30 -0
- package/dist/mjs/utils/pss.js +14 -0
- package/dist/mjs/utils/stamps.js +13 -0
- package/dist/mjs/utils/stream.js +131 -0
- package/dist/mjs/utils/tar.js +18 -0
- package/dist/mjs/utils/type.js +296 -0
- package/dist/mjs/utils/uint64.js +23 -0
- package/dist/mjs/utils/url.js +50 -0
- package/dist/{src → types}/bee-debug.d.ts +47 -1
- package/dist/{src → types}/bee.d.ts +0 -0
- package/dist/{src → types}/chunk/bmt.d.ts +0 -0
- package/dist/{src → types}/chunk/cac.d.ts +0 -0
- package/dist/{src → types}/chunk/serialize.d.ts +0 -0
- package/dist/{src → types}/chunk/signer.d.ts +0 -0
- package/dist/{src → types}/chunk/soc.d.ts +0 -0
- package/dist/{src → types}/chunk/span.d.ts +0 -0
- package/dist/{src → types}/feed/index.d.ts +0 -0
- package/dist/{src → types}/feed/json.d.ts +0 -0
- package/dist/{src → types}/feed/topic.d.ts +0 -0
- package/dist/{src → types}/feed/type.d.ts +0 -0
- package/dist/{src → types}/index.d.ts +0 -0
- package/dist/{src → types}/modules/bytes.d.ts +0 -0
- package/dist/{src → types}/modules/bzz.d.ts +0 -0
- package/dist/{src → types}/modules/chunk.d.ts +0 -0
- package/dist/{src → types}/modules/debug/balance.d.ts +0 -0
- package/dist/{src → types}/modules/debug/chequebook.d.ts +0 -0
- package/dist/{src → types}/modules/debug/chunk.d.ts +0 -0
- package/dist/{src → types}/modules/debug/connectivity.d.ts +0 -0
- package/dist/{src → types}/modules/debug/settlements.d.ts +0 -0
- package/dist/{src → types}/modules/debug/stamps.d.ts +0 -0
- package/dist/{src → types}/modules/debug/states.d.ts +0 -0
- package/dist/types/modules/debug/status.d.ts +72 -0
- package/dist/{src → types}/modules/debug/tag.d.ts +0 -0
- package/dist/{src → types}/modules/debug/transactions.d.ts +0 -0
- package/dist/{src → types}/modules/feed.d.ts +0 -0
- package/dist/{src → types}/modules/pinning.d.ts +0 -0
- package/dist/{src → types}/modules/pss.d.ts +0 -0
- package/dist/{src → types}/modules/soc.d.ts +0 -0
- package/dist/{src → types}/modules/status.d.ts +0 -0
- package/dist/{src → types}/modules/stewardship.d.ts +0 -0
- package/dist/{src → types}/modules/tag.d.ts +0 -0
- package/dist/{src → types}/types/debug.d.ts +8 -0
- package/dist/{src → types}/types/index.d.ts +1 -0
- package/dist/{src → types}/utils/bytes.d.ts +0 -0
- package/dist/types/utils/collection.browser.d.ts +15 -0
- package/dist/{src → types}/utils/collection.d.ts +0 -14
- package/dist/types/utils/collection.node.d.ts +15 -0
- package/dist/{src → types}/utils/data.browser.d.ts +0 -0
- package/dist/{src → types}/utils/data.d.ts +0 -0
- package/dist/{src → types}/utils/error.d.ts +0 -0
- package/dist/{src → types}/utils/eth.d.ts +0 -0
- package/dist/{src → types}/utils/expose.d.ts +2 -1
- package/dist/{src → types}/utils/file.d.ts +0 -0
- package/dist/{src → types}/utils/hash.d.ts +0 -0
- package/dist/{src → types}/utils/headers.d.ts +0 -0
- package/dist/{src → types}/utils/hex.d.ts +0 -0
- package/dist/{src → types}/utils/http.d.ts +0 -0
- package/dist/{src → types}/utils/merge.d.ts +0 -0
- package/dist/{src → types}/utils/pss.d.ts +0 -0
- package/dist/{src → types}/utils/stamps.d.ts +0 -0
- package/dist/{src → types}/utils/stream.d.ts +0 -0
- package/dist/{src → types}/utils/tar.d.ts +0 -0
- package/dist/{src → types}/utils/type.d.ts +0 -0
- package/dist/{src → types}/utils/uint64.d.ts +0 -0
- package/dist/{src → types}/utils/url.d.ts +0 -0
- package/package.json +39 -23
- package/dist/index.browser.min.js +0 -3
- package/dist/index.browser.min.js.LICENSE.txt +0 -59
- package/dist/index.browser.min.js.map +0 -1
- package/dist/index.min.js +0 -3
- package/dist/index.min.js.LICENSE.txt +0 -50
- package/dist/index.min.js.map +0 -1
- package/dist/src/modules/debug/status.d.ts +0 -24
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bmtHash = void 0;
|
|
4
|
+
const js_sha3_1 = require("js-sha3");
|
|
5
|
+
const error_1 = require("../utils/error");
|
|
6
|
+
const hash_1 = require("../utils/hash");
|
|
7
|
+
const MAX_CHUNK_PAYLOAD_SIZE = 4096;
|
|
8
|
+
const SEGMENT_SIZE = 32;
|
|
9
|
+
const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE;
|
|
10
|
+
const HASH_SIZE = 32;
|
|
11
|
+
/**
|
|
12
|
+
* Calculate a Binary Merkle Tree hash for a chunk
|
|
13
|
+
*
|
|
14
|
+
* The BMT chunk address is the hash of the 8 byte span and the root
|
|
15
|
+
* hash of a binary Merkle tree (BMT) built on the 32-byte segments
|
|
16
|
+
* of the underlying data.
|
|
17
|
+
*
|
|
18
|
+
* If the chunk content is less than 4k, the hash is calculated as
|
|
19
|
+
* if the chunk was padded with all zeros up to 4096 bytes.
|
|
20
|
+
*
|
|
21
|
+
* @param chunkContent Chunk data including span and payload as well
|
|
22
|
+
*
|
|
23
|
+
* @returns the keccak256 hash in a byte array
|
|
24
|
+
*/
|
|
25
|
+
function bmtHash(chunkContent) {
|
|
26
|
+
const span = chunkContent.slice(0, 8);
|
|
27
|
+
const payload = chunkContent.slice(8);
|
|
28
|
+
const rootHash = bmtRootHash(payload);
|
|
29
|
+
const chunkHashInput = new Uint8Array([...span, ...rootHash]);
|
|
30
|
+
const chunkHash = (0, hash_1.keccak256Hash)(chunkHashInput);
|
|
31
|
+
return chunkHash;
|
|
32
|
+
}
|
|
33
|
+
exports.bmtHash = bmtHash;
|
|
34
|
+
function bmtRootHash(payload) {
|
|
35
|
+
if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
|
|
36
|
+
throw new error_1.BeeArgumentError('invalid data length', payload);
|
|
37
|
+
}
|
|
38
|
+
// create an input buffer padded with zeros
|
|
39
|
+
let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
|
|
40
|
+
while (input.length !== HASH_SIZE) {
|
|
41
|
+
const output = new Uint8Array(input.length / 2);
|
|
42
|
+
// in each round we hash the segment pairs together
|
|
43
|
+
for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
|
|
44
|
+
const hashNumbers = js_sha3_1.keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
|
|
45
|
+
output.set(hashNumbers, offset / 2);
|
|
46
|
+
}
|
|
47
|
+
input = output;
|
|
48
|
+
}
|
|
49
|
+
return input;
|
|
50
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertValidChunkData = exports.isValidChunkData = exports.makeContentAddressedChunk = exports.MAX_PAYLOAD_SIZE = exports.MIN_PAYLOAD_SIZE = void 0;
|
|
4
|
+
const error_1 = require("../utils/error");
|
|
5
|
+
const bmt_1 = require("./bmt");
|
|
6
|
+
const bytes_1 = require("../utils/bytes");
|
|
7
|
+
const serialize_1 = require("./serialize");
|
|
8
|
+
const span_1 = require("./span");
|
|
9
|
+
exports.MIN_PAYLOAD_SIZE = 1;
|
|
10
|
+
exports.MAX_PAYLOAD_SIZE = 4096;
|
|
11
|
+
const CAC_SPAN_OFFSET = 0;
|
|
12
|
+
const CAC_PAYLOAD_OFFSET = CAC_SPAN_OFFSET + span_1.SPAN_SIZE;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a content addressed chunk and verifies the payload size.
|
|
15
|
+
*
|
|
16
|
+
* @param payloadBytes the data to be stored in the chunk
|
|
17
|
+
*/
|
|
18
|
+
function makeContentAddressedChunk(payloadBytes) {
|
|
19
|
+
const span = (0, span_1.makeSpan)(payloadBytes.length);
|
|
20
|
+
(0, bytes_1.assertFlexBytes)(payloadBytes, exports.MIN_PAYLOAD_SIZE, exports.MAX_PAYLOAD_SIZE);
|
|
21
|
+
const data = (0, serialize_1.serializeBytes)(span, payloadBytes);
|
|
22
|
+
return {
|
|
23
|
+
data,
|
|
24
|
+
span: () => span,
|
|
25
|
+
payload: () => (0, bytes_1.flexBytesAtOffset)(data, CAC_PAYLOAD_OFFSET, exports.MIN_PAYLOAD_SIZE, exports.MAX_PAYLOAD_SIZE),
|
|
26
|
+
address: () => (0, bmt_1.bmtHash)(data),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
exports.makeContentAddressedChunk = makeContentAddressedChunk;
|
|
30
|
+
/**
|
|
31
|
+
* Type guard for valid content addressed chunk data
|
|
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!');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.assertValidChunkData = assertValidChunkData;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serializeBytes = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Helper function for serialize byte arrays
|
|
6
|
+
*
|
|
7
|
+
* @param arrays Any number of byte array arguments
|
|
8
|
+
*/
|
|
9
|
+
function serializeBytes(...arrays) {
|
|
10
|
+
const length = arrays.reduce((prev, curr) => prev + curr.length, 0);
|
|
11
|
+
const buffer = new Uint8Array(length);
|
|
12
|
+
let offset = 0;
|
|
13
|
+
arrays.forEach(arr => {
|
|
14
|
+
buffer.set(arr, offset);
|
|
15
|
+
offset += arr.length;
|
|
16
|
+
});
|
|
17
|
+
return buffer;
|
|
18
|
+
}
|
|
19
|
+
exports.serializeBytes = serializeBytes;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.sign = exports.makeSigner = exports.assertSigner = exports.makePrivateKeySigner = exports.recoverAddress = exports.defaultSign = void 0;
|
|
13
|
+
const elliptic_1 = require("elliptic");
|
|
14
|
+
const error_1 = require("../utils/error");
|
|
15
|
+
const bytes_1 = require("../utils/bytes");
|
|
16
|
+
const hash_1 = require("../utils/hash");
|
|
17
|
+
const hex_1 = require("../utils/hex");
|
|
18
|
+
const types_1 = require("../types");
|
|
19
|
+
const type_1 = require("../utils/type");
|
|
20
|
+
const UNCOMPRESSED_RECOVERY_ID = 27;
|
|
21
|
+
function hashWithEthereumPrefix(data) {
|
|
22
|
+
const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
|
|
23
|
+
const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
|
|
24
|
+
return (0, hash_1.keccak256Hash)(prefixBytes, data);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The default signer function that can be used for integrating with
|
|
28
|
+
* other applications (e.g. wallets).
|
|
29
|
+
*
|
|
30
|
+
* @param data The data to be signed
|
|
31
|
+
* @param privateKey The private key used for signing the data
|
|
32
|
+
*/
|
|
33
|
+
function defaultSign(data, privateKey) {
|
|
34
|
+
const curve = new elliptic_1.ec('secp256k1');
|
|
35
|
+
const keyPair = curve.keyFromPrivate(privateKey);
|
|
36
|
+
const hashedDigest = hashWithEthereumPrefix(data);
|
|
37
|
+
const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined });
|
|
38
|
+
if (sigRaw.recoveryParam === null) {
|
|
39
|
+
throw new error_1.BeeError('signDigest recovery param was null');
|
|
40
|
+
}
|
|
41
|
+
const signature = new Uint8Array([
|
|
42
|
+
...sigRaw.r.toArray('be', 32),
|
|
43
|
+
...sigRaw.s.toArray('be', 32),
|
|
44
|
+
sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
|
|
45
|
+
]);
|
|
46
|
+
return signature;
|
|
47
|
+
}
|
|
48
|
+
exports.defaultSign = defaultSign;
|
|
49
|
+
function publicKeyToAddress(pubKey) {
|
|
50
|
+
const pubBytes = pubKey.encode('array', false);
|
|
51
|
+
return (0, hash_1.keccak256Hash)(pubBytes.slice(1)).slice(12);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Recovers the ethereum address from a given signature.
|
|
55
|
+
*
|
|
56
|
+
* Can be used for verifying a piece of data when the public key is
|
|
57
|
+
* known.
|
|
58
|
+
*
|
|
59
|
+
* @param signature The signature
|
|
60
|
+
* @param digest The digest of the data
|
|
61
|
+
*
|
|
62
|
+
* @returns the recovered address
|
|
63
|
+
*/
|
|
64
|
+
function recoverAddress(signature, digest) {
|
|
65
|
+
const curve = new elliptic_1.ec('secp256k1');
|
|
66
|
+
const sig = {
|
|
67
|
+
r: signature.slice(0, 32),
|
|
68
|
+
s: signature.slice(32, 64),
|
|
69
|
+
};
|
|
70
|
+
const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
|
|
71
|
+
const hash = hashWithEthereumPrefix(digest);
|
|
72
|
+
const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
|
|
73
|
+
return publicKeyToAddress(recPubKey);
|
|
74
|
+
}
|
|
75
|
+
exports.recoverAddress = recoverAddress;
|
|
76
|
+
/**
|
|
77
|
+
* Creates a singer object that can be used when the private key is known.
|
|
78
|
+
*
|
|
79
|
+
* @param privateKey The private key
|
|
80
|
+
*/
|
|
81
|
+
function makePrivateKeySigner(privateKey) {
|
|
82
|
+
const curve = new elliptic_1.ec('secp256k1');
|
|
83
|
+
const keyPair = curve.keyFromPrivate(privateKey);
|
|
84
|
+
const address = publicKeyToAddress(keyPair.getPublic());
|
|
85
|
+
return {
|
|
86
|
+
sign: (digest) => defaultSign(digest, privateKey),
|
|
87
|
+
address,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.makePrivateKeySigner = makePrivateKeySigner;
|
|
91
|
+
function assertSigner(signer) {
|
|
92
|
+
if (!(0, type_1.isStrictlyObject)(signer)) {
|
|
93
|
+
throw new TypeError('Signer must be an object!');
|
|
94
|
+
}
|
|
95
|
+
const typedSigner = signer;
|
|
96
|
+
if (!(0, bytes_1.isBytes)(typedSigner.address, 20)) {
|
|
97
|
+
throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
|
|
98
|
+
}
|
|
99
|
+
if (typeof typedSigner.sign !== 'function') {
|
|
100
|
+
throw new TypeError('Signer sign property needs to be function!');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.assertSigner = assertSigner;
|
|
104
|
+
function makeSigner(signer) {
|
|
105
|
+
if (typeof signer === 'string') {
|
|
106
|
+
const hexKey = (0, hex_1.makeHexString)(signer, 64);
|
|
107
|
+
const keyBytes = (0, hex_1.hexToBytes)(hexKey); // HexString is verified for 64 length => 32 is guaranteed
|
|
108
|
+
return makePrivateKeySigner(keyBytes);
|
|
109
|
+
}
|
|
110
|
+
else if (signer instanceof Uint8Array) {
|
|
111
|
+
(0, bytes_1.assertBytes)(signer, 32);
|
|
112
|
+
return makePrivateKeySigner(signer);
|
|
113
|
+
}
|
|
114
|
+
assertSigner(signer);
|
|
115
|
+
return signer;
|
|
116
|
+
}
|
|
117
|
+
exports.makeSigner = makeSigner;
|
|
118
|
+
function sign(signer, data) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const result = yield signer.sign((0, bytes_1.wrapBytesWithHelpers)(data));
|
|
121
|
+
if (typeof result === 'string') {
|
|
122
|
+
const hexString = (0, hex_1.makeHexString)(result, types_1.SIGNATURE_HEX_LENGTH);
|
|
123
|
+
return (0, hex_1.hexToBytes)(hexString);
|
|
124
|
+
}
|
|
125
|
+
if (result instanceof Uint8Array) {
|
|
126
|
+
(0, bytes_1.assertBytes)(result, types_1.SIGNATURE_BYTES_LENGTH);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
throw new TypeError('Invalid output of sign function!');
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
exports.sign = sign;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.downloadSingleOwnerChunk = exports.uploadSingleOwnerChunkData = exports.uploadSingleOwnerChunk = exports.makeSingleOwnerChunk = exports.makeSOCAddress = exports.makeSingleOwnerChunkFromData = void 0;
|
|
32
|
+
const bytes_1 = require("../utils/bytes");
|
|
33
|
+
const bmt_1 = require("./bmt");
|
|
34
|
+
const signer_1 = require("./signer");
|
|
35
|
+
const hash_1 = require("../utils/hash");
|
|
36
|
+
const span_1 = require("./span");
|
|
37
|
+
const serialize_1 = require("./serialize");
|
|
38
|
+
const error_1 = require("../utils/error");
|
|
39
|
+
const cac_1 = require("./cac");
|
|
40
|
+
const hex_1 = require("../utils/hex");
|
|
41
|
+
const socAPI = __importStar(require("../modules/soc"));
|
|
42
|
+
const chunkAPI = __importStar(require("../modules/chunk"));
|
|
43
|
+
const type_1 = require("../utils/type");
|
|
44
|
+
const IDENTIFIER_SIZE = 32;
|
|
45
|
+
const SIGNATURE_SIZE = 65;
|
|
46
|
+
const SOC_IDENTIFIER_OFFSET = 0;
|
|
47
|
+
const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
|
|
48
|
+
const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
|
|
49
|
+
const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + span_1.SPAN_SIZE;
|
|
50
|
+
function recoverChunkOwner(data) {
|
|
51
|
+
const cacData = data.slice(SOC_SPAN_OFFSET);
|
|
52
|
+
const chunkAddress = (0, bmt_1.bmtHash)(cacData);
|
|
53
|
+
const signature = (0, bytes_1.bytesAtOffset)(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
|
|
54
|
+
const identifier = (0, bytes_1.bytesAtOffset)(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
|
|
55
|
+
const digest = (0, hash_1.keccak256Hash)(identifier, chunkAddress);
|
|
56
|
+
const ownerAddress = (0, signer_1.recoverAddress)(signature, digest);
|
|
57
|
+
return ownerAddress;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Verifies if the data is a valid single owner chunk
|
|
61
|
+
*
|
|
62
|
+
* @param data The chunk data
|
|
63
|
+
* @param address The address of the single owner chunk
|
|
64
|
+
*
|
|
65
|
+
* @returns a single owner chunk or throws error
|
|
66
|
+
*/
|
|
67
|
+
function makeSingleOwnerChunkFromData(data, address) {
|
|
68
|
+
const ownerAddress = recoverChunkOwner(data);
|
|
69
|
+
const identifier = (0, bytes_1.bytesAtOffset)(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
|
|
70
|
+
const socAddress = (0, hash_1.keccak256Hash)(identifier, ownerAddress);
|
|
71
|
+
if (!(0, bytes_1.bytesEqual)(address, socAddress)) {
|
|
72
|
+
throw new error_1.BeeError('SOC Data does not match given address!');
|
|
73
|
+
}
|
|
74
|
+
const signature = () => (0, bytes_1.bytesAtOffset)(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
|
|
75
|
+
const span = () => (0, bytes_1.bytesAtOffset)(data, SOC_SPAN_OFFSET, span_1.SPAN_SIZE);
|
|
76
|
+
const payload = () => (0, bytes_1.flexBytesAtOffset)(data, SOC_PAYLOAD_OFFSET, cac_1.MIN_PAYLOAD_SIZE, cac_1.MAX_PAYLOAD_SIZE);
|
|
77
|
+
return {
|
|
78
|
+
data,
|
|
79
|
+
identifier: () => identifier,
|
|
80
|
+
signature,
|
|
81
|
+
span,
|
|
82
|
+
payload,
|
|
83
|
+
address: () => socAddress,
|
|
84
|
+
owner: () => ownerAddress,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
exports.makeSingleOwnerChunkFromData = makeSingleOwnerChunkFromData;
|
|
88
|
+
function makeSOCAddress(identifier, address) {
|
|
89
|
+
return (0, hash_1.keccak256Hash)(identifier, address);
|
|
90
|
+
}
|
|
91
|
+
exports.makeSOCAddress = makeSOCAddress;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a single owner chunk object
|
|
94
|
+
*
|
|
95
|
+
* @param chunk A chunk object used for the span and payload
|
|
96
|
+
* @param identifier The identifier of the chunk
|
|
97
|
+
* @param signer The singer interface for signing the chunk
|
|
98
|
+
*/
|
|
99
|
+
function makeSingleOwnerChunk(chunk, identifier, signer) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const chunkAddress = chunk.address();
|
|
102
|
+
(0, cac_1.assertValidChunkData)(chunk.data, chunkAddress);
|
|
103
|
+
const digest = (0, hash_1.keccak256Hash)(identifier, chunkAddress);
|
|
104
|
+
const signature = yield (0, signer_1.sign)(signer, digest);
|
|
105
|
+
const data = (0, serialize_1.serializeBytes)(identifier, signature, chunk.span(), chunk.payload());
|
|
106
|
+
const address = makeSOCAddress(identifier, signer.address);
|
|
107
|
+
return {
|
|
108
|
+
data,
|
|
109
|
+
identifier: () => identifier,
|
|
110
|
+
signature: () => signature,
|
|
111
|
+
span: () => chunk.span(),
|
|
112
|
+
payload: () => chunk.payload(),
|
|
113
|
+
address: () => address,
|
|
114
|
+
owner: () => signer.address,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
exports.makeSingleOwnerChunk = makeSingleOwnerChunk;
|
|
119
|
+
/**
|
|
120
|
+
* Helper function to upload a chunk.
|
|
121
|
+
*
|
|
122
|
+
* It uses the Chunk API and calculates the address before uploading.
|
|
123
|
+
*
|
|
124
|
+
* @param ky Ky instance
|
|
125
|
+
* @param chunk A chunk object
|
|
126
|
+
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
|
|
127
|
+
* @param options Upload options
|
|
128
|
+
*/
|
|
129
|
+
function uploadSingleOwnerChunk(ky, chunk, postageBatchId, options) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
const owner = (0, hex_1.bytesToHex)(chunk.owner());
|
|
132
|
+
const identifier = (0, hex_1.bytesToHex)(chunk.identifier());
|
|
133
|
+
const signature = (0, hex_1.bytesToHex)(chunk.signature());
|
|
134
|
+
const data = (0, serialize_1.serializeBytes)(chunk.span(), chunk.payload());
|
|
135
|
+
return socAPI.upload(ky, owner, identifier, signature, data, postageBatchId, options);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
exports.uploadSingleOwnerChunk = uploadSingleOwnerChunk;
|
|
139
|
+
/**
|
|
140
|
+
* Helper function to create and upload SOC.
|
|
141
|
+
*
|
|
142
|
+
* @param ky Ky instance
|
|
143
|
+
* @param signer The singer interface for signing the chunk
|
|
144
|
+
* @param postageBatchId
|
|
145
|
+
* @param identifier The identifier of the chunk
|
|
146
|
+
* @param data The chunk data
|
|
147
|
+
* @param options
|
|
148
|
+
*/
|
|
149
|
+
function uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, data, options) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
(0, type_1.assertAddress)(postageBatchId);
|
|
152
|
+
const cac = (0, cac_1.makeContentAddressedChunk)(data);
|
|
153
|
+
const soc = yield makeSingleOwnerChunk(cac, identifier, signer);
|
|
154
|
+
return uploadSingleOwnerChunk(ky, soc, postageBatchId, options);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
exports.uploadSingleOwnerChunkData = uploadSingleOwnerChunkData;
|
|
158
|
+
/**
|
|
159
|
+
* Helper function to download SOC.
|
|
160
|
+
*
|
|
161
|
+
* @param url The url of the Bee service
|
|
162
|
+
* @param ownerAddress The singer interface for signing the chunk
|
|
163
|
+
* @param identifier The identifier of the chunk
|
|
164
|
+
*/
|
|
165
|
+
function downloadSingleOwnerChunk(ky, ownerAddress, identifier) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
const address = makeSOCAddress(identifier, ownerAddress);
|
|
168
|
+
const data = yield chunkAPI.download(ky, (0, hex_1.bytesToHex)(address));
|
|
169
|
+
return makeSingleOwnerChunkFromData(data, address);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
exports.downloadSingleOwnerChunk = downloadSingleOwnerChunk;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeSpan = exports.SPAN_SIZE = void 0;
|
|
4
|
+
const error_1 = require("../utils/error");
|
|
5
|
+
exports.SPAN_SIZE = 8;
|
|
6
|
+
// we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
|
|
7
|
+
const MAX_SPAN_LENGTH = Math.pow(2, 32) - 1;
|
|
8
|
+
/**
|
|
9
|
+
* Create a span for storing the length of the chunk
|
|
10
|
+
*
|
|
11
|
+
* The length is encoded in 64-bit little endian.
|
|
12
|
+
*
|
|
13
|
+
* @param length The length of the span
|
|
14
|
+
*/
|
|
15
|
+
function makeSpan(length) {
|
|
16
|
+
if (length <= 0) {
|
|
17
|
+
throw new error_1.BeeArgumentError('invalid length for span', length);
|
|
18
|
+
}
|
|
19
|
+
if (length > MAX_SPAN_LENGTH) {
|
|
20
|
+
throw new error_1.BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
|
|
21
|
+
}
|
|
22
|
+
const span = new Uint8Array(exports.SPAN_SIZE);
|
|
23
|
+
const dataView = new DataView(span.buffer);
|
|
24
|
+
const littleEndian = true;
|
|
25
|
+
const lengthLower32 = length & 0xffffffff;
|
|
26
|
+
dataView.setUint32(0, lengthLower32, littleEndian);
|
|
27
|
+
return span;
|
|
28
|
+
}
|
|
29
|
+
exports.makeSpan = makeSpan;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdate = exports.verifyChunkReference = exports.updateFeed = exports.findNextIndex = exports.uploadFeedUpdate = exports.makeFeedIdentifier = exports.makeFeedIndexBytes = exports.makeSequentialFeedIdentifier = exports.isEpoch = void 0;
|
|
32
|
+
const hash_1 = require("../utils/hash");
|
|
33
|
+
const serialize_1 = require("../chunk/serialize");
|
|
34
|
+
const soc_1 = require("../chunk/soc");
|
|
35
|
+
const feed_1 = require("../modules/feed");
|
|
36
|
+
const types_1 = require("../types");
|
|
37
|
+
const bytes_1 = require("../utils/bytes");
|
|
38
|
+
const error_1 = require("../utils/error");
|
|
39
|
+
const hex_1 = require("../utils/hex");
|
|
40
|
+
const uint64_1 = require("../utils/uint64");
|
|
41
|
+
const chunkAPI = __importStar(require("../modules/chunk"));
|
|
42
|
+
const eth_1 = require("../utils/eth");
|
|
43
|
+
const type_1 = require("../utils/type");
|
|
44
|
+
const TIMESTAMP_PAYLOAD_OFFSET = 0;
|
|
45
|
+
const TIMESTAMP_PAYLOAD_SIZE = 8;
|
|
46
|
+
const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE;
|
|
47
|
+
const REFERENCE_PAYLOAD_MIN_SIZE = 32;
|
|
48
|
+
const REFERENCE_PAYLOAD_MAX_SIZE = 64;
|
|
49
|
+
const INDEX_HEX_LENGTH = 16;
|
|
50
|
+
function isEpoch(epoch) {
|
|
51
|
+
return typeof epoch === 'object' && epoch !== null && 'time' in epoch && 'level' in epoch;
|
|
52
|
+
}
|
|
53
|
+
exports.isEpoch = isEpoch;
|
|
54
|
+
function hashFeedIdentifier(topic, index) {
|
|
55
|
+
return (0, hash_1.keccak256Hash)((0, hex_1.hexToBytes)(topic), index);
|
|
56
|
+
}
|
|
57
|
+
function makeSequentialFeedIdentifier(topic, index) {
|
|
58
|
+
const indexBytes = (0, uint64_1.writeUint64BigEndian)(index);
|
|
59
|
+
return hashFeedIdentifier(topic, indexBytes);
|
|
60
|
+
}
|
|
61
|
+
exports.makeSequentialFeedIdentifier = makeSequentialFeedIdentifier;
|
|
62
|
+
function makeFeedIndexBytes(s) {
|
|
63
|
+
const hex = (0, hex_1.makeHexString)(s, INDEX_HEX_LENGTH);
|
|
64
|
+
return (0, hex_1.hexToBytes)(hex);
|
|
65
|
+
}
|
|
66
|
+
exports.makeFeedIndexBytes = makeFeedIndexBytes;
|
|
67
|
+
function makeFeedIdentifier(topic, index) {
|
|
68
|
+
if (typeof index === 'number') {
|
|
69
|
+
return makeSequentialFeedIdentifier(topic, index);
|
|
70
|
+
}
|
|
71
|
+
else if (typeof index === 'string') {
|
|
72
|
+
const indexBytes = makeFeedIndexBytes(index);
|
|
73
|
+
return hashFeedIdentifier(topic, indexBytes);
|
|
74
|
+
}
|
|
75
|
+
else if (isEpoch(index)) {
|
|
76
|
+
throw new TypeError('epoch is not yet implemented');
|
|
77
|
+
}
|
|
78
|
+
return hashFeedIdentifier(topic, index);
|
|
79
|
+
}
|
|
80
|
+
exports.makeFeedIdentifier = makeFeedIdentifier;
|
|
81
|
+
function uploadFeedUpdate(ky, signer, topic, index, reference, postageBatchId, options) {
|
|
82
|
+
var _a;
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const identifier = makeFeedIdentifier(topic, index);
|
|
85
|
+
const at = (_a = options === null || options === void 0 ? void 0 : options.at) !== null && _a !== void 0 ? _a : Date.now() / 1000.0;
|
|
86
|
+
const timestamp = (0, uint64_1.writeUint64BigEndian)(at);
|
|
87
|
+
const payloadBytes = (0, serialize_1.serializeBytes)(timestamp, reference);
|
|
88
|
+
return (0, soc_1.uploadSingleOwnerChunkData)(ky, signer, postageBatchId, identifier, payloadBytes, options);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
exports.uploadFeedUpdate = uploadFeedUpdate;
|
|
92
|
+
function findNextIndex(ky, owner, topic, options) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
try {
|
|
95
|
+
const feedUpdate = yield (0, feed_1.fetchFeedUpdate)(ky, owner, topic, options);
|
|
96
|
+
return (0, hex_1.makeHexString)(feedUpdate.feedIndexNext, INDEX_HEX_LENGTH);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
if (e instanceof error_1.BeeResponseError && e.status === 404) {
|
|
100
|
+
return (0, hex_1.bytesToHex)((0, bytes_1.makeBytes)(8));
|
|
101
|
+
}
|
|
102
|
+
throw e;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
exports.findNextIndex = findNextIndex;
|
|
107
|
+
function updateFeed(ky, signer, topic, reference, postageBatchId, options) {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
const ownerHex = (0, eth_1.makeHexEthAddress)(signer.address);
|
|
110
|
+
const nextIndex = yield findNextIndex(ky, ownerHex, topic, options);
|
|
111
|
+
return uploadFeedUpdate(ky, signer, topic, nextIndex, reference, postageBatchId, options);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
exports.updateFeed = updateFeed;
|
|
115
|
+
function verifyChunkReferenceAtOffset(offset, data) {
|
|
116
|
+
try {
|
|
117
|
+
return (0, bytes_1.bytesAtOffset)(data, offset, REFERENCE_PAYLOAD_MAX_SIZE);
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
return (0, bytes_1.bytesAtOffset)(data, offset, REFERENCE_PAYLOAD_MIN_SIZE);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function verifyChunkReference(data) {
|
|
124
|
+
return verifyChunkReferenceAtOffset(0, data);
|
|
125
|
+
}
|
|
126
|
+
exports.verifyChunkReference = verifyChunkReference;
|
|
127
|
+
function downloadFeedUpdate(ky, owner, topic, index) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
const identifier = makeFeedIdentifier(topic, index);
|
|
130
|
+
const address = (0, hash_1.keccak256Hash)(identifier, owner);
|
|
131
|
+
const addressHex = (0, hex_1.bytesToHex)(address);
|
|
132
|
+
const data = yield chunkAPI.download(ky, addressHex);
|
|
133
|
+
const soc = (0, soc_1.makeSingleOwnerChunkFromData)(data, address);
|
|
134
|
+
const payload = soc.payload();
|
|
135
|
+
const timestampBytes = (0, bytes_1.bytesAtOffset)(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
|
|
136
|
+
const timestamp = (0, uint64_1.readUint64BigEndian)(timestampBytes);
|
|
137
|
+
const reference = verifyChunkReferenceAtOffset(REFERENCE_PAYLOAD_OFFSET, payload);
|
|
138
|
+
return {
|
|
139
|
+
timestamp,
|
|
140
|
+
reference,
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
exports.downloadFeedUpdate = downloadFeedUpdate;
|
|
145
|
+
function makeFeedReader(ky, type, topic, owner) {
|
|
146
|
+
const download = (options) => __awaiter(this, void 0, void 0, function* () { return (0, feed_1.fetchFeedUpdate)(ky, owner, topic, Object.assign(Object.assign({}, options), { type })); });
|
|
147
|
+
return {
|
|
148
|
+
type,
|
|
149
|
+
owner,
|
|
150
|
+
topic,
|
|
151
|
+
download,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
exports.makeFeedReader = makeFeedReader;
|
|
155
|
+
function makeChunkReference(reference) {
|
|
156
|
+
if (typeof reference === 'string') {
|
|
157
|
+
try {
|
|
158
|
+
// Non-encrypted chunk hex string reference
|
|
159
|
+
const hexReference = (0, hex_1.makeHexString)(reference, types_1.REFERENCE_HEX_LENGTH);
|
|
160
|
+
return (0, hex_1.hexToBytes)(hexReference);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
if (!(e instanceof TypeError)) {
|
|
164
|
+
throw e;
|
|
165
|
+
}
|
|
166
|
+
// Encrypted chunk hex string reference
|
|
167
|
+
const hexReference = (0, hex_1.makeHexString)(reference, types_1.ENCRYPTED_REFERENCE_HEX_LENGTH);
|
|
168
|
+
return (0, hex_1.hexToBytes)(hexReference);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (reference instanceof Uint8Array) {
|
|
172
|
+
return verifyChunkReference(reference);
|
|
173
|
+
}
|
|
174
|
+
throw new TypeError('invalid chunk reference');
|
|
175
|
+
}
|
|
176
|
+
function makeFeedWriter(ky, type, topic, signer) {
|
|
177
|
+
const upload = (postageBatchId, reference, options) => __awaiter(this, void 0, void 0, function* () {
|
|
178
|
+
(0, type_1.assertAddress)(postageBatchId);
|
|
179
|
+
const canonicalReference = makeChunkReference(reference);
|
|
180
|
+
return updateFeed(ky, signer, topic, canonicalReference, postageBatchId, Object.assign(Object.assign({}, options), { type }));
|
|
181
|
+
});
|
|
182
|
+
return Object.assign(Object.assign({}, makeFeedReader(ky, type, topic, (0, eth_1.makeHexEthAddress)(signer.address))), { upload });
|
|
183
|
+
}
|
|
184
|
+
exports.makeFeedWriter = makeFeedWriter;
|