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

View File

@@ -55,6 +55,7 @@
"typescript": "^3.6.4"
},
"dependencies": {
"async-mutex": "^0.1.4",
"bcrypto": "^4.2.3",
"bn.js": "^5.0.0",
"buffer": "^5.4.3",

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}`);
}

23
test/keycache.test.ts Normal file
View File

@@ -0,0 +1,23 @@
import { expect, assert } from "chai";
import { KeyCache } from "../src/keycache";
import {createPeerIdsFromFixtures} from "./fixtures/peer";
describe("KeyCache", () => {
let peerA, peerB;
before(async () => {
[peerA, peerB] = await createPeerIdsFromFixtures(2);
});
it("should store and load same key successfully", async() => {
try {
const key = Buffer.from("this is id 007");
await KeyCache.store(peerA, key);
const result = await KeyCache.load(peerA);
assert(result.equals(key), "Stored and loaded key are not the same");
} catch (e) {
console.error(e);
assert(false, `Test failed - ${e.message}`)
}
});
});

View File

@@ -1086,6 +1086,11 @@ async-each@^1.0.1:
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.3.tgz#b727dbf87d7651602f06f4d4ac387f47d91b0cbf"
integrity sha512-z/WhQ5FPySLdvREByI2vZiTWwCnF0moMJ1hK9YQwDTHKh6I7/uSckMetoRGb5UBZPC1z0jlw+n/XCgjeH7y1AQ==
async-mutex@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.1.4.tgz#a47d1eebf584f7dcdd760e3642dc2c58613bef5c"
integrity sha512-zVWTmAnxxHaeB2B1te84oecI8zTDJ/8G49aVBblRX6be0oq6pAybNcUSxwfgVOmOjSCvN4aYZAqwtyNI8e1YGw==
atob@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"