handshake propose

This commit is contained in:
morrigan
2019-11-20 22:52:08 +01:00
parent af95dc2fcd
commit f6bc40baf4
2 changed files with 19 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession, XXHandshake } from "./xx";
import { KeyPair, PeerId } from "./@types/libp2p";
import {Buffer} from "buffer";
import { KeyPair } from "./@types/libp2p";
import { Buffer } from "buffer";
type handshakeType = "XX";
@ -11,6 +11,7 @@ export class Handshake {
private signedPayload: bytes;
private prologue: bytes32;
private staticKeys: KeyPair;
private connection: any;
constructor(
type: handshakeType,
@ -18,23 +19,32 @@ export class Handshake {
prologue: bytes32,
signedPayload: bytes,
staticKeys: KeyPair,
connection,
) {
this.type = type;
this.remotePublicKey = remotePublicKey;
this.signedPayload = signedPayload;
this.prologue = prologue;
this.staticKeys = staticKeys;
this.connection = connection;
}
// stage 0
async propose(isInitiator: boolean) : Promise<NoiseSession> {
const xx = new XXHandshake();
const nsInit = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
const ns = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
if (isInitiator) {
const message = Buffer.concat([Buffer.alloc(0), this.signedPayload]);
const messageBuffer = await xx.sendMessage(nsInit, message);
const messageBuffer = await xx.sendMessage(ns, message);
this.connection.writeLP(messageBuffer);
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await xx.recvMessage(ns, receivedMessageBuffer);
}
return nsInit;
return ns;
}
async exchange() : Promise<NoiseSession> {