1
0
mirror of https://github.com/fluencelabs/js-libp2p synced 2025-04-01 15:21:04 +00:00
js-libp2p/test/core/get-public-key.spec.ts
Alex Potsides f439d9b589
deps!: update all deps to support no-copy operations ()
Updates all deps needed to support passing lists of byte arrays where they have been created from multiple input buffers.

When reading multiplexed data, all messages arrive in length-prefixed buffers, which means the first few bytes tell the consumer how many bytes long next chunk will be.

One length prefixed chunk can be delivered in several payloads from the underlying network transport. The first payload can also include the length prefix and some or all of the data, so we stitch these together in a `Uint8ArrayList` to avoid having to concatenate `Uint8Array`s together.

Previously once we'd received enough bytes to satisfy the length prefix we'd concatenate the bytes together, but this is a potentially expensive operation where transports have small message sizes so instead just pass the `Uint8ArrayList` to the consumer and let them decide wether to concatenate or not as some consumers will be smart enough to operate on lists of `Uint8Array`s instead of always requiring a contiguous block of memory.

BREAKING CHANGE: Streams are now `Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>`
2022-08-11 13:21:04 +01:00

79 lines
2.0 KiB
TypeScript

/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { WebSockets } from '@libp2p/websockets'
import { Plaintext } from '../../src/insecure/index.js'
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: [
new Plaintext()
],
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)
})
})