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

Commit

Permalink
fix: use multihasing-async #14 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Jun 26, 2018
1 parent eab262f commit 77354ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
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

0 comments on commit 77354ac

Please sign in to comment.