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

Commit f2acc29

Browse files
committed
add loadPeerInfo and change how id works underneath
1 parent 47271e5 commit f2acc29

File tree

7 files changed

+82
-40
lines changed

7 files changed

+82
-40
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"lodash.get": "^4.0.0",
8484
"lodash.set": "^4.0.0",
8585
"peer-id": "^0.6.0",
86+
"peer-info": "^0.6.0",
8687
"ronin": "^0.3.11",
8788
"temp": "^0.8.3"
8889
}

src/http-api/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ exports.start = (callback) => {
4848
// load routes
4949
require('./routes')(server)
5050

51+
// TODO start libp2p.Node
52+
5153
server.start((err) => {
5254
if (err) {
5355
return callback(err)
@@ -64,5 +66,6 @@ exports.start = (callback) => {
6466
exports.stop = (callback) => {
6567
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
6668
fs.unlinkSync(repoPath + '/api')
69+
// TODO stop libp2p
6770
exports.server.stop(callback)
6871
}

src/ipfs-core/index.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const Block = blocks.Block
88
const mDAG = require('ipfs-merkle-dag')
99
const DAGNode = mDAG.DAGNode
1010
const DAGService = mDAG.DAGService
11-
const peerId = require('peer-id')
11+
const Id = require('peer-id')
12+
const Info = require('peer-info')
13+
const multiaddr = require('multiaddr')
1214

1315
exports = module.exports = IPFS
1416

@@ -23,6 +25,29 @@ function IPFS (repo) {
2325

2426
const blockS = new BlockService(repo)
2527
const dagS = new DAGService(blockS)
28+
var peerInfo
29+
// var libp2pNode
30+
31+
// TODO populate peerInfo
32+
loadPeerInfo()
33+
function loadPeerInfo () {
34+
repo.exists((err, exists) => {
35+
if (err) {
36+
throw err
37+
}
38+
39+
repo.config.get((err, config) => {
40+
if (err) {
41+
throw err
42+
}
43+
const pid = Id.createFromPrivKey(config.Identity.PrivKey)
44+
peerInfo = new Info(pid)
45+
config.Addresses.Swarm.forEach((addr) => {
46+
peerInfo.multiaddr.add(multiaddr(addr))
47+
})
48+
})
49+
})
50+
}
2651

2752
this.version = (opts, callback) => {
2853
if (typeof opts === 'function') {
@@ -46,23 +71,20 @@ function IPFS (repo) {
4671
callback = opts
4772
opts = {}
4873
}
49-
repo.exists((err, exists) => {
50-
if (err) { return callback(err) }
51-
52-
repo.config.get((err, config) => {
53-
if (err) {
54-
return callback(err)
55-
}
56-
var pid = peerId.createFromPrivKey(config.Identity.PrivKey)
57-
callback(null, {
58-
ID: config.Identity.PeerID,
59-
PublicKey: pid.pubKey.toString('base64'),
60-
Addresses: config.Addresses,
61-
AgentVersion: 'js-ipfs',
62-
ProtocolVersion: '9000'
63-
})
74+
if (!peerInfo) { // because of split second warmup
75+
setTimeout(ready, 100)
76+
} else {
77+
ready()
78+
}
79+
function ready () {
80+
callback(null, {
81+
ID: peerInfo.id.toB58String(),
82+
PublicKey: peerInfo.id.pubKey.toString('base64'),
83+
Addresses: peerInfo.multiaddrs.map((ma) => { return ma.toString() }),
84+
AgentVersion: 'js-ipfs',
85+
ProtocolVersion: '9000'
6486
})
65-
})
87+
}
6688
}
6789

6890
this.repo = {
@@ -281,4 +303,26 @@ function IPFS (repo) {
281303
})
282304
}
283305
}
306+
307+
this.libp2p = {
308+
start: () => {
309+
310+
},
311+
stop: () => {
312+
},
313+
swarm: {
314+
peers: notImpl,
315+
addrs: notImpl,
316+
connect: notImpl,
317+
disconnect: notImpl,
318+
filters: notImpl
319+
},
320+
routing: {},
321+
records: {},
322+
ping: notImpl
323+
}
324+
}
325+
326+
function notImpl () {
327+
throw new Error('Not implemented yet')
284328
}

tests/test-cli/test-id.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ describe('id', () => {
1111
.run((err, stdout, exitcode) => {
1212
var expected = [ "{ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',",
1313
" PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',",
14-
' Addresses: ',
15-
" { Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],",
16-
" API: '/ip4/127.0.0.1/tcp/6001',",
17-
" Gateway: '/ip4/127.0.0.1/tcp/9090' },",
14+
" Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],",
1815
" AgentVersion: 'js-ipfs',",
1916
" ProtocolVersion: '9000' }" ]
2017

@@ -53,10 +50,7 @@ describe('id', () => {
5350
.run((err, stdout, exitcode) => {
5451
var expected = [ "{ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',",
5552
" PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',",
56-
' Addresses: ',
57-
" { Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],",
58-
" API: '/ip4/127.0.0.1/tcp/6001',",
59-
" Gateway: '/ip4/127.0.0.1/tcp/9090' },",
53+
" Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],",
6054
" AgentVersion: 'js-ipfs',",
6155
" ProtocolVersion: '9000' }" ]
6256

tests/test-core/browser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ describe('IPFS Repo Tests on the Browser', function () {
4343
}, done)
4444
})
4545

46-
const testsContext = require.context('.', true, /test-*/)
47-
testsContext
48-
.keys()
49-
.forEach((key) => testsContext(key))
46+
it('', () => {
47+
const testsContext = require.context('.', true, /test-*/)
48+
testsContext
49+
.keys()
50+
.forEach((key) => testsContext(key))
51+
})
5052
})

tests/test-core/test-id.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ process.env.IPFS_PATH = process.cwd() + '/tests/repo-example'
66
const IPFS = require('../../src/ipfs-core')
77

88
describe('id', () => {
9+
var ipfs
10+
11+
before((done) => {
12+
ipfs = new IPFS()
13+
setTimeout(done, 200)
14+
})
15+
916
it('get id', (done) => {
10-
const ipfs = new IPFS()
1117
ipfs.id((err, id) => {
1218
expect(err).to.not.exist
1319
expect(id).to.deep.equal({ ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',
1420
PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',
15-
Addresses: {
16-
Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],
17-
API: '/ip4/127.0.0.1/tcp/6001',
18-
Gateway: '/ip4/127.0.0.1/tcp/9090'
19-
},
21+
Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],
2022
AgentVersion: 'js-ipfs',
2123
ProtocolVersion: '9000'
2224
})

tests/test-http-api/test-id.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ describe('id', () => {
4646
const idResult = {
4747
ID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',
4848
PublicKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE=',
49-
Addresses: {
50-
Swarm: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],
51-
API: '/ip4/127.0.0.1/tcp/6001',
52-
Gateway: '/ip4/127.0.0.1/tcp/9090'
53-
},
49+
Addresses: [ '/ip4/0.0.0.0/tcp/4001', '/ip6/::/tcp/4001' ],
5450
AgentVersion: 'js-ipfs',
5551
ProtocolVersion: '9000'
5652
}

0 commit comments

Comments
 (0)