diff --git a/src/types/index.ts b/src/types/index.ts index 8e70c8f4..d8b3b0d8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -152,7 +152,13 @@ export interface BeeOptions extends RequestOptions { } export interface UploadResultWithCid extends UploadResult { - cid: string + /** + * Function that converts the reference into Swarm CIDs + * + * @throws TypeError if the reference is encrypted reference (eq. 128 chars long) which is not supported in CID + * @see https://github.com/ethersphere/swarm-cid-js + */ + cid: () => string } /** diff --git a/src/utils/type.ts b/src/utils/type.ts index e65c5669..9d8eac6a 100644 --- a/src/utils/type.ts +++ b/src/utils/type.ts @@ -202,7 +202,7 @@ export function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceTyp export function addCidConversionFunction(result: UploadResult, cidType: ReferenceType): UploadResultWithCid { return { ...result, - get cid() { + cid() { return encodeReference(result.reference, cidType).toString() }, } diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index 805fa872..a6c15d8e 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -85,7 +85,7 @@ describe('Bee class', () => { const contentType = 'text/html' const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType }) - const file = await bee.downloadFile(result.cid) + const file = await bee.downloadFile(result.cid()) expect(file.name).toEqual(name) expect(file.data).toEqual(content) @@ -254,7 +254,7 @@ describe('Bee class', () => { ] const result = await bee.uploadCollection(getPostageBatch(), directoryStructure) - const file = await bee.downloadFile(result.cid, directoryStructure[0].path) + const file = await bee.downloadFile(result.cid(), directoryStructure[0].path) expect(file.name).toEqual(directoryStructure[0].path) expect(file.data.text()).toEqual('hello-CID-world') diff --git a/test/unit/bee-class.spec.ts b/test/unit/bee-class.spec.ts index a8166b91..572ac052 100644 --- a/test/unit/bee-class.spec.ts +++ b/test/unit/bee-class.spec.ts @@ -14,6 +14,7 @@ import { import { randomByteArray, testBatchId, + testChunkEncryptedReference, testChunkHash, testIdentity, testJsonCid, @@ -85,11 +86,32 @@ describe('Bee class', () => { const bee = new Bee(MOCK_SERVER_URL, { defaultHeaders }) const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt') - expect(reference).toEqual({ - cid: testJsonCid, - reference: testJsonHash, - tagUid: 123, - }) + expect(reference).toEqual( + expect.objectContaining({ + reference: testJsonHash, + tagUid: 123, + }), + ) + + expect(reference.cid()).toEqual(testJsonCid) + }) + + it('cid should throw for encrypted references', async () => { + uploadFileMock(testBatchId, 'nice.txt').reply(200, { + reference: testChunkEncryptedReference, + } as ReferenceResponse) + + const bee = new Bee(MOCK_SERVER_URL) + const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt') + + expect(reference).toEqual( + expect.objectContaining({ + reference: testChunkEncryptedReference, + tagUid: 123, + }), + ) + + expect(() => reference.cid()).toThrow(TypeError) }) describe('uploadData', () => { @@ -896,11 +918,14 @@ describe('Bee class', () => { const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt', { encrypt: true }) - expect(reference).toEqual({ - cid: testJsonCid, - reference: testJsonHash, - tagUid: 123, - }) + expect(reference).toEqual( + expect.objectContaining({ + reference: testJsonHash, + tagUid: 123, + }), + ) + + expect(reference.cid()).toEqual(testJsonCid) expect(requestSpy.mock.calls.length).toEqual(1) expect(requestSpy.mock.calls[0].length).toEqual(1) diff --git a/test/utils.ts b/test/utils.ts index e4d72e6f..408b64a9 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -431,6 +431,8 @@ export const testChunkSpan = new Uint8Array([testChunkPayload.length, 0, 0, 0, 0 export const testChunkData = new Uint8Array([...testChunkSpan, ...testChunkPayload]) // the hash is hardcoded because we would need the bmt hasher otherwise export const testChunkHash = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Reference +export const testChunkEncryptedReference = + 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Reference export const testAddress = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Address export const testBatchId = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as BatchId