Skip to content

Commit

Permalink
fix: query routing for RSA public key (#2350)
Browse files Browse the repository at this point in the history
Updates the test to assert that the routing is, in fact, queried for a missing RSA public key.
  • Loading branch information
achingbrain committed Jan 9, 2024
1 parent 821a38e commit ee7ffe9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
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 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
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
}
} catch (err: any) {
if (err.code !== codes.ERR_NOT_FOUND) {
throw err
}
}

const peerKey = uint8ArrayConcat([
Expand All @@ -324,6 +330,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends

// 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')
})
})

0 comments on commit ee7ffe9

Please sign in to comment.