Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: pick appropriate CID version
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw committed Nov 9, 2018
1 parent a8f78cd commit e7ddd79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/cli/commands/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { cidToString } = require('../../../utils/cid')
function addBlock (data, opts) {
const ipfs = opts.ipfs

ipfs.block.put(data, (err, block) => {
ipfs.block.put(data, opts, (err, block) => {
if (err) {
throw err
}
Expand Down Expand Up @@ -38,8 +38,7 @@ module.exports = {
},
version: {
describe: 'cid version',
type: 'number',
default: 0
type: 'number'
},
'cid-base': {
describe: 'Number base to display CIDs in.',
Expand Down
18 changes: 16 additions & 2 deletions src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,29 @@ module.exports = function block (self) {

const mhtype = options.mhtype || 'sha2-256'
const format = options.format || 'dag-pb'
const cidVersion = options.version || 0
let cidVersion
// const mhlen = options.mhlen || 0

if (options.version == null) {
// Pick appropriate CID version
cidVersion = mhtype === 'sha2-256' && format === 'dag-pb' ? 0 : 1
} else {
cidVersion = options.version
}

multihashing(block, mhtype, (err, multihash) => {
if (err) {
return cb(err)
}

cb(null, new Block(block, new CID(cidVersion, format, multihash)))
let cid
try {
cid = new CID(cidVersion, format, multihash)
} catch (err) {
return cb(err)
}

cb(null, new Block(block, cid))
})
},
(block, cb) => self._blockService.put(block, (err) => {
Expand Down

0 comments on commit e7ddd79

Please sign in to comment.