diff --git a/src/utils/cid.ts b/src/utils/cid.ts index 2d8c3e96..e65c72ee 100644 --- a/src/utils/cid.ts +++ b/src/utils/cid.ts @@ -34,8 +34,8 @@ export function convertReferenceToCid(reference: Reference | string, type: 'feed } export function convertCidToReference(cid: string): DecodedCID { - const bytes = Binary.base32ToUint8Array(cid.toLowerCase()) - const codec = bytes[2] + const bytes = Binary.base32ToUint8Array(cid.toUpperCase().slice(1)) + const codec = bytes[1] if (!CODEC_TABLE[codec]) { throw new Error('Unknown codec') diff --git a/src/utils/expose.ts b/src/utils/expose.ts index 5bf74fc0..acbf5b61 100644 --- a/src/utils/expose.ts +++ b/src/utils/expose.ts @@ -2,32 +2,32 @@ export { getCollectionSize } from './collection' export { getFolderSize } from './collection.node' export { + Bytes, + FlexBytes, assertBytes, assertFlexBytes, - Bytes, bytesAtOffset, bytesEqual, - FlexBytes, flexBytesAtOffset, isBytes, isFlexBytes, } from './bytes' export { + HexString, + PrefixedHexString, assertHexString, assertPrefixedHexString, bytesToHex, - HexString, hexToBytes, intToHex, isHexString, makeHexString, - PrefixedHexString, } from './hex' export { - capitalizeAddressERC55, EthAddress, + capitalizeAddressERC55, ethToSwarmAddress, fromLittleEndian, isHexEthAddress, @@ -52,3 +52,5 @@ export { } from './stamps' export { approximateOverheadForRedundancyLevel, getRedundancyStat, getRedundancyStats } from './redundancy' + +export { convertCidToReference, convertReferenceToCid } from './cid' diff --git a/test/unit/utils/cid.spec.ts b/test/unit/utils/cid.spec.ts new file mode 100644 index 00000000..ccf1c82e --- /dev/null +++ b/test/unit/utils/cid.spec.ts @@ -0,0 +1,23 @@ +import { Utils } from '../../../src' + +describe('cid', () => { + it('should convert cid', () => { + expect( + Utils.convertReferenceToCid('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', 'manifest'), + ).toBe('bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a') + + expect( + Utils.convertReferenceToCid('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', 'feed'), + ).toBe('bah5qcgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a') + + expect(Utils.convertCidToReference('bah5acgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')).toStrictEqual({ + type: 'manifest', + reference: 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', + }) + + expect(Utils.convertCidToReference('bah5qcgzazjrvpieogf6rl3cwb7xtjzgel6hrt4a4g4vkody5u4v7u7y2im4a')).toStrictEqual({ + type: 'feed', + reference: 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338', + }) + }) +})