@ethersphere/bee-js 10.2.0 → 10.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mjs/bee.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Binary, Objects, System, Types } from 'cafe-utility';
2
- import { makeContentAddressedChunk } from "./chunk/cac.js";
3
- import { downloadSingleOwnerChunk, makeSOCAddress, makeSingleOwnerChunk, uploadSingleOwnerChunkData } from "./chunk/soc.js";
2
+ import { makeContentAddressedChunk, unmarshalContentAddressedChunk } from "./chunk/cac.js";
3
+ import { downloadSingleOwnerChunk, makeSOCAddress, makeSingleOwnerChunk, unmarshalSingleOwnerChunk, uploadSingleOwnerChunkData } from "./chunk/soc.js";
4
4
  import { makeFeedReader, makeFeedWriter } from "./feed/index.js";
5
5
  import { areAllSequentialFeedsUpdateRetrievable } from "./feed/retrievable.js";
6
6
  import * as bytes from "./modules/bytes.js";
@@ -37,7 +37,7 @@ import { ResourceLocator } from "./utils/resource-locator.js";
37
37
  import { getAmountForDuration, getDepthForSize, getStampCost } from "./utils/stamps.js";
38
38
  import { BZZ, DAI } from "./utils/tokens.js";
39
39
  import { asNumberString, assertData, assertFileData, makeTagUid, prepareAllTagsOptions, prepareBeeRequestOptions, prepareCollectionUploadOptions, prepareDownloadOptions, prepareFileUploadOptions, prepareGsocMessageHandler, preparePostageBatchOptions, preparePssMessageHandler, prepareRedundantUploadOptions, prepareTransactionOptions, prepareUploadOptions } from "./utils/type.js";
40
- import { BatchId, EthAddress, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Span, Topic, TransactionId } from "./utils/typed-bytes.js";
40
+ import { BatchId, EthAddress, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Signature, Span, Topic, TransactionId } from "./utils/typed-bytes.js";
41
41
  import { assertBeeUrl, stripLastSlash } from "./utils/url.js";
42
42
  /**
43
43
  * The main component that abstracts operations available on the Bee API.
@@ -158,7 +158,7 @@ export class Bee {
158
158
  * Chunks uploaded with this method should be retrieved with the {@link downloadChunk} method.
159
159
  *
160
160
  * @param stamp Postage Batch ID or an Envelope created with the {@link createEnvelope} method.
161
- * @param data Raw chunk to be uploaded
161
+ * @param data Raw chunk to be uploaded (Content Addressed Chunk or Single Owner Chunk)
162
162
  * @param options Additional options like tag, encryption, pinning, content-type and request options
163
163
  * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
164
164
  *
@@ -167,6 +167,7 @@ export class Bee {
167
167
  * @see [Bee API reference - `POST /chunks`](https://docs.ethswarm.org/api/#tag/Chunk/paths/~1chunks/post)
168
168
  */
