js-libp2p-noise/src/keycache.ts

30 lines
575 B
TypeScript
Raw Normal View History

2020-08-11 15:14:11 +01:00
import { bytes32 } from './@types/basic'
2020-06-19 12:49:40 +02:00
import PeerId from 'peer-id'
2020-01-13 15:38:14 +01:00
2020-01-13 16:40:42 +01:00
/**
* Storage for static keys of previously connected peers.
*/
2020-01-13 15:38:14 +01:00
class Keycache {
2021-01-26 21:39:37 +00:00
private readonly storage = new Map<Uint8Array, bytes32>()
2020-01-13 15:38:14 +01:00
2020-06-19 12:49:40 +02:00
public store (peerId: PeerId, key: bytes32): void {
this.storage.set(peerId.id, key)
2020-01-13 15:38:14 +01:00
}
2020-06-19 12:49:40 +02:00
public load (peerId?: PeerId): bytes32 | null {
if (!peerId) {
return null
2020-02-07 20:21:27 +01:00
}
2021-01-26 21:39:37 +00:00
return this.storage.get(peerId.id) ?? null
2020-01-13 15:38:14 +01:00
}
2020-06-19 12:49:40 +02:00
public resetStorage (): void {
this.storage.clear()
2020-01-13 16:40:42 +01:00
}
2020-01-13 15:38:14 +01:00
}
2020-06-19 12:49:40 +02:00
const KeyCache = new Keycache()
2020-01-13 15:38:14 +01:00
export {
2020-06-19 12:49:40 +02:00
KeyCache
2020-01-13 15:38:14 +01:00
}