This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: new IPLD Format API #39
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,70 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
|
||
const util = require('./util') | ||
|
||
/** | ||
* @callback ResolveCallback | ||
* @param {?Error} error - Error if path can't be resolved | ||
* @param {Object} result - Result of the path it it was resolved successfully | ||
* @param {*} result.value - Value the path resolves to | ||
* @param {string} result.remainderPath - If the path resolves half-way to a | ||
* link, then the `remainderPath` is the part after the link that can be used | ||
* for further resolving. | ||
*/ | ||
/** | ||
* Resolves a path in a Zcash block. | ||
* Resolves a path within a Zcash block. | ||
* | ||
* Returns the value or a link and the partial mising path. This way the | ||
* IPLD Resolver can fetch the link and continue to resolve. | ||
* | ||
* @param {Buffer} binaryBlob - Binary representation of a Zcash block | ||
* @param {string} [path='/'] - Path that should be resolved | ||
* @param {ResolveCallback} callback - Callback that handles the return value | ||
* @returns {void} | ||
* @returns {Object} result - Result of the path it it was resolved successfully | ||
* @returns {*} result.value - Value the path resolves to | ||
* @returns {string} result.remainderPath - If the path resolves half-way to a | ||
* link, then the `remainderPath` is the part after the link that can be used | ||
* for further resolving | ||
*/ | ||
const resolve = (binaryBlob, path, callback) => { | ||
if (typeof path === 'function') { | ||
callback = path | ||
path = undefined | ||
} | ||
exports.resolve = (binaryBlob, path) => { | ||
let node = util.deserialize(binaryBlob) | ||
|
||
util.deserialize(binaryBlob, (err, dagNode) => { | ||
if (err) { | ||
return callback(err) | ||
const parts = path.split('/').filter(Boolean) | ||
while (parts.length) { | ||
const key = parts.shift() | ||
if (node[key] === undefined) { | ||
throw new Error(`Object has no property '${key}'`) | ||
} | ||
|
||
// Return the deserialized block if no path is given | ||
if (!path) { | ||
return callback(null, { | ||
value: dagNode, | ||
remainderPath: '' | ||
}) | ||
node = node[key] | ||
if (CID.isCID(node)) { | ||
return { | ||
value: node, | ||
remainderPath: parts.join('/') | ||
} | ||
} | ||
} | ||
|
||
const pathArray = path.split('/') | ||
const value = resolveField(dagNode, pathArray[0]) | ||
if (value === null) { | ||
return callback(new Error('No such path'), null) | ||
} | ||
return { | ||
value: node, | ||
remainderPath: '' | ||
} | ||
} | ||
|
||
let remainderPath = pathArray.slice(1).join('/') | ||
// It is a link, hence it may have a remainder | ||
if (value['/'] !== undefined) { | ||
return callback(null, { | ||
value: value, | ||
remainderPath: remainderPath | ||
}) | ||
} else { | ||
if (remainderPath.length > 0) { | ||
return callback(new Error('No such path'), null) | ||
} else { | ||
return callback(null, { | ||
value: value, | ||
remainderPath: '' | ||
}) | ||
} | ||
} | ||
}) | ||
const traverse = function * (node, path) { | ||
// Traverse only objects and arrays | ||
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' || | ||
node === null) { | ||
return | ||
} | ||
for (const item of Object.keys(node)) { | ||
const nextpath = path === undefined ? item : path + '/' + item | ||
yield nextpath | ||
yield * traverse(node[item], nextpath) | ||
} | ||
} | ||
|
||
/** | ||
* @callback TreeCallback | ||
* @param {?Error} error - Error if paths can't be retreived | ||
* @param {string[] | Object.<string, *>[]} result - The result depends on | ||
* `options.values`, whether it returns only the paths, or the paths with | ||
* the corresponding values | ||
*/ | ||
/** | ||
* Return all available paths of a block. | ||
* | ||
* @generator | ||
* @param {Buffer} binaryBlob - Binary representation of a Zcash block | ||
* @param {Object} [options] - Possible options | ||
* @param {boolean} [options.values=false] - Retun only the paths by default. | ||
* If it is `true` also return the values | ||
* @param {TreeCallback} callback - Callback that handles the return value | ||
* @returns {void} | ||
* @yields {string} - A single path | ||
*/ | ||
const tree = (binaryBlob, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = undefined | ||
} | ||
options = options || {} | ||
|
||
util.deserialize(binaryBlob, (err, dagNode) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
const paths = ['version', 'timestamp', 'difficulty', 'nonce', | ||
'solution', 'reserved', 'parent', 'tx'] | ||
|
||
if (options.values === true) { | ||
const pathValues = {} | ||
for (let path of paths) { | ||
pathValues[path] = resolveField(dagNode, path) | ||
} | ||
return callback(null, pathValues) | ||
} else { | ||
return callback(null, paths) | ||
} | ||
}) | ||
} | ||
|
||
// Return top-level fields. Returns `null` if field doesn't exist | ||
const resolveField = (header, field) => { | ||
switch (field) { | ||
case 'version': | ||
return header.version | ||
case 'timestamp': | ||
return header.time | ||
case 'difficulty': | ||
return header.bits | ||
case 'nonce': | ||
return header.nonce | ||
case 'solution': | ||
return header.solution | ||
case 'reserved': | ||
return header.reserved | ||
case 'parent': | ||
return { '/': util.hashToCid(header.prevHash) } | ||
case 'tx': | ||
return { '/': util.hashToCid(header.merkleRoot) } | ||
default: | ||
return null | ||
} | ||
} | ||
exports.tree = function * (binaryBlob) { | ||
const node = util.deserialize(binaryBlob) | ||
|
||
module.exports = { | ||
multicodec: 'zcash-block', | ||
defaultHashAlg: 'dbl-sha2-256', | ||
resolve: resolve, | ||
tree: tree | ||
yield * traverse(node) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
==