Skip to content
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

fix: query routing for RSA public key #2350

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/libp2p/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,16 @@
return peer.publicKey
}

const peerInfo = await this.peerStore.get(peer)
try {
const peerInfo = await this.peerStore.get(peer)

if (peerInfo.id.publicKey != null) {
return peerInfo.id.publicKey
if (peerInfo.id.publicKey != null) {
return peerInfo.id.publicKey
}

Check warning on line 319 in packages/libp2p/src/libp2p.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/libp2p.ts#L317-L319

Added lines #L317 - L319 were not covered by tests
} catch (err: any) {
if (err.code !== codes.ERR_NOT_FOUND) {
throw err
}

Check warning on line 323 in packages/libp2p/src/libp2p.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/libp2p.ts#L322-L323

Added lines #L322 - L323 were not covered by tests
}

const peerKey = uint8ArrayConcat([
Expand All @@ -324,6 +330,7 @@

// search any available content routing methods
const bytes = await this.contentRouting.get(peerKey, options)

// ensure the returned key is valid
unmarshalPublicKey(bytes)

Expand Down
23 changes: 15 additions & 8 deletions packages/libp2p/test/core/get-public-key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */

import { contentRoutingSymbol } from '@libp2p/interface'
import { peerIdFromString } from '@libp2p/peer-id'
import { createEd25519PeerId, createRSAPeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import { stubInterface } from 'sinon-ts'
Expand All @@ -13,9 +14,8 @@ describe('getPublicKey', () => {
let router: StubbedInstance<ContentRouting & ContentRoutingProvider>

beforeEach(async () => {
router = stubInterface<ContentRouting & ContentRoutingProvider>({
[contentRoutingSymbol]: router
})
router = stubInterface<ContentRouting & ContentRoutingProvider>()
router[contentRoutingSymbol] = router

node = await createLibp2p({
services: {
Expand Down Expand Up @@ -57,14 +57,21 @@ describe('getPublicKey', () => {
it('should query content routing when the key is not in the keystore', async () => {
const otherPeer = await createRSAPeerId()

if (otherPeer.publicKey == null) {
throw new Error('Public key was missing')
}
router.get.callsFake(async () => {
if (otherPeer.publicKey == null) {
throw new Error('Public key was missing')
}

return Promise.resolve(otherPeer.publicKey)
})

router.get.resolves(otherPeer.publicKey)
// create a copy of the RSA key, this will not have the public key
const otherPeerWithoutPublicKey = peerIdFromString(otherPeer.toString())
expect(otherPeerWithoutPublicKey).to.have.property('publicKey', undefined)

const key = await node.getPublicKey(otherPeer)
const key = await node.getPublicKey(otherPeerWithoutPublicKey)

expect(otherPeer.publicKey).to.equalBytes(key)
expect(router.get.called).to.be.true('routing was not queried')
})
})
Loading