-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add keysize through an option to spawn #203
Changes from 12 commits
2595fc4
718e0ce
c9c25da
c3fca62
55142c5
17d578e
f5f400e
ee74427
b167a3c
ac70cda
6c7a028
2ca9aa1
1351dde
fb30e0e
174e9dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ class DaemonClient { | |
} | ||
|
||
this.initialized = res.body.initialized | ||
cb(null, res.body) | ||
cb() | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,26 @@ | |
|
||
const fs = require('fs') | ||
const waterfall = require('async/waterfall') | ||
const series = require('async/series') | ||
const ipfs = require('ipfs-api') | ||
const multiaddr = require('multiaddr') | ||
const rimraf = require('rimraf') | ||
const path = require('path') | ||
const once = require('once') | ||
const truthy = require('truthy') | ||
const flatten = require('./utils/flatten') | ||
const defaults = require('lodash.defaults') | ||
const debug = require('debug') | ||
const os = require('os') | ||
const hat = require('hat') | ||
const log = debug('ipfsd-ctl:daemon') | ||
|
||
const safeParse = require('safe-json-parse/callback') | ||
const safeStringify = require('safe-json-stringify') | ||
|
||
const parseConfig = require('./utils/parse-config') | ||
const tmpDir = require('./utils/tmp-dir') | ||
const findIpfsExecutable = require('./utils/find-ipfs-executable') | ||
const setConfigValue = require('./utils/set-config-value') | ||
const configureNode = require('./utils/configure-node') | ||
const run = require('./utils/run') | ||
|
||
const GRACE_PERIOD = 10500 // amount of ms to wait before sigkill | ||
|
@@ -42,8 +45,6 @@ class Daemon { | |
const type = truthy(process.env.IPFS_TYPE) | ||
|
||
this.opts = opts || { type: type || 'go' } | ||
this.opts.config = flatten(this.opts.config) | ||
|
||
const td = tmpDir(opts.type === 'js') | ||
this.path = this.opts.disposable | ||
? td | ||
|
@@ -57,6 +58,7 @@ class Daemon { | |
this._gatewayAddr = null | ||
this._started = false | ||
this.api = null | ||
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null | ||
|
||
if (this.opts.env) { | ||
Object.assign(this.env, this.opts.env) | ||
|
@@ -111,41 +113,50 @@ class Daemon { | |
/** | ||
* Initialize a repo. | ||
* | ||
* @param {Object} [initOpts={}] | ||
* @param {number} [initOpts.keysize=2048] - The bit size of the identiy key. | ||
* @param {string} [initOpts.directory=IPFS_PATH] - The location of the repo. | ||
* @param {string} [initOpts.pass] - The passphrase of the keychain. | ||
* @param {Object} [initOptions={}] | ||
* @param {number} [initOptions.bits=2048] - The bit size of the identiy key. | ||
* @param {string} [initOptions.directory=IPFS_PATH] - The location of the repo. | ||
* @param {string} [initOptions.pass] - The passphrase of the keychain. | ||
* @param {function (Error, Node)} callback | ||
* @returns {undefined} | ||
*/ | ||
init (initOpts, callback) { | ||
if (!callback) { | ||
callback = initOpts | ||
initOpts = {} | ||
init (initOptions, callback) { | ||
if (typeof initOptions === 'function') { | ||
callback = initOptions | ||
initOptions = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that is mandatory to always pass a initOptions object? There should be a check to see if initOptions is a func. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're checking for |
||
} | ||
|
||
if (initOpts.directory && initOpts.directory !== this.path) { | ||
this.path = initOpts.directory | ||
if (initOptions.directory && initOptions.directory !== this.path) { | ||
this.path = initOptions.directory | ||
} | ||
|
||
const args = ['init', '-b', initOpts.keysize || 2048] | ||
if (initOpts.pass) { | ||
const bits = initOptions.bits || this.bits | ||
const args = ['init'] | ||
// do not just set a default keysize, | ||
// in case we decide to change it at | ||
// the daemon level in the future | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence doesn't read right. Do you mean "Skip if default daemon keysize"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, ipfs daemons have a default keysize when using |
||
if (bits) { | ||
args.concat(['-b', bits]) | ||
log(`initializing with keysize: ${bits}`) | ||
} | ||
if (initOptions.pass) { | ||
args.push('--pass') | ||
args.push('"' + initOpts.pass + '"') | ||
args.push('"' + initOptions.pass + '"') | ||
} | ||
run(this, args, { env: this.env }, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
configureNode(this, this.opts.config, (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
this.clean = false | ||
this.initialized = true | ||
callback(null, this) | ||
const self = this | ||
waterfall([ | ||
(cb) => this.getConfig(cb), | ||
(conf, cb) => this.replaceConfig(defaults({}, this.opts.config, conf), cb) | ||
], (err) => { | ||
if (err) { return callback(err) } | ||
self.clean = false | ||
self.initialized = true | ||
return callback() | ||
}) | ||
}) | ||
} | ||
|
@@ -328,7 +339,7 @@ class Daemon { | |
cb | ||
), | ||
(config, cb) => { | ||
if (!key) { | ||
if (key === 'show') { | ||
return safeParse(config, cb) | ||
} | ||
cb(null, config.trim()) | ||
|
@@ -348,6 +359,32 @@ class Daemon { | |
setConfigValue(this, key, value, callback) | ||
} | ||
|
||
/** | ||
* Replace the current config with the provided one | ||
* | ||
* @param {object} config | ||
* @param {function(Error)} callback | ||
* @return {undefined} | ||
*/ | ||
replaceConfig (config, callback) { | ||
const tmpFile = path.join(os.tmpdir(), hat()) | ||
// TODO: we're using tmp file here until | ||
// https://github.com/ipfs/js-ipfs/pull/785 | ||
// is ready | ||
series([ | ||
(cb) => fs.writeFile(tmpFile, safeStringify(config), cb), | ||
(cb) => run( | ||
this, | ||
['config', 'replace', `${tmpFile}`], | ||
{ env: this.env }, | ||
cb | ||
) | ||
], (err) => { | ||
if (err) { return callback(err) } | ||
fs.unlink(tmpFile, callback) | ||
}) | ||
} | ||
|
||
/** | ||
* Get the version of ipfs | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
'use strict' | ||
|
||
const eachOf = require('async/eachOf') | ||
const multiaddr = require('multiaddr') | ||
const defaults = require('lodash.defaultsdeep') | ||
const defaultsDeep = require('lodash.defaultsdeep') | ||
const createRepo = require('./utils/repo/create-nodejs') | ||
const flatten = require('./utils/flatten') | ||
const defaults = require('lodash.defaults') | ||
const waterfall = require('async/waterfall') | ||
const debug = require('debug') | ||
|
||
const log = debug('ipfsd-ctl:in-proc') | ||
|
||
/** | ||
* ipfsd for a js-ipfs instance (aka in-process IPFS node) | ||
|
@@ -32,8 +35,9 @@ class Node { | |
this._started = false | ||
this.initialized = false | ||
this.api = null | ||
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null | ||
|
||
this.opts.EXPERIMENTAL = defaults({}, opts.EXPERIMENTAL, { | ||
this.opts.EXPERIMENTAL = defaultsDeep({}, opts.EXPERIMENTAL, { | ||
pubsub: false, | ||
sharding: false, | ||
relay: { | ||
|
@@ -55,6 +59,7 @@ class Node { | |
throw new Error('Unkown argument ' + arg) | ||
} | ||
}) | ||
|
||
this.exec = new IPFS({ | ||
repo: this.repo, | ||
init: false, | ||
|
@@ -113,36 +118,41 @@ class Node { | |
/** | ||
* Initialize a repo. | ||
* | ||
* @param {Object} [initOpts={}] | ||
* @param {number} [initOpts.keysize=2048] - The bit size of the identiy key. | ||
* @param {string} [initOpts.directory=IPFS_PATH] - The location of the repo. | ||
* @param {string} [initOpts.pass] - The passphrase of the keychain. | ||
* @param {Object} [initOptions={}] | ||
* @param {number} [initOptions.bits=2048] - The bit size of the identiy key. | ||
* @param {string} [initOptions.directory=IPFS_PATH] - The location of the repo. | ||
* @param {string} [initOptions.pass] - The passphrase of the keychain. | ||
* @param {function (Error, Node)} callback | ||
* @returns {undefined} | ||
*/ | ||
init (initOpts, callback) { | ||
if (!callback) { | ||
callback = initOpts | ||
initOpts = {} | ||
init (initOptions, callback) { | ||
if (typeof initOptions === 'function') { | ||
callback = initOptions | ||
initOptions = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
} | ||
|
||
initOpts.bits = initOpts.keysize || 2048 | ||
this.exec.init(initOpts, (err) => { | ||
const bits = initOptions.keysize ? initOptions.bits : this.bits | ||
// do not just set a default keysize, | ||
// in case we decide to change it at | ||
// the daemon level in the future | ||
if (bits) { | ||
initOptions.bits = bits | ||
log(`initializing with keysize: ${bits}`) | ||
} | ||
this.exec.init(initOptions, (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
const conf = flatten(this.opts.config) | ||
eachOf(conf, (val, key, cb) => { | ||
this.setConfig(key, val, cb) | ||
}, (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
this.initialized = true | ||
this.clean = false | ||
callback(null, this) | ||
const self = this | ||
waterfall([ | ||
(cb) => this.getConfig(cb), | ||
(conf, cb) => this.replaceConfig(defaults({}, this.opts.config, conf), cb) | ||
], (err) => { | ||
if (err) { return callback } | ||
self.clean = false | ||
self.initialized = true | ||
return callback() | ||
}) | ||
}) | ||
} | ||
|
@@ -278,6 +288,17 @@ class Node { | |
this.exec.config.set(key, value, callback) | ||
} | ||
|
||
/** | ||
* Replace the current config with the provided one | ||
* | ||
* @param {object} config | ||
* @param {function(Error)} callback | ||
* @return {undefined} | ||
*/ | ||
replaceConfig (config, callback) { | ||
this.exec.config.replace(config, callback) | ||
} | ||
|
||
/** | ||
* Get the version of ipfs | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you are at it, mind upgrading ipfs-api to latest?