js-libp2p-noise/src/keycache.ts

28 lines
533 B
TypeScript
Raw Normal View History

2020-01-13 15:38:14 +01:00
import {PeerId} from "./@types/libp2p";
import {bytes, bytes32} from "./@types/basic";
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 {
private storage = new Map<bytes, bytes32>();
2020-01-18 16:31:05 +01:00
public store(peerId: PeerId, key: bytes32): void {
2020-01-13 18:25:28 +01:00
this.storage.set(peerId.id, key);
2020-01-13 15:38:14 +01:00
}
2020-01-18 16:31:05 +01:00
public load(peerId: PeerId): bytes32|undefined {
2020-01-15 17:27:32 +01:00
return this.storage.get(peerId.id);
2020-01-13 15:38:14 +01:00
}
2020-01-13 16:40:42 +01:00
public resetStorage(): void {
this.storage.clear();
}
2020-01-13 15:38:14 +01:00
}
const KeyCache = new Keycache();
export {
KeyCache,
}