Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: deserialize Uint8Arrays
Browse files Browse the repository at this point in the history
We've recently started to use Uint8Arrays instead of node Buffers,
but `zcash-block` calls methods on it's input that are only available
on Buffers, so convert binary blobs to Buffers before passing them in.
  • Loading branch information
achingbrain authored and vmx committed Jul 23, 2020
1 parent 453548f commit 9a3edba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const CID = require('cids')
const multicodec = require('multicodec')
const multihashes = require('multihashes')
const multihashing = require('multihashing-async')
const { Buffer } = require('buffer')

const ZCASH_BLOCK_HEADER_SIZE = 1487
const CODEC = multicodec.ZCASH_BLOCK
Expand All @@ -28,6 +29,11 @@ const serialize = (dagNode) => {
const deserialize = (binaryBlob) => {
let deserialized

if (!Buffer.isBuffer(binaryBlob)) {
// zcash only takes Buffers, not Uint8Arrays
binaryBlob = Buffer.from(binaryBlob)
}

if (binaryBlob.length < ZCASH_BLOCK_HEADER_SIZE) {
throw new Error(`Zcash block must at least include the ${ZCASH_BLOCK_HEADER_SIZE} header bytes`)
} else if (binaryBlob.length === ZCASH_BLOCK_HEADER_SIZE) {
Expand Down
4 changes: 4 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ describe('IPLD format util API cid()', () => {
IpldZcash.util.cid(fixtures.blockHeader, { hashAlg: 0xffffff })
).to.be.rejectedWith('Unrecognized function code: 16777215')
})

it('should deserialize a Uint8Array', () => {
IpldZcash.util.deserialize(Uint8Array.from(fixtures.blockHeader))
})
})

const verifyBlock = (header, expected) => {
Expand Down

0 comments on commit 9a3edba

Please sign in to comment.