169
169
  async uploadChunk(stamp, data, options, requestOptions) {
170
+ const isSOC = 'identifier' in data && 'signature' in data && 'owner' in data;
170
171
  data = data instanceof Uint8Array ? data : data.data;
171
172
  if (options) {
172
173
  options = prepareUploadOptions(options);
@@ -174,8 +175,11 @@ export class Bee {
174
175
  if (data.length < Span.LENGTH) {
175
176
  throw new BeeArgumentError(`Chunk has to have size of at least ${Span.LENGTH}.`, data);
176
177
  }
177
- if (data.length > CHUNK_SIZE + Span.LENGTH) {
178
- throw new BeeArgumentError(`Chunk has to have size of at most ${CHUNK_SIZE + Span.LENGTH}.`, data);
178
+ if (!isSOC && data.length > CHUNK_SIZE + Span.LENGTH) {
179
+ throw new BeeArgumentError(`Content Addressed Chunk must not exceed ${CHUNK_SIZE + Span.LENGTH} bytes.`, data);
180
+ }
181
+ if (isSOC && data.length > CHUNK_SIZE + Span.LENGTH + Signature.LENGTH + Identifier.LENGTH) {
182
+ throw new BeeArgumentError(`Single Owner Chunk must not exceed ${CHUNK_SIZE + Span.LENGTH + Signature.LENGTH + Identifier.LENGTH} bytes.`, data);
179
183
  }
180
184
  return chunk.upload(this.getRequestOptionsForCall(requestOptions), data, stamp, options);
181
185
  }
@@ -810,7 +814,7 @@ export class Bee {
810
814
  signer = new PrivateKey(signer);
811
815
  identifier = new Identifier(identifier);
812
816
  const cac = makeContentAddressedChunk(data);
813
- const soc = makeSingleOwnerChunk(cac, identifier, signer);
817
+ const soc = cac.toSingleOwnerChunk(identifier, signer);
814
818
  return gsoc.send(this.getRequestOptionsForCall(requestOptions), soc, postageBatchId, options);
815
819
  }
816
820
  /**
@@ -940,6 +944,70 @@ export class Bee {
940
944
  owner = new EthAddress(owner);
941
945
  return fetchLatestFeedUpdate(this.getRequestOptionsForCall(requestOptions), owner, topic);
942
946
  }
947
+ /**
948
+ * Creates a Content Addressed Chunk.
949
+ *
950
+ * To be uploaded with the {@link uploadChunk} method.
951
+ *
952
+ * Payload size must be between 1 and 4096 bytes.
953
+ *
954
+ * @param rawPayload Data to be stored in the chunk. If the data is a string, it will be converted to UTF-8 bytes.
955
+ * @param span Optional span for the chunk. If not provided, it will be set to the length of the payload.
956
+ *
957
+ * @example
958
+ *
959
+ */
960
+ makeContentAddressedChunk(rawPayload, span) {
961
+ return makeContentAddressedChunk(rawPayload, span);
962
+ }
963
+ /**
964
+ * Attempts to unmarshal arbitrary data into a Content Addressed Chunk.
965
+ * Throws an error if the data is not a valid CAC.
966
+ *
967
+ * @param data The chunk data (`span` and `payload`)
968
+ */
969
+ unmarshalContentAddressedChunk(data) {
970
+ return unmarshalContentAddressedChunk(data);
971
+ }
972
+ /**
973
+ * Creates a Single Owner Chunk.
974
+ *
975
+ * To be uploaded with the {@link uploadChunk} method.
976
+ *
977
+ * Identical to chaining `makeContentAddressedChunk` and `toSingleOwnerChunk`.
978
+ *
979
+ * Payload size must be between 1 and 4096 bytes.
980
+ *
981
+ * @param address Address of the Content Addressed Chunk
982
+ * @param span Span of the Content Addressed Chunk
983
+ * @param payload Payload of the Content Addressed Chunk
984
+ * @param identifier The identifier of the chunk
985
+ * @param signer The signer interface for signing the chunk
986
+ */
987
+ makeSingleOwnerChunk(address, span, payload, identifier, signer) {
988
+ return makeSingleOwnerChunk(address, span, payload, identifier, signer);
989
+ }
990
+ /**
991
+ * Calculates the address of a Single Owner Chunk based on its identifier and owner address.
992
+ *
993
+ * @param identifier
994
+ * @param address
995
+ */
996
+ calculateSingleOwnerChunkAddress(identifier, address) {
997
+ return makeSOCAddress(identifier, address);
998
+ }
999
+ /**
1000
+ * Attempts to unmarshal arbitrary data into a Single Owner Chunk.
1001
+ * Throws an error if the data is not a valid SOC.
1002
+ *
1003
+ * @param data The chunk data
1004
+ * @param address The address of the single owner chunk
1005
+ *
1006
+ * @returns a single owner chunk or throws error
1007
+ */
1008
+ unmarshalSingleOwnerChunk(data, address) {
1009
+ return unmarshalSingleOwnerChunk(data, address);
1010
+ }
943
1011
  /**
944
1012
  * Returns an object for reading single owner chunks
945
1013
  *
@@ -1535,7 +1603,7 @@ export class Bee {
1535
1603
  const blockTime = this.network === 'gnosis' ? 5 : 15;
1536
1604
  const additionalAmount = getAmountForDuration(duration, chainState.currentPrice, blockTime);
1537
1605
  const currentAmount = getAmountForDuration(batch.duration, chainState.currentPrice, blockTime);
1538
- const targetAmount = duration.isZero() ? currentAmount * multiplier : currentAmount + additionalAmount * multiplier;
1606
+ const targetAmount = duration.isZero() ? currentAmount * multiplier : (currentAmount + additionalAmount) * multiplier;
1539
1607
  const amountDelta = targetAmount - currentAmount;
1540
1608
  const transactionId = await this.topUpBatch(batch.batchID, amountDelta, requestOptions);
1541
1609
  if (depthDelta > 0) {
@@ -1,41 +1,32 @@
1
- import { Binary } from 'cafe-utility';
1
+ import { Binary, Types } from 'cafe-utility';
2
2
  import { Bytes } from "../utils/bytes.js";
3
3
  import { Span } from "../utils/typed-bytes.js";
4
4
  import { calculateChunkAddress } from "./bmt.js";
5
+ import { makeSingleOwnerChunk } from "./soc.js";
5
6
  export const MIN_PAYLOAD_SIZE = 1;
6
7
  export const MAX_PAYLOAD_SIZE = 4096;
7
- const ENCODER = new TextEncoder();
8
- /**
9
- * Creates a content addressed chunk and verifies the payload size.
10
- *
11
- * @param payloadBytes the data to be stored in the chunk
12
- */
13
- export function makeContentAddressedChunk(payloadBytes) {
14
- if (!(payloadBytes instanceof Uint8Array)) {
15
- payloadBytes = ENCODER.encode(payloadBytes);
16
- }
17
- if (payloadBytes.length < MIN_PAYLOAD_SIZE || payloadBytes.length > MAX_PAYLOAD_SIZE) {
18
- throw new RangeError(`payload size ${payloadBytes.length} exceeds limits [${MIN_PAYLOAD_SIZE}, ${MAX_PAYLOAD_SIZE}]`);
19
- }
20
- const span = Span.fromBigInt(BigInt(payloadBytes.length));
21
- const data = Binary.concatBytes(span.toUint8Array(), payloadBytes);
22
- return {
23
- data,
24
- span,
25
- payload: Bytes.fromSlice(data, Span.LENGTH),
26
- address: calculateChunkAddress(data)
27
- };
8
+ export function unmarshalContentAddressedChunk(data) {
9
+ data = new Bytes(data);
10
+ return makeContentAddressedChunk(data.toUint8Array().slice(Span.LENGTH), Span.fromSlice(data.toUint8Array(), 0));
28
11
  }
29
- export function asContentAddressedChunk(chunkBytes) {
30
- if (chunkBytes.length < MIN_PAYLOAD_SIZE + Span.LENGTH || chunkBytes.length > MAX_PAYLOAD_SIZE + Span.LENGTH) {
31
- throw new RangeError(`chunk size ${chunkBytes.length} exceeds limits [${MIN_PAYLOAD_SIZE + Span.LENGTH}, ${Span.LENGTH}]`);
12
+ export function makeContentAddressedChunk(rawPayload, span) {
13
+ if (Types.isString(rawPayload)) {
14
+ rawPayload = Bytes.fromUtf8(rawPayload);
15
+ }
16
+ if (rawPayload.length < MIN_PAYLOAD_SIZE || rawPayload.length > MAX_PAYLOAD_SIZE) {
17
+ throw new RangeError(`payload size ${rawPayload.length} exceeds limits [${MIN_PAYLOAD_SIZE}, ${MAX_PAYLOAD_SIZE}]`);
32
18
  }
33
- const span = Span.fromSlice(chunkBytes, 0);
34
- const data = Binary.concatBytes(span.toUint8Array(), chunkBytes.slice(Span.LENGTH));
19
+ const typedSpan = span ? typeof span === 'bigint' ? Span.fromBigInt(span) : span : Span.fromBigInt(BigInt(rawPayload.length));
20
+ const payload = new Bytes(rawPayload);
21
+ const data = Binary.concatBytes(typedSpan.toUint8Array(), payload.toUint8Array());
22
+ const address = calculateChunkAddress(data);
35
23
  return {
36
24
  data,
37
- span,
38
- payload: Bytes.fromSlice(data, Span.LENGTH),
39
- address: calculateChunkAddress(data)
25
+ span: typedSpan,
26
+ payload,
27
+ address,
28
+ toSingleOwnerChunk: (identifier, signer) => {
29
+ return makeSingleOwnerChunk(address, typedSpan, payload, identifier, signer);
30
+ }
40
31
  };
41
32
  }
@@ -5,10 +5,10 @@ import { Bytes } from "../utils/bytes.js";
5
5
  import { BeeError } from "../utils/error.js";
6
6
  import { EthAddress, Identifier, PrivateKey, Reference, Signature, Span } from "../utils/typed-bytes.js";
7
7
  import { calculateChunkAddress } from "./bmt.js";
8
- import { asContentAddressedChunk, makeContentAddressedChunk } from "./cac.js";
8
+ import { makeContentAddressedChunk } from "./cac.js";
9
9
  const SOC_SIGNATURE_OFFSET = Identifier.LENGTH;
10
- const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + Signature.LENGTH;
11
- const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + Span.LENGTH;
10
+ const SOC_SPAN_OFFSET = Identifier.LENGTH + Signature.LENGTH;
11
+ const SOC_PAYLOAD_OFFSET = Identifier.LENGTH + Signature.LENGTH + Span.LENGTH;
12
12
  function recoverChunkOwner(data) {
13
13
  const cacData = data.slice(SOC_SPAN_OFFSET);
14
14
  const chunkAddress = calculateChunkAddress(cacData);
@@ -19,21 +19,22 @@ function recoverChunkOwner(data) {
19
19
  return ownerAddress;
20
20
  }
21
21
  /**
22
- * Verifies if the data is a valid single owner chunk
22
+ * Unmarshals arbitrary data into a Single Owner Chunk.
23
+ * Throws an error if the data is not a valid SOC.
23
24
  *
24
25
  * @param data The chunk data
25
26
  * @param address The address of the single owner chunk
26
27
  *
27
28
  * @returns a single owner chunk or throws error
28
29
  */
29
- export function makeSingleOwnerChunkFromData(data, address) {
30
+ export function unmarshalSingleOwnerChunk(data, address) {
30
31
  data = data instanceof Bytes ? data.toUint8Array() : data;
31
32
  address = new Reference(address);
32
33
  const ownerAddress = recoverChunkOwner(data);
33
34
  const identifier = Bytes.fromSlice(data, 0, Identifier.LENGTH);
34
35
  const socAddress = new Reference(Binary.keccak256(Binary.concatBytes(identifier.toUint8Array(), ownerAddress.toUint8Array())));
35
36
  if (!Binary.equals(address.toUint8Array(), socAddress.toUint8Array())) {
36
- throw new BeeError('SOC Data does not match given address!');
37
+ throw new BeeError('SOC data does not match given address!');
37
38
  }
38
39
  const signature = Signature.fromSlice(data, SOC_SIGNATURE_OFFSET);
39
40
  const span = Span.fromSlice(data, SOC_SPAN_OFFSET);
@@ -58,21 +59,19 @@ export function makeSOCAddress(identifier, address) {
58
59
  * @param identifier The identifier of the chunk
59
60
  * @param signer The signer interface for signing the chunk
60
61
  */
61
- export function makeSingleOwnerChunk(chunk, identifier, signer) {
62
+ export function makeSingleOwnerChunk(address, span, payload, identifier, signer) {
62
63
  identifier = new Identifier(identifier);
63
64
  signer = new PrivateKey(signer);
64
- const address = makeSOCAddress(identifier, signer.publicKey().address());
65
- const signature = signer.sign(Binary.concatBytes(identifier.toUint8Array(), chunk.address.toUint8Array()));
66
- const data = Binary.concatBytes(identifier.toUint8Array(), signature.toUint8Array(), chunk.data);
67
- const span = Span.fromSlice(chunk.data, 0);
68
- const payload = Bytes.fromSlice(chunk.data, Span.LENGTH);
65
+ const socAddress = makeSOCAddress(identifier, signer.publicKey().address());
66
+ const signature = signer.sign(Binary.concatBytes(identifier.toUint8Array(), address.toUint8Array()));
67
+ const data = Binary.concatBytes(identifier.toUint8Array(), signature.toUint8Array(), span.toUint8Array(), payload.toUint8Array());
69
68
  return {
70
69
  data,
71
70
  identifier,
72
71
  signature,
73
72
  span,
74
73
  payload,
75
- address,
74
+ address: socAddress,
76
75
  owner: signer.publicKey().address()
77
76
  };
78
77
  }
@@ -104,13 +103,13 @@ export async function uploadSingleOwnerChunkData(requestOptions, signer, stamp,
104
103
  signer = new PrivateKey(signer);
105
104
  identifier = new Identifier(identifier);
106
105
  const cac = makeContentAddressedChunk(data);
107
- const soc = makeSingleOwnerChunk(cac, identifier, signer);
106
+ const soc = cac.toSingleOwnerChunk(identifier, signer);
108
107
  return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
109
108
  }
110
- export async function uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, stamp, identifier, rootChunk, options) {
109
+ export async function uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, stamp, identifier, wrappedChunk, options) {
111
110
  signer = new PrivateKey(signer);
112
111
  identifier = new Identifier(identifier);
113
- const soc = makeSingleOwnerChunk(asContentAddressedChunk(rootChunk), identifier, signer);
112
+ const soc = wrappedChunk.toSingleOwnerChunk(identifier, signer);
114
113
  return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
115
114
  }
116
115
  /**
@@ -125,5 +124,5 @@ export async function downloadSingleOwnerChunk(requestOptions, ownerAddress, ide
125
124
  ownerAddress = new EthAddress(ownerAddress);
126
125
  const address = makeSOCAddress(identifier, ownerAddress);
127
126
  const cac = await chunkAPI.download(requestOptions, address.toHex());
128
- return makeSingleOwnerChunkFromData(cac, address);
127
+ return unmarshalSingleOwnerChunk(cac, address);
129
128
  }
@@ -1,13 +1,13 @@
1
1
  import { Binary, Optional, Types } from 'cafe-utility';
2
- import { asContentAddressedChunk } from "../chunk/cac.js";
3
- import { makeSingleOwnerChunkFromData, uploadSingleOwnerChunkData, uploadSingleOwnerChunkWithWrappedChunk } from "../chunk/soc.js";
2
+ import { makeContentAddressedChunk, unmarshalContentAddressedChunk } from "../chunk/cac.js";
3
+ import { unmarshalSingleOwnerChunk, uploadSingleOwnerChunkData, uploadSingleOwnerChunkWithWrappedChunk } from "../chunk/soc.js";
4
4
  import * as bytes from "../modules/bytes.js";
5
5
  import * as chunkAPI from "../modules/chunk.js";
6
6
  import { fetchLatestFeedUpdate, probeFeed } from "../modules/feed.js";
7
7
  import { Bytes } from "../utils/bytes.js";
8
8
  import { BeeResponseError } from "../utils/error.js";
9
9
  import { ResourceLocator } from "../utils/resource-locator.js";
10
- import { FeedIndex, Identifier, Reference, Signature } from "../utils/typed-bytes.js";
10
+ import { FeedIndex, Reference } from "../utils/typed-bytes.js";
11
11
  import { makeFeedIdentifier } from "./identifier.js";
12
12
  const TIMESTAMP_PAYLOAD_OFFSET = 0;
13
13
  const TIMESTAMP_PAYLOAD_SIZE = 8;
@@ -40,7 +40,7 @@ export async function updateFeedWithPayload(requestOptions, signer, topic, data,
40
40
  const identifier = makeFeedIdentifier(topic, nextIndex);
41
41
  if (data.length > 4096) {
42
42
  const uploadResult = await bytes.upload(requestOptions, data, postageBatchId, options);
43
- const rootChunk = await chunkAPI.download(requestOptions, uploadResult.reference);
43
+ const rootChunk = unmarshalContentAddressedChunk(await chunkAPI.download(requestOptions, uploadResult.reference));
44
44
  return uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, postageBatchId, identifier, rootChunk, options);
45
45
  }
46
46
  return uploadSingleOwnerChunkData(requestOptions, signer, postageBatchId, identifier, Types.isString(data) ? Bytes.fromUtf8(data).toUint8Array() : data, options);
@@ -53,7 +53,7 @@ export async function downloadFeedUpdate(requestOptions, owner, topic, index, ha
53
53
  index = typeof index === 'number' ? FeedIndex.fromBigInt(BigInt(index)) : index;
54
54
  const address = getFeedUpdateChunkReference(owner, topic, index);
55
55
  const data = await chunkAPI.download(requestOptions, address.toHex());
56
- const soc = makeSingleOwnerChunkFromData(data, address);
56
+ const soc = unmarshalSingleOwnerChunk(data, address);
57
57
  let timestamp = Optional.empty();
58
58
  if (hasTimestamp) {
59
59
  const timestampBytes = Bytes.fromSlice(soc.payload.toUint8Array(), TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
@@ -68,7 +68,8 @@ export async function downloadFeedUpdateAsCAC(requestOptions, owner, topic, inde
68
68
  index = typeof index === 'number' ? FeedIndex.fromBigInt(BigInt(index)) : index;
69
69
  const address = getFeedUpdateChunkReference(owner, topic, index);
70
70
  const data = await chunkAPI.download(requestOptions, address);
71
- return asContentAddressedChunk(data.slice(Identifier.LENGTH + Signature.LENGTH));
71
+ const soc = unmarshalSingleOwnerChunk(data, address);
72
+ return makeContentAddressedChunk(soc.payload, soc.span);
72
73
  }
73
74
  export function makeFeedReader(requestOptions, topic, owner) {
74
75
  // TODO: remove after enough time has passed in deprecated version
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable } from 'stream';
3
3
  import { Chunk } from './chunk/cac';
4
+ import { SingleOwnerChunk } from './chunk/soc';
4
5
  import { FeedPayloadResult } from './modules/feed';
5
6
  import type { AllSettlements, BalanceResponse, BeeOptions, BeeRequestOptions, BeeVersions, ChainState, ChequebookAddressResponse, ChequebookBalanceResponse, CollectionUploadOptions, DebugStatus, DownloadOptions, EnvelopeWithBatchId, FeedReader, FeedWriter, FileData, FileUploadOptions, GetGranteesResult, GlobalPostageBatch, GranteesResult, GsocMessageHandler, GsocSubscription, Health, LastCashoutActionResponse, LastChequesForPeerResponse, LastChequesResponse, NodeAddresses, NodeInfo, NumberString, Peer, PeerBalance, Pin, PingResponse, PostageBatch, PostageBatchBuckets, PssMessageHandler, PssSubscription, Readiness, RedistributionState, RedundancyLevel, RedundantUploadOptions, ReferenceInformation, RemovePeerResponse, ReserveState, SOCReader, SOCWriter, Settlements, Tag, Topology, TransactionInfo, UploadOptions, WalletBalance } from './types';
6
7
  import { AllTagsOptions, Collection, PostageBatchOptions, TransactionOptions, UploadResult } from './types';
@@ -8,7 +9,7 @@ import { Bytes } from './utils/bytes';
8
9
  import { Duration } from './utils/duration';
9
10
  import { Size } from './utils/size';
10
11
  import { BZZ, DAI } from './utils/tokens';
11
- import { BatchId, EthAddress, FeedIndex, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Topic, TransactionId } from './utils/typed-bytes';
12
+ import { BatchId, EthAddress, FeedIndex, Identifier, PeerAddress, PrivateKey, PublicKey, Reference, Span, Topic, TransactionId } from './utils/typed-bytes';
12
13
  import { UploadProgress } from './utils/upload-progress';
13
14
  /**
14
15
  * The main component that abstracts operations available on the Bee API.
@@ -123,7 +124,7 @@ export declare class Bee {
123
124
  * Chunks uploaded with this method should be retrieved with the {@link downloadChunk} method.
124
125
  *
125
126
  * @param stamp Postage Batch ID or an Envelope created with the {@link createEnvelope} method.
126
- * @param data Raw chunk to be uploaded
127
+ * @param data Raw chunk to be uploaded (Content Addressed Chunk or Single Owner Chunk)
127
128
  * @param options Additional options like tag, encryption, pinning, content-type and request options
128
129
  * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
129
130
  *
@@ -131,7 +132,7 @@ export declare class Bee {
131
132
  * @see [Bee docs - Upload and download](https://docs.ethswarm.org/docs/develop/access-the-swarm/upload-and-download)
132
133
  * @see [Bee API reference - `POST /chunks`](https://docs.ethswarm.org/api/#tag/Chunk/paths/~1chunks/post)
133
134
  */
134
- uploadChunk(stamp: EnvelopeWithBatchId | BatchId | Uint8Array | string, data: Uint8Array | Chunk, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
135
+ uploadChunk(stamp: EnvelopeWithBatchId | BatchId | Uint8Array | string, data: Uint8Array | Chunk | SingleOwnerChunk, options?: UploadOptions, requestOptions?: BeeRequestOptions): Promise<UploadResult>;
135
136
  /**
136
137
  * Downloads a chunk as a `Uint8Array`.
137
138
  *
@@ -608,6 +609,60 @@ export declare class Bee {
608
609
  */
609
610
  makeFeedWriter(topic: Topic | Uint8Array | string, signer?: PrivateKey | Uint8Array | string, requestOptions?: BeeRequestOptions): FeedWriter;
610
611
  fetchLatestFeedUpdate(topic: Topic | Uint8Array | string, owner: EthAddress | Uint8Array | string, requestOptions?: BeeRequestOptions): Promise<FeedPayloadResult>;
612
+ /**
613
+ * Creates a Content Addressed Chunk.
614
+ *
615
+ * To be uploaded with the {@link uploadChunk} method.
616
+ *
617
+ * Payload size must be between 1 and 4096 bytes.
618
+ *
619
+ * @param rawPayload Data to be stored in the chunk. If the data is a string, it will be converted to UTF-8 bytes.
620
+ * @param span Optional span for the chunk. If not provided, it will be set to the length of the payload.
621
+ *
622
+ * @example
623
+ *
624
+ */
625
+ makeContentAddressedChunk(rawPayload: Bytes | Uint8Array | string, span?: Span | bigint): Chunk;
626
+ /**
627
+ * Attempts to unmarshal arbitrary data into a Content Addressed Chunk.
628
+ * Throws an error if the data is not a valid CAC.
629
+ *
630
+ * @param data The chunk data (`span` and `payload`)
631
+ */
632
+ unmarshalContentAddressedChunk(data: Bytes | Uint8Array): Chunk;
633
+ /**
634
+ * Creates a Single Owner Chunk.
635
+ *
636
+ * To be uploaded with the {@link uploadChunk} method.
637
+ *
638
+ * Identical to chaining `makeContentAddressedChunk` and `toSingleOwnerChunk`.
639
+ *
640
+ * Payload size must be between 1 and 4096 bytes.
641
+ *
642
+ * @param address Address of the Content Addressed Chunk
643
+ * @param span Span of the Content Addressed Chunk
644
+ * @param payload Payload of the Content Addressed Chunk
645
+ * @param identifier The identifier of the chunk
646
+ * @param signer The signer interface for signing the chunk
647
+ */
648
+ makeSingleOwnerChunk(address: Reference, span: Span, payload: Bytes, identifier: Identifier | Uint8Array | string, signer: PrivateKey | Uint8Array | string): SingleOwnerChunk;
649
+ /**
650
+ * Calculates the address of a Single Owner Chunk based on its identifier and owner address.
651
+ *
652
+ * @param identifier
653
+ * @param address
654
+ */
655
+ calculateSingleOwnerChunkAddress(identifier: Identifier, address: EthAddress): Reference;
656
+ /**
657
+ * Attempts to unmarshal arbitrary data into a Single Owner Chunk.
658
+ * Throws an error if the data is not a valid SOC.
659
+ *
660
+ * @param data The chunk data
661
+ * @param address The address of the single owner chunk
662
+ *
663
+ * @returns a single owner chunk or throws error
664
+ */
665
+ unmarshalSingleOwnerChunk(data: Bytes | Uint8Array, address: Reference | Uint8Array | string): SingleOwnerChunk;
611
666
  /**
612
667
  * Returns an object for reading single owner chunks
613
668
  *
@@ -1,26 +1,40 @@
1
1
  import { Bytes } from '../utils/bytes';
2
- import { Reference, Span } from '../utils/typed-bytes';
2
+ import { Identifier, PrivateKey, Reference, Span } from '../utils/typed-bytes';
3
+ import { SingleOwnerChunk } from './soc';
3
4
  export declare const MIN_PAYLOAD_SIZE = 1;
4
5
  export declare const MAX_PAYLOAD_SIZE = 4096;
5
6
  /**
6
- * General chunk interface for Swarm
7
+ * Content Addressed Chunk (CAC) is the immutable building block of Swarm,
8
+ * holding at most 4096 bytes of payload.
7
9
  *
8
- * It stores the serialized data and provides functions to access
9
- * the fields of a chunk.
10
+ * - `span` indicates the size of the `payload` in bytes.
11
+ * - `payload` contains the actual data or the body of the chunk.
12
+ * - `data` contains the full chunk data - `span` and `payload`.
13
+ * - `address` is the Swarm hash (or reference) of the chunk.
10
14
  *
11
- * It also provides an address function to calculate the address of
12
- * the chunk that is required for the Chunk API.
15
+ * The `toSingleOwnerChunk` method allows converting the CAC into a Single Owner Chunk (SOC).
13
16
  */
14
17
  export interface Chunk {
18
+ /**
19
+ * Contains the full chunk data - `span` + `payload`.
20
+ */
15
21
  readonly data: Uint8Array;
22
+ /**
23
+ * Indicates the size of the `payload` in bytes.
24
+ */
16
25
  span: Span;
26
+ /**
27
+ * Contains the actual data or the body of the chunk.
28
+ */
17
29
  payload: Bytes;
30
+ /**
31
+ * The Swarm hash (or reference) of the chunk.
32
+ */
18
33
  address: Reference;
34
+ /**
35
+ * Converts the CAC into a Single Owner Chunk (SOC).
36
+ */
37
+ toSingleOwnerChunk: (identifier: Identifier | Uint8Array | string, signer: PrivateKey | Uint8Array | string) => SingleOwnerChunk;
19
38
  }
20
- /**
21
- * Creates a content addressed chunk and verifies the payload size.
22
- *
23
- * @param payloadBytes the data to be stored in the chunk
24
- */
25
- export declare function makeContentAddressedChunk(payloadBytes: Uint8Array | string): Chunk;
26
- export declare function asContentAddressedChunk(chunkBytes: Uint8Array): Chunk;
39
+ export declare function unmarshalContentAddressedChunk(data: Bytes | Uint8Array): Chunk;
40
+ export declare function makeContentAddressedChunk(rawPayload: Bytes | Uint8Array | string, span?: Span | bigint): Chunk;
@@ -1,29 +1,61 @@
1
1
  import { BeeRequestOptions, UploadOptions, UploadResult } from '../types';
2
2
  import { Bytes } from '../utils/bytes';
3
- import { BatchId, EthAddress, Identifier, PrivateKey, Reference, Signature } from '../utils/typed-bytes';
3
+ import { BatchId, EthAddress, Identifier, PrivateKey, Reference, Signature, Span } from '../utils/typed-bytes';
4
4
  import { Chunk } from './cac';
5
5
  /**
6
- * With single owner chunks, a user can assign arbitrary data to an
7
- * address and attest chunk integrity with their digital signature.
6
+ * Single Owner Chunk (SOC) is a chunk type where the address is determined by the owner and an arbitrary identifier.
7
+ * Its integrity is attested by the owner's digital signature.
8
8
  *
9
- * This interface extends the Chunk interface so it has the same
10
- * properties, but the address calculation is based on the identifier
11
- * and the owner of the chunk.
9
+ * Similar to Content Addressed Chunks (CAC), SOCs have a maximum payload size of 4096 bytes.
10
+ *
11
+ * - `span` indicates the size of the `payload` in bytes.
12
+ * - `payload` contains the actual data or the body of the chunk.
13
+ * - `data` contains the full chunk data - `span` and `payload`.
14
+ * - `address` is the Swarm hash (or reference) of the chunk.
15
+ * - `identifier` is an arbitrary identifier selected by the uploader.
16
+ * - `signature` is the digital signature of the owner over the identifier and the underlying CAC address.
17
+ * - `owner` is the Ethereum address of the chunk owner.
12
18
  */
13
- export interface SingleOwnerChunk extends Chunk {
19
+ export interface SingleOwnerChunk {
20
+ /**
21
+ * Contains the full chunk data - `span` + `payload`.
22
+ */
23
+ readonly data: Uint8Array;
24
+ /**
25
+ * Indicates the size of the `payload` in bytes.
26
+ */
27
+ span: Span;
28
+ /**
29
+ * Contains the actual data or the body of the chunk.
30
+ */
31
+ payload: Bytes;
32
+ /**
33
+ * The Swarm hash (or reference) of the chunk.
34
+ */
35
+ address: Reference;
36
+ /**
37
+ * An arbitrary identifier selected by the uploader.
38
+ */
14
39
  identifier: Identifier;
40
+ /**
41
+ * The digital signature of the owner over the identifier and the underlying CAC address.
42
+ */
15
43
  signature: Signature;
44
+ /**
45
+ * The Ethereum address of the chunk owner.
46
+ */
16
47
  owner: EthAddress;
17
48
  }
18
49
  /**
19
- * Verifies if the data is a valid single owner chunk
50
+ * Unmarshals arbitrary data into a Single Owner Chunk.
51
+ * Throws an error if the data is not a valid SOC.
20
52
  *
21
53
  * @param data The chunk data
22
54
  * @param address The address of the single owner chunk
23
55
  *
24
56
  * @returns a single owner chunk or throws error
25
57
  */
26
- export declare function makeSingleOwnerChunkFromData(data: Bytes | Uint8Array, address: Reference | Uint8Array | string): SingleOwnerChunk;
58
+ export declare function unmarshalSingleOwnerChunk(data: Bytes | Uint8Array, address: Reference | Uint8Array | string): SingleOwnerChunk;
27
59
  export declare function makeSOCAddress(identifier: Identifier, address: EthAddress): Reference;
28
60
  /**
29
61
  * Creates a single owner chunk object
@@ -32,7 +64,7 @@ export declare function makeSOCAddress(identifier: Identifier, address: EthAddre
32
64
  * @param identifier The identifier of the chunk
33
65
  * @param signer The signer interface for signing the chunk
34
66
  */
35
- export declare function makeSingleOwnerChunk(chunk: Chunk, identifier: Identifier | Uint8Array | string, signer: PrivateKey | Uint8Array | string): SingleOwnerChunk;
67
+ export declare function makeSingleOwnerChunk(address: Reference, span: Span, payload: Bytes, identifier: Identifier | Uint8Array | string, signer: PrivateKey | Uint8Array | string): SingleOwnerChunk;
36
68
  /**
37
69
  * Helper function to upload a chunk.
38
70
  *
@@ -55,7 +87,7 @@ export declare function uploadSingleOwnerChunk(requestOptions: BeeRequestOptions
55
87
  * @param options
56
88
  */
57
89
  export declare function uploadSingleOwnerChunkData(requestOptions: BeeRequestOptions, signer: PrivateKey | Uint8Array | string, stamp: BatchId | Uint8Array | string, identifier: Identifier | Uint8Array | string, data: Uint8Array, options?: UploadOptions): Promise<UploadResult>;
58
- export declare function uploadSingleOwnerChunkWithWrappedChunk(requestOptions: BeeRequestOptions, signer: PrivateKey | Uint8Array | string, stamp: BatchId | Uint8Array | string, identifier: Identifier | Uint8Array | string, rootChunk: Uint8Array, options?: UploadOptions): Promise<UploadResult>;
90
+ export declare function uploadSingleOwnerChunkWithWrappedChunk(requestOptions: BeeRequestOptions, signer: PrivateKey | Uint8Array | string, stamp: BatchId | Uint8Array | string, identifier: Identifier | Uint8Array | string, wrappedChunk: Chunk, options?: UploadOptions): Promise<UploadResult>;
59
91
  /**
60
92
  * Helper function to download SOC.
61
93
  *
@@ -2,6 +2,8 @@ import { Bee } from './bee';
2
2
  import { BeeDev } from './bee-dev';
3
3
  import { Stamper } from './stamper/stamper';
4
4
  export { MerkleTree } from 'cafe-utility';
5
+ export { Chunk } from './chunk/cac';
6
+ export { SingleOwnerChunk } from './chunk/soc';
5
7
  export { MantarayNode } from './manifest/manifest';
6
8
  export { SUPPORTED_BEE_VERSION, SUPPORTED_BEE_VERSION_EXACT } from './modules/debug/status';
7
9
  export * from './types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "10.2.0",
3
+ "version": "10.3.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "axios": "^0.30.0",
65
- "cafe-utility": "^32.2.0",
65
+ "cafe-utility": "^33.3.4",
66
66
  "debug": "^4.4.1",
67
67
  "isomorphic-ws": "^4.0.1",
68
68
  "semver": "^7.3.5",