@ethersphere/bee-js 6.0.0-pre.0 → 6.0.0-pre.10

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.
Files changed (193) hide show
  1. package/README.md +18 -17
  2. package/dist/cjs/bee-debug.js +572 -0
  3. package/dist/cjs/bee.js +876 -0
  4. package/dist/cjs/chunk/bmt.js +55 -0
  5. package/dist/cjs/chunk/cac.js +56 -0
  6. package/dist/cjs/chunk/serialize.js +19 -0
  7. package/dist/cjs/chunk/signer.js +126 -0
  8. package/dist/cjs/chunk/soc.js +159 -0
  9. package/dist/cjs/chunk/span.js +29 -0
  10. package/dist/cjs/feed/identifier.js +35 -0
  11. package/dist/cjs/feed/index.js +116 -0
  12. package/dist/cjs/feed/json.js +28 -0
  13. package/dist/cjs/feed/retrievable.js +58 -0
  14. package/dist/cjs/feed/topic.js +25 -0
  15. package/dist/cjs/feed/type.js +15 -0
  16. package/dist/cjs/index.js +39 -0
  17. package/dist/cjs/modules/bytes.js +61 -0
  18. package/dist/cjs/modules/bzz.js +125 -0
  19. package/dist/cjs/modules/chunk.js +48 -0
  20. package/dist/cjs/modules/debug/balance.js +60 -0
  21. package/dist/cjs/modules/debug/chequebook.js +142 -0
  22. package/dist/cjs/modules/debug/chunk.js +38 -0
  23. package/dist/cjs/modules/debug/connectivity.js +54 -0
  24. package/dist/cjs/modules/debug/settlements.js +32 -0
  25. package/dist/cjs/modules/debug/stake.js +57 -0
  26. package/dist/cjs/modules/debug/stamps.js +68 -0
  27. package/dist/cjs/modules/debug/states.js +64 -0
  28. package/dist/cjs/modules/debug/status.js +148 -0
  29. package/dist/cjs/modules/debug/tag.js +19 -0
  30. package/dist/cjs/modules/debug/transactions.js +64 -0
  31. package/dist/cjs/modules/feed.js +66 -0
  32. package/dist/cjs/modules/pinning.js +63 -0
  33. package/dist/cjs/modules/pss.js +43 -0
  34. package/dist/cjs/modules/soc.js +32 -0
  35. package/dist/cjs/modules/status.js +15 -0
  36. package/dist/cjs/modules/stewardship.js +28 -0
  37. package/dist/cjs/modules/tag.js +77 -0
  38. package/dist/cjs/package.json +8 -0
  39. package/dist/cjs/types/debug.js +10 -0
  40. package/dist/cjs/types/index.js +51 -0
  41. package/dist/cjs/utils/bytes.js +121 -0
  42. package/dist/cjs/utils/collection.browser.js +23 -0
  43. package/dist/cjs/utils/collection.js +59 -0
  44. package/dist/cjs/utils/collection.node.js +71 -0
  45. package/dist/cjs/utils/data.browser.js +61 -0
  46. package/dist/cjs/utils/data.js +45 -0
  47. package/dist/cjs/utils/error.js +16 -0
  48. package/dist/cjs/utils/eth.js +199 -0
  49. package/dist/cjs/utils/expose.js +44 -0
  50. package/dist/cjs/utils/file.js +38 -0
  51. package/dist/cjs/utils/hash.js +21 -0
  52. package/dist/cjs/utils/headers.js +59 -0
  53. package/dist/cjs/utils/hex.js +150 -0
  54. package/dist/cjs/utils/http.js +40 -0
  55. package/dist/cjs/utils/pss.js +18 -0
  56. package/dist/cjs/utils/reference.js +36 -0
  57. package/dist/cjs/utils/stamps.js +17 -0
  58. package/dist/cjs/utils/stream.js +146 -0
  59. package/dist/cjs/utils/tar.js +25 -0
  60. package/dist/cjs/utils/type.js +434 -0
  61. package/dist/cjs/utils/uint64.js +29 -0
  62. package/dist/cjs/utils/url.js +56 -0
  63. package/dist/index.browser.min.js +3 -0
  64. package/dist/index.browser.min.js.LICENSE.txt +50 -0
  65. package/dist/index.browser.min.js.map +1 -0
  66. package/dist/{src → mjs}/bee-debug.js +102 -183
  67. package/dist/{src → mjs}/bee.js +82 -232
  68. package/dist/{src → mjs}/chunk/bmt.js +4 -10
  69. package/dist/{src → mjs}/chunk/cac.js +0 -3
  70. package/dist/{src → mjs}/chunk/signer.js +1 -18
  71. package/dist/{src → mjs}/chunk/soc.js +17 -29
  72. package/dist/{src → mjs}/chunk/span.js +2 -5
  73. package/dist/{src → mjs}/feed/identifier.js +0 -6
  74. package/dist/{src → mjs}/feed/index.js +24 -27
  75. package/dist/{src → mjs}/feed/json.js +2 -5
  76. package/dist/{src → mjs}/feed/retrievable.js +7 -21
  77. package/dist/{src → mjs}/feed/topic.js +0 -2
  78. package/dist/{src → mjs}/index.js +2 -2
  79. package/dist/{src → mjs}/modules/bytes.js +16 -20
  80. package/dist/{src → mjs}/modules/bzz.js +33 -40
  81. package/dist/{src → mjs}/modules/chunk.js +11 -13
  82. package/dist/mjs/modules/debug/balance.js +53 -0
  83. package/dist/mjs/modules/debug/chequebook.js +135 -0
  84. package/dist/{src → mjs}/modules/debug/chunk.js +10 -12
  85. package/dist/mjs/modules/debug/connectivity.js +45 -0
  86. package/dist/mjs/modules/debug/settlements.js +27 -0
  87. package/dist/mjs/modules/debug/stake.js +51 -0
  88. package/dist/mjs/modules/debug/stamps.js +61 -0
  89. package/dist/mjs/modules/debug/states.js +58 -0
  90. package/dist/{src → mjs}/modules/debug/status.js +46 -37
  91. package/dist/mjs/modules/debug/tag.js +15 -0
  92. package/dist/mjs/modules/debug/transactions.js +59 -0
  93. package/dist/{src → mjs}/modules/feed.js +15 -21
  94. package/dist/mjs/modules/pinning.js +56 -0
  95. package/dist/{src → mjs}/modules/pss.js +7 -10
  96. package/dist/{src → mjs}/modules/soc.js +6 -7
  97. package/dist/mjs/modules/status.js +11 -0
  98. package/dist/mjs/modules/stewardship.js +23 -0
  99. package/dist/mjs/modules/tag.js +72 -0
  100. package/dist/mjs/package.json +8 -0
  101. package/dist/{src → mjs}/types/debug.js +0 -1
  102. package/dist/{src → mjs}/types/index.js +0 -4
  103. package/dist/{src → mjs}/utils/bytes.js +8 -17
  104. package/dist/{src/utils/collection-browser.js → mjs/utils/collection.browser.js} +0 -1
  105. package/dist/{src → mjs}/utils/collection.js +0 -12
  106. package/dist/{src/utils/collection-node.js → mjs/utils/collection.node.js} +0 -13
  107. package/dist/{src → mjs}/utils/data.browser.js +2 -11
  108. package/dist/{src → mjs}/utils/data.js +0 -7
  109. package/dist/mjs/utils/error.js +11 -0
  110. package/dist/{src → mjs}/utils/eth.js +6 -28
  111. package/dist/{src → mjs}/utils/expose.js +1 -1
  112. package/dist/{src → mjs}/utils/file.js +4 -9
  113. package/dist/{src → mjs}/utils/hash.js +0 -1
  114. package/dist/{src → mjs}/utils/headers.js +5 -13
  115. package/dist/{src → mjs}/utils/hex.js +6 -25
  116. package/dist/mjs/utils/http.js +34 -0
  117. package/dist/{src → mjs}/utils/pss.js +0 -2
  118. package/dist/{src → mjs}/utils/reference.js +2 -5
  119. package/dist/{src → mjs}/utils/stream.js +3 -26
  120. package/dist/{src → mjs}/utils/tar.js +3 -6
  121. package/dist/{src → mjs}/utils/type.js +23 -80
  122. package/dist/{src → mjs}/utils/uint64.js +0 -1
  123. package/dist/{src → mjs}/utils/url.js +2 -10
  124. package/dist/types/bee-debug.d.ts +71 -46
  125. package/dist/types/bee.d.ts +34 -58
  126. package/dist/types/chunk/bmt.d.ts +1 -1
  127. package/dist/types/chunk/cac.d.ts +1 -1
  128. package/dist/types/chunk/soc.d.ts +9 -9
  129. package/dist/types/chunk/span.d.ts +1 -1
  130. package/dist/types/feed/index.d.ts +10 -10
  131. package/dist/types/feed/json.d.ts +2 -2
  132. package/dist/types/feed/retrievable.d.ts +2 -2
  133. package/dist/types/feed/type.d.ts +1 -1
  134. package/dist/types/index.d.ts +13 -2
  135. package/dist/types/modules/bytes.d.ts +5 -5
  136. package/dist/types/modules/bzz.d.ts +8 -8
  137. package/dist/types/modules/chunk.d.ts +4 -4
  138. package/dist/types/modules/debug/balance.d.ts +9 -9
  139. package/dist/types/modules/debug/chequebook.d.ts +17 -17
  140. package/dist/types/modules/debug/chunk.d.ts +5 -5
  141. package/dist/types/modules/debug/connectivity.d.ts +7 -7
  142. package/dist/types/modules/debug/settlements.d.ts +5 -5
  143. package/dist/types/modules/debug/stake.d.ts +21 -0
  144. package/dist/types/modules/debug/stamps.d.ts +7 -7
  145. package/dist/types/modules/debug/states.d.ts +7 -7
  146. package/dist/types/modules/debug/status.d.ts +21 -15
  147. package/dist/types/modules/debug/tag.d.ts +3 -3
  148. package/dist/types/modules/debug/transactions.d.ts +5 -5
  149. package/dist/types/modules/feed.d.ts +3 -3
  150. package/dist/types/modules/pinning.d.ts +7 -7
  151. package/dist/types/modules/pss.d.ts +3 -3
  152. package/dist/types/modules/soc.d.ts +2 -2
  153. package/dist/types/modules/status.d.ts +3 -3
  154. package/dist/types/modules/stewardship.d.ts +3 -3
  155. package/dist/types/modules/tag.d.ts +6 -6
  156. package/dist/types/types/debug.d.ts +35 -2
  157. package/dist/types/types/index.d.ts +48 -89
  158. package/dist/types/utils/error.d.ts +0 -26
  159. package/dist/types/utils/eth.d.ts +6 -6
  160. package/dist/types/utils/expose.d.ts +4 -7
  161. package/dist/types/utils/hash.d.ts +1 -1
  162. package/dist/types/utils/headers.d.ts +1 -1
  163. package/dist/types/utils/hex.d.ts +4 -4
  164. package/dist/types/utils/http.d.ts +11 -21
  165. package/dist/types/utils/stream.d.ts +1 -1
  166. package/dist/types/utils/type.d.ts +3 -2
  167. package/dist/types/utils/uint64.d.ts +1 -1
  168. package/package.json +59 -52
  169. package/dist/index.browser.js +0 -33
  170. package/dist/index.browser.js.map +0 -7
  171. package/dist/src/modules/debug/balance.js +0 -57
  172. package/dist/src/modules/debug/chequebook.js +0 -150
  173. package/dist/src/modules/debug/connectivity.js +0 -45
  174. package/dist/src/modules/debug/settlements.js +0 -29
  175. package/dist/src/modules/debug/stamps.js +0 -64
  176. package/dist/src/modules/debug/states.js +0 -46
  177. package/dist/src/modules/debug/tag.js +0 -16
  178. package/dist/src/modules/debug/transactions.js +0 -69
  179. package/dist/src/modules/pinning.js +0 -60
  180. package/dist/src/modules/status.js +0 -12
  181. package/dist/src/modules/stewardship.js +0 -24
  182. package/dist/src/modules/tag.js +0 -77
  183. package/dist/src/utils/error.js +0 -47
  184. package/dist/src/utils/http.js +0 -162
  185. package/dist/src/utils/merge.js +0 -36
  186. package/dist/src/utils/sleep.js +0 -8
  187. package/dist/types/utils/merge.d.ts +0 -8
  188. package/dist/types/utils/sleep.d.ts +0 -6
  189. /package/dist/{src → mjs}/chunk/serialize.js +0 -0
  190. /package/dist/{src → mjs}/feed/type.js +0 -0
  191. /package/dist/{src → mjs}/utils/stamps.js +0 -0
  192. /package/dist/types/utils/{collection-browser.d.ts → collection.browser.d.ts} +0 -0
  193. /package/dist/types/utils/{collection-node.d.ts → collection.node.d.ts} +0 -0
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.bmtHash = void 0;
7
+ // For ESM compatibility
8
+ const js_sha3_1 = __importDefault(require("js-sha3"));
9
+ const { keccak256 } = js_sha3_1.default;
10
+ const error_1 = require("../utils/error");
11
+ const hash_1 = require("../utils/hash");
12
+ const MAX_CHUNK_PAYLOAD_SIZE = 4096;
13
+ const SEGMENT_SIZE = 32;
14
+ const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE;
15
+ const HASH_SIZE = 32;
16
+ /**
17
+ * Calculate a Binary Merkle Tree hash for a chunk
18
+ *
19
+ * The BMT chunk address is the hash of the 8 byte span and the root
20
+ * hash of a binary Merkle tree (BMT) built on the 32-byte segments
21
+ * of the underlying data.
22
+ *
23
+ * If the chunk content is less than 4k, the hash is calculated as
24
+ * if the chunk was padded with all zeros up to 4096 bytes.
25
+ *
26
+ * @param chunkContent Chunk data including span and payload as well
27
+ *
28
+ * @returns the keccak256 hash in a byte array
29
+ */
30
+ function bmtHash(chunkContent) {
31
+ const span = chunkContent.slice(0, 8);
32
+ const payload = chunkContent.slice(8);
33
+ const rootHash = bmtRootHash(payload);
34
+ const chunkHashInput = new Uint8Array([...span, ...rootHash]);
35
+ const chunkHash = (0, hash_1.keccak256Hash)(chunkHashInput);
36
+ return chunkHash;
37
+ }
38
+ exports.bmtHash = bmtHash;
39
+ function bmtRootHash(payload) {
40
+ if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
41
+ throw new error_1.BeeArgumentError('invalid data length', payload);
42
+ }
43
+ // create an input buffer padded with zeros
44
+ let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
45
+ while (input.length !== HASH_SIZE) {
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;
55
+ }
@@ -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,126 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.sign = exports.makeSigner = exports.assertSigner = exports.makePrivateKeySigner = exports.recoverAddress = exports.defaultSign = void 0;
7
+ // For ESM compatibility
8
+ const elliptic_1 = __importDefault(require("elliptic"));
9
+ const { ec } = elliptic_1.default;
10
+ const error_1 = require("../utils/error");
11
+ const bytes_1 = require("../utils/bytes");
12
+ const hash_1 = require("../utils/hash");
13
+ const hex_1 = require("../utils/hex");
14
+ const types_1 = require("../types");
15
+ const type_1 = require("../utils/type");
16
+ const UNCOMPRESSED_RECOVERY_ID = 27;
17
+ function hashWithEthereumPrefix(data) {
18
+ const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
19
+ const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
20
+ return (0, hash_1.keccak256Hash)(prefixBytes, data);
21
+ }
22
+ /**
23
+ * The default signer function that can be used for integrating with
24
+ * other applications (e.g. wallets).
25
+ *
26
+ * @param data The data to be signed
27
+ * @param privateKey The private key used for signing the data
28
+ */
29
+ function defaultSign(data, privateKey) {
30
+ const curve = new ec('secp256k1');
31
+ const keyPair = curve.keyFromPrivate(privateKey);
32
+ const hashedDigest = hashWithEthereumPrefix(data);
33
+ const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined });
34
+ if (sigRaw.recoveryParam === null) {
35
+ throw new error_1.BeeError('signDigest recovery param was null');
36
+ }
37
+ const signature = new Uint8Array([
38
+ ...sigRaw.r.toArray('be', 32),
39
+ ...sigRaw.s.toArray('be', 32),
40
+ sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
41
+ ]);
42
+ return signature;
43
+ }
44
+ exports.defaultSign = defaultSign;
45
+ function publicKeyToAddress(pubKey) {
46
+ const pubBytes = pubKey.encode('array', false);
47
+ return (0, hash_1.keccak256Hash)(pubBytes.slice(1)).slice(12);
48
+ }
49
+ /**
50
+ * Recovers the ethereum address from a given signature.
51
+ *
52
+ * Can be used for verifying a piece of data when the public key is
53
+ * known.
54
+ *
55
+ * @param signature The signature
56
+ * @param digest The digest of the data
57
+ *
58
+ * @returns the recovered address
59
+ */
60
+ function recoverAddress(signature, digest) {
61
+ const curve = new ec('secp256k1');
62
+ const sig = {
63
+ r: signature.slice(0, 32),
64
+ s: signature.slice(32, 64),
65
+ };
66
+ const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
67
+ const hash = hashWithEthereumPrefix(digest);
68
+ const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
69
+ return publicKeyToAddress(recPubKey);
70
+ }
71
+ exports.recoverAddress = recoverAddress;
72
+ /**
73
+ * Creates a singer object that can be used when the private key is known.
74
+ *
75
+ * @param privateKey The private key
76
+ */
77
+ function makePrivateKeySigner(privateKey) {
78
+ const curve = new ec('secp256k1');
79
+ const keyPair = curve.keyFromPrivate(privateKey);
80
+ const address = publicKeyToAddress(keyPair.getPublic());
81
+ return {
82
+ sign: (digest) => defaultSign(digest, privateKey),
83
+ address,
84
+ };
85
+ }
86
+ exports.makePrivateKeySigner = makePrivateKeySigner;
87
+ function assertSigner(signer) {
88
+ if (!(0, type_1.isStrictlyObject)(signer)) {
89
+ throw new TypeError('Signer must be an object!');
90
+ }
91
+ const typedSigner = signer;
92
+ if (!(0, bytes_1.isBytes)(typedSigner.address, 20)) {
93
+ throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
94
+ }
95
+ if (typeof typedSigner.sign !== 'function') {
96
+ throw new TypeError('Signer sign property needs to be function!');
97
+ }
98
+ }
99
+ exports.assertSigner = assertSigner;
100
+ function makeSigner(signer) {
101
+ if (typeof signer === 'string') {
102
+ const hexKey = (0, hex_1.makeHexString)(signer, 64);
103
+ const keyBytes = (0, hex_1.hexToBytes)(hexKey); // HexString is verified for 64 length => 32 is guaranteed
104
+ return makePrivateKeySigner(keyBytes);
105
+ }
106
+ else if (signer instanceof Uint8Array) {
107
+ (0, bytes_1.assertBytes)(signer, 32);
108
+ return makePrivateKeySigner(signer);
109
+ }
110
+ assertSigner(signer);
111
+ return signer;
112
+ }
113
+ exports.makeSigner = makeSigner;
114
+ async function sign(signer, data) {
115
+ const result = await signer.sign((0, bytes_1.wrapBytesWithHelpers)(data));
116
+ if (typeof result === 'string') {
117
+ const hexString = (0, hex_1.makeHexString)(result, types_1.SIGNATURE_HEX_LENGTH);
118
+ return (0, hex_1.hexToBytes)(hexString);
119
+ }
120
+ if (result instanceof Uint8Array) {
121
+ (0, bytes_1.assertBytes)(result, types_1.SIGNATURE_BYTES_LENGTH);
122
+ return result;
123
+ }
124
+ throw new TypeError('Invalid output of sign function!');
125
+ }
126
+ exports.sign = sign;
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.downloadSingleOwnerChunk = exports.uploadSingleOwnerChunkData = exports.uploadSingleOwnerChunk = exports.makeSingleOwnerChunk = exports.makeSOCAddress = exports.makeSingleOwnerChunkFromData = void 0;
27
+ const chunkAPI = __importStar(require("../modules/chunk"));
28
+ const socAPI = __importStar(require("../modules/soc"));
29
+ const bytes_1 = require("../utils/bytes");
30
+ const error_1 = require("../utils/error");
31
+ const hash_1 = require("../utils/hash");
32
+ const hex_1 = require("../utils/hex");
33
+ const type_1 = require("../utils/type");
34
+ const bmt_1 = require("./bmt");
35
+ const cac_1 = require("./cac");
36
+ const serialize_1 = require("./serialize");
37
+ const signer_1 = require("./signer");
38
+ const span_1 = require("./span");
39
+ const IDENTIFIER_SIZE = 32;
40
+ const SIGNATURE_SIZE = 65;
41
+ const SOC_IDENTIFIER_OFFSET = 0;
42
+ const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
43
+ const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
44
+ const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + span_1.SPAN_SIZE;
45
+ function recoverChunkOwner(data) {
46
+ const cacData = data.slice(SOC_SPAN_OFFSET);
47
+ const chunkAddress = (0, bmt_1.bmtHash)(cacData);
48
+ const signature = (0, bytes_1.bytesAtOffset)(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
49
+ const identifier = (0, bytes_1.bytesAtOffset)(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
50
+ const digest = (0, hash_1.keccak256Hash)(identifier, chunkAddress);
51
+ const ownerAddress = (0, signer_1.recoverAddress)(signature, digest);
52
+ return ownerAddress;
53
+ }
54
+ /**
55
+ * Verifies if the data is a valid single owner chunk
56
+ *
57
+ * @param data The chunk data
58
+ * @param address The address of the single owner chunk
59
+ *
60
+ * @returns a single owner chunk or throws error
61
+ */
62
+ function makeSingleOwnerChunkFromData(data, address) {
63
+ const ownerAddress = recoverChunkOwner(data);
64
+ const identifier = (0, bytes_1.bytesAtOffset)(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
65
+ const socAddress = (0, hash_1.keccak256Hash)(identifier, ownerAddress);
66
+ if (!(0, bytes_1.bytesEqual)(address, socAddress)) {
67
+ throw new error_1.BeeError('SOC Data does not match given address!');
68
+ }
69
+ const signature = () => (0, bytes_1.bytesAtOffset)(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
70
+ const span = () => (0, bytes_1.bytesAtOffset)(data, SOC_SPAN_OFFSET, span_1.SPAN_SIZE);
71
+ const payload = () => (0, bytes_1.flexBytesAtOffset)(data, SOC_PAYLOAD_OFFSET, cac_1.MIN_PAYLOAD_SIZE, cac_1.MAX_PAYLOAD_SIZE);
72
+ return {
73
+ data,
74
+ identifier: () => identifier,
75
+ signature,
76
+ span,
77
+ payload,
78
+ address: () => socAddress,
79
+ owner: () => ownerAddress,
80
+ };
81
+ }
82
+ exports.makeSingleOwnerChunkFromData = makeSingleOwnerChunkFromData;
83
+ function makeSOCAddress(identifier, address) {
84
+ return (0, hash_1.keccak256Hash)(identifier, address);
85
+ }
86
+ exports.makeSOCAddress = makeSOCAddress;
87
+ /**
88
+ * Creates a single owner chunk object
89
+ *
90
+ * @param chunk A chunk object used for the span and payload
91
+ * @param identifier The identifier of the chunk
92
+ * @param signer The singer interface for signing the chunk
93
+ */
94
+ async function makeSingleOwnerChunk(chunk, identifier, signer) {
95
+ const chunkAddress = chunk.address();
96
+ (0, cac_1.assertValidChunkData)(chunk.data, chunkAddress);
97
+ const digest = (0, hash_1.keccak256Hash)(identifier, chunkAddress);
98
+ const signature = await (0, signer_1.sign)(signer, digest);
99
+ const data = (0, serialize_1.serializeBytes)(identifier, signature, chunk.span(), chunk.payload());
100
+ const address = makeSOCAddress(identifier, signer.address);
101
+ return {
102
+ data,
103
+ identifier: () => identifier,
104
+ signature: () => signature,
105
+ span: () => chunk.span(),
106
+ payload: () => chunk.payload(),
107
+ address: () => address,
108
+ owner: () => signer.address,
109
+ };
110
+ }
111
+ exports.makeSingleOwnerChunk = makeSingleOwnerChunk;
112
+ /**
113
+ * Helper function to upload a chunk.
114
+ *
115
+ * It uses the Chunk API and calculates the address before uploading.
116
+ *
117
+ * @param kyOptions Ky Options for making requests
118
+ * @param chunk A chunk object
119
+ * @param postageBatchId Postage BatchId that will be assigned to uploaded data
120
+ * @param options Upload options
121
+ */
122
+ async function uploadSingleOwnerChunk(requestOptions, chunk, postageBatchId, options) {
123
+ const owner = (0, hex_1.bytesToHex)(chunk.owner());
124
+ const identifier = (0, hex_1.bytesToHex)(chunk.identifier());
125
+ const signature = (0, hex_1.bytesToHex)(chunk.signature());
126
+ const data = (0, serialize_1.serializeBytes)(chunk.span(), chunk.payload());
127
+ return socAPI.upload(requestOptions, owner, identifier, signature, data, postageBatchId, options);
128
+ }
129
+ exports.uploadSingleOwnerChunk = uploadSingleOwnerChunk;
130
+ /**
131
+ * Helper function to create and upload SOC.
132
+ *
133
+ * @param kyOptions Ky Options for making requests
134
+ * @param signer The singer interface for signing the chunk
135
+ * @param postageBatchId
136
+ * @param identifier The identifier of the chunk
137
+ * @param data The chunk data
138
+ * @param options
139
+ */
140
+ async function uploadSingleOwnerChunkData(requestOptions, signer, postageBatchId, identifier, data, options) {
141
+ (0, type_1.assertAddress)(postageBatchId);
142
+ const cac = (0, cac_1.makeContentAddressedChunk)(data);
143
+ const soc = await makeSingleOwnerChunk(cac, identifier, signer);
144
+ return uploadSingleOwnerChunk(requestOptions, soc, postageBatchId, options);
145
+ }
146
+ exports.uploadSingleOwnerChunkData = uploadSingleOwnerChunkData;
147
+ /**
148
+ * Helper function to download SOC.
149
+ *
150
+ * @param url The url of the Bee service
151
+ * @param ownerAddress The singer interface for signing the chunk
152
+ * @param identifier The identifier of the chunk
153
+ */
154
+ async function downloadSingleOwnerChunk(requestOptions, ownerAddress, identifier) {
155
+ const address = makeSOCAddress(identifier, ownerAddress);
156
+ const data = await chunkAPI.download(requestOptions, (0, hex_1.bytesToHex)(address));
157
+ return makeSingleOwnerChunkFromData(data, address);
158
+ }
159
+ 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 = 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,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeFeedIdentifier = void 0;
4
+ const types_1 = require("../types");
5
+ const hash_1 = require("../utils/hash");
6
+ const hex_1 = require("../utils/hex");
7
+ const uint64_1 = require("../utils/uint64");
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 = (0, uint64_1.writeUint64BigEndian)(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
+ }
22
+ function makeFeedIdentifier(topic, index) {
23
+ if (typeof index === 'number') {
24
+ return makeSequentialFeedIdentifier(topic, index);
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);
34
+ }
35
+ exports.makeFeedIdentifier = makeFeedIdentifier;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdate = exports.getFeedUpdateChunkReference = exports.updateFeed = exports.findNextIndex = void 0;
27
+ const serialize_1 = require("../chunk/serialize");
28
+ const soc_1 = require("../chunk/soc");
29
+ const chunkAPI = __importStar(require("../modules/chunk"));
30
+ const feed_1 = require("../modules/feed");
31
+ const types_1 = require("../types");
32
+ const bytes_1 = require("../utils/bytes");
33
+ const eth_1 = require("../utils/eth");
34
+ const hash_1 = require("../utils/hash");
35
+ const hex_1 = require("../utils/hex");
36
+ const reference_1 = require("../utils/reference");
37
+ const type_1 = require("../utils/type");
38
+ const uint64_1 = require("../utils/uint64");
39
+ const identifier_1 = require("./identifier");
40
+ const TIMESTAMP_PAYLOAD_OFFSET = 0;
41
+ const TIMESTAMP_PAYLOAD_SIZE = 8;
42
+ const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE;
43
+ async function findNextIndex(requestOptions, owner, topic, options) {
44
+ try {
45
+ const feedUpdate = await (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic, options);
46
+ return (0, hex_1.makeHexString)(feedUpdate.feedIndexNext, types_1.FEED_INDEX_HEX_LENGTH);
47
+ }
48
+ catch (e) {
49
+ if (e?.response?.status === 404) {
50
+ return (0, hex_1.bytesToHex)((0, bytes_1.makeBytes)(8));
51
+ }
52
+ throw e;
53
+ }
54
+ }
55
+ exports.findNextIndex = findNextIndex;
56
+ async function updateFeed(requestOptions, signer, topic, reference, postageBatchId, options, index = 'latest') {
57
+ const ownerHex = (0, eth_1.makeHexEthAddress)(signer.address);
58
+ const nextIndex = index === 'latest' ? await findNextIndex(requestOptions, ownerHex, topic, options) : index;
59
+ const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
60
+ const at = options?.at ?? Date.now() / 1000.0;
61
+ const timestamp = (0, uint64_1.writeUint64BigEndian)(at);
62
+ const payloadBytes = (0, serialize_1.serializeBytes)(timestamp, reference);
63
+ return (0, soc_1.uploadSingleOwnerChunkData)(requestOptions, signer, postageBatchId, identifier, payloadBytes, options);
64
+ }
65
+ exports.updateFeed = updateFeed;
66
+ function getFeedUpdateChunkReference(owner, topic, index) {
67
+ const identifier = (0, identifier_1.makeFeedIdentifier)(topic, index);
68
+ return (0, hash_1.keccak256Hash)(identifier, owner);
69
+ }
70
+ exports.getFeedUpdateChunkReference = getFeedUpdateChunkReference;
71
+ async function downloadFeedUpdate(requestOptions, owner, topic, index) {
72
+ const address = getFeedUpdateChunkReference(owner, topic, index);
73
+ const addressHex = (0, hex_1.bytesToHex)(address);
74
+ const data = await chunkAPI.download(requestOptions, addressHex);
75
+ const soc = (0, soc_1.makeSingleOwnerChunkFromData)(data, address);
76
+ const payload = soc.payload();
77
+ const timestampBytes = (0, bytes_1.bytesAtOffset)(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
78
+ const timestamp = (0, uint64_1.readUint64BigEndian)(timestampBytes);
79
+ const reference = (0, reference_1.makeBytesReference)(payload, REFERENCE_PAYLOAD_OFFSET);
80
+ return {
81
+ timestamp,
82
+ reference,
83
+ };
84
+ }
85
+ exports.downloadFeedUpdate = downloadFeedUpdate;
86
+ function makeFeedReader(requestOptions, type, topic, owner) {
87
+ return {
88
+ type,
89
+ owner,
90
+ topic,
91
+ async download(options) {
92
+ if (!options?.index) {
93
+ return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic, { ...options, type });
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
+ };
103
+ }
104
+ exports.makeFeedReader = makeFeedReader;
105
+ function makeFeedWriter(requestOptions, type, topic, signer) {
106
+ const upload = async (postageBatchId, reference, options) => {
107
+ (0, type_1.assertAddress)(postageBatchId);
108
+ const canonicalReference = (0, reference_1.makeBytesReference)(reference);
109
+ return updateFeed(requestOptions, signer, topic, canonicalReference, postageBatchId, { ...options, type });
110
+ };
111
+ return {
112
+ ...makeFeedReader(requestOptions, type, topic, (0, eth_1.makeHexEthAddress)(signer.address)),
113
+ upload,
114
+ };
115
+ }
116
+ exports.makeFeedWriter = makeFeedWriter;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setJsonData = exports.getJsonData = void 0;
4
+ const type_1 = require("../utils/type");
5
+ function serializeJson(data) {
6
+ try {
7
+ const jsonString = JSON.stringify(data);
8
+ return new TextEncoder().encode(jsonString);
9
+ }
10
+ catch (e) {
11
+ if ((0, type_1.isError)(e)) {
12
+ e.message = `JsonFeed: ${e.message}`;
13
+ }
14
+ throw e;
15
+ }
16
+ }
17
+ async function getJsonData(bee, reader) {
18
+ const feedUpdate = await reader.download();
19
+ const retrievedData = await bee.downloadData(feedUpdate.reference);
20
+ return retrievedData.json();
21
+ }
22
+ exports.getJsonData = getJsonData;
23
+ async function setJsonData(bee, writer, postageBatchId, data, options, requestOptions) {
24
+ const serializedData = serializeJson(data);
25
+ const { reference } = await bee.uploadData(postageBatchId, serializedData, options, requestOptions);
26
+ return writer.upload(postageBatchId, reference);
27
+ }
28
+ exports.setJsonData = setJsonData;