From 42851991a1febfc02d5331ba18d4a97f4ebf4251 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 10 Dec 2015 16:13:15 -0800 Subject: [PATCH 1/2] add jsipfs id feature --- src/cli/commands/id.js | 20 ++++++++++++++++++-- src/ipfs-core/index.js | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index deedf71f28..80ddd05fad 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,7 +1,23 @@ var Command = require('ronin').Command +var IPFS = require('../../ipfs-core') module.exports = Command.extend({ - desc: '', + desc: 'Shows IPFS Node ID info', - run: function (name) {} + options: { + format: { + alias: 'f', + type: 'string' + } + }, + + run: function (name) { + var node = new IPFS() + node.id(function (err, id) { + if (err) { + return console.error(err) + } + console.log(JSON.stringify(id)) + }) + } }) diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 2329d6bd25..4fa57991bf 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -41,10 +41,7 @@ function IPFS () { callback = opts opts = {} } - - if (!repo.exists()) { - callback(new Error('Repo does not exist, you must init repo first')) - } else { repo.load() } + repoExists(callback) repo.config.read(function (err, config) { if (err) { @@ -54,7 +51,27 @@ function IPFS () { }) } - self.id = function (format, callback) {} + self.id = function (opts, callback) { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + repoExists(callback) + + repo.config.read(function (err, config) { + if (err) { + return callback(err) + } + callback(null, { + ID: config.Identity.PeerID, + // TODO needs https://github.com/diasdavid/js-peer-id/blob/master/src/index.js#L76 + PublicKey: '', + Addresses: config.Addresses, + AgentVersion: 'js-ipfs', + ProtocolVersion: '9000' + }) + }) + } self.repo = { init: function (bits, force, empty, callback) { @@ -66,13 +83,17 @@ function IPFS () { callback = opts opts = {} } - if (!repo.exists()) { - callback(new Error('Repo does not exist, you must init repo first')) - } else { repo.load() } + repoExists(callback) repo.version.read(callback) }, gc: function () {} } + + function repoExists (callback) { + if (!repo.exists()) { + callback(new Error('Repo does not exist, you must init repo first')) + } else { repo.load() } + } } From 91a358feeea40b2b8ec003bf2aa2aa4fadb9679d Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 10 Dec 2015 16:14:32 -0800 Subject: [PATCH 2/2] add test to jsipfs id --- src/cli/commands/id.js | 2 +- tests/jsipfs-test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index 80ddd05fad..52bc904f9d 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -17,7 +17,7 @@ module.exports = Command.extend({ if (err) { return console.error(err) } - console.log(JSON.stringify(id)) + console.log(id) }) } }) diff --git a/tests/jsipfs-test.js b/tests/jsipfs-test.js index 872993ccbd..49ac1f0bd1 100644 --- a/tests/jsipfs-test.js +++ b/tests/jsipfs-test.js @@ -46,4 +46,12 @@ describe('IPFS Repo Tests', function () { done() }) }) + + it('check id info', function (done) { + node.id(function (err, id) { + expect(err).to.equal(null) + expect(id).to.be.a('object') + done() + }) + }) })