Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

fix: use multihashing-async #19

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"dependencies": {
"bitcoinjs-lib": "^3.3.2",
"cids": "~0.5.2",
"hash.js": "^1.1.3",
"multihashes": "~0.4.12"
"multihashes": "~0.4.12",
"multihashing-async": "~0.5.1"
},
"devDependencies": {
"aegir": "^13.0.0",
Expand Down
26 changes: 12 additions & 14 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const BitcoinjsBlock = require('bitcoinjs-lib').Block
const CID = require('cids')
const multihashes = require('multihashes')
const sha256 = require('hash.js/lib/hash/sha/256')
const multihashing = require('multihashing-async')
const waterfall = require('async/waterfall')

/**
* @callback SerializeCallback
Expand Down Expand Up @@ -76,19 +77,16 @@ const cid = (dagNode, options, callback) => {
options = {}
}
options = options || {}
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)
}
waterfall([
(cb) => {
try {
multihashing(dagNode.toBuffer(true), 'dbl-sha2-256', cb)
} catch (err) {
cb(err)
}
},
(mh, cb) => cb(null, new CID(1, 'bitcoin-block', mh))
], callback)
}

// Convert a Bitcoin hash (as Buffer) to a CID
Expand Down