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
/
resolver.js
139 lines (127 loc) · 3.75 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
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.
*
* 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}
*/
const resolve = (binaryBlob, path, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}
util.deserialize(binaryBlob, (err, dagNode) => {
if (err) {
return callback(err)
}
// Return the deserialized block if no path is given
if (!path) {
return callback(null, {
value: dagNode,
remainderPath: ''
})
}
const pathArray = path.split('/')
const value = resolveField(dagNode, pathArray[0])
if (value === null) {
return callback(new Error('No such path'), null)
}
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: ''
})
}
}
})
}
/**
* @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.
*
* @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}
*/
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
}
}
module.exports = {
multicodec: 'bitcoin-block',
defaultHashAlg: 'dbl-sha2-256',
resolve: resolve,
tree: tree
}