Add types and encryption methods

This commit is contained in:
morrigan
2019-11-12 14:02:59 +01:00
parent d2c844d598
commit 26b112f712
6 changed files with 49 additions and 20 deletions

13
src/crypto.ts Normal file
View File

@ -0,0 +1,13 @@
import { Duplex } from "./types/libp2p";
import { NoiseSession } from "./xx";
// Send encrypted payload from the user to stream
export async function encryptStreams(streams: Duplex, session: NoiseSession) : Promise<void> {
}
// Decrypt received payload from the stream and pipe to user
export async function decryptStreams(streams: Duplex, session: NoiseSession) : Promise<void> {
}

View File

@ -6,10 +6,11 @@ import { InsecureConnection, NoiseConnection, PeerId, SecureConnection, KeyPair
import { Handshake } from "./handshake";
import { generateKeypair, signPayload } from "./utils";
import {encryptStream} from "./crypto";
export class Noise implements NoiseConnection {
private readonly privateKey: bytes;
private staticKeys?: KeyPair;
private staticKeys: KeyPair;
private earlyData?: bytes;
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
@ -22,6 +23,8 @@ export class Noise implements NoiseConnection {
privateKey: staticNoiseKey,
publicKey,
}
} else {
// todo: generate new static key
}
}
@ -43,14 +46,6 @@ export class Noise implements NoiseConnection {
public async secureInbound(connection: InsecureConnection) : Promise<SecureConnection> {
}
private async read(ciphertext: bytes) {
}
private async write(plaintext: bytes) {
}
private async createSecureConnection(
connection: InsecureConnection,
remotePublicKey: bytes,
@ -69,13 +64,13 @@ export class Noise implements NoiseConnection {
const prologue = Buffer.from(this.protocol());
const session = await Handshake.runXX(isInitiator, remotePublicKey, prologue, signedPayload, this.staticKeys);
await encryptStream(connection.streams, session);
return {
insecure: connection,
...connection,
initiator: isInitiator,
prologue,
// localKey: get public key,
localPeer: connection.localPeer,
remotePeer: connection.remotePeer,
local: {
noiseKey: this.staticKeys.publicKey,
// libp2pKey:

View File

@ -22,16 +22,23 @@ type ConnectionStats = {
encryption: string,
}
type Stream = {
sink(),
// Also seen as Pair
export type Stream = {
sink(source: Iterable<any>),
source: Object,
}
export type Duplex = [Stream, Stream];
export interface InsecureConnection {
localPeer: PeerId,
remotePeer: PeerId,
local: PeerInfo,
remote: PeerInfo,
stats: ConnectionStats,
streams(): [Stream],
streams(): Duplex,
addStream(muxedStream: any) : Stream,
}
@ -42,14 +49,9 @@ export interface NoiseConnection {
}
export interface SecureConnection {
insecure: InsecureConnection,
initiator: boolean,
prologue: bytes32,
localKey: bytes,
localPeer: PeerId,
remotePeer: PeerId,
local: PeerInfo,
remote: PeerInfo,
xxNoiseSession: NoiseSession,
xxComplete: boolean,

View File

@ -227,6 +227,9 @@ export class XXHandshake {
private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
let ns = Buffer.alloc(0);
hs.e = await generateKeypair();
if (!hs.e) {
throw new Error("Handshake state has keypair missing.");
}
const ne = hs.e.publicKey;
this.mixHash(hs.ss, ne);
@ -237,6 +240,9 @@ export class XXHandshake {
private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
hs.e = await generateKeypair();
if (!hs.e) {
throw new Error("Handshake state has keypair missing.");
}
const ne = hs.e.publicKey;
this.mixHash(hs.ss, ne);