@ethersphere/bee-js 12.3.2-upcoming.116 → 13.0.0-upcoming.gc013bdf

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 (268) hide show
  1. package/dist/cjs/api/balance.js +24 -0
  2. package/dist/cjs/api/batches.js +26 -0
  3. package/dist/cjs/{modules → api}/bytes.js +4 -29
  4. package/dist/cjs/{modules → api}/bzz.js +6 -34
  5. package/dist/cjs/{modules/debug → api}/chequebook.js +48 -87
  6. package/dist/cjs/api/chunk.js +43 -0
  7. package/dist/cjs/api/connectivity.js +86 -0
  8. package/dist/cjs/api/consumed.js +24 -0
  9. package/dist/cjs/{modules → api}/envelope.js +3 -2
  10. package/dist/cjs/api/feed.js +55 -0
  11. package/dist/cjs/api/grantee.js +62 -0
  12. package/dist/cjs/{modules/pinning.js → api/pin.js} +13 -34
  13. package/dist/cjs/api/pss.js +19 -0
  14. package/dist/cjs/api/redistributionstate.js +17 -0
  15. package/dist/cjs/{modules/debug/settlements.js → api/settlement.js} +3 -11
  16. package/dist/cjs/api/soc.js +34 -0
  17. package/dist/cjs/{modules/debug → api}/stake.js +15 -43
  18. package/dist/cjs/{modules/debug/stamps.js → api/stamp.js} +41 -57
  19. package/dist/cjs/api/status.js +67 -0
  20. package/dist/cjs/{modules → api}/stewardship.js +1 -6
  21. package/dist/cjs/api/tag.js +51 -0
  22. package/dist/cjs/{modules/debug/transactions.js → api/transaction.js} +10 -31
  23. package/dist/cjs/{modules/debug/states.js → api/wallet.js} +4 -36
  24. package/dist/cjs/bee.js +115 -1978
  25. package/dist/cjs/chunk/soc.js +23 -65
  26. package/dist/cjs/feed/identifier.js +3 -4
  27. package/dist/cjs/feed/index.js +20 -22
  28. package/dist/cjs/feed/retrievable.js +3 -3
  29. package/dist/cjs/index.js +18 -8
  30. package/dist/cjs/manifest/manifest.js +99 -335
  31. package/dist/cjs/modules/balance.js +86 -0
  32. package/dist/cjs/modules/cheque.js +92 -0
  33. package/dist/cjs/modules/chequebook.js +90 -0
  34. package/dist/cjs/modules/chunk.js +87 -53
  35. package/dist/cjs/modules/collection.js +145 -0
  36. package/dist/cjs/modules/connectivity.js +141 -0
  37. package/dist/cjs/modules/context.js +2 -0
  38. package/dist/cjs/modules/data.js +119 -0
  39. package/dist/cjs/modules/feed.js +94 -70
  40. package/dist/cjs/modules/file.js +113 -0
  41. package/dist/cjs/modules/grantee.js +86 -55
  42. package/dist/cjs/modules/messaging.js +248 -0
  43. package/dist/cjs/modules/pin.js +100 -0
  44. package/dist/cjs/modules/settlement.js +67 -0
  45. package/dist/cjs/modules/soc.js +39 -36
  46. package/dist/cjs/modules/stake.js +110 -0
  47. package/dist/cjs/modules/stamp.js +199 -0
  48. package/dist/cjs/modules/status.js +128 -20
  49. package/dist/cjs/modules/storage.js +191 -0
  50. package/dist/cjs/modules/tag.js +97 -71
  51. package/dist/cjs/modules/transaction.js +92 -0
  52. package/dist/cjs/modules/wallet.js +82 -0
  53. package/dist/cjs/types/schema/chequebook.js +5 -5
  54. package/dist/cjs/types/schema/connectivity.js +5 -5
  55. package/dist/cjs/types/schema/envelope.js +5 -5
  56. package/dist/cjs/types/schema/grantee.js +4 -4
  57. package/dist/cjs/types/schema/stake.js +2 -2
  58. package/dist/cjs/types/schema/stamps.js +4 -4
  59. package/dist/cjs/types/schema/states.js +2 -2
  60. package/dist/cjs/types/schema/transactions.js +3 -3
  61. package/dist/cjs/types/schema/upload.js +2 -2
  62. package/dist/cjs/utils/chunk-stream.browser.js +24 -16
  63. package/dist/cjs/utils/chunk-stream.js +17 -9
  64. package/dist/cjs/utils/constants.js +3 -3
  65. package/dist/cjs/utils/expose.js +4 -4
  66. package/dist/cjs/utils/headers.js +4 -4
  67. package/dist/cjs/utils/pss.js +2 -2
  68. package/dist/cjs/utils/resource-locator.js +2 -2
  69. package/dist/cjs/utils/schema.js +4 -4
  70. package/dist/cjs/utils/stamps.js +15 -130
  71. package/dist/index.browser.min.js +2 -1
  72. package/dist/index.browser.min.js.LICENSE.txt +3 -0
  73. package/dist/index.browser.min.js.map +1 -1
  74. package/dist/mjs/api/balance.js +20 -0
  75. package/dist/mjs/api/batches.js +22 -0
  76. package/dist/mjs/{modules → api}/bytes.js +2 -27
  77. package/dist/mjs/{modules → api}/bzz.js +2 -30
  78. package/dist/mjs/{modules/debug → api}/chequebook.js +50 -89
  79. package/dist/mjs/api/chunk.js +37 -0
  80. package/dist/mjs/api/connectivity.js +76 -0
  81. package/dist/mjs/api/consumed.js +20 -0
  82. package/dist/mjs/{modules → api}/envelope.js +3 -2
  83. package/dist/mjs/api/feed.js +52 -0
  84. package/dist/mjs/api/grantee.js +59 -0
  85. package/dist/mjs/{modules/pinning.js → api/pin.js} +14 -35
  86. package/dist/mjs/api/pss.js +18 -0
  87. package/dist/mjs/api/redistributionstate.js +14 -0
  88. package/dist/mjs/{modules/debug/settlements.js → api/settlement.js} +3 -11
  89. package/dist/mjs/api/soc.js +31 -0
  90. package/dist/mjs/{modules/debug → api}/stake.js +14 -41
  91. package/dist/mjs/{modules/debug/stamps.js → api/stamp.js} +37 -51
  92. package/dist/mjs/api/status.js +59 -0
  93. package/dist/mjs/{modules → api}/stewardship.js +1 -6
  94. package/dist/mjs/api/tag.js +49 -0
  95. package/dist/mjs/api/transaction.js +41 -0
  96. package/dist/mjs/api/wallet.js +38 -0
  97. package/dist/mjs/bee.js +115 -1965
  98. package/dist/mjs/chunk/soc.js +13 -51
  99. package/dist/mjs/feed/identifier.js +2 -3
  100. package/dist/mjs/feed/index.js +9 -11
  101. package/dist/mjs/feed/retrievable.js +2 -2
  102. package/dist/mjs/index.js +2 -5
  103. package/dist/mjs/manifest/manifest.js +97 -341
  104. package/dist/mjs/modules/balance.js +49 -0
  105. package/dist/mjs/modules/cheque.js +55 -0
  106. package/dist/mjs/modules/chequebook.js +65 -0
  107. package/dist/mjs/modules/chunk.js +52 -49
  108. package/dist/mjs/modules/collection.js +108 -0
  109. package/dist/mjs/modules/connectivity.js +102 -0
  110. package/dist/mjs/modules/context.js +1 -0
  111. package/dist/mjs/modules/data.js +82 -0
  112. package/dist/mjs/modules/feed.js +89 -67
  113. package/dist/mjs/modules/file.js +78 -0
  114. package/dist/mjs/modules/grantee.js +52 -55
  115. package/dist/mjs/modules/messaging.js +213 -0
  116. package/dist/mjs/modules/pin.js +63 -0
  117. package/dist/mjs/modules/settlement.js +30 -0
  118. package/dist/mjs/modules/soc.js +36 -34
  119. package/dist/mjs/modules/stake.js +76 -0
  120. package/dist/mjs/modules/stamp.js +170 -0
  121. package/dist/mjs/modules/status.js +98 -18
  122. package/dist/mjs/modules/storage.js +185 -0
  123. package/dist/mjs/modules/tag.js +61 -68
  124. package/dist/mjs/modules/transaction.js +58 -0
  125. package/dist/mjs/modules/wallet.js +45 -0
  126. package/dist/mjs/types/schema/chequebook.js +1 -1
  127. package/dist/mjs/types/schema/connectivity.js +1 -1
  128. package/dist/mjs/types/schema/envelope.js +5 -5
  129. package/dist/mjs/types/schema/grantee.js +1 -1
  130. package/dist/mjs/types/schema/stake.js +1 -1
  131. package/dist/mjs/types/schema/stamps.js +1 -1
  132. package/dist/mjs/types/schema/states.js +1 -1
  133. package/dist/mjs/types/schema/transactions.js +1 -1
  134. package/dist/mjs/types/schema/upload.js +1 -1
  135. package/dist/mjs/utils/chunk-stream.browser.js +28 -18
  136. package/dist/mjs/utils/chunk-stream.js +21 -11
  137. package/dist/mjs/utils/constants.js +1 -1
  138. package/dist/mjs/utils/expose.js +1 -1
  139. package/dist/mjs/utils/headers.js +1 -1
  140. package/dist/mjs/utils/pss.js +1 -1
  141. package/dist/mjs/utils/resource-locator.js +1 -1
  142. package/dist/mjs/utils/schema.js +1 -1
  143. package/dist/mjs/utils/stamps.js +5 -99
  144. package/dist/types/api/balance.d.ts +7 -0
  145. package/dist/types/api/batches.d.ts +7 -0
  146. package/dist/types/api/bytes.d.ts +11 -0
  147. package/dist/types/api/bzz.d.ts +12 -0
  148. package/dist/types/api/chequebook.d.ts +21 -0
  149. package/dist/types/api/chunk.d.ts +7 -0
  150. package/dist/types/api/connectivity.d.ts +22 -0
  151. package/dist/types/api/consumed.d.ts +7 -0
  152. package/dist/types/{modules → api}/envelope.d.ts +1 -1
  153. package/dist/types/api/feed.d.ts +40 -0
  154. package/dist/types/api/grantee.d.ts +11 -0
  155. package/dist/types/api/pin.d.ts +10 -0
  156. package/dist/types/api/pss.d.ts +6 -0
  157. package/dist/types/api/redistributionstate.d.ts +5 -0
  158. package/dist/types/api/settlement.d.ts +7 -0
  159. package/dist/types/api/soc.d.ts +6 -0
  160. package/dist/types/api/stake.d.ts +11 -0
  161. package/dist/types/api/stamp.d.ts +12 -0
  162. package/dist/types/api/status.d.ts +11 -0
  163. package/dist/types/api/stewardship.d.ts +7 -0
  164. package/dist/types/api/tag.d.ts +12 -0
  165. package/dist/types/api/transaction.d.ts +9 -0
  166. package/dist/types/api/wallet.d.ts +9 -0
  167. package/dist/types/bee.d.ts +71 -1199
  168. package/dist/types/chunk/soc.d.ts +3 -48
  169. package/dist/types/feed/identifier.d.ts +1 -1
  170. package/dist/types/feed/index.d.ts +2 -4
  171. package/dist/types/feed/retrievable.d.ts +1 -1
  172. package/dist/types/index.d.ts +17 -20
  173. package/dist/types/manifest/manifest.d.ts +25 -34
  174. package/dist/types/modules/balance.d.ts +38 -0
  175. package/dist/types/modules/cheque.d.ts +40 -0
  176. package/dist/types/modules/chequebook.d.ts +41 -0
  177. package/dist/types/modules/chunk.d.ts +30 -20
  178. package/dist/types/modules/collection.d.ts +77 -0
  179. package/dist/types/modules/connectivity.d.ts +74 -0
  180. package/dist/types/modules/context.d.ts +24 -0
  181. package/dist/types/modules/data.d.ts +55 -0
  182. package/dist/types/modules/feed.d.ts +47 -49
  183. package/dist/types/modules/file.d.ts +43 -0
  184. package/dist/types/modules/grantee.d.ts +40 -8
  185. package/dist/types/modules/messaging.d.ts +79 -0
  186. package/dist/types/modules/pin.d.ts +47 -0
  187. package/dist/types/modules/settlement.d.ts +25 -0
  188. package/dist/types/modules/soc.d.ts +23 -11
  189. package/dist/types/modules/stake.d.ts +55 -0
  190. package/dist/types/modules/stamp.d.ts +96 -0
  191. package/dist/types/modules/status.d.ts +66 -5
  192. package/dist/types/modules/storage.d.ts +107 -0
  193. package/dist/types/modules/tag.d.ts +44 -33
  194. package/dist/types/modules/transaction.d.ts +42 -0
  195. package/dist/types/modules/wallet.d.ts +35 -0
  196. package/dist/types/types/debug.d.ts +1 -1
  197. package/dist/types/types/index.d.ts +3 -12
  198. package/dist/types/types/schema/chequebook.d.ts +1 -1
  199. package/dist/types/types/schema/connectivity.d.ts +1 -1
  200. package/dist/types/types/schema/grantee.d.ts +1 -1
  201. package/dist/types/types/schema/stake.d.ts +1 -1
  202. package/dist/types/types/schema/stamps.d.ts +1 -1
  203. package/dist/types/types/schema/states.d.ts +1 -1
  204. package/dist/types/types/schema/transactions.d.ts +1 -1
  205. package/dist/types/types/schema/upload.d.ts +1 -1
  206. package/dist/types/utils/chunk-stream.browser.d.ts +1 -1
  207. package/dist/types/utils/chunk-stream.d.ts +2 -2
  208. package/dist/types/utils/constants.d.ts +1 -1
  209. package/dist/types/utils/expose.d.ts +1 -1
  210. package/dist/types/utils/headers.d.ts +1 -1
  211. package/dist/types/utils/pss.d.ts +1 -1
  212. package/dist/types/utils/resource-locator.d.ts +1 -1
  213. package/dist/types/utils/schema.d.ts +1 -1
  214. package/dist/types/utils/stamps.d.ts +2 -27
  215. package/dist/types/utils/tar-uploader.browser.d.ts +1 -1
  216. package/dist/types/utils/tar-uploader.d.ts +1 -1
  217. package/package.json +4 -2
  218. package/dist/cjs/chunk/bmt.js +0 -37
  219. package/dist/cjs/chunk/cac.js +0 -43
  220. package/dist/cjs/modules/debug/balance.js +0 -60
  221. package/dist/cjs/modules/debug/connectivity.js +0 -54
  222. package/dist/cjs/modules/debug/status.js +0 -104
  223. package/dist/cjs/modules/gsoc.js +0 -23
  224. package/dist/cjs/modules/pss.js +0 -48
  225. package/dist/cjs/stamper/stamper.js +0 -44
  226. package/dist/cjs/utils/bytes.js +0 -116
  227. package/dist/cjs/utils/cid.js +0 -37
  228. package/dist/cjs/utils/redundancy.js +0 -133
  229. package/dist/cjs/utils/typed-bytes.js +0 -186
  230. package/dist/mjs/chunk/bmt.js +0 -34
  231. package/dist/mjs/chunk/cac.js +0 -34
  232. package/dist/mjs/modules/debug/balance.js +0 -54
  233. package/dist/mjs/modules/debug/connectivity.js +0 -46
  234. package/dist/mjs/modules/debug/states.js +0 -68
  235. package/dist/mjs/modules/debug/status.js +0 -99
  236. package/dist/mjs/modules/debug/transactions.js +0 -62
  237. package/dist/mjs/modules/gsoc.js +0 -16
  238. package/dist/mjs/modules/pss.js +0 -43
  239. package/dist/mjs/stamper/stamper.js +0 -40
  240. package/dist/mjs/utils/bytes.js +0 -102
  241. package/dist/mjs/utils/cid.js +0 -32
  242. package/dist/mjs/utils/redundancy.js +0 -98
  243. package/dist/mjs/utils/typed-bytes.js +0 -167
  244. package/dist/types/chunk/bmt.d.ts +0 -16
  245. package/dist/types/chunk/cac.d.ts +0 -40
  246. package/dist/types/modules/bytes.d.ts +0 -35
  247. package/dist/types/modules/bzz.d.ts +0 -40
  248. package/dist/types/modules/debug/balance.d.ts +0 -28
  249. package/dist/types/modules/debug/chequebook.d.ts +0 -60
  250. package/dist/types/modules/debug/connectivity.d.ts +0 -9
  251. package/dist/types/modules/debug/settlements.d.ts +0 -15
  252. package/dist/types/modules/debug/stake.d.ts +0 -30
  253. package/dist/types/modules/debug/stamps.d.ts +0 -11
  254. package/dist/types/modules/debug/states.d.ts +0 -23
  255. package/dist/types/modules/debug/status.d.ts +0 -46
  256. package/dist/types/modules/debug/transactions.d.ts +0 -30
  257. package/dist/types/modules/gsoc.d.ts +0 -6
  258. package/dist/types/modules/pinning.d.ts +0 -30
  259. package/dist/types/modules/pss.d.ts +0 -22
  260. package/dist/types/modules/stewardship.d.ts +0 -10
  261. package/dist/types/stamper/stamper.d.ts +0 -15
  262. package/dist/types/utils/bytes.d.ts +0 -19
  263. package/dist/types/utils/cid.d.ts +0 -10
  264. package/dist/types/utils/redundancy.d.ts +0 -21
  265. package/dist/types/utils/typed-bytes.d.ts +0 -69
  266. /package/dist/cjs/{modules → api}/rchash.js +0 -0
  267. /package/dist/mjs/{modules → api}/rchash.js +0 -0
  268. /package/dist/types/{modules → api}/rchash.d.ts +0 -0
