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

Commit 159e76a

Browse files
author
Alan Shaw
committed
fix: pick appropriate CID version
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 49194ad commit 159e76a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/cli/commands/block/put.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { cidToString } = require('../../../utils/cid')
99
function addBlock (data, opts) {
1010
const ipfs = opts.ipfs
1111

12-
ipfs.block.put(data, (err, block) => {
12+
ipfs.block.put(data, opts, (err, block) => {
1313
if (err) {
1414
throw err
1515
}
@@ -38,8 +38,7 @@ module.exports = {
3838
},
3939
version: {
4040
describe: 'cid version',
41-
type: 'number',
42-
default: 0
41+
type: 'number'
4342
},
4443
'cid-base': {
4544
describe: 'Number base to display CIDs in.',

src/core/components/block.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,29 @@ module.exports = function block (self) {
5858

5959
const mhtype = options.mhtype || 'sha2-256'
6060
const format = options.format || 'dag-pb'
61-
const cidVersion = options.version || 0
61+
let cidVersion
6262
// const mhlen = options.mhlen || 0
6363

64+
if (options.version == null) {
65+
// Pick appropriate CID version
66+
cidVersion = mhtype === 'sha2-256' && format === 'dag-pb' ? 0 : 1
67+
} else {
68+
cidVersion = options.version
69+
}
70+
6471
multihashing(block, mhtype, (err, multihash) => {
6572
if (err) {
6673
return cb(err)
6774
}
6875

69-
cb(null, new Block(block, new CID(cidVersion, format, multihash)))
76+
let cid
77+
try {
78+
cid = new CID(cidVersion, format, multihash)
79+
} catch (err) {
80+
return cb(err)
81+
}
82+
83+
cb(null, new Block(block, cid))
7084
})
7185
},
7286
(block, cb) => self._blockService.put(block, (err) => {

0 commit comments

Comments
 (0)