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
/
util.js
105 lines (97 loc) · 2.72 KB
/
util.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
'use strict'
const BitcoinjsBlock = require('bitcoinjs-lib').Block
const CID = require('cids')
const multihashes = require('multihashes')
const sha256 = require('hash.js/lib/hash/sha/256')
/**
* @callback SerializeCallback
* @param {?Error} error - Error if serialization failed
* @param {?Buffer} binaryBlob - Binary Bitcoin block if serialization was
* successful
*/
/**
* Serialize internal representation into a binary Bitcoin block.
*
* @param {BitcoinBlock} dagNode - Internal representation of a Bitcoin block
* @param {SerializeCallback} callback - Callback that handles the
* return value
* @returns {void}
*/
const serialize = (dagNode, callback) => {
let err = null
let binaryBlob
try {
binaryBlob = dagNode.toBuffer()
} catch (serializeError) {
err = serializeError
} finally {
callback(err, binaryBlob)
}
}
/**
* @callback DeserializeCallback
* @param {?Error} error - Error if deserialization failed
* @param {?BitcoinBlock} dagNode - Internal representation of a Bitcoin block
* if deserialization was successful
*/
/**
* Deserialize Bitcoin block into the internal representation,
*
* @param {Buffer} binaryBlob - Binary representation of a Bitcoin block
* @param {DeserializeCallback} callback - Callback that handles the
* return value
* @returns {void}
*/
const deserialize = (binaryBlob, callback) => {
let err = null
let dagNode
try {
dagNode = BitcoinjsBlock.fromBuffer(binaryBlob)
} catch (deserializeError) {
err = deserializeError
} finally {
callback(err, dagNode)
}
}
/**
* @callback CidCallback
* @param {?Error} error - Error if getting the CID failed
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
*
* @param {BitcoinBlock} dagNode - Internal representation of a Bitcoin block
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
const cid = (dagNode, callback) => {
let err = null
let cid
try {
// Bitcoin double hashes
const firstHash = sha256().update(dagNode.toBuffer(true)).digest()
const headerHash = sha256().update(Buffer.from(firstHash)).digest()
cid = hashToCid(Buffer.from(headerHash))
} catch (cidError) {
err = cidError
} finally {
callback(err, cid)
}
}
// Convert a Bitcoin hash (as Buffer) to a CID
const hashToCid = (hash) => {
// avoid deadly embrace between resolver and util
const defaultHashAlg = require('./resolver').defaultHashAlg
const multihash = multihashes.encode(hash, defaultHashAlg)
const cidVersion = 1
const cid = new CID(cidVersion, 'bitcoin-block', multihash)
return cid
}
module.exports = {
hashToCid: hashToCid,
// Public API
cid: cid,
deserialize: deserialize,
serialize: serialize
}