Create handshake handler files

This commit is contained in:
Belma Gutlic
2020-01-03 14:53:14 +01:00
parent b084207c52
commit 2bb63e3e91
6 changed files with 51 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import { Buffer } from "buffer";
import { Handshake } from "./handshake";
import { Handshake } from "./handshake-xx";
interface ReturnEncryptionWrapper {
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>;

44
src/handshake-ik.ts Normal file
View File

@ -0,0 +1,44 @@
import {NoiseSession} from "./@types/handshake";
import {bytes, bytes32} from "./@types/basic";
import {KeyPair, PeerId} from "./@types/libp2p";
import {WrappedConnection} from "./noise";
import {IKHandshake} from "./handshakes/ik";
export class Handshake {
public isInitiator: boolean;
public session: NoiseSession;
private libp2pPrivateKey: bytes;
private libp2pPublicKey: bytes;
private prologue: bytes32;
private staticKeys: KeyPair;
private connection: WrappedConnection;
private remotePeer: PeerId;
private ik: IKHandshake;
constructor(
isInitiator: boolean,
libp2pPrivateKey: bytes,
libp2pPublicKey: bytes,
prologue: bytes32,
staticKeys: KeyPair,
connection: WrappedConnection,
remotePeer: PeerId,
handshake?: IKHandshake,
) {
this.isInitiator = isInitiator;
this.libp2pPrivateKey = libp2pPrivateKey;
this.libp2pPublicKey = libp2pPublicKey;
this.prologue = prologue;
this.staticKeys = staticKeys;
this.connection = connection;
this.remotePeer = remotePeer;
this.ik = handshake || new IKHandshake();
// Dummy data
// TODO: Load remote static keys if found
const remoteStaticKeys = this.staticKeys;
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeys, remoteStaticKeys.publicKey);
}
}

View File

@ -6,7 +6,7 @@ import ensureBuffer from 'it-buffer';
import pipe from 'it-pipe';
import lp from 'it-length-prefixed';
import { Handshake } from "./handshake";
import { Handshake } from "./handshake-xx";
import { generateKeypair } from "./utils";
import { uint16BEDecode, uint16BEEncode } from "./encoder";
import { decryptStream, encryptStream } from "./crypto";
@ -48,7 +48,7 @@ export class Noise implements NoiseConnection {
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
const wrappedConnection = Wrap(connection);
const libp2pPublicKey = localPeer.marshalPubKey();
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer);
const handshake = await this.performXXHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer);
const conn = await this.createSecureConnection(wrappedConnection, handshake);
return {
@ -67,7 +67,7 @@ export class Noise implements NoiseConnection {
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
const wrappedConnection = Wrap(connection);
const libp2pPublicKey = localPeer.marshalPubKey();
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer);
const handshake = await this.performXXHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer);
const conn = await this.createSecureConnection(wrappedConnection, handshake);
return {
@ -76,7 +76,7 @@ export class Noise implements NoiseConnection {
};
}
private async performHandshake(
private async performXXHandshake(
connection: WrappedConnection,
isInitiator: boolean,
libp2pPublicKey: bytes,