Expose noise class

This commit is contained in:
morrigan
2019-11-11 15:39:09 +01:00
parent b69769f8dd
commit 8cf420fd88
4 changed files with 65 additions and 4 deletions

View File

@ -1,17 +1,55 @@
import { x25519 } from 'bcrypto';
import { bytes } from "./types/basic";
import { Connection } from "./types/libp2p";
import { KeyPair, XXHandshake } from "./xx";
import { signPayload } from "../test/utils";
import {Buffer} from "buffer";
export class Noise {
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
private readonly privateKey: bytes;
private staticKeys?: KeyPair;
private earlyData?: bytes;
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
this.privateKey = privateKey;
this.earlyData = earlyData;
if (staticNoiseKey) {
const publicKey = x25519.publicKeyCreate(staticNoiseKey);
this.staticKeys = {
privateKey: staticNoiseKey,
publicKey,
}
}
}
public tag() {
return '/noise';
}
public encrypt(InsecureConnection: Connection, remotePublicKey: bytes) {
public async encrypt(InsecureConnection: Connection, remotePublicKey: bytes) {
const isInitiator = InsecureConnection.stats.direction === "outbound";
const secretKey = await this.doHandshake(isInitiator, remotePublicKey);
}
private async doHandshake(isInitiator: boolean, remotePublicKey: bytes) : Promise<bytes> {
const xx = new XXHandshake();
if (!this.staticKeys) {
this.staticKeys = await xx.generateKeypair();
}
let signedPayload;
if (this.earlyData) {
const payload = Buffer.concat([this.earlyData, this.staticKeys.publicKey])
signedPayload = await signPayload(this.privateKey, payload);
}
const prologue = Buffer.from(this.tag());
const nsInit = await xx.initSession(isInitiator, prologue, this.staticKeys, remotePublicKey);
// TODO: Send messages, confirm handshake and return shared key
return Buffer.alloc(0);
}
}

View File

@ -1,15 +1,18 @@
import { bytes } from "./basic";
type PeerId = {
id: string,
privKey: string,
pubKey: string,
};
type ConnectionStat = {
type ConnectionStats = {
direction: "inbound" | "outbound",
encryption: string,
}
export interface Connection {
localPeer: PeerId,
remotePeer: PeerId,
stat: ConnectionStat,
stats: ConnectionStats,
}