@ethersphere/bee-js 8.3.1 → 9.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/README.md +192 -50
  2. package/dist/cjs/bee-dev.js +78 -0
  3. package/dist/cjs/bee.js +341 -417
  4. package/dist/cjs/chunk/bmt.js +15 -32
  5. package/dist/cjs/chunk/cac.js +26 -36
  6. package/dist/cjs/chunk/soc.js +56 -51
  7. package/dist/cjs/feed/identifier.js +3 -28
  8. package/dist/cjs/feed/index.js +95 -44
  9. package/dist/cjs/feed/retrievable.js +11 -22
  10. package/dist/cjs/index.js +17 -1
  11. package/dist/cjs/manifest/manifest.js +369 -0
  12. package/dist/cjs/modules/bytes.js +24 -12
  13. package/dist/cjs/modules/bzz.js +24 -34
  14. package/dist/cjs/modules/chunk.js +13 -7
  15. package/dist/cjs/modules/debug/balance.js +29 -4
  16. package/dist/cjs/modules/debug/chequebook.js +60 -16
  17. package/dist/cjs/modules/debug/connectivity.js +82 -4
  18. package/dist/cjs/modules/debug/settlements.js +22 -2
  19. package/dist/cjs/modules/debug/stake.js +29 -11
  20. package/dist/cjs/modules/debug/stamps.js +88 -7
  21. package/dist/cjs/modules/debug/states.js +25 -3
  22. package/dist/cjs/modules/debug/status.js +45 -47
  23. package/dist/cjs/modules/debug/transactions.js +27 -5
  24. package/dist/cjs/modules/envelope.js +8 -6
  25. package/dist/cjs/modules/feed.js +25 -10
  26. package/dist/cjs/modules/grantee.js +18 -12
  27. package/dist/cjs/modules/gsoc.js +24 -0
  28. package/dist/cjs/modules/pinning.js +13 -2
  29. package/dist/cjs/modules/pss.js +9 -3
  30. package/dist/cjs/modules/soc.js +9 -4
  31. package/dist/cjs/modules/stewardship.js +7 -3
  32. package/dist/cjs/modules/tag.js +35 -3
  33. package/dist/cjs/package.json +1 -0
  34. package/dist/cjs/stamper/stamper.js +43 -0
  35. package/dist/cjs/types/debug.js +16 -1
  36. package/dist/cjs/types/index.js +2 -24
  37. package/dist/cjs/utils/bytes.js +67 -115
  38. package/dist/cjs/utils/chunk-size.js +17 -0
  39. package/dist/cjs/utils/chunk-stream.browser.js +85 -0
  40. package/dist/cjs/utils/chunk-stream.js +84 -0
  41. package/dist/cjs/utils/cid.js +5 -6
  42. package/dist/cjs/utils/collection.browser.js +2 -2
  43. package/dist/cjs/utils/collection.js +3 -2
  44. package/dist/cjs/utils/collection.node.js +0 -2
  45. package/dist/cjs/utils/constants.js +7 -3
  46. package/dist/cjs/utils/data.browser.js +6 -3
  47. package/dist/cjs/utils/data.js +8 -4
  48. package/dist/cjs/utils/duration.js +51 -0
  49. package/dist/cjs/utils/expose.js +11 -40
  50. package/dist/cjs/utils/headers.js +79 -52
  51. package/dist/cjs/utils/http.js +34 -10
  52. package/dist/cjs/utils/mime.js +78 -0
  53. package/dist/cjs/utils/pss.js +3 -4
  54. package/dist/cjs/utils/redundancy.js +18 -14
  55. package/dist/cjs/utils/resource-locator.js +17 -0
  56. package/dist/cjs/utils/size.js +35 -0
  57. package/dist/cjs/utils/stamps.js +67 -51
  58. package/dist/cjs/utils/tar-uploader.browser.js +2 -2
  59. package/dist/cjs/utils/tar-uploader.js +2 -2
  60. package/dist/cjs/utils/tokens.js +144 -0
  61. package/dist/cjs/utils/type.js +142 -350
  62. package/dist/cjs/utils/typed-bytes.js +179 -0
  63. package/dist/cjs/utils/upload-progress.js +2 -0
  64. package/dist/cjs/utils/url.js +0 -4
  65. package/dist/cjs/utils/workaround.js +27 -0
  66. package/dist/index.browser.min.js +1 -2
  67. package/dist/index.browser.min.js.map +1 -1
  68. package/dist/mjs/bee-dev.js +98 -0
  69. package/dist/mjs/bee.js +367 -411
  70. package/dist/mjs/chunk/bmt.js +13 -29
  71. package/dist/mjs/chunk/cac.js +26 -34
  72. package/dist/mjs/chunk/soc.js +57 -53
  73. package/dist/mjs/feed/identifier.js +3 -26
  74. package/dist/mjs/feed/index.js +98 -51
  75. package/dist/mjs/feed/retrievable.js +12 -23
  76. package/dist/mjs/index.js +10 -1
  77. package/dist/mjs/manifest/manifest.js +371 -0
  78. package/dist/mjs/modules/bytes.js +27 -15
  79. package/dist/mjs/modules/bzz.js +25 -32
  80. package/dist/mjs/modules/chunk.js +16 -8
  81. package/dist/mjs/modules/debug/balance.js +61 -4
  82. package/dist/mjs/modules/debug/chequebook.js +116 -16
  83. package/dist/mjs/modules/debug/connectivity.js +144 -3
  84. package/dist/mjs/modules/debug/settlements.js +46 -2
  85. package/dist/mjs/modules/debug/stake.js +69 -11
  86. package/dist/mjs/modules/debug/stamps.js +184 -7
  87. package/dist/mjs/modules/debug/states.js +55 -3
  88. package/dist/mjs/modules/debug/status.js +97 -45
  89. package/dist/mjs/modules/debug/transactions.js +61 -5
  90. package/dist/mjs/modules/envelope.js +11 -9
  91. package/dist/mjs/modules/feed.js +26 -10
  92. package/dist/mjs/modules/grantee.js +35 -13
  93. package/dist/mjs/modules/gsoc.js +16 -0
  94. package/dist/mjs/modules/pinning.js +23 -2
  95. package/dist/mjs/modules/pss.js +10 -4
  96. package/dist/mjs/modules/soc.js +10 -5
  97. package/dist/mjs/modules/stewardship.js +14 -4
  98. package/dist/mjs/modules/tag.js +93 -3
  99. package/dist/mjs/package.json +2 -1
  100. package/dist/mjs/stamper/stamper.js +39 -0
  101. package/dist/mjs/types/debug.js +15 -1
  102. package/dist/mjs/types/index.js +2 -24
  103. package/dist/mjs/utils/bytes.js +63 -104
  104. package/dist/mjs/utils/chunk-size.js +13 -0
  105. package/dist/mjs/utils/chunk-stream.browser.js +85 -0
  106. package/dist/mjs/utils/chunk-stream.js +87 -0
  107. package/dist/mjs/utils/cid.js +5 -6
  108. package/dist/mjs/utils/collection.browser.js +2 -2
  109. package/dist/mjs/utils/collection.js +2 -2
  110. package/dist/mjs/utils/collection.node.js +0 -2
  111. package/dist/mjs/utils/constants.js +6 -2
  112. package/dist/mjs/utils/data.browser.js +9 -3
  113. package/dist/mjs/utils/data.js +12 -4
  114. package/dist/mjs/utils/duration.js +47 -0
  115. package/dist/mjs/utils/expose.js +2 -7
  116. package/dist/mjs/utils/headers.js +73 -48
  117. package/dist/mjs/utils/http.js +33 -11
  118. package/dist/mjs/utils/mime.js +75 -0
  119. package/dist/mjs/utils/pss.js +3 -4
  120. package/dist/mjs/utils/redundancy.js +18 -8
  121. package/dist/mjs/utils/resource-locator.js +13 -0
  122. package/dist/mjs/utils/size.js +31 -0
  123. package/dist/mjs/utils/stamps.js +47 -44
  124. package/dist/mjs/utils/tar-uploader.browser.js +2 -2
  125. package/dist/mjs/utils/tar-uploader.js +2 -2
  126. package/dist/mjs/utils/tokens.js +139 -0
  127. package/dist/mjs/utils/type.js +216 -310
  128. package/dist/mjs/utils/typed-bytes.js +160 -0
  129. package/dist/mjs/utils/upload-progress.js +1 -0
  130. package/dist/mjs/utils/url.js +0 -4
  131. package/dist/mjs/utils/workaround.js +22 -0
  132. package/dist/types/bee-dev.d.ts +5 -0
  133. package/dist/types/bee.d.ts +88 -165
  134. package/dist/types/chunk/bmt.d.ts +2 -2
  135. package/dist/types/chunk/cac.d.ts +7 -24
  136. package/dist/types/chunk/soc.d.ts +15 -15
  137. package/dist/types/feed/identifier.d.ts +2 -4
  138. package/dist/types/feed/index.d.ts +14 -18
  139. package/dist/types/feed/retrievable.d.ts +3 -4
  140. package/dist/types/index.d.ts +28 -1
  141. package/dist/types/manifest/manifest.d.ts +106 -0
  142. package/dist/types/modules/bytes.d.ts +8 -5
  143. package/dist/types/modules/bzz.d.ts +8 -10
  144. package/dist/types/modules/chunk.d.ts +4 -3
  145. package/dist/types/modules/debug/balance.d.ts +3 -2
  146. package/dist/types/modules/debug/chequebook.d.ts +7 -6
  147. package/dist/types/modules/debug/connectivity.d.ts +5 -3
  148. package/dist/types/modules/debug/settlements.d.ts +2 -1
  149. package/dist/types/modules/debug/stake.d.ts +4 -2
  150. package/dist/types/modules/debug/stamps.d.ts +5 -4
  151. package/dist/types/modules/debug/status.d.ts +6 -25
  152. package/dist/types/modules/debug/transactions.d.ts +5 -4
  153. package/dist/types/modules/envelope.d.ts +3 -2
  154. package/dist/types/modules/feed.d.ts +20 -16
  155. package/dist/types/modules/grantee.d.ts +7 -6
  156. package/dist/types/modules/gsoc.d.ts +7 -0
  157. package/dist/types/modules/pinning.d.ts +2 -4
  158. package/dist/types/modules/pss.d.ts +4 -3
  159. package/dist/types/modules/soc.d.ts +3 -2
  160. package/dist/types/modules/stewardship.d.ts +4 -4
  161. package/dist/types/modules/tag.d.ts +2 -1
  162. package/dist/types/stamper/stamper.d.ts +15 -0
  163. package/dist/types/types/debug.d.ts +63 -115
  164. package/dist/types/types/index.d.ts +103 -204
  165. package/dist/types/utils/bytes.d.ts +16 -90
  166. package/dist/types/utils/chunk-size.d.ts +1 -0
  167. package/dist/types/utils/chunk-stream.browser.d.ts +6 -0
  168. package/dist/types/utils/chunk-stream.d.ts +6 -0
  169. package/dist/types/utils/cid.d.ts +3 -2
  170. package/dist/types/utils/collection.browser.d.ts +2 -2
  171. package/dist/types/utils/collection.d.ts +2 -1
  172. package/dist/types/utils/collection.node.d.ts +0 -1
  173. package/dist/types/utils/constants.d.ts +4 -1
  174. package/dist/types/utils/duration.d.ts +17 -0
  175. package/dist/types/utils/error.d.ts +2 -2
  176. package/dist/types/utils/expose.d.ts +2 -7
  177. package/dist/types/utils/headers.d.ts +3 -4
  178. package/dist/types/utils/mime.d.ts +1 -0
  179. package/dist/types/utils/pss.d.ts +2 -2
  180. package/dist/types/utils/resource-locator.d.ts +6 -0
  181. package/dist/types/utils/size.d.ts +16 -0
  182. package/dist/types/utils/stamps.d.ts +27 -33
  183. package/dist/types/utils/tar-uploader.browser.d.ts +3 -4
  184. package/dist/types/utils/tar-uploader.d.ts +3 -4
  185. package/dist/types/utils/tokens.d.ts +77 -0
  186. package/dist/types/utils/type.d.ts +20 -52
  187. package/dist/types/utils/typed-bytes.d.ts +68 -0
  188. package/dist/types/utils/upload-progress.d.ts +4 -0
  189. package/dist/types/utils/workaround.d.ts +2 -0
  190. package/package.json +11 -17
  191. package/dist/cjs/chunk/signer.js +0 -126
  192. package/dist/cjs/chunk/span.js +0 -25
  193. package/dist/cjs/feed/json.js +0 -28
  194. package/dist/cjs/feed/topic.js +0 -25
  195. package/dist/cjs/feed/type.js +0 -15
  196. package/dist/cjs/modules/debug/chunk.js +0 -21
  197. package/dist/cjs/modules/debug/tag.js +0 -19
  198. package/dist/cjs/utils/eth.js +0 -216
  199. package/dist/cjs/utils/hash.js +0 -21
  200. package/dist/cjs/utils/hex.js +0 -150
  201. package/dist/cjs/utils/reference.js +0 -36
  202. package/dist/index.browser.min.js.LICENSE.txt +0 -8
  203. package/dist/mjs/chunk/signer.js +0 -114
  204. package/dist/mjs/chunk/span.js +0 -21
  205. package/dist/mjs/feed/json.js +0 -26
  206. package/dist/mjs/feed/topic.js +0 -19
  207. package/dist/mjs/feed/type.js +0 -10
  208. package/dist/mjs/modules/debug/chunk.js +0 -17
  209. package/dist/mjs/modules/debug/tag.js +0 -15
  210. package/dist/mjs/utils/eth.js +0 -192
  211. package/dist/mjs/utils/hash.js +0 -16
  212. package/dist/mjs/utils/hex.js +0 -135
  213. package/dist/mjs/utils/reference.js +0 -29
  214. package/dist/types/chunk/signer.d.ts +0 -31
  215. package/dist/types/chunk/span.d.ts +0 -10
  216. package/dist/types/feed/json.d.ts +0 -4
  217. package/dist/types/feed/topic.d.ts +0 -3
  218. package/dist/types/feed/type.d.ts +0 -6
  219. package/dist/types/modules/debug/chunk.d.ts +0 -10
  220. package/dist/types/modules/debug/tag.d.ts +0 -8
  221. package/dist/types/utils/eth.d.ts +0 -67
  222. package/dist/types/utils/hash.d.ts +0 -9
  223. package/dist/types/utils/hex.d.ts +0 -86
  224. package/dist/types/utils/reference.d.ts +0 -2
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeBytesReference = void 0;
4
- const types_1 = require("../types");
5
- const bytes_1 = require("./bytes");
6
- const hex_1 = require("./hex");
7
- function makeBytesReference(reference, offset = 0) {
8
- if (typeof reference === 'string') {
9
- if (offset) {
10
- throw new Error('Offset property can be set only for UintArray reference!');
11
- }
12
- try {
13
- // Non-encrypted chunk hex string reference
14
- const hexReference = (0, hex_1.makeHexString)(reference, types_1.REFERENCE_HEX_LENGTH);
15
- return (0, hex_1.hexToBytes)(hexReference);
16
- }
17
- catch (e) {
18
- if (!(e instanceof TypeError)) {
19
- throw e;
20
- }
21
- // Encrypted chunk hex string reference
22
- const hexReference = (0, hex_1.makeHexString)(reference, types_1.ENCRYPTED_REFERENCE_HEX_LENGTH);
23
- return (0, hex_1.hexToBytes)(hexReference);
24
- }
25
- }
26
- else if (reference instanceof Uint8Array) {
27
- if ((0, bytes_1.hasBytesAtOffset)(reference, offset, types_1.ENCRYPTED_REFERENCE_BYTES_LENGTH)) {
28
- return (0, bytes_1.bytesAtOffset)(reference, offset, types_1.ENCRYPTED_REFERENCE_BYTES_LENGTH);
29
- }
30
- else if ((0, bytes_1.hasBytesAtOffset)(reference, offset, types_1.REFERENCE_BYTES_LENGTH)) {
31
- return (0, bytes_1.bytesAtOffset)(reference, offset, types_1.REFERENCE_BYTES_LENGTH);
32
- }
33
- }
34
- throw new TypeError('invalid chunk reference');
35
- }
36
- exports.makeBytesReference = makeBytesReference;
@@ -1,8 +0,0 @@
1
- /**
2
- * [js-sha3]{@link https://github.com/emn178/js-sha3}
3
- *
4
- * @version 0.8.0
5
- * @author Chen, Yi-Cyuan [emn178@gmail.com]
6
- * @copyright Chen, Yi-Cyuan 2015-2018
7
- * @license MIT
8
- */
@@ -1,114 +0,0 @@
1
- // For ESM compatibility
2
- import pkg from 'elliptic';
3
- const {
4
- ec
5
- } = pkg;
6
- import { BeeError } from "../utils/error.js";
7
- import { isBytes, assertBytes, wrapBytesWithHelpers } from "../utils/bytes.js";
8
- import { keccak256Hash } from "../utils/hash.js";
9
- import { hexToBytes, makeHexString } from "../utils/hex.js";
10
- import { SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH } from "../types/index.js";
11
- import { isStrictlyObject } from "../utils/type.js";
12
- const UNCOMPRESSED_RECOVERY_ID = 27;
13
- function hashWithEthereumPrefix(data) {
14
- const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
15
- const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
16
- return keccak256Hash(prefixBytes, data);
17
- }
18
- /**
19
- * The default signer function that can be used for integrating with
20
- * other applications (e.g. wallets).
21
- *
22
- * @param data The data to be signed
23
- * @param privateKey The private key used for signing the data
24
- */
25
- export function defaultSign(data, privateKey) {
26
- const curve = new ec('secp256k1');
27
- const keyPair = curve.keyFromPrivate(privateKey);
28
- const hashedDigest = hashWithEthereumPrefix(data);
29
- const sigRaw = curve.sign(hashedDigest, keyPair, {
30
- canonical: true,
31
- pers: undefined
32
- });
33
- if (sigRaw.recoveryParam === null) {
34
- throw new BeeError('signDigest recovery param was null');
35
- }
36
- const signature = new Uint8Array([...sigRaw.r.toArray('be', 32), ...sigRaw.s.toArray('be', 32), sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID]);
37
- return signature;
38
- }
39
- function publicKeyToAddress(pubKey) {
40
- const pubBytes = pubKey.encode('array', false);
41
- return keccak256Hash(pubBytes.slice(1)).slice(12);
42
- }
43
- /**
44
- * Recovers the ethereum address from a given signature.
45
- *
46
- * Can be used for verifying a piece of data when the public key is
47
- * known.
48
- *
49
- * @param signature The signature
50
- * @param digest The digest of the data
51
- *
52
- * @returns the recovered address
53
- */
54
- export function recoverAddress(signature, digest) {
55
- const curve = new ec('secp256k1');
56
- const sig = {
57
- r: signature.slice(0, 32),
58
- s: signature.slice(32, 64)
59
- };
60
- const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
61
- const hash = hashWithEthereumPrefix(digest);
62
- const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
63
- return publicKeyToAddress(recPubKey);
64
- }
65
- /**
66
- * Creates a singer object that can be used when the private key is known.
67
- *
68
- * @param privateKey The private key
69
- */
70
- export function makePrivateKeySigner(privateKey) {
71
- const curve = new ec('secp256k1');
72
- const keyPair = curve.keyFromPrivate(privateKey);
73
- const address = publicKeyToAddress(keyPair.getPublic());
74
- return {
75
- sign: digest => defaultSign(digest, privateKey),
76
- address
77
- };
78
- }
79
- export function assertSigner(signer) {
80
- if (!isStrictlyObject(signer)) {
81
- throw new TypeError('Signer must be an object!');
82
- }
83
- const typedSigner = signer;
84
- if (!isBytes(typedSigner.address, 20)) {
85
- throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
86
- }
87
- if (typeof typedSigner.sign !== 'function') {
88
- throw new TypeError('Signer sign property needs to be function!');
89
- }
90
- }
91
- export function makeSigner(signer) {
92
- if (typeof signer === 'string') {
93
- const hexKey = makeHexString(signer, 64);
94
- const keyBytes = hexToBytes(hexKey); // HexString is verified for 64 length => 32 is guaranteed
95
- return makePrivateKeySigner(keyBytes);
96
- } else if (signer instanceof Uint8Array) {
97
- assertBytes(signer, 32);
98
- return makePrivateKeySigner(signer);
99
- }
100
- assertSigner(signer);
101
- return signer;
102
- }
103
- export async function sign(signer, data) {
104
- const result = await signer.sign(wrapBytesWithHelpers(data));
105
- if (typeof result === 'string') {
106
- const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH);
107
- return hexToBytes(hexString);
108
- }
109
- if (result instanceof Uint8Array) {
110
- assertBytes(result, SIGNATURE_BYTES_LENGTH);
111
- return result;
112
- }
113
- throw new TypeError('Invalid output of sign function!');
114
- }
@@ -1,21 +0,0 @@
1
- import { Binary } from 'cafe-utility';
2
- import { BeeArgumentError } from "../utils/error.js";
3
- export const SPAN_SIZE = 8;
4
- // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
5
- const MAX_SPAN_LENGTH = 2 ** 32 - 1;
6
- /**
7
- * Create a span for storing the length of the chunk
8
- *
9
- * The length is encoded in 64-bit little endian.
10
- *
11
- * @param length The length of the span
12
- */
13
- export function makeSpan(length) {
14
- if (length <= 0) {
15
- throw new BeeArgumentError('invalid length for span', length);
16
- }
17
- if (length > MAX_SPAN_LENGTH) {
18
- throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
19
- }
20
- return Binary.numberToUint64LE(length);
21
- }
@@ -1,26 +0,0 @@
1
- import { isError } from "../utils/type.js";
2
- function serializeJson(data) {
3
- try {
4
- const jsonString = JSON.stringify(data);
5
- return new TextEncoder().encode(jsonString);
6
- } catch (e) {
7
- if (isError(e)) {
8
- e.message = `JsonFeed: ${e.message}`;
9
- }
10
- throw e;
11
- }
12
- }
13
- export async function getJsonData(bee, reader) {
14
- const feedUpdate = await reader.download();
15
- const retrievedData = await bee.downloadData(feedUpdate.reference);
16
- return retrievedData.json();
17
- }
18
- export async function setJsonData(bee, writer, postageBatchId, data, options, requestOptions) {
19
- const serializedData = serializeJson(data);
20
- const {
21
- reference
22
- } = await bee.uploadData(postageBatchId, serializedData, options, requestOptions);
23
- return writer.upload(postageBatchId, reference, {
24
- pin: options?.pin
25
- });
26
- }
@@ -1,19 +0,0 @@
1
- import { keccak256Hash } from "../utils/hash.js";
2
- import { assertBytes } from "../utils/bytes.js";
3
- import { makeHexString, bytesToHex } from "../utils/hex.js";
4
- import { TOPIC_BYTES_LENGTH, TOPIC_HEX_LENGTH } from "../types/index.js";
5
- export function makeTopic(topic) {
6
- if (typeof topic === 'string') {
7
- return makeHexString(topic, TOPIC_HEX_LENGTH);
8
- } else if (topic instanceof Uint8Array) {
9
- assertBytes(topic, TOPIC_BYTES_LENGTH);
10
- return bytesToHex(topic, TOPIC_HEX_LENGTH);
11
- }
12
- throw new TypeError('invalid topic');
13
- }
14
- export function makeTopicFromString(s) {
15
- if (typeof s !== 'string') {
16
- throw new TypeError('topic has to be string!');
17
- }
18
- return bytesToHex(keccak256Hash(s), TOPIC_HEX_LENGTH);
19
- }
@@ -1,10 +0,0 @@
1
- const feedTypes = ['sequence', 'epoch'];
2
- export const DEFAULT_FEED_TYPE = 'sequence';
3
- export function isFeedType(type) {
4
- return typeof type === 'string' && feedTypes.includes(type);
5
- }
6
- export function assertFeedType(type) {
7
- if (!isFeedType(type)) {
8
- throw new TypeError('invalid feed type');
9
- }
10
- }
@@ -1,17 +0,0 @@
1
- import { http } from "../../utils/http.js";
2
- const endpoint = 'chunks';
3
- /**
4
- * Check if chunk at address exists locally
5
- *
6
- * @param requestOptions Options for making requests
7
- * @param address Swarm address of chunk
8
- *
9
- * @returns BeeGenericResponse if chunk is found or throws an exception
10
- */
11
- export async function checkIfChunkExistsLocally(requestOptions, address) {
12
- const response = await http(requestOptions, {
13
- url: `${endpoint}/${address}`,
14
- responseType: 'json'
15
- });
16
- return response.data;
17
- }
@@ -1,15 +0,0 @@
1
- import { http } from "../../utils/http.js";
2
- const endpoint = 'tags';
3
- /**
4
- * Retrieve tag with extended information from Bee node
5
- *
6
- * @param requestOptions Options for making requests
7
- * @param uid UID of tag to be retrieved
8
- */
9
- export async function retrieveExtendedTag(requestOptions, uid) {
10
- const response = await http(requestOptions, {
11
- url: `${endpoint}/${uid}`,
12
- responseType: 'json'
13
- });
14
- return response.data;
15
- }
@@ -1,192 +0,0 @@
1
- // For ESM compatibility
2
- import pkg from 'js-sha3';
3
- const {
4
- keccak256,
5
- sha3_256
6
- } = pkg;
7
- import { assertBytes } from "./bytes.js";
8
- import { assertHexString, hexToBytes, intToHex, makeHexString } from "./hex.js";
9
- const ETH_ADDR_BYTES_LENGTH = 20;
10
- const ETH_ADDR_HEX_LENGTH = 40;
11
- export function capitalizeAddressERC55(address) {
12
- if (address.startsWith('0x')) {
13
- address = address.slice(2);
14
- }
15
- const addressHash = keccak256(address.toLowerCase());
16
- let result = '0x';
17
- for (let i = 0; i < address.length; i++) {
18
- if (parseInt(addressHash[i], 16) > 7) {
19
- result += address[i].toUpperCase();
20
- } else {
21
- result += address[i].toLowerCase();
22
- }
23
- }
24
- return result;
25
- }
26
- export function makeEthAddress(address) {
27
- if (typeof address === 'string') {
28
- const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
29
- const ownerBytes = hexToBytes(hexAddr);
30
- assertBytes(ownerBytes, ETH_ADDR_BYTES_LENGTH);
31
- return ownerBytes;
32
- } else if (address instanceof Uint8Array) {
33
- assertBytes(address, ETH_ADDR_BYTES_LENGTH);
34
- return address;
35
- }
36
- throw new TypeError('Invalid EthAddress');
37
- }
38
- export function makeHexEthAddress(address) {
39
- try {
40
- return makeHexString(address, ETH_ADDR_HEX_LENGTH);
41
- } catch (e) {
42
- if (e instanceof TypeError) {
43
- e.message = `Invalid HexEthAddress: ${e.message}`;
44
- }
45
- throw e;
46
- }
47
- }
48
- /**
49
- * Check if this is all caps or small caps eth address (=address without checksum)
50
- *
51
- * @param address Ethereum address as hex string
52
- */
53
- function isEthAddrCaseIns(address) {
54
- // Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
55
- return typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address));
56
- }
57
- /**
58
- * Check if this is checksummed ethereum address
59
- *
60
- * @param address Ethereum address as hex string
61
- */
62
- function isValidChecksummedEthAddress(address) {
63
- try {
64
- // Check for valid case insensitive hex type string, 40 chars
65
- const addr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
66
- // Check the checksum
67
- const addressHash = keccak256(addr.toLowerCase());
68
- for (let i = 0; i < 40; i += 1) {
69
- // the nth letter should be uppercase if the nth digit of casemap is 1
70
- if (parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i] || parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i]) {
71
- return false;
72
- }
73
- }
74
- return true;
75
- } catch (e) {
76
- if (e instanceof TypeError) {
77
- return false;
78
- }
79
- throw e;
80
- }
81
- }
82
- /**
83
- * Check if is valid ethereum address
84
- *
85
- * Pretty much typed version from web3js
86
- * https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-utils/src/utils.js
87
- *
88
- * @param address Ethereum address as hex string
89
- *
90
- * @return True if is valid eth address
91
- */
92
- export function isHexEthAddress(address) {
93
- return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
94
- }
95
- /**
96
- * Convert big-endian hex or number to little-endian.
97
- * Note: Before conversion it is automatically padded to even length hexstring
98
- *
99
- * @param bigEndian Big-endian hex string or number to convert
100
- * @param pad Length to which the string should be padded before conversion (defaul: 2)
101
- *
102
- * @return little-endian encoded hexstring
103
- */
104
- export function toLittleEndian(bigEndian, pad = 2) {
105
- if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
106
- throw new TypeError('minimal padding for conversion needs to be positive even integer');
107
- }
108
- let hexRep;
109
- if (typeof bigEndian === 'string') hexRep = makeHexString(bigEndian);else if (typeof bigEndian === 'number') hexRep = intToHex(bigEndian);else throw new TypeError('incorrect input type');
110
- hexRep = hexRep.padStart(pad, '0');
111
- // Extend to an even length hexstring
112
- if (hexRep.length % 2 !== 0) hexRep = hexRep.padStart(hexRep.length + 1, '0');
113
- // Match all two pairs in the hexstring, reverse the pairs and join it again
114
- const littleEndian = hexRep.match(/../g)?.reverse().join('');
115
- if (littleEndian) return littleEndian;
116
- throw new Error('failed to convert');
117
- }
118
- /**
119
- * Convert little-endian hex or number to big-endian
120
- * Note: Before conversion it is automatically padded to even length hexstring
121
- *
122
- * @param littleEndian Little-endian hex string or number to convert
123
- * @param pad Length to which the string should be padded before conversion (defaul: 2)
124
- *
125
- * @return big-endian encoded hexstring
126
- */
127
- export function fromLittleEndian(littleEndian, pad = 2) {
128
- // It's a reversible function
129
- return toLittleEndian(littleEndian, pad);
130
- }
131
- function assertEthAddress(ethAddress) {
132
- if (!isHexEthAddress(ethAddress)) throw new TypeError('invalid ETH address');
133
- }
134
- function assertSwarmNetworkId(networkId) {
135
- if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
136
- throw new TypeError('swarm network id must be positive integer');
137
- }
138
- }
139
- /**
140
- * Get swarm overlay address from public ethereum address and swarm network id
141
- *
142
- * @param ethAddress Public ethereum address
143
- * @param networkId Swarm network id
144
- *
145
- * @return Swarm overlay address
146
- */
147
- export function ethToSwarmAddress(ethAddress, networkId = 1) {
148
- assertEthAddress(ethAddress);
149
- assertSwarmNetworkId(networkId);
150
- const hex = `${makeHexString(ethAddress)}${toLittleEndian(networkId, 16)}`;
151
- assertHexString(hex);
152
- const overlayAddress = sha3_256(hexToBytes(hex));
153
- return overlayAddress;
154
- }
155
- /**
156
- * Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
157
- * uses `personal_sign` method to sign requested data.
158
- *
159
- * @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
160
- * @param ethAddress Optional address of the account which the data should be signed with. If not specified `eth_requestAccounts` request is used to get the account address.
161
- */
162
- export async function makeEthereumWalletSigner(provider, ethAddress) {
163
- let executorFnc;
164
- if (typeof provider !== 'object' || provider === null) {
165
- throw new TypeError('We need JsonRPC provider object!');
166
- }
167
- if (provider.request) {
168
- executorFnc = provider.request;
169
- } else if (provider.sendAsync) {
170
- executorFnc = provider.sendAsync;
171
- } else {
172
- throw new Error('Incompatible interface of given provider!');
173
- }
174
- if (!ethAddress) {
175
- ethAddress = (await executorFnc({
176
- method: 'eth_requestAccounts'
177
- }))[0];
178
- }
179
- const bytesEthAddress = makeEthAddress(ethAddress);
180
- const hexEthAddress = makeHexEthAddress(ethAddress);
181
- return {
182
- address: bytesEthAddress,
183
- sign: async data => {
184
- const result = await executorFnc({
185
- jsonrpc: '2.0',
186
- method: 'personal_sign',
187
- params: ['0x' + data.hex(), '0x' + hexEthAddress]
188
- });
189
- return result;
190
- }
191
- };
192
- }
@@ -1,16 +0,0 @@
1
- // For ESM compatibility
2
- import pkg from 'js-sha3';
3
- const {
4
- keccak256
5
- } = pkg;
6
- /**
7
- * Helper function for calculating the keccak256 hash with
8
- * correct types.
9
- *
10
- * @param messages Any number of messages (strings, byte arrays etc.)
11
- */
12
- export function keccak256Hash(...messages) {
13
- const hasher = keccak256.create();
14
- messages.forEach(bytes => hasher.update(bytes));
15
- return Uint8Array.from(hasher.digest());
16
- }
@@ -1,135 +0,0 @@
1
- import { makeBytes } from "./bytes.js";
2
- /**
3
- * Creates unprefixed hex string from wide range of data.
4
- *
5
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
6
- *
7
- * @param input
8
- * @param len of the resulting HexString WITHOUT prefix!
9
- */
10
- export function makeHexString(input, len) {
11
- if (typeof input === 'number') {
12
- return intToHex(input, len);
13
- }
14
- if (input instanceof Uint8Array) {
15
- return bytesToHex(input, len);
16
- }
17
- if (typeof input === 'string') {
18
- if (isPrefixedHexString(input)) {
19
- const hex = input.slice(2);
20
- if (len && hex.length !== len) {
21
- throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`);
22
- }
23
- return hex;
24
- } else {
25
- // We use assertHexString() as there might be more reasons why a string is not valid hex string
26
- // and usage of isHexString() would not give enough information to the user on what is going
27
- // wrong.
28
- assertHexString(input, len);
29
- return input;
30
- }
31
- }
32
- throw new TypeError('Not HexString compatible type!');
33
- }
34
- /**
35
- * Converts a hex string to Uint8Array
36
- *
37
- * @param hex string input without 0x prefix!
38
- */
39
- export function hexToBytes(hex) {
40
- assertHexString(hex);
41
- const bytes = makeBytes(hex.length / 2);
42
- for (let i = 0; i < bytes.length; i++) {
43
- const hexByte = hex.substr(i * 2, 2);
44
- bytes[i] = parseInt(hexByte, 16);
45
- }
46
- return bytes;
47
- }
48
- /**
49
- * Converts array of number or Uint8Array to HexString without prefix.
50
- *
51
- * @param bytes The input array
52
- * @param len The length of the non prefixed HexString
53
- */
54
- export function bytesToHex(bytes, len) {
55
- const hexByte = n => n.toString(16).padStart(2, '0');
56
- const hex = Array.from(bytes, hexByte).join('');
57
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
58
- if (len && hex.length !== len) {
59
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
60
- }
61
- return hex;
62
- }
63
- /**
64
- * Converts integer number to hex string.
65
- *
66
- * Optionally provides '0x' prefix or padding
67
- *
68
- * @param int The positive integer to be converted
69
- * @param len The length of the non prefixed HexString
70
- */
71
- export function intToHex(int, len) {
72
- if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer');
73
- if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer');
74
- if (int < 0) throw new TypeError('the value provided is a negative integer');
75
- const hex = int.toString(16);
76
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
77
- if (len && hex.length !== len) {
78
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`);
79
- }
80
- return hex;
81
- }
82
- /**
83
- * Type guard for HexStrings.
84
- * Requires no 0x prefix!
85
- *
86
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
87
- *
88
- * @param s string input
89
- * @param len expected length of the HexString
90
- */
91
- export function isHexString(s, len) {
92
- return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len);
93
- }
94
- /**
95
- * Type guard for PrefixedHexStrings.
96
- * Does enforce presence of 0x prefix!
97
- *
98
- * @param s string input
99
- */
100
- export function isPrefixedHexString(s) {
101
- return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s);
102
- }
103
- /**
104
- * Verifies if the provided input is a HexString.
105
- *
106
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
107
- *
108
- * @param s string input
109
- * @param len expected length of the HexString
110
- * @param name optional name for the asserted value
111
- * @returns HexString or throws error
112
- */
113
- export function assertHexString(s, len, name = 'value') {
114
- if (!isHexString(s, len)) {
115
- if (isPrefixedHexString(s)) {
116
- throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`);
117
- }
118
- // Don't display length error if no length specified in order not to confuse user
119
- const lengthMsg = len ? ` of length ${len}` : '';
120
- throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`);
121
- }
122
- }
123
- /**
124
- * Verifies if the provided input is a PrefixedHexString.
125
- *
126
- * @param s string input
127
- * @param len expected length of the HexString
128
- * @param name optional name for the asserted value
129
- * @returns HexString or throws error
130
- */
131
- export function assertPrefixedHexString(s, name = 'value') {
132
- if (!isPrefixedHexString(s)) {
133
- throw new TypeError(`${name} not valid prefixed hex string: ${s}`);
134
- }
135
- }
@@ -1,29 +0,0 @@
1
- import { ENCRYPTED_REFERENCE_BYTES_LENGTH, ENCRYPTED_REFERENCE_HEX_LENGTH, REFERENCE_BYTES_LENGTH, REFERENCE_HEX_LENGTH } from "../types/index.js";
2
- import { bytesAtOffset, hasBytesAtOffset } from "./bytes.js";
3
- import { hexToBytes, makeHexString } from "./hex.js";
4
- export function makeBytesReference(reference, offset = 0) {
5
- if (typeof reference === 'string') {
6
- if (offset) {
7
- throw new Error('Offset property can be set only for UintArray reference!');
8
- }
9
- try {
10
- // Non-encrypted chunk hex string reference
11
- const hexReference = makeHexString(reference, REFERENCE_HEX_LENGTH);
12
- return hexToBytes(hexReference);
13
- } catch (e) {
14
- if (!(e instanceof TypeError)) {
15
- throw e;
16
- }
17
- // Encrypted chunk hex string reference
18
- const hexReference = makeHexString(reference, ENCRYPTED_REFERENCE_HEX_LENGTH);
19
- return hexToBytes(hexReference);
20
- }
21
- } else if (reference instanceof Uint8Array) {
22
- if (hasBytesAtOffset(reference, offset, ENCRYPTED_REFERENCE_BYTES_LENGTH)) {
23
- return bytesAtOffset(reference, offset, ENCRYPTED_REFERENCE_BYTES_LENGTH);
24
- } else if (hasBytesAtOffset(reference, offset, REFERENCE_BYTES_LENGTH)) {
25
- return bytesAtOffset(reference, offset, REFERENCE_BYTES_LENGTH);
26
- }
27
- }
28
- throw new TypeError('invalid chunk reference');
29
- }
@@ -1,31 +0,0 @@
1
- import { EthAddress } from '../utils/eth';
2
- import { PrivateKeyBytes, Signature, Signer } from '../types';
3
- /**
4
- * The default signer function that can be used for integrating with
5
- * other applications (e.g. wallets).
6
- *
7
- * @param data The data to be signed
8
- * @param privateKey The private key used for signing the data
9
- */
10
- export declare function defaultSign(data: Uint8Array, privateKey: PrivateKeyBytes): Signature;
11
- /**
12
- * Recovers the ethereum address from a given signature.
13
- *
14
- * Can be used for verifying a piece of data when the public key is
15
- * known.
16
- *
17
- * @param signature The signature
18
- * @param digest The digest of the data
19
- *
20
- * @returns the recovered address
21
- */
22
- export declare function recoverAddress(signature: Signature, digest: Uint8Array): EthAddress;
23
- /**
24
- * Creates a singer object that can be used when the private key is known.
25
- *
26
- * @param privateKey The private key
27
- */
28
- export declare function makePrivateKeySigner(privateKey: PrivateKeyBytes): Signer;
29
- export declare function assertSigner(signer: unknown): asserts signer is Signer;
30
- export declare function makeSigner(signer: Signer | Uint8Array | string | unknown): Signer;
31
- export declare function sign(signer: Signer, data: Uint8Array): Promise<Signature>;