@ethersphere/bee-js 3.0.0 → 3.3.1

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 (196) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/bee-debug.js +615 -0
  3. package/dist/cjs/bee.js +922 -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 +137 -0
  8. package/dist/cjs/chunk/soc.js +172 -0
  9. package/dist/cjs/chunk/span.js +29 -0
  10. package/dist/cjs/feed/index.js +184 -0
  11. package/dist/cjs/feed/json.js +41 -0
  12. package/dist/cjs/feed/topic.js +25 -0
  13. package/dist/cjs/feed/type.js +15 -0
  14. package/dist/cjs/index.js +35 -0
  15. package/dist/cjs/modules/bytes.js +74 -0
  16. package/dist/cjs/modules/bzz.js +131 -0
  17. package/dist/cjs/modules/chunk.js +58 -0
  18. package/dist/cjs/modules/debug/balance.js +77 -0
  19. package/dist/cjs/modules/debug/chequebook.js +167 -0
  20. package/dist/cjs/modules/debug/chunk.js +51 -0
  21. package/dist/cjs/modules/debug/connectivity.js +75 -0
  22. package/dist/cjs/modules/debug/settlements.js +45 -0
  23. package/dist/cjs/modules/debug/stamps.js +89 -0
  24. package/dist/cjs/modules/debug/states.js +47 -0
  25. package/dist/cjs/modules/debug/status.js +153 -0
  26. package/dist/cjs/modules/debug/tag.js +30 -0
  27. package/dist/cjs/modules/debug/transactions.js +81 -0
  28. package/dist/cjs/modules/feed.js +76 -0
  29. package/dist/cjs/modules/pinning.js +84 -0
  30. package/dist/cjs/modules/pss.js +55 -0
  31. package/dist/cjs/modules/soc.js +40 -0
  32. package/dist/cjs/modules/status.js +26 -0
  33. package/dist/cjs/modules/stewardship.js +41 -0
  34. package/dist/cjs/modules/tag.js +96 -0
  35. package/dist/cjs/package.json +8 -0
  36. package/dist/cjs/types/debug.js +9 -0
  37. package/dist/cjs/types/index.js +46 -0
  38. package/dist/cjs/types/ky-options.js +8 -0
  39. package/dist/cjs/types/ky-universal/common.js +8 -0
  40. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  41. package/dist/cjs/types/ky-universal/retry.js +8 -0
  42. package/dist/cjs/utils/bytes.js +107 -0
  43. package/dist/cjs/utils/collection.browser.js +36 -0
  44. package/dist/cjs/utils/collection.js +70 -0
  45. package/dist/cjs/utils/collection.node.js +115 -0
  46. package/dist/cjs/utils/data.browser.js +78 -0
  47. package/dist/cjs/utils/data.js +60 -0
  48. package/dist/cjs/utils/error.js +50 -0
  49. package/dist/cjs/utils/eth.js +211 -0
  50. package/dist/cjs/utils/expose.js +44 -0
  51. package/dist/cjs/utils/file.js +49 -0
  52. package/dist/cjs/utils/hash.js +21 -0
  53. package/dist/cjs/utils/headers.js +59 -0
  54. package/dist/cjs/utils/hex.js +150 -0
  55. package/dist/cjs/utils/http.js +166 -0
  56. package/dist/cjs/utils/merge.js +34 -0
  57. package/dist/cjs/utils/pss.js +18 -0
  58. package/dist/cjs/utils/stamps.js +17 -0
  59. package/dist/cjs/utils/stream.js +146 -0
  60. package/dist/cjs/utils/tar.js +25 -0
  61. package/dist/cjs/utils/type.js +327 -0
  62. package/dist/cjs/utils/uint64.js +29 -0
  63. package/dist/cjs/utils/url.js +56 -0
  64. package/dist/index.browser.min.js +1 -1
  65. package/dist/index.browser.min.js.LICENSE.txt +5 -12
  66. package/dist/index.browser.min.js.map +1 -1
  67. package/dist/mjs/bee-debug.js +609 -0
  68. package/dist/mjs/bee.js +944 -0
  69. package/dist/mjs/chunk/bmt.js +56 -0
  70. package/dist/mjs/chunk/cac.js +52 -0
  71. package/dist/mjs/chunk/serialize.js +15 -0
  72. package/dist/mjs/chunk/signer.js +131 -0
  73. package/dist/mjs/chunk/soc.js +139 -0
  74. package/dist/mjs/chunk/span.js +28 -0
  75. package/dist/mjs/feed/index.js +145 -0
  76. package/dist/mjs/feed/json.js +27 -0
  77. package/dist/mjs/feed/topic.js +21 -0
  78. package/dist/mjs/feed/type.js +10 -0
  79. package/dist/mjs/index.js +7 -0
  80. package/dist/mjs/modules/bytes.js +59 -0
  81. package/dist/mjs/modules/bzz.js +122 -0
  82. package/dist/mjs/modules/chunk.js +45 -0
  83. package/dist/mjs/modules/debug/balance.js +57 -0
  84. package/dist/mjs/modules/debug/chequebook.js +150 -0
  85. package/dist/mjs/modules/debug/chunk.js +35 -0
  86. package/dist/mjs/modules/debug/connectivity.js +45 -0
  87. package/dist/mjs/modules/debug/settlements.js +29 -0
  88. package/dist/mjs/modules/debug/stamps.js +64 -0
  89. package/dist/mjs/modules/debug/states.js +31 -0
  90. package/dist/mjs/modules/debug/status.js +134 -0
  91. package/dist/mjs/modules/debug/tag.js +16 -0
  92. package/dist/mjs/modules/debug/transactions.js +63 -0
  93. package/dist/mjs/modules/feed.js +67 -0
  94. package/dist/mjs/modules/pinning.js +66 -0
  95. package/dist/mjs/modules/pss.js +40 -0
  96. package/dist/mjs/modules/soc.js +31 -0
  97. package/dist/mjs/modules/status.js +12 -0
  98. package/dist/mjs/modules/stewardship.js +24 -0
  99. package/dist/mjs/modules/tag.js +77 -0
  100. package/dist/mjs/package.json +8 -0
  101. package/dist/mjs/types/debug.js +7 -0
  102. package/dist/mjs/types/index.js +37 -0
  103. package/dist/mjs/types/ky-options.js +7 -0
  104. package/dist/mjs/types/ky-universal/common.js +7 -0
  105. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  106. package/dist/mjs/types/ky-universal/retry.js +7 -0
  107. package/dist/mjs/utils/bytes.js +101 -0
  108. package/dist/mjs/utils/collection.browser.js +19 -0
  109. package/dist/mjs/utils/collection.js +64 -0
  110. package/dist/mjs/utils/collection.node.js +74 -0
  111. package/dist/mjs/utils/data.browser.js +73 -0
  112. package/dist/mjs/utils/data.js +43 -0
  113. package/dist/mjs/utils/error.js +56 -0
  114. package/dist/mjs/utils/eth.js +199 -0
  115. package/dist/mjs/utils/expose.js +9 -0
  116. package/dist/mjs/utils/file.js +36 -0
  117. package/dist/mjs/utils/hash.js +17 -0
  118. package/dist/mjs/utils/headers.js +58 -0
  119. package/dist/mjs/utils/hex.js +154 -0
  120. package/dist/mjs/utils/http.js +155 -0
  121. package/dist/mjs/utils/merge.js +36 -0
  122. package/dist/mjs/utils/pss.js +16 -0
  123. package/dist/mjs/utils/stamps.js +17 -0
  124. package/dist/mjs/utils/stream.js +156 -0
  125. package/dist/mjs/utils/tar.js +21 -0
  126. package/dist/mjs/utils/type.js +336 -0
  127. package/dist/mjs/utils/uint64.js +23 -0
  128. package/dist/mjs/utils/url.js +57 -0
  129. package/dist/{src → types}/bee-debug.d.ts +51 -1
  130. package/dist/{src → types}/bee.d.ts +21 -0
  131. package/dist/{src → types}/chunk/bmt.d.ts +0 -0
  132. package/dist/{src → types}/chunk/cac.d.ts +0 -0
  133. package/dist/{src → types}/chunk/serialize.d.ts +0 -0
  134. package/dist/{src → types}/chunk/signer.d.ts +0 -0
  135. package/dist/{src → types}/chunk/soc.d.ts +0 -0
  136. package/dist/{src → types}/chunk/span.d.ts +0 -0
  137. package/dist/{src → types}/feed/index.d.ts +0 -0
  138. package/dist/{src → types}/feed/json.d.ts +0 -0
  139. package/dist/{src → types}/feed/topic.d.ts +0 -0
  140. package/dist/{src → types}/feed/type.d.ts +0 -0
  141. package/dist/{src → types}/index.d.ts +0 -0
  142. package/dist/{src → types}/modules/bytes.d.ts +0 -0
  143. package/dist/{src → types}/modules/bzz.d.ts +0 -0
  144. package/dist/{src → types}/modules/chunk.d.ts +2 -10
  145. package/dist/{src → types}/modules/debug/balance.d.ts +0 -0
  146. package/dist/{src → types}/modules/debug/chequebook.d.ts +0 -0
  147. package/dist/{src → types}/modules/debug/chunk.d.ts +0 -0
  148. package/dist/{src → types}/modules/debug/connectivity.d.ts +0 -0
  149. package/dist/{src → types}/modules/debug/settlements.d.ts +0 -0
  150. package/dist/{src → types}/modules/debug/stamps.d.ts +0 -0
  151. package/dist/{src → types}/modules/debug/states.d.ts +0 -0
  152. package/dist/types/modules/debug/status.d.ts +72 -0
  153. package/dist/{src → types}/modules/debug/tag.d.ts +0 -0
  154. package/dist/{src → types}/modules/debug/transactions.d.ts +0 -0
  155. package/dist/{src → types}/modules/feed.d.ts +0 -0
  156. package/dist/{src → types}/modules/pinning.d.ts +0 -0
  157. package/dist/{src → types}/modules/pss.d.ts +0 -0
  158. package/dist/{src → types}/modules/soc.d.ts +0 -0
  159. package/dist/{src → types}/modules/status.d.ts +0 -0
  160. package/dist/{src → types}/modules/stewardship.d.ts +0 -0
  161. package/dist/{src → types}/modules/tag.d.ts +0 -0
  162. package/dist/{src → types}/types/debug.d.ts +25 -0
  163. package/dist/{src → types}/types/index.d.ts +32 -2
  164. package/dist/types/types/ky-options.d.ts +221 -0
  165. package/dist/types/types/ky-universal/common.d.ts +13 -0
  166. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  167. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  168. package/dist/{src → types}/utils/bytes.d.ts +0 -0
  169. package/dist/types/utils/collection.browser.d.ts +15 -0
  170. package/dist/{src → types}/utils/collection.d.ts +0 -14
  171. package/dist/types/utils/collection.node.d.ts +15 -0
  172. package/dist/{src → types}/utils/data.browser.d.ts +0 -0
  173. package/dist/{src → types}/utils/data.d.ts +0 -0
  174. package/dist/types/utils/error.d.ts +33 -0
  175. package/dist/{src → types}/utils/eth.d.ts +0 -0
  176. package/dist/{src → types}/utils/expose.d.ts +3 -1
  177. package/dist/{src → types}/utils/file.d.ts +0 -0
  178. package/dist/{src → types}/utils/hash.d.ts +0 -0
  179. package/dist/{src → types}/utils/headers.d.ts +0 -0
  180. package/dist/{src → types}/utils/hex.d.ts +0 -0
  181. package/dist/{src → types}/utils/http.d.ts +7 -9
  182. package/dist/{src → types}/utils/merge.d.ts +0 -0
  183. package/dist/{src → types}/utils/pss.d.ts +0 -0
  184. package/dist/types/utils/stamps.d.ts +12 -0
  185. package/dist/{src → types}/utils/stream.d.ts +11 -8
  186. package/dist/{src → types}/utils/tar.d.ts +0 -0
  187. package/dist/{src → types}/utils/type.d.ts +6 -0
  188. package/dist/{src → types}/utils/uint64.d.ts +0 -0
  189. package/dist/{src → types}/utils/url.d.ts +0 -0
  190. package/package.json +61 -43
  191. package/CHANGELOG.md +0 -546
  192. package/dist/index.min.js +0 -3
  193. package/dist/index.min.js.LICENSE.txt +0 -50
  194. package/dist/index.min.js.map +0 -1
  195. package/dist/src/modules/debug/status.d.ts +0 -18
  196. package/dist/src/utils/error.d.ts +0 -14
