2020-01-13 15:38:14 +01:00
|
|
|
import {Mutex} from 'async-mutex';
|
|
|
|
import {PeerId} from "./@types/libp2p";
|
|
|
|
import {bytes, bytes32} from "./@types/basic";
|
|
|
|
|
|
|
|
|
|
|
|
class Keycache {
|
|
|
|
private mutex: Mutex;
|
|
|
|
private storage = new Map<bytes, bytes32>();
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.mutex = new Mutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async store(peerId: PeerId, key: bytes32): Promise<void> {
|
|
|
|
const release = await this.mutex.acquire();
|
|
|
|
try {
|
|
|
|
this.storage.set(peerId.id, key);
|
|
|
|
} finally {
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:33:58 +01:00
|
|
|
public async load(peerId: PeerId): Promise<bytes32|null> {
|
2020-01-13 15:38:14 +01:00
|
|
|
const release = await this.mutex.acquire();
|
|
|
|
let key;
|
|
|
|
try {
|
2020-01-13 16:33:58 +01:00
|
|
|
key = this.storage.get(peerId.id) || null;
|
2020-01-13 15:38:14 +01:00
|
|
|
} finally {
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const KeyCache = new Keycache();
|
|
|
|
export {
|
|
|
|
KeyCache,
|
|
|
|
}
|