diff --git a/src/block.ts b/src/block.ts index 0383f99f..356a6c9e 100644 --- a/src/block.ts +++ b/src/block.ts @@ -224,7 +224,7 @@ interface CreateInput ({ bytes, cid, hasher, codec }: CreateInput): Promise> { if (bytes == null) { throw new Error('Missing required argument "bytes"') } if (hasher == null) { throw new Error('Missing required argument "hasher"') } - const value = codec.decode(bytes) + const hash = await hasher.digest(bytes) if (!binary.equals(cid.multihash.bytes, hash.bytes)) { throw new Error('CID hash does not match bytes') @@ -233,7 +233,6 @@ export async function create { await assert.isRejected(main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes') }) + it('create verifies the cid hash before decoding the bytes', async () => { + const block = await main.encode({ value: fixture, codec, hasher }) + const badBytes = bytes.fromString('not valid json') + await assert.isRejected( + main.create({ bytes: badBytes, cid: block.cid, codec, hasher }), + 'CID hash does not match bytes' + ) + }) + it('get', async () => { const block = await main.encode({ value: fixture, codec, hasher }) assert.throws(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]')