mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
Remove async from keycache
This commit is contained in:
parent
244348c596
commit
15db3b53ce
@ -32,7 +32,7 @@ export class IKHandshake implements IHandshake {
|
|||||||
handshake?: IK,
|
handshake?: IK,
|
||||||
) {
|
) {
|
||||||
this.isInitiator = isInitiator;
|
this.isInitiator = isInitiator;
|
||||||
this.payload = payload;
|
this.payload = Buffer.from(payload);
|
||||||
this.prologue = prologue;
|
this.prologue = prologue;
|
||||||
this.staticKeypair = staticKeypair;
|
this.staticKeypair = staticKeypair;
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
@ -7,11 +7,11 @@ import {bytes, bytes32} from "./@types/basic";
|
|||||||
class Keycache {
|
class Keycache {
|
||||||
private storage = new Map<bytes, bytes32>();
|
private storage = new Map<bytes, bytes32>();
|
||||||
|
|
||||||
public async store(peerId: PeerId, key: bytes32): Promise<void> {
|
public store(peerId: PeerId, key: bytes32): void {
|
||||||
this.storage.set(peerId.id, key);
|
this.storage.set(peerId.id, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async load(peerId: PeerId): Promise<bytes32|undefined> {
|
public load(peerId: PeerId): bytes32|undefined {
|
||||||
return this.storage.get(peerId.id);
|
return this.storage.get(peerId.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ export class Noise implements INoiseConnection {
|
|||||||
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
|
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
|
||||||
|
|
||||||
let tryIK = this.useNoisePipes;
|
let tryIK = this.useNoisePipes;
|
||||||
const foundRemoteStaticKey = await KeyCache.load(params.remotePeer);
|
const foundRemoteStaticKey = KeyCache.load(params.remotePeer);
|
||||||
if (tryIK && params.isInitiator && !foundRemoteStaticKey) {
|
if (tryIK && params.isInitiator && !foundRemoteStaticKey) {
|
||||||
tryIK = false;
|
tryIK = false;
|
||||||
logger(`Static key not found.`)
|
logger(`Static key not found.`)
|
||||||
@ -124,7 +124,7 @@ export class Noise implements INoiseConnection {
|
|||||||
throw new Error("Remote static key should be initialized.");
|
throw new Error("Remote static key should be initialized.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const IKhandshake = new IKHandshake(isInitiator, Buffer.from(payload), this.prologue, this.staticKeys, connection, remotePeer, foundRemoteStaticKey);
|
const IKhandshake = new IKHandshake(isInitiator, payload, this.prologue, this.staticKeys, connection, remotePeer, foundRemoteStaticKey);
|
||||||
try {
|
try {
|
||||||
return await this.performIKHandshake(IKhandshake);
|
return await this.performIKHandshake(IKhandshake);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -176,7 +176,7 @@ export class Noise implements INoiseConnection {
|
|||||||
await handshake.finish();
|
await handshake.finish();
|
||||||
|
|
||||||
if (this.useNoisePipes) {
|
if (this.useNoisePipes) {
|
||||||
await KeyCache.store(remotePeer, handshake.getRemoteStaticKey());
|
KeyCache.store(remotePeer, handshake.getRemoteStaticKey());
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`Error occurred during XX handshake: ${e.message}`);
|
throw new Error(`Error occurred during XX handshake: ${e.message}`);
|
||||||
|
@ -137,8 +137,8 @@ describe("Noise", () => {
|
|||||||
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
||||||
|
|
||||||
// Prepare key cache for noise pipes
|
// Prepare key cache for noise pipes
|
||||||
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||||
await KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
||||||
|
|
||||||
const xxSpy = sandbox.spy(noiseInit, "performXXHandshake");
|
const xxSpy = sandbox.spy(noiseInit, "performXXHandshake");
|
||||||
const xxFallbackSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
const xxFallbackSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
@ -171,8 +171,8 @@ describe("Noise", () => {
|
|||||||
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
|
|
||||||
// Prepare key cache for noise pipes
|
// Prepare key cache for noise pipes
|
||||||
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||||
await KeyCache.store(remotePeer, generateKeypair().publicKey);
|
KeyCache.store(remotePeer, generateKeypair().publicKey);
|
||||||
|
|
||||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
const [outbound, inbound] = await Promise.all([
|
const [outbound, inbound] = await Promise.all([
|
||||||
@ -204,8 +204,8 @@ describe("Noise", () => {
|
|||||||
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||||
|
|
||||||
// Prepare key cache for noise pipes
|
// Prepare key cache for noise pipes
|
||||||
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||||
await KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
KeyCache.store(remotePeer, staticKeysResponder.publicKey);
|
||||||
|
|
||||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
|
|
||||||
@ -239,7 +239,7 @@ describe("Noise", () => {
|
|||||||
const xxRespSpy = sandbox.spy(noiseResp, "performXXFallbackHandshake");
|
const xxRespSpy = sandbox.spy(noiseResp, "performXXFallbackHandshake");
|
||||||
|
|
||||||
// Prepare key cache for noise pipes
|
// Prepare key cache for noise pipes
|
||||||
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||||
|
|
||||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user