Skip to content

Commit 53aeea2

Browse files
committed
Allow hash algorithm to be specified
1 parent 4802221 commit 53aeea2

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/cli/commands/files/add.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const pull = require('pull-stream')
88
const paramap = require('pull-paramap')
99
const zip = require('pull-zip')
1010
const toPull = require('stream-to-pull-stream')
11+
const mh = require('multihashes')
1112
const utils = require('../../utils')
1213
const print = require('../../utils').print
1314

@@ -154,7 +155,7 @@ module.exports = {
154155
},
155156
hash: {
156157
type: 'string',
157-
// TODO: choices: [],
158+
choices: [undefined].concat(Object.keys(mh.names)),
158159
describe: 'Hash function to use. Will set Cid version to 1 if used. (experimental)'
159160
}
160161
},
@@ -165,8 +166,8 @@ module.exports = {
165166
const options = {
166167
strategy: argv.trickle ? 'trickle' : 'balanced',
167168
shardSplitThreshold: argv.enableShardingExperiment ? argv.shardSplitThreshold : Infinity,
168-
'cid-version': argv['cid-version'],
169-
'raw-leaves': argv['raw-leaves'],
169+
cidVersion: argv.cidVersion,
170+
rawLeaves: argv.rawLeaves,
170171
hashAlg: argv.hash
171172
}
172173

@@ -178,11 +179,11 @@ module.exports = {
178179
// cid-version > 0 unless explicitly set to false.
179180
//
180181
// This retains feature parity without having to implement raw-leaves.
181-
if (argv['cid-version'] > 0 && argv['raw-leaves'] !== false) {
182+
if (argv.cidVersion > 0 && argv.rawLeaves !== false) {
182183
throw new Error('Implied argument raw-leaves must be passed and set to false when cid-version is > 0')
183184
}
184185

185-
if (argv['raw-leaves']) {
186+
if (argv.rawLeaves) {
186187
throw new Error('Not implemented: raw-leaves')
187188
}
188189

src/core/components/files.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ module.exports = function files (self) {
2020
shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity
2121
}, options)
2222

23+
// CID v0 is for multihashes encoded with sha2-256
24+
if (opts.hashAlg && opts.hashAlg !== 'sha2-256' && opts.cidVersion !== 1) {
25+
opts.cidVersion = 1
26+
}
27+
2328
return pull(
2429
pull.map(normalizeContent),
2530
pull.flatten(),
@@ -65,6 +70,11 @@ module.exports = function files (self) {
6570
return callback(new Error('Invalid arguments, data must be an object, Buffer or readable stream'))
6671
}
6772

73+
// CID v0 is for multihashes encoded with sha2-256
74+
if (options && options.hashAlg && options.hashAlg !== 'sha2-256' && options.cidVersion !== 1) {
75+
options.cidVersion = 1
76+
}
77+
6878
pull(
6979
pull.values(normalizeContent(data)),
7080
importer(self._ipldResolver, options),
@@ -120,7 +130,7 @@ function prepareFile (self, opts, file, callback) {
120130
waterfall([
121131
(cb) => self.object.get(file.multihash, cb),
122132
(node, cb) => {
123-
let cid = new CID(node.multihash)
133+
let cid = new CID(file.multihash)
124134

125135
if (opts.cidVersion === 1) {
126136
cid = cid.toV1()

src/http-api/resources/files.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ exports.add = {
158158
then: Joi.boolean().valid(false).required(),
159159
otherwise: Joi.boolean().valid(false)
160160
}),
161-
// TODO: Validate against multihash alg names
162-
hash: Joi.string()
161+
hash: Joi.string().valid(Object.keys(mh.names))
163162
})
164163
// TODO: Necessary until validate "recursive", "stream-channels" etc.
165164
.options({ allowUnknown: true })
@@ -210,9 +209,9 @@ exports.add = {
210209
})
211210

212211
const options = {
213-
'cid-version': request.query['cid-version'],
214-
'raw-leaves': request.query['raw-leaves'],
215-
'hashAlg': request.query['hash']
212+
cidVersion: request.query['cid-version'],
213+
rawLeaves: request.query['raw-leaves'],
214+
hashAlg: request.query['hash']
216215
}
217216

218217
pull(

test/cli/files.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const fs = require('fs')
66
const path = require('path')
77
const compareDir = require('dir-compare').compareSync
88
const rimraf = require('rimraf').sync
9+
const CID = require('cids')
10+
const mh = require('multihashes')
911
const runOnAndOff = require('../utils/on-and-off')
1012

1113
describe('files', () => runOnAndOff((thing) => {
@@ -222,6 +224,17 @@ describe('files', () => runOnAndOff((thing) => {
222224
})
223225
})
224226

227+
Object.keys(mh.names).forEach((name) => {
228+
it('add with hash=' + name, () => {
229+
return ipfs('add src/init-files/init-docs/readme --hash=' + name)
230+
.then((out) => {
231+
const hash = out.split(' ')[1]
232+
const cid = new CID(hash)
233+
expect(mh.decode(cid.multihash).name).to.equal(name)
234+
})
235+
})
236+
})
237+
225238
it('cat', () => {
226239
return ipfs('files cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
227240
.then((out) => {

0 commit comments

Comments
 (0)