11import { Binary , Objects , System , Types } from 'cafe-utility'
22import { Readable } from 'stream'
3- import { Chunk , makeContentAddressedChunk } from './chunk/cac'
4- import { downloadSingleOwnerChunk , makeSOCAddress , makeSingleOwnerChunk , uploadSingleOwnerChunkData } from './chunk/soc'
3+ import { Chunk , makeContentAddressedChunk , unmarshalContentAddressedChunk } from './chunk/cac'
4+ import {
5+ SingleOwnerChunk ,
6+ downloadSingleOwnerChunk ,
7+ makeSOCAddress ,
8+ makeSingleOwnerChunk ,
9+ unmarshalSingleOwnerChunk ,
10+ uploadSingleOwnerChunkData ,
11+ } from './chunk/soc'
512import { makeFeedReader , makeFeedWriter } from './feed'
613import { areAllSequentialFeedsUpdateRetrievable } from './feed/retrievable'
714import * as bytes from './modules/bytes'
@@ -127,6 +134,7 @@ import {
127134 PrivateKey ,
128135 PublicKey ,
129136 Reference ,
137+ Signature ,
130138 Span ,
131139 Topic ,
132140 TransactionId ,
@@ -316,7 +324,7 @@ export class Bee {
316324 * Chunks uploaded with this method should be retrieved with the {@link downloadChunk} method.
317325 *
318326 * @param stamp Postage Batch ID or an Envelope created with the {@link createEnvelope} method.
319- * @param data Raw chunk to be uploaded
327+ * @param data Raw chunk to be uploaded (Content Addressed Chunk or Single Owner Chunk)
320328 * @param options Additional options like tag, encryption, pinning, content-type and request options
321329 * @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
322330 *
@@ -326,10 +334,12 @@ export class Bee {
326334 */
327335 async uploadChunk (
328336 stamp : EnvelopeWithBatchId | BatchId | Uint8Array | string ,
329- data : Uint8Array | Chunk ,
337+ data : Uint8Array | Chunk | SingleOwnerChunk ,
330338 options ?: UploadOptions ,
331339 requestOptions ?: BeeRequestOptions ,
332340 ) : Promise < UploadResult > {
341+ const isSOC = 'identifier' in data && 'signature' in data && 'owner' in data
342+
333343 data = data instanceof Uint8Array ? data : data . data
334344
335345 if ( options ) {
@@ -340,8 +350,15 @@ export class Bee {
340350 throw new BeeArgumentError ( `Chunk has to have size of at least ${ Span . LENGTH } .` , data )
341351 }
342352
343- if ( data . length > CHUNK_SIZE + Span . LENGTH ) {
344- throw new BeeArgumentError ( `Chunk has to have size of at most ${ CHUNK_SIZE + Span . LENGTH } .` , data )
353+ if ( ! isSOC && data . length > CHUNK_SIZE + Span . LENGTH ) {
354+ throw new BeeArgumentError ( `Content Addressed Chunk must not exceed ${ CHUNK_SIZE + Span . LENGTH } bytes.` , data )
355+ }
356+
357+ if ( isSOC && data . length > CHUNK_SIZE + Span . LENGTH + Signature . LENGTH + Identifier . LENGTH ) {
358+ throw new BeeArgumentError (
359+ `Single Owner Chunk must not exceed ${ CHUNK_SIZE + Span . LENGTH + Signature . LENGTH + Identifier . LENGTH } bytes.` ,
360+ data ,
361+ )
345362 }
346363
347364 return chunk . upload ( this . getRequestOptionsForCall ( requestOptions ) , data , stamp , options )
@@ -1184,7 +1201,7 @@ export class Bee {
11841201 identifier = new Identifier ( identifier )
11851202
11861203 const cac = makeContentAddressedChunk ( data )
1187- const soc = makeSingleOwnerChunk ( cac , identifier , signer )
1204+ const soc = cac . toSingleOwnerChunk ( identifier , signer )
11881205
11891206 return gsoc . send ( this . getRequestOptionsForCall ( requestOptions ) , soc , postageBatchId , options )
11901207 }
@@ -1357,6 +1374,81 @@ export class Bee {
13571374 return fetchLatestFeedUpdate ( this . getRequestOptionsForCall ( requestOptions ) , owner , topic )
13581375 }
13591376
1377+ /**
1378+ * Creates a Content Addressed Chunk.
1379+ *
1380+ * To be uploaded with the {@link uploadChunk} method.
1381+ *
1382+ * Payload size must be between 1 and 4096 bytes.
1383+ *
1384+ * @param rawPayload Data to be stored in the chunk. If the data is a string, it will be converted to UTF-8 bytes.
1385+ * @param span Optional span for the chunk. If not provided, it will be set to the length of the payload.
1386+ *
1387+ * @example
1388+ *
1389+ */
1390+ makeContentAddressedChunk ( rawPayload : Bytes | Uint8Array | string , span ?: Span | bigint ) : Chunk {
1391+ return makeContentAddressedChunk ( rawPayload , span )
1392+ }
1393+
1394+ /**
1395+ * Attempts to unmarshal arbitrary data into a Content Addressed Chunk.
1396+ * Throws an error if the data is not a valid CAC.
1397+ *
1398+ * @param data The chunk data (`span` and `payload`)
1399+ */
1400+ unmarshalContentAddressedChunk ( data : Bytes | Uint8Array ) : Chunk {
1401+ return unmarshalContentAddressedChunk ( data )
1402+ }
1403+
1404+ /**
1405+ * Creates a Single Owner Chunk.
1406+ *
1407+ * To be uploaded with the {@link uploadChunk} method.
1408+ *
1409+ * Identical to chaining `makeContentAddressedChunk` and `toSingleOwnerChunk`.
1410+ *
1411+ * Payload size must be between 1 and 4096 bytes.
1412+ *
1413+ * @param address Address of the Content Addressed Chunk
1414+ * @param span Span of the Content Addressed Chunk
1415+ * @param payload Payload of the Content Addressed Chunk
1416+ * @param identifier The identifier of the chunk
1417+ * @param signer The signer interface for signing the chunk
1418+ */
1419+ makeSingleOwnerChunk (
1420+ address : Reference ,
1421+ span : Span ,
1422+ payload : Bytes ,
1423+ identifier : Identifier | Uint8Array | string ,
1424+ signer : PrivateKey | Uint8Array | string ,
1425+ ) : SingleOwnerChunk {
1426+ return makeSingleOwnerChunk ( address , span , payload , identifier , signer )
1427+ }
1428+
1429+ /**
1430+ * Calculates the address of a Single Owner Chunk based on its identifier and owner address.
1431+ *
1432+ * @param identifier
1433+ * @param address
1434+ */
1435+ calculateSingleOwnerChunkAddress ( identifier : Identifier , address : EthAddress ) : Reference {
1436+ return makeSOCAddress ( identifier , address )
1437+ }
1438+
1439+ /**
1440+ * Attempts to unmarshal arbitrary data into a Single Owner Chunk.
1441+ * Throws an error if the data is not a valid SOC.
1442+ *
1443+ * @param data The chunk data
1444+ * @param address The address of the single owner chunk
1445+ *
1446+ * @returns a single owner chunk or throws error
1447+ */
1448+ unmarshalSingleOwnerChunk ( data : Bytes | Uint8Array , address : Reference | Uint8Array | string ) : SingleOwnerChunk {
1449+ return unmarshalSingleOwnerChunk ( data , address )
1450+ }
1451+
13601452 /**
13611453 * Returns an object for reading single owner chunks
13621454 *
0 commit comments