@@ -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,137 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.sign = exports.makeSigner = exports.assertSigner = exports.makePrivateKeySigner = exports.recoverAddress = exports.defaultSign = void 0;
16
+ // For ESM compatibility
17
+ const elliptic_1 = __importDefault(require("elliptic"));
18
+ const { ec } = elliptic_1.default;
19
+ const error_1 = require("../utils/error");
20
+ const bytes_1 = require("../utils/bytes");
21
+ const hash_1 = require("../utils/hash");
22
+ const hex_1 = require("../utils/hex");
23
+ const types_1 = require("../types");
24
+ const type_1 = require("../utils/type");
25
+ const UNCOMPRESSED_RECOVERY_ID = 27;
26
+ function hashWithEthereumPrefix(data) {
27
+ const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
28
+ const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
29
+ return (0, hash_1.keccak256Hash)(prefixBytes, data);
30
+ }
31
+ /**
32
+ * The default signer function that can be used for integrating with
33
+ * other applications (e.g. wallets).
34
+ *
35
+ * @param data The data to be signed
36
+ * @param privateKey The private key used for signing the data
37
+ */
38
+ function defaultSign(data, privateKey) {
39
+ const curve = new ec('secp256k1');
40
+ const keyPair = curve.keyFromPrivate(privateKey);
41
+ const hashedDigest = hashWithEthereumPrefix(data);
42
+ const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined });
43
+ if (sigRaw.recoveryParam === null) {
44
+ throw new error_1.BeeError('signDigest recovery param was null');
45
+ }
46
+ const signature = new Uint8Array([
47
+ ...sigRaw.r.toArray('be', 32),
48
+ ...sigRaw.s.toArray('be', 32),
49
+ sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
50
+ ]);
51
+ return signature;
52
+ }
53
+ exports.defaultSign = defaultSign;
54
+ function publicKeyToAddress(pubKey) {
55
+ const pubBytes = pubKey.encode('array', false);
56
+ return (0, hash_1.keccak256Hash)(pubBytes.slice(1)).slice(12);
57
+ }
58
+ /**
59
+ * Recovers the ethereum address from a given signature.
60
+ *
61
+ * Can be used for verifying a piece of data when the public key is
62
+ * known.
63
+ *
64
+ * @param signature The signature
65
+ * @param digest The digest of the data
66
+ *
67
+ * @returns the recovered address
68
+ */
69
+ function recoverAddress(signature, digest) {
70
+ const curve = new ec('secp256k1');
71
+ const sig = {
72
+ r: signature.slice(0, 32),
73
+ s: signature.slice(32, 64),
74
+ };
75
+ const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
76
+ const hash = hashWithEthereumPrefix(digest);
77
+ const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
78
+ return publicKeyToAddress(recPubKey);
79
+ }
80
+ exports.recoverAddress = recoverAddress;
81
+ /**
82
+ * Creates a singer object that can be used when the private key is known.
83
+ *
84
+ * @param privateKey The private key
85
+ */
86
+ function makePrivateKeySigner(privateKey) {
87
+ const curve = new ec('secp256k1');
88
+ const keyPair = curve.keyFromPrivate(privateKey);
89
+ const address = publicKeyToAddress(keyPair.getPublic());
90
+ return {
91
+ sign: (digest) => defaultSign(digest, privateKey),
92
+ address,
93
+ };
94
+ }
95
+ exports.makePrivateKeySigner = makePrivateKeySigner;
96
+ function assertSigner(signer) {
97
+ if (!(0, type_1.isStrictlyObject)(signer)) {
98
+ throw new TypeError('Signer must be an object!');
99
+ }
100
+ const typedSigner = signer;
101
+ if (!(0, bytes_1.isBytes)(typedSigner.address, 20)) {
102
+ throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
103
+ }
104
+ if (typeof typedSigner.sign !== 'function') {
105
+ throw new TypeError('Signer sign property needs to be function!');
106
+ }
107
+ }
108
+ exports.assertSigner = assertSigner;
109
+ function makeSigner(signer) {
110
+ if (typeof signer === 'string') {
111
+ const hexKey = (0, hex_1.makeHexString)(signer, 64);
112
+ const keyBytes = (0, hex_1.hexToBytes)(hexKey); // HexString is verified for 64 length => 32 is guaranteed
113
+ return makePrivateKeySigner(keyBytes);
114
+ }
115
+ else if (signer instanceof Uint8Array) {
116
+ (0, bytes_1.assertBytes)(signer, 32);
117
+ return makePrivateKeySigner(signer);
118
+ }
119
+ assertSigner(signer);
120
+ return signer;
121
+ }
122
+ exports.makeSigner = makeSigner;
123
+ function sign(signer, data) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ const result = yield signer.sign((0, bytes_1.wrapBytesWithHelpers)(data));
126
+ if (typeof result === 'string') {
127
+ const hexString = (0, hex_1.makeHexString)(result, types_1.SIGNATURE_HEX_LENGTH);
128
+ return (0, hex_1.hexToBytes)(hexString);
129
+ }
130
+ if (result instanceof Uint8Array) {
131
+ (0, bytes_1.assertBytes)(result, types_1.SIGNATURE_BYTES_LENGTH);
132
+ return result;
133
+ }
134
+ throw new TypeError('Invalid output of sign function!');
135
+ });
136
+ }
137
+ 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;