mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-07-31 01:21:59 +00:00
Create key cache
This commit is contained in:
40
src/keycache.ts
Normal file
40
src/keycache.ts
Normal 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,
|
||||
}
|
@@ -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}`);
|
||||
}
|
||||
|
Reference in New Issue
Block a user