2
2
3
3
const ZcashBitcoreBlockHeader = require ( 'zcash-bitcore-lib' ) . BlockHeader
4
4
const CID = require ( 'cids' )
5
+ const multicodec = require ( 'multicodec' )
5
6
const multihashes = require ( 'multihashes' )
6
7
const multihashing = require ( 'multihashing-async' )
7
- const waterfall = require ( 'async/waterfall' )
8
8
9
9
const ZCASH_BLOCK_HEADER_SIZE = 1487
10
+ const CODEC = multicodec . ZCASH_BLOCK
11
+ const DEFAULT_HASH_ALG = multicodec . DBL_SHA2_256
10
12
11
- /**
12
- * @callback SerializeCallback
13
- * @param {?Error } error - Error if serialization failed
14
- * @param {?Buffer } binaryBlob - Binary Zcash block if serialization was
15
- * successful
16
- */
17
13
/**
18
14
* Serialize internal representation into a binary Zcash block.
19
15
*
20
16
* @param {ZcashBlock } dagNode - Internal representation of a Zcash block
21
- * @param {SerializeCallback } callback - Callback that handles the
22
- * return value
23
- * @returns {void }
17
+ * @returns {Buffer }
24
18
*/
25
- const serialize = ( dagNode , callback ) => {
26
- let err = null
27
- let binaryBlob
28
- try {
29
- binaryBlob = dagNode . toBuffer ( )
30
- } catch ( serializeError ) {
31
- err = serializeError
32
- } finally {
33
- callback ( err , binaryBlob )
34
- }
19
+ const serialize = ( dagNode ) => {
20
+ return dagNode . toBuffer ( )
35
21
}
36
22
37
23
/**
38
- * @callback DeserializeCallback
39
- * @param {?Error } error - Error if deserialization failed
40
- * @param {?ZcashBlock } dagNode - Internal representation of a Zcash block
41
- * if deserialization was successful
42
- */
43
- /**
44
- * Deserialize Zcash block into the internal representation,
24
+ * Deserialize Zcash block into the internal representation.
45
25
*
46
26
* @param {Buffer } binaryBlob - Binary representation of a Zcash block
47
- * @param {DeserializeCallback } callback - Callback that handles the
48
- * return value
49
- * @returns {void }
27
+ * @returns {ZcashBlock }
50
28
*/
51
- const deserialize = ( binaryBlob , callback ) => {
29
+ const deserialize = ( binaryBlob ) => {
52
30
if ( binaryBlob . length !== ZCASH_BLOCK_HEADER_SIZE ) {
53
- const err = new Error (
31
+ throw new Error (
54
32
`Zcash block header needs to be ${ ZCASH_BLOCK_HEADER_SIZE } bytes` )
55
- return callback ( err )
56
33
}
57
34
58
- const dagNode = ZcashBitcoreBlockHeader . fromBuffer ( binaryBlob )
59
- callback ( null , dagNode )
35
+ const deserialized = ZcashBitcoreBlockHeader . fromBuffer ( binaryBlob )
36
+
37
+ const getters = {
38
+ difficulty : function ( ) {
39
+ return this . bits
40
+ } ,
41
+ parent : function ( ) {
42
+ return hashToCid ( this . prevHash )
43
+ } ,
44
+ tx : function ( ) {
45
+ return hashToCid ( this . merkleRoot )
46
+ }
47
+ }
48
+ Object . entries ( getters ) . forEach ( ( [ name , fun ] ) => {
49
+ Object . defineProperty ( deserialized , name , {
50
+ enumerable : true ,
51
+ get : fun
52
+ } )
53
+ } )
54
+
55
+ const removeEnumberables = [
56
+ 'bits' ,
57
+ 'merkleRoot' ,
58
+ 'prevHash' ,
59
+ 'time'
60
+ ]
61
+ removeEnumberables . forEach ( ( field ) => {
62
+ if ( field in deserialized ) {
63
+ Object . defineProperty ( deserialized , field , { enumerable : false } )
64
+ }
65
+ } )
66
+
67
+ return deserialized
60
68
}
61
69
62
70
/**
63
- * @callback CidCallback
64
- * @param {?Error } error - Error if getting the CID failed
65
- * @param {?CID } cid - CID if call was successful
66
- */
67
- /**
68
- * Get the CID of the DAG-Node.
71
+ * Calculate the CID of the binary blob.
69
72
*
70
- * @param {ZcashBlock } dagNode - Internal representation of a Zcash block
71
- * @param {Object } [options] - Options to create the CID
72
- * @param {number } [options.version=1] - CID version number
73
- * @param {string } [options.hashAlg='dbl-sha2-256'] - Hashing algorithm
74
- * @param {CidCallback } callback - Callback that handles the return value
75
- * @returns {void }
73
+ * @param {Object } binaryBlob - Encoded IPLD Node
74
+ * @param {Object } [userOptions] - Options to create the CID
75
+ * @param {number } [userOptions.cidVersion=1] - CID version number
76
+ * @param {string } [UserOptions.hashAlg] - Defaults to the defaultHashAlg of the format
77
+ * @returns {Promise.<CID> }
76
78
*/
77
- const cid = ( dagNode , options , callback ) => {
78
- if ( typeof options === 'function' ) {
79
- callback = options
80
- options = { }
81
- }
82
- options = options || { }
83
- // avoid deadly embrace between resolver and util
84
- const hashAlg = options . hashAlg || require ( './resolver' ) . defaultHashAlg
85
- const version = typeof options . version === 'undefined' ? 1 : options . version
86
- waterfall ( [
87
- ( cb ) => {
88
- try {
89
- multihashing ( dagNode . toBuffer ( ) , hashAlg , cb )
90
- } catch ( err ) {
91
- cb ( err )
92
- }
93
- } ,
94
- ( mh , cb ) => cb ( null , new CID ( version , 'zcash-block' , mh ) )
95
- ] , callback )
79
+ const cid = async ( binaryBlob , userOptions ) => {
80
+ const defaultOptions = { cidVersion : 1 , hashAlg : DEFAULT_HASH_ALG }
81
+ const options = Object . assign ( defaultOptions , userOptions )
82
+
83
+ const multihash = await multihashing ( binaryBlob , options . hashAlg )
84
+ const codecName = multicodec . print [ CODEC ]
85
+ const cid = new CID ( options . cidVersion , codecName , multihash )
86
+
87
+ return cid
96
88
}
97
89
98
90
// Convert a Zcash hash (as Buffer) to a CID
99
91
const hashToCid = ( hash ) => {
100
- // avoid deadly embrace between resolver and util
101
- const defaultHashAlg = require ( './resolver' ) . defaultHashAlg
102
- const multihash = multihashes . encode ( hash , defaultHashAlg )
92
+ const multihash = multihashes . encode ( hash , DEFAULT_HASH_ALG )
103
93
const cidVersion = 1
104
94
const cid = new CID ( cidVersion , 'zcash-block' , multihash )
105
95
return cid
@@ -108,6 +98,8 @@ const hashToCid = (hash) => {
108
98
module . exports = {
109
99
hashToCid : hashToCid ,
110
100
ZCASH_BLOCK_HEADER_SIZE : ZCASH_BLOCK_HEADER_SIZE ,
101
+ codec : CODEC ,
102
+ defaultHashAlg : DEFAULT_HASH_ALG ,
111
103
112
104
// Public API
113
105
cid : cid ,
0 commit comments