Create key cache

This commit is contained in:
Belma Gutlic
2020-01-13 15:38:14 +01:00
parent 86c909d399
commit ce7f4118c2
5 changed files with 76 additions and 1 deletions

40
src/keycache.ts Normal file
View File

@@ -0,0 +1,40 @@
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();
}
}
public async load(peerId: PeerId): Promise<bytes32> {
const release = await this.mutex.acquire();
let key;
try {
key = this.storage.get(peerId.id);
} finally {
release();
}
return key;
}
}
const KeyCache = new Keycache();
export {
KeyCache,
}

View File

@@ -32,9 +32,11 @@ export class Noise implements INoiseConnection {
private readonly prologue = Buffer.from(this.protocol);
private readonly staticKeys: KeyPair;
private readonly earlyData?: bytes;
private useNoisePipes: boolean;
constructor(staticNoiseKey?: bytes, earlyData?: bytes) {
constructor(staticNoiseKey?: bytes, earlyData?: bytes, useNoisePipes = true) {
this.earlyData = earlyData || Buffer.alloc(0);
this.useNoisePipes = useNoisePipes;
if (staticNoiseKey) {
const publicKey = x25519.publicKeyCreate(staticNoiseKey); // TODO: verify this
@@ -152,6 +154,10 @@ export class Noise implements INoiseConnection {
await handshake.propose();
await handshake.exchange();
await handshake.finish();
if (this.useNoisePipes) {
}
} catch (e) {
throw new Error(`Error occurred during XX handshake: ${e.message}`);
}