Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ interface CreateInput <T, Code extends number, Alg extends number, V extends API
export async function create <T, Code extends number, Alg extends number, V extends API.Version> ({ bytes, cid, hasher, codec }: CreateInput<T, Code, Alg, V>): Promise<API.BlockView<T, Code, Alg, V>> {
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)

Comment thread
rvagg marked this conversation as resolved.
const hash = await hasher.digest(bytes)
if (!binary.equals(cid.multihash.bytes, hash.bytes)) {
throw new Error('CID hash does not match bytes')
Expand All @@ -233,7 +233,6 @@ export async function create <T, Code extends number, Alg extends number, V exte
return createUnsafe({
bytes,
cid,
value,
codec
})
}
9 changes: 9 additions & 0 deletions test/test-block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ describe('block', () => {
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 () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👌

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"]')
Expand Down
Loading