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

Commit

Permalink
feat: switch to zcash-block for decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 29, 2019
1 parent 6a3869d commit 22bc170
Show file tree
Hide file tree
Showing 7 changed files with 718 additions and 172 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"multicodec": "~0.5.1",
"multihashes": "~0.4.12",
"multihashing-async": "~0.8.0",
"zcash-bitcore-lib": "~0.13.20-rc3"
"zcash-block": "^2.0.0"
},
"devDependencies": {
"aegir": "^20.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports.resolve = (binaryBlob, path) => {
const traverse = function * (node, path) {
// Traverse only objects and arrays
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
node === null) {
node == null) {
return
}
for (const item of Object.keys(node)) {
Expand Down
54 changes: 17 additions & 37 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const ZcashBitcoreBlockHeader = require('zcash-bitcore-lib').BlockHeader
const ZcashBlock = require('zcash-block')
const CID = require('cids')
const multicodec = require('multicodec')
const multihashes = require('multihashes')
Expand All @@ -11,13 +11,12 @@ const CODEC = multicodec.ZCASH_BLOCK
const DEFAULT_HASH_ALG = multicodec.DBL_SHA2_256

/**
* Serialize internal representation into a binary Zcash block.
* Unsupported, this codec cannot serialize Zcash blocks.
*
* @param {ZcashBlock} dagNode - Internal representation of a Zcash block
* @returns {Buffer}
*/
const serialize = (dagNode) => {
return dagNode.toBuffer()
throw new Error('Unsupported operation')
}

/**
Expand All @@ -27,42 +26,23 @@ const serialize = (dagNode) => {
* @returns {ZcashBlock}
*/
const deserialize = (binaryBlob) => {
if (binaryBlob.length !== ZCASH_BLOCK_HEADER_SIZE) {
throw new Error(
`Zcash block header needs to be ${ZCASH_BLOCK_HEADER_SIZE} bytes`)
}

const deserialized = ZcashBitcoreBlockHeader.fromBuffer(binaryBlob)
let deserialized

const getters = {
difficulty: function () {
return this.bits
},
parent: function () {
return hashToCid(this.prevHash)
},
tx: function () {
return hashToCid(this.merkleRoot)
}
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) {
deserialized = ZcashBlock.decodeHeaderOnly(binaryBlob)
} else {
deserialized = ZcashBlock.decode(binaryBlob)
}
Object.entries(getters).forEach(([name, fun]) => {
Object.defineProperty(deserialized, name, {
enumerable: true,
get: fun
})
})

const removeEnumberables = [
'bits',
'merkleRoot',
'prevHash',
'time'
]
removeEnumberables.forEach((field) => {
if (field in deserialized) {
Object.defineProperty(deserialized, field, { enumerable: false })
}
})
// for go-ipld-zcash compatibility
deserialized.timestamp = deserialized.time
deserialized.reserved = deserialized.finalsaplingroot
deserialized.tx = deserialized.merkleroot

deserialized.cid = hashToCid(deserialized.hash)
deserialized.parent = hashToCid(deserialized.previousblockhash)

return deserialized
}
Expand Down
Loading

0 comments on commit 22bc170

Please sign in to comment.