This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: The API is now async/await based There are numerous changes, the most significant one is that the API is no longer callback based, but it using async/await. For the full new API please see the [IPLD Formats spec]. [IPLD Formats spec]: https://github.com/ipld/interface-ipld-format
- Loading branch information
Showing
8 changed files
with
272 additions
and
450 deletions.
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,139 +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 Bitcoin block. | ||
* Resolves a path within a Bitcoin 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 Bitcoin 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 Bitcoin 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', | ||
'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 = (dagNode, field) => { | ||
switch (field) { | ||
case 'version': | ||
return dagNode.version | ||
case 'timestamp': | ||
return dagNode.timestamp | ||
case 'difficulty': | ||
return dagNode.bits | ||
case 'nonce': | ||
return dagNode.nonce | ||
case 'parent': | ||
return { '/': util.hashToCid(dagNode.prevHash) } | ||
case 'tx': | ||
return { '/': util.hashToCid(dagNode.merkleRoot) } | ||
default: | ||
return null | ||
} | ||
} | ||
exports.tree = function * (binaryBlob) { | ||
const node = util.deserialize(binaryBlob) | ||
|
||
module.exports = { | ||
multicodec: 'bitcoin-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.