diff --git a/src/@types/libp2p.ts b/src/@types/libp2p.ts index ce33941..edf4dbc 100644 --- a/src/@types/libp2p.ts +++ b/src/@types/libp2p.ts @@ -15,7 +15,7 @@ export type PeerId = { export interface NoiseConnection { remoteEarlyData?(): bytes, secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise, - secureInbound(remotePeer: PeerId, insecure: any): Promise, + secureInbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise, } export type SecureOutbound = { diff --git a/src/handshake.ts b/src/handshake.ts index ecc3fda..b3eee05 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -53,6 +53,7 @@ export class Handshake { return ns; } + // stage 1 async exchange(isInitiator: boolean, session: NoiseSession) : Promise { if (isInitiator) { const receivedMessageBuffer = (await this.connection.readLP()).slice(); @@ -68,6 +69,7 @@ export class Handshake { } } + // stage 2 async finish(isInitiator: boolean, session: NoiseSession) : Promise { if (isInitiator) { const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0)); diff --git a/src/noise.ts b/src/noise.ts index 263ef92..e936609 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -3,18 +3,20 @@ import { Buffer } from "buffer"; import Wrap from 'it-pb-rpc'; import { Handshake } from "./handshake"; -import { createHandshakePayload, generateKeypair, getHandshakePayload, signPayload } from "./utils"; +import { generateKeypair } from "./utils"; import { decryptStreams, encryptStreams } from "./crypto"; import { bytes } from "./@types/basic"; import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p"; import { Duplex } from "./@types/it-pair"; +type WrappedConnection = ReturnType; + export class Noise implements NoiseConnection { public protocol = "/noise"; private readonly privateKey: bytes; - private staticKeys: KeyPair; - private earlyData?: bytes; + private readonly staticKeys: KeyPair; + private readonly earlyData?: bytes; constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) { this.privateKey = privateKey; @@ -27,7 +29,7 @@ export class Noise implements NoiseConnection { publicKey, } } else { - // todo: generate new static key + this.staticKeys = generateKeypair(); } } @@ -56,18 +58,19 @@ export class Noise implements NoiseConnection { * @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades. * @returns {Promise} */ - public async secureInbound(localPeer: PeerId, connection: any, remotePeer?: PeerId) : Promise { + // tslint:disable-next-line + public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId) : Promise { + return { + conn: undefined, + remotePeer + } } private async createSecureConnection( - connection, + connection: WrappedConnection, remotePublicKey: bytes, isInitiator: boolean, ) : Promise { - if (!this.staticKeys) { - this.staticKeys = await generateKeypair(); - } - const prologue = Buffer.from(this.protocol); const handshake = new Handshake('XX', remotePublicKey, prologue, this.staticKeys, connection); diff --git a/src/utils.ts b/src/utils.ts index 6c3b236..8fe5d09 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -10,7 +10,7 @@ export async function loadPayloadProto () { return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload"); } -export async function generateKeypair() : Promise { +export function generateKeypair() : KeyPair { const privateKey = x25519.privateKeyGenerate(); const publicKey = x25519.publicKeyCreate(privateKey); diff --git a/src/xx.ts b/src/xx.ts index e78b182..3b1299c 100644 --- a/src/xx.ts +++ b/src/xx.ts @@ -226,7 +226,7 @@ export class XXHandshake { private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise { let ns = Buffer.alloc(0); - hs.e = await generateKeypair(); + hs.e = generateKeypair(); if (!hs.e) { throw new Error("Handshake state has keypair missing."); } @@ -239,7 +239,7 @@ export class XXHandshake { } private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise { - hs.e = await generateKeypair(); + hs.e = generateKeypair(); if (!hs.e) { throw new Error("Handshake state has keypair missing."); } diff --git a/test/handshake.test.ts b/test/handshake.test.ts new file mode 100644 index 0000000..d79a80b --- /dev/null +++ b/test/handshake.test.ts @@ -0,0 +1,11 @@ +import { expect } from "chai"; +import DuplexPair from 'it-pair/duplex'; + +import { Noise } from "../src"; +import {generateEd25519Keys} from "./utils"; + +describe("Handshake", () => { + it("should propose, exchange and finish handshake", async() => { + + }) +}); diff --git a/test/xx.test.ts b/test/xx.test.ts index 9702140..afd7f9d 100644 --- a/test/xx.test.ts +++ b/test/xx.test.ts @@ -33,8 +33,8 @@ describe("Index", () => { }); async function doHandshake(xx) { - const kpInit = await xx.generateKeypair(); - const kpResp = await xx.generateKeypair(); + const kpInit = await generateKeypair(); + const kpResp = await generateKeypair(); // initiator setup const libp2pInitKeys = await generateEd25519Keys();