-
Notifications
You must be signed in to change notification settings - Fork 510
feat: keybook #626
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: keybook #626
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Co-authored-by: Jacob Heun <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ chai.use(require('chai-bytes')) | |
| const { expect } = chai | ||
| const sinon = require('sinon') | ||
|
|
||
| const PeerId = require('peer-id') | ||
| const PeerStore = require('../../src/peer-store') | ||
|
|
||
| const peerUtils = require('../utils/creators/peer') | ||
|
|
@@ -24,7 +23,7 @@ describe('keyBook', () => { | |
| kb = peerStore.keyBook | ||
| }) | ||
|
|
||
| it('throwns invalid parameters error if invalid PeerId is provided', () => { | ||
| it('throwns invalid parameters error if invalid PeerId is provided in set', () => { | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| kb.set('invalid peerId') | ||
| } catch (err) { | ||
|
|
@@ -34,9 +33,19 @@ describe('keyBook', () => { | |
| throw new Error('invalid peerId should throw error') | ||
|
Contributor
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. No need to change but you could instead do something like: expect(() => kb.set('invalid peerId')).to.throw()
.that.has.property('code', ERR_INVALID_PARAMETERS)which avoids potentially forgetting the return/throw statements when a failure occurs.
Member
Author
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. I tried this out and I got: I will skip this for now, we can revisit later |
||
| }) | ||
|
|
||
| it('throwns invalid parameters error if invalid PeerId is provided in get', () => { | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| kb.get('invalid peerId') | ||
| } catch (err) { | ||
| expect(err.code).to.equal(ERR_INVALID_PARAMETERS) | ||
| return | ||
| } | ||
| throw new Error('invalid peerId should throw error') | ||
| }) | ||
|
|
||
| it('stores the peerId in the book and returns the public key', () => { | ||
| // Set PeerId | ||
| kb.set(peerId) | ||
| kb.set(peerId, peerId.pubKey) | ||
|
|
||
| // Get public key | ||
| const pubKey = kb.get(peerId) | ||
|
|
@@ -47,22 +56,9 @@ describe('keyBook', () => { | |
| const spy = sinon.spy(kb, '_setData') | ||
|
|
||
| // Set PeerId | ||
| kb.set(peerId) | ||
| kb.set(peerId) | ||
| kb.set(peerId, peerId.pubKey) | ||
| kb.set(peerId, peerId.pubKey) | ||
|
|
||
| expect(spy).to.have.property('callCount', 1) | ||
| }) | ||
|
|
||
| it('stores if already stored but there was no public key stored', () => { | ||
| const spy = sinon.spy(kb, '_setData') | ||
|
|
||
| // Set PeerId without public key | ||
| const p = PeerId.createFromB58String(peerId.toB58String()) | ||
| kb.set(p) | ||
|
|
||
| // Set complete peerId | ||
| kb.set(peerId) | ||
|
|
||
| expect(spy).to.have.property('callCount', 2) | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 'use strict' | ||
| /* eslint-env mocha */ | ||
|
|
||
| const chai = require('chai') | ||
| chai.use(require('dirty-chai')) | ||
| chai.use(require('chai-as-promised')) | ||
| const { expect } = chai | ||
| const sinon = require('sinon') | ||
|
|
||
| const baseOptions = require('../utils/base-options') | ||
| const peerUtils = require('../utils/creators/peer') | ||
|
|
||
| describe('libp2p.peerStore', () => { | ||
| let libp2p, remoteLibp2p | ||
|
|
||
| beforeEach(async () => { | ||
| [libp2p, remoteLibp2p] = await peerUtils.createPeer({ | ||
| number: 2, | ||
| populateAddressBooks: false, | ||
| config: { | ||
| ...baseOptions | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| it('adds peer address to AddressBook when establishing connection', async () => { | ||
| const spyAddressBook = sinon.spy(libp2p.peerStore.addressBook, 'add') | ||
| const remoteMultiaddr = `${remoteLibp2p.multiaddrs[0]}/p2p/${remoteLibp2p.peerId.toB58String()}` | ||
| const conn = await libp2p.dial(remoteMultiaddr) | ||
|
|
||
| expect(conn).to.exist() | ||
| expect(spyAddressBook).to.have.property('callCount', 1) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.
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.
@jacobheun from what we discussed, I could only not do this by now.
I think that it makes sense, but afaik noise uses static public keys, which I would not expect to have here. In addition, we will need to provide libp2p to the crypto module for adding the key to the keyBook or change the crypto interface to also require the public key, so that we can update this in the
upgrader.js.We should probably give some thought on this and follow up with a good solution.
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.
Noise has its own keycache and I dont think we need to worry about persistence of the crypto transport keys, just the libp2p id keys. I think this section is achieved by updating the peer after we've established a connection, because the crypto handshake will result in libp2p public key exchange and verification.
Uh oh!
There was an error while loading. Please reload this page.
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.
For the inbound connection, we do have the public key after the connection being established. The same is not true for the outbound connection. Both for
libp2p-secioandlibp2p-noiseI went deeper on secio to figure out why this is happening and I found this: https://github.com/libp2p/js-libp2p-secio/blob/master/src/handshake/crypto.js#L66-L73
When an inbound connection, we go to the else statement while in an outbound connection we go to the if side of things. In the
ifpart we basically do not add the public key. If I just addstate.id.remote = remoteIdinside the if and after the validation condition, it works. Is this the expected?In noise, I could not follow the flow so easily yet...
Uh oh!
There was an error while loading. Please reload this page.
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.
Meanwhile, I already added the code for this here and a test. The validation of the outbound connection key being exchanged is currently commented as a result of what I described above
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.
Note: this is fixed in secio now, I'll look into it in noise as part of my work there.
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.
what do you recommend here? change the test configuration to use secio for now and create an issue to track this?
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.
Leave it for now, I'll work on getting a patch for noise to correct this. We can add a TODO item to the peer store epic to track getting this in.