@@ -0,0 +1,78 @@
1
+ import { BatchId, Reference } from 'swarm-core';
2
+ import { fileArrayBuffer, isFile } from "../utils/file.js";
3
+ import { ResourceLocator } from "../utils/resource-locator.js";
4
+ import { DownloadOptionsSchema, FileUploadOptionsSchema } from "../utils/schema.js";
5
+ import { assertFileData } from "../utils/type.js";
6
+ import * as api from "../api/bzz.js";
7
+ /**
8
+ * Single-file operations backed by the `/bzz` endpoint.
9
+ *
10
+ * Accessed as `bee.file`.
11
+ */
12
+ export class File {
13
+ constructor(context) {
14
+ this.context = context;
15
+ }
16
+ /**
17
+ * Uploads a single file to a Bee node.
18
+ *
19
+ * To download the file, use {@link download}.
20
+ *
21
+ * @param postageBatchId Postage BatchId to be used to upload the data with
22
+ * @param data Data or file to be uploaded
23
+ * @param name Optional name of the uploaded file
24
+ * @param options Additional options like tag, encryption, pinning, content-type and request options
25
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
26
+ */
27
+ async upload(postageBatchId, data, name, options, requestOptions) {
28
+ const batchId = new BatchId(postageBatchId);
29
+ assertFileData(data);
30
+ if (options) {
31
+ options = FileUploadOptionsSchema.parse(options);
32
+ }
33
+ if (name && typeof name !== 'string') {
34
+ throw new TypeError('name has to be string or undefined!');
35
+ }
36
+ if (isFile(data)) {
37
+ const fileData = await fileArrayBuffer(data);
38
+ const fileName = name ?? data.name;
39
+ const contentType = data.type;
40
+ const fileOptions = {
41
+ ...options,
42
+ contentType
43
+ };
44
+ return api.uploadFile(this.context.getRequestOptionsForCall(requestOptions), fileData, batchId, fileName, fileOptions);
45
+ } else {
46
+ return api.uploadFile(this.context.getRequestOptionsForCall(requestOptions), data, batchId, name, options);
47
+ }
48
+ }
49
+ /**
50
+ * Downloads a single file.
51
+ *
52
+ * @param resource Swarm reference, Swarm CID, or ENS domain.
53
+ * @param path If reference points to manifest, then this parameter defines path to the file
54
+ * @param options Options that affects the request behavior
55
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
56
+ */
57
+ async download(resource, path = '', options, requestOptions) {
58
+ if (options) {
59
+ options = DownloadOptionsSchema.parse(options);
60
+ }
61
+ return api.downloadFile(this.context.getRequestOptionsForCall(requestOptions), new ResourceLocator(resource), path, options);
62
+ }
63
+ /**
64
+ * Downloads a single file as a readable stream.
65
+ *
66
+ * @param reference Bee file reference in hex string (either 64 or 128 chars long), ENS domain or Swarm CID.
67
+ * @param path If reference points to manifest / collections, then this parameter defines path to the file
68
+ * @param options Options that affects the request behavior
69
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
70
+ */
71
+ async downloadReadable(reference, path = '', options, requestOptions) {
72
+ const ref = new Reference(reference);
73
+ if (options) {
74
+ options = DownloadOptionsSchema.parse(options);
75
+ }
76
+ return api.downloadFileReadable(this.context.getRequestOptionsForCall(requestOptions), ref, path, options);
77
+ }
78
+ }
@@ -1,56 +1,53 @@
1
- import { GetGranteesBodyResponse, GranteesResultBodyResponse } from "../types/schema/grantee.js";
2
- import { prepareRequestHeaders } from "../utils/headers.js";
3
- import { http } from "../utils/http.js";
4
- const granteeEndpoint = 'grantee';
5
- export async function getGrantees(reference, requestOptions) {
6
- const response = await http(requestOptions, {
7
- method: 'get',
8
- url: `${granteeEndpoint}/${reference}`,
9
- responseType: 'json'
10
- });
11
- return {
12
- status: response.status,
13
- statusText: response.statusText,
14
- grantees: GetGranteesBodyResponse.parse(response.data)
15
- };
16
- }
17
- export async function createGrantees(requestOptions, postageBatchId, grantees) {
18
- const response = await http(requestOptions, {
19
- method: 'post',
20
- url: granteeEndpoint,
21
- data: {
22
- grantees: grantees.map(x => x.toCompressedHex())
23
- },
24
- headers: prepareRequestHeaders(postageBatchId),
25
- responseType: 'json'
26
- });
27
- const body = GranteesResultBodyResponse.parse(response.data);
28
- return {
29
- status: response.status,
30
- statusText: response.statusText,
31
- ref: body.ref,
32
- historyref: body.historyref
33
- };
34
- }
35
- export async function patchGrantees(postageBatchId, reference, historyRef, grantees, requestOptions) {
36
- const response = await http(requestOptions, {
37
- method: 'patch',
38
- url: `${granteeEndpoint}/${reference}`,
39
- data: {
40
- add: grantees.add?.map(x => x.toCompressedHex()),
41
- revoke: grantees.revoke?.map(x => x.toCompressedHex())
42
- },
43
- headers: {
44
- ...prepareRequestHeaders(postageBatchId),
45
- 'swarm-act-history-address': historyRef.toHex()
46
- },
47
- responseType: 'json'
48
- });
49
- const body = GranteesResultBodyResponse.parse(response.data);
50
- return {
51
- status: response.status,
52
- statusText: response.statusText,
53
- ref: body.ref,
54
- historyref: body.historyref
55
- };
1
+ import { BatchId, PublicKey, Reference } from 'swarm-core';
2
+ import * as api from "../api/grantee.js";
3
+ /**
4
+ * Grantee (access control) operations.
5
+ *
6
+ * Accessed as `bee.grantee`.
7
+ */
8
+ export class Grantee {
9
+ constructor(context) {
10
+ this.context = context;
11
+ }
12
+ /**
13
+ * Creates grantees for a postage batch.
14
+ *
15
+ * @param postageBatchId
16
+ * @param grantees Public keys of the grantees.
17
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
18
+ */
19
+ async create(postageBatchId, grantees, requestOptions) {
20
+ const batchId = new BatchId(postageBatchId);
21
+ const publicKeys = grantees.map(x => new PublicKey(x));
22
+ return api.createGrantees(this.context.getRequestOptionsForCall(requestOptions), batchId, publicKeys);
23
+ }
24
+ /**
25
+ * Retrieves the grantees for a given reference.
26
+ *
27
+ * @param reference The reference.
28
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
29
+ */
30
+ async get(reference, requestOptions) {
31
+ const ref = new Reference(reference);
32
+ return api.getGrantees(this.context.getRequestOptionsForCall(requestOptions), ref);
33
+ }
34
+ /**
35
+ * Updates the grantees of a specific reference and history.
36
+ *
37
+ * @param postageBatchId The ID of the postage batch.
38
+ * @param reference The reference.
39
+ * @param history The history.
40
+ * @param grantees The grantees to add and/or revoke.
41
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
42
+ */
43
+ async patch(postageBatchId, reference, history, grantees, requestOptions) {
44
+ const batchId = new BatchId(postageBatchId);
45
+ const ref = new Reference(reference);
46
+ const historyRef = new Reference(history);
47
+ const publicKeys = {
48
+ add: grantees.add?.map(x => new PublicKey(x)) ?? [],
49
+ revoke: grantees.revoke?.map(x => new PublicKey(x)) ?? []
50
+ };
51
+ return api.patchGrantees(this.context.getRequestOptionsForCall(requestOptions), batchId, ref, historyRef, publicKeys);
52
+ }
56
53
  }
@@ -0,0 +1,213 @@
1
+ import { Binary, System } from 'cafe-utility';
2
+ import WebSocket from 'isomorphic-ws';
3
+ import { BatchId, Bytes, EthAddress, Identifier, makeContentAddressedChunk, numberToUint256, PeerAddress, PrivateKey, PublicKey } from 'swarm-core';
4
+ import * as pssApi from "../api/pss.js";
5
+ import { makeSOCAddress, uploadSingleOwnerChunk } from "../chunk/soc.js";
6
+ import { prepareWebsocketData } from "../utils/data.js";
7
+ import { BeeError } from "../utils/error.js";
8
+ import { GsocMessageHandlerSchema, PssMessageHandlerSchema } from "../utils/schema.js";
9
+ import { assertData } from "../utils/type.js";
10
+ const PSS_ENDPOINT = 'pss';
11
+ const GSOC_ENDPOINT = 'gsoc';
12
+ /**
13
+ * Messaging protocols — PSS (Postal Service for Swarm) and GSOC. Method names keep
14
+ * their `pss`/`gsoc` prefixes to distinguish the two protocols within the namespace.
15
+ *
16
+ * Accessed as `bee.messaging`.
17
+ */
18
+ export class Messaging {
19
+ constructor(context) {
20
+ this.context = context;
21
+ this.cancelled = new WeakSet();
22
+ }
23
+ /**
24
+ * Sends data to a recipient or target with the Postal Service for Swarm.
25
+ *
26
+ * **Warning! Only full nodes can accept PSS messages.**
27
+ *
28
+ * @param postageBatchId Postage BatchId that will be assigned to the sent message.
29
+ * @param topic Topic name
30
+ * @param target Target message address prefix
31
+ * @param data Message to send
32
+ * @param recipient Optional recipient public key
33
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
34
+ */
35
+ async pssSend(postageBatchId, topic, target, data, recipient, requestOptions) {
36
+ const batchId = new BatchId(postageBatchId);
37
+ assertData(data);
38
+ const recipientKey = recipient ? new PublicKey(recipient) : undefined;
39
+ await pssApi.send(this.context.getRequestOptionsForCall(requestOptions), batchId, topic, target, data, recipientKey);
40
+ }
41
+ /**
42
+ * Subscribes to messages for the given topic with the Postal Service for Swarm.
43
+ *
44
+ * **Warning! Only full nodes can accept PSS messages.**
45
+ *
46
+ * @param topic Topic name
47
+ * @param handler Message handler interface
48
+ */
49
+ pssSubscribe(topic, handler) {
50
+ handler = PssMessageHandlerSchema.parse(handler);
51
+ const ws = this.openWebSocket(`${PSS_ENDPOINT}/subscribe/${topic.toHex()}`);
52
+ const cancel = this.makeCancel(ws);
53
+ const subscription = {
54
+ topic,
55
+ cancel
56
+ };
57
+ ws.onmessage = async event => {
58
+ const data = await prepareWebsocketData(event.data);
59
+ if (data.length) {
60
+ handler.onMessage(new Bytes(data), subscription);
61
+ }
62
+ };
63
+ ws.onerror = event => {
64
+ if (!this.isCancelled(ws)) {
65
+ handler.onError(new BeeError(event.message), subscription);
66
+ }
67
+ };
68
+ ws.onclose = () => {
69
+ handler.onClose(subscription);
70
+ };
71
+ return subscription;
72
+ }
73
+ /**
74
+ * Receives a single message using the Postal Service for Swarm.
75
+ *
76
+ * **Warning! Only full nodes can accept PSS messages.**
77
+ *
78
+ * @param topic Topic name
79
+ * @param timeoutMsec Timeout in milliseconds
80
+ */
81
+ async pssReceive(topic, timeoutMsec = 0) {
82
+ if (typeof timeoutMsec !== 'number') {
83
+ throw new TypeError('timeoutMsc parameter has to be a number!');
84
+ }
85
+ return new Promise((resolve, reject) => {
86
+ let timeout;
87
+ const subscription = this.pssSubscribe(topic, {
88
+ onError: error => {
89
+ clearTimeout(timeout);
90
+ subscription.cancel();
91
+ reject(error.message);
92
+ },
93
+ onMessage: message => {
94
+ clearTimeout(timeout);
95
+ subscription.cancel();
96
+ resolve(message);
97
+ },
98
+ onClose: () => {
99
+ clearTimeout(timeout);
100
+ subscription.cancel();
101
+ }
102
+ });
103
+ if (timeoutMsec > 0) {
104
+ timeout = setTimeout(() => {
105
+ subscription.cancel();
106
+ reject(new BeeError('pssReceive timeout'));
107
+ }, timeoutMsec);
108
+ }
109
+ });
110
+ }
111
+ /**
112
+ * Mines the signer (a private key) to be used to send GSOC messages to the specific target overlay address.
113
+ *
114
+ * @param targetOverlay
115
+ * @param identifier
116
+ * @param proximity
117
+ */
118
+ gsocMine(targetOverlay, identifier, proximity = 12) {
119
+ const overlay = new PeerAddress(targetOverlay);
120
+ const id = new Identifier(identifier);
121
+ const start = 0xb33n;
122
+ for (let i = 0n; i < 0xffffn; i++) {
123
+ const signer = new PrivateKey(numberToUint256(start + i, 'BE'));
124
+ const socAddress = makeSOCAddress(id, signer.publicKey().address());
125
+ // TODO: test the significance of the hardcoded 256
126
+ const actualProximity = 256 - Binary.proximity(socAddress.toUint8Array(), overlay.toUint8Array());
127
+ if (actualProximity <= 256 - proximity) {
128
+ return signer;
129
+ }
130
+ }
131
+ throw Error('Could not mine a valid signer');
132
+ }
133
+ /**
134
+ * Sends a GSOC message with the specified signer and identifier.
135
+ *
136
+ * **Warning! Only full nodes can accept GSOC messages.**
137
+ *
138
+ * @param postageBatchId
139
+ * @param signer
140
+ * @param identifier
141
+ * @param data
142
+ * @param options
143
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
144
+ */
145
+ async gsocSend(postageBatchId, signer, identifier, data, options, requestOptions) {
146
+ const batchId = new BatchId(postageBatchId);
147
+ const key = new PrivateKey(signer);
148
+ const id = new Identifier(identifier);
149
+ const cac = makeContentAddressedChunk(data);
150
+ const soc = cac.toSingleOwnerChunk(id, key);
151
+ return uploadSingleOwnerChunk(this.context.getRequestOptionsForCall(requestOptions), soc, batchId, options);
152
+ }
153
+ /**
154
+ * Subscribes to GSOC messages for the specified owner (of the signer) and identifier.
155
+ *
156
+ * **Warning! Only full nodes can accept GSOC messages.**
157
+ *
158
+ * @param address
159
+ * @param identifier
160
+ * @param handler
161
+ */
162
+ gsocSubscribe(address, identifier, handler) {
163
+ const ethAddress = new EthAddress(address);
164
+ const id = new Identifier(identifier);
165
+ handler = GsocMessageHandlerSchema.parse(handler);
166
+ const socAddress = makeSOCAddress(id, ethAddress);
167
+ const ws = this.openWebSocket(`${GSOC_ENDPOINT}/subscribe/${socAddress.toHex()}`);
168
+ const cancel = this.makeCancel(ws);
169
+ const subscription = {
170
+ address: ethAddress,
171
+ cancel
172
+ };
173
+ ws.onmessage = async event => {
174
+ const data = await prepareWebsocketData(event.data);
175
+ if (data.length) {
176
+ handler.onMessage(new Bytes(data), subscription);
177
+ }
178
+ };
179
+ ws.onerror = event => {
180
+ if (!this.isCancelled(ws)) {
181
+ handler.onError(new BeeError(event.message), subscription);
182
+ }
183
+ };
184
+ ws.onclose = () => {
185
+ handler.onClose(subscription);
186
+ };
187
+ return subscription;
188
+ }
189
+ openWebSocket(path) {
190
+ const wsUrl = this.context.url.replace(/^http/i, 'ws');
191
+ if (System.whereAmI() === 'browser') {
192
+ return new WebSocket(`${wsUrl}/${path}`);
193
+ }
194
+ return new WebSocket(`${wsUrl}/${path}`, {
195
+ headers: this.context.getRequestOptionsForCall().headers
196
+ });
197
+ }
198
+ makeCancel(ws) {
199
+ return () => {
200
+ if (!this.cancelled.has(ws)) {
201
+ this.cancelled.add(ws);
202
+ if (ws.terminate) {
203
+ ws.terminate();
204
+ } else {
205
+ ws.close();
206
+ }
207
+ }
208
+ };
209
+ }
210
+ isCancelled(ws) {
211
+ return this.cancelled.has(ws);
212
+ }
213
+ }
@@ -0,0 +1,63 @@
1
+ import { BatchId, Reference } from 'swarm-core';
2
+ import * as pinApi from "../api/pin.js";
3
+ import * as stewardshipApi from "../api/stewardship.js";
4
+ /**
5
+ * Local pinning operations.
6
+ *
7
+ * Accessed as `bee.pin`.
8
+ */
9
+ export class Pin {
10
+ constructor(context) {
11
+ this.context = context;
12
+ }
13
+ /**
14
+ * Pins local data with the given reference.
15
+ *
16
+ * @param reference Data reference
17
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
18
+ */
19
+ async add(reference, requestOptions) {
20
+ const ref = new Reference(reference);
21
+ await pinApi.pin(this.context.getRequestOptionsForCall(requestOptions), ref);
22
+ }
23
+ /**
24
+ * Unpins local data with the given reference.
25
+ *
26
+ * @param reference Data reference
27
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
28
+ */
29
+ async remove(reference, requestOptions) {
30
+ const ref = new Reference(reference);
31
+ await pinApi.unpin(this.context.getRequestOptionsForCall(requestOptions), ref);
32
+ }
33
+ /**
34
+ * Gets the list of all locally pinned references.
35
+ *
36
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
37
+ */
38
+ async getAll(requestOptions) {
39
+ return pinApi.getAllPins(this.context.getRequestOptionsForCall(requestOptions));
40
+ }
41
+ /**
42
+ * Gets the pinning status of the chunk with the given reference.
43
+ *
44
+ * @param reference Data reference
45
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
46
+ */
47
+ async get(reference, requestOptions) {
48
+ const ref = new Reference(reference);
49
+ return pinApi.getPin(this.context.getRequestOptionsForCall(requestOptions), ref);
50
+ }
51
+ /**
52
+ * Instructs the Bee node to reupload locally pinned data into the network.
53
+ *
54
+ * @param postageBatchId Postage Batch ID that will be used to re-upload the data.
55
+ * @param reference Data reference to be re-uploaded.
56
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
57
+ */
58
+ async reuploadData(postageBatchId, reference, requestOptions) {
59
+ const batchId = new BatchId(postageBatchId);
60
+ const ref = new Reference(reference);
61
+ await stewardshipApi.reupload(this.context.getRequestOptionsForCall(requestOptions), batchId, ref);
62
+ }
63
+ }
@@ -0,0 +1,30 @@
1
+ import { PeerAddress } from 'swarm-core';
2
+ import * as api from "../api/settlement.js";
3
+ /**
4
+ * Settlement operations. Related to the bandwidth incentives and the chequebook.
5
+ *
6
+ * Accessed as `bee.settlement`.
7
+ */
8
+ export class Settlement {
9
+ constructor(context) {
10
+ this.context = context;
11
+ }
12
+ /**
13
+ * Gets the amount of sent and received micropayments from settlements with a peer.
14
+ *
15
+ * @param address Swarm address of peer
16
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
17
+ */
18
+ async get(address, requestOptions) {
19
+ const peer = new PeerAddress(address);
20
+ return api.getSettlements(this.context.getRequestOptionsForCall(requestOptions), peer);
21
+ }
22
+ /**
23
+ * Gets settlements with all known peers and total amount sent or received.
24
+ *
25
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
26
+ */
27
+ async getAll(requestOptions) {
28
+ return api.getAllSettlements(this.context.getRequestOptionsForCall(requestOptions));
29
+ }
30
+ }
@@ -1,39 +1,41 @@
1
- import { Optional } from 'cafe-utility';
2
- import { UploadResultBody } from "../types/schema/upload.js";
3
- import { prepareRequestHeaders } from "../utils/headers.js";
4
- import { http } from "../utils/http.js";
5
- import { makeTagUid } from "../utils/type.js";
6
- import { Reference } from "../utils/typed-bytes.js";
7
- const socEndpoint = 'soc';
1
+ import { EthAddress, PrivateKey } from 'swarm-core';
2
+ import { downloadSingleOwnerChunk, uploadSingleOwnerChunkData } from "../chunk/soc.js";
8
3
  /**
9
- * Upload single owner chunk (SOC) to a Bee node
4
+ * Single owner chunk (SOC) reader/writer operations.
10
5
  *
11
- * @param requestOptions Options for making requests
12
- * @param owner Owner's ethereum address in hex
13
- * @param identifier Arbitrary identifier in hex
14
- * @param signature Signature in hex
15
- * @param data Content addressed chunk data to be uploaded
16
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
17
- * @param options Additional options like tag, encryption, pinning
6
+ * Accessed as `bee.soc`.
18
7
  */
19
- export async function upload(requestOptions, owner, identifier, signature, data, stamp, options) {
20
- const response = await http(requestOptions, {
21
- method: 'post',
22
- url: `${socEndpoint}/${owner}/${identifier}`,
23
- data,
24
- headers: {
25
- 'content-type': 'application/octet-stream',
26
- ...prepareRequestHeaders(stamp, options)
27
- },
28
- responseType: 'json',
29
- params: {
30
- sig: signature.toHex()
8
+ export class Soc {
9
+ constructor(context) {
10
+ this.context = context;
11
+ }
12
+ /**
13
+ * Returns an object for reading single owner chunks.
14
+ *
15
+ * @param ownerAddress The ethereum address of the owner
16
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
17
+ */
18
+ makeReader(ownerAddress, requestOptions) {
19
+ const owner = new EthAddress(ownerAddress);
20
+ return {
21
+ owner,
22
+ download: downloadSingleOwnerChunk.bind(null, this.context.getRequestOptionsForCall(requestOptions), owner)
23
+ };
24
+ }
25
+ /**
26
+ * Returns an object for reading and writing single owner chunks.
27
+ *
28
+ * @param signer The signer's private key. Falls back to the Bee instance signer.
29
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
30
+ */
31
+ makeWriter(signer, requestOptions) {
32
+ const key = signer ? new PrivateKey(signer) : this.context.signer;
33
+ if (!key) {
34
+ throw Error('No signer provided');
31
35
  }
32
- });
33
- const body = UploadResultBody.parse(response.data);
34
- return {
35
- reference: body.reference,
36
- tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
37
- historyAddress: response.headers['swarm-act-history-address'] ? Optional.of(new Reference(response.headers['swarm-act-history-address'])) : Optional.empty()
38
- };
36
+ return {
37
+ ...this.makeReader(key.publicKey().address(), requestOptions),
38
+ upload: uploadSingleOwnerChunkData.bind(null, this.context.getRequestOptionsForCall(requestOptions), key)
39
+ };
40
+ }
39
41
  }
@@ -0,0 +1,76 @@
1
+ import { TransactionOptionsSchema } from "../utils/schema.js";
2
+ import { BZZ } from "../utils/tokens.js";
3
+ import { asNumberString } from "../utils/type.js";
4
+ import * as stakeApi from "../api/stake.js";
5
+ import * as redistributionApi from "../api/redistributionstate.js";
6
+ /**
7
+ * Staking operations.
8
+ *
9
+ * Accessed as `bee.stake`.
10
+ */
11
+ export class Stake {
12
+ constructor(context) {
13
+ this.context = context;
14
+ }
15
+ /**
16
+ * Gets the amount of staked BZZ.
17
+ *
18
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
19
+ */
20
+ async get(requestOptions) {
21
+ return stakeApi.getStake(this.context.getRequestOptionsForCall(requestOptions));
22
+ }
23
+ /**
24
+ * Gets the amount of withdrawable staked BZZ.
25
+ *
26
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
27
+ */
28
+ async getWithdrawable(requestOptions) {
29
+ return stakeApi.getWithdrawableStake(this.context.getRequestOptionsForCall(requestOptions));
30
+ }
31
+ /**
32
+ * Stakes the given amount of BZZ. Initial deposit must be at least 10 BZZ.
33
+ *
34
+ * Be aware that staked BZZ tokens can **not** be withdrawn.
35
+ *
36
+ * @param amount Amount of BZZ tokens to be staked. If not providing a `BZZ` instance, the amount is denoted in PLUR.
37
+ * @param options
38
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
39
+ */
40
+ async deposit(amount, options, requestOptions) {
41
+ const amountString = amount instanceof BZZ ? amount.toPLURString() : asNumberString(amount, {
42
+ min: 1n,
43
+ name: 'amount'
44
+ });
45
+ if (options) {
46
+ options = TransactionOptionsSchema.parse(options);
47
+ }
48
+ return stakeApi.depositStake(this.context.getRequestOptionsForCall(requestOptions), amountString, options);
49
+ }
50
+ /**
51
+ * Withdraws all surplus staked BZZ to the node wallet.
52
+ *
53
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
54
+ */
55
+ async withdrawSurplus(requestOptions) {
56
+ return stakeApi.withdrawSurplusStake(this.context.getRequestOptionsForCall(requestOptions));
57
+ }
58
+ /**
59
+ * Withdraws all staked BZZ to the node wallet.
60
+ *
61
+ * **Only available when the staking contract is paused and is in the process of being migrated to a new contract!**
62
+ *
63
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
64
+ */
65
+ async migrate(requestOptions) {
66
+ return stakeApi.migrateStake(this.context.getRequestOptionsForCall(requestOptions));
67
+ }
68
+ /**
69
+ * Gets current status of node in redistribution game.
70
+ *
71
+ * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
72
+ */
73
+ async getRedistributionState(requestOptions) {
74
+ return redistributionApi.getRedistributionState(this.context.getRequestOptionsForCall(requestOptions));
75
+ }
76
+ }