mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-22 21:41:33 +00:00
fix: expose getPublicKey (#1188)
This is used externally by IPFS so expose the method
This commit is contained in:
78
test/core/get-public-key.spec.ts
Normal file
78
test/core/get-public-key.spec.ts
Normal file
@ -0,0 +1,78 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
import { expect } from 'aegir/chai'
|
||||
import { WebSockets } from '@libp2p/websockets'
|
||||
import { NOISE } from '@chainsafe/libp2p-noise'
|
||||
import { createPeerId } from '../utils/creators/peer.js'
|
||||
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
||||
import type { Libp2pOptions } from '../../src/index.js'
|
||||
import sinon from 'sinon'
|
||||
import { KadDHT } from '@libp2p/kad-dht'
|
||||
|
||||
describe('getPublicKey', () => {
|
||||
let libp2p: Libp2pNode
|
||||
|
||||
beforeEach(async () => {
|
||||
const peerId = await createPeerId()
|
||||
const config: Libp2pOptions = {
|
||||
peerId,
|
||||
transports: [
|
||||
new WebSockets()
|
||||
],
|
||||
connectionEncryption: [
|
||||
NOISE
|
||||
],
|
||||
dht: new KadDHT()
|
||||
}
|
||||
libp2p = await createLibp2pNode(config)
|
||||
|
||||
await libp2p.start()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await libp2p.stop()
|
||||
})
|
||||
|
||||
it('should extract embedded public key', async () => {
|
||||
const otherPeer = await createPeerId()
|
||||
|
||||
const key = await libp2p.getPublicKey(otherPeer)
|
||||
|
||||
expect(otherPeer.publicKey).to.equalBytes(key)
|
||||
})
|
||||
|
||||
it('should get key from the keystore', async () => {
|
||||
const otherPeer = await createPeerId({ opts: { type: 'rsa' } })
|
||||
|
||||
if (otherPeer.publicKey == null) {
|
||||
throw new Error('Public key was missing')
|
||||
}
|
||||
|
||||
await libp2p.peerStore.keyBook.set(otherPeer, otherPeer.publicKey)
|
||||
|
||||
const key = await libp2p.getPublicKey(otherPeer)
|
||||
|
||||
expect(otherPeer.publicKey).to.equalBytes(key)
|
||||
})
|
||||
|
||||
it('should query the DHT when the key is not in the keystore', async () => {
|
||||
const otherPeer = await createPeerId({ opts: { type: 'rsa' } })
|
||||
|
||||
if (otherPeer.publicKey == null) {
|
||||
throw new Error('Public key was missing')
|
||||
}
|
||||
|
||||
if (libp2p.dht == null) {
|
||||
throw new Error('DHT was not configured')
|
||||
}
|
||||
|
||||
libp2p.dht.get = sinon.stub().returns([{
|
||||
name: 'VALUE',
|
||||
value: otherPeer.publicKey
|
||||
}])
|
||||
|
||||
const key = await libp2p.getPublicKey(otherPeer)
|
||||
|
||||
expect(otherPeer.publicKey).to.equalBytes(key)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user