mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
Write handshake test
This commit is contained in:
parent
f5888f6405
commit
7e3c35ed8a
4
src/@types/it-pb-rpc/index.d.ts
vendored
4
src/@types/it-pb-rpc/index.d.ts
vendored
@ -2,8 +2,10 @@ declare module "it-pb-rpc" {
|
|||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Duplex } from "it-pair";
|
import { Duplex } from "it-pair";
|
||||||
type WrappedDuplex = {
|
type WrappedDuplex = {
|
||||||
read(input: Buffer): Buffer,
|
read(bytes: number): Buffer,
|
||||||
|
readLP(): Buffer,
|
||||||
write(input: Buffer): void,
|
write(input: Buffer): void,
|
||||||
|
writeLP(input: Buffer): void,
|
||||||
unwrap(): Duplex
|
unwrap(): Duplex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,14 @@ import { bytes, bytes32 } from "./@types/basic";
|
|||||||
import { NoiseSession, XXHandshake } from "./xx";
|
import { NoiseSession, XXHandshake } from "./xx";
|
||||||
import { KeyPair } from "./@types/libp2p";
|
import { KeyPair } from "./@types/libp2p";
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import {createHandshakePayload, getHandshakePayload, signPayload} from "./utils";
|
import {
|
||||||
|
createHandshakePayload,
|
||||||
|
decodeMessageBuffer,
|
||||||
|
encodeMessageBuffer,
|
||||||
|
getHandshakePayload,
|
||||||
|
signPayload
|
||||||
|
} from "./utils";
|
||||||
|
import { WrappedConnection } from "./noise";
|
||||||
|
|
||||||
type handshakeType = "XX";
|
type handshakeType = "XX";
|
||||||
|
|
||||||
@ -11,7 +18,7 @@ export class Handshake {
|
|||||||
private remotePublicKey: bytes;
|
private remotePublicKey: bytes;
|
||||||
private prologue: bytes32;
|
private prologue: bytes32;
|
||||||
private staticKeys: KeyPair;
|
private staticKeys: KeyPair;
|
||||||
private connection: any;
|
private connection: WrappedConnection;
|
||||||
private xx: XXHandshake;
|
private xx: XXHandshake;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -19,7 +26,7 @@ export class Handshake {
|
|||||||
remotePublicKey: bytes,
|
remotePublicKey: bytes,
|
||||||
prologue: bytes32,
|
prologue: bytes32,
|
||||||
staticKeys: KeyPair,
|
staticKeys: KeyPair,
|
||||||
connection,
|
connection: WrappedConnection,
|
||||||
) {
|
) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.remotePublicKey = remotePublicKey;
|
this.remotePublicKey = remotePublicKey;
|
||||||
@ -44,10 +51,10 @@ export class Handshake {
|
|||||||
);
|
);
|
||||||
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
||||||
const messageBuffer = await this.xx.sendMessage(ns, message);
|
const messageBuffer = await this.xx.sendMessage(ns, message);
|
||||||
this.connection.writeLP(messageBuffer);
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
} else {
|
} else {
|
||||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||||
const plaintext = await this.xx.recvMessage(ns, receivedMessageBuffer);
|
const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ns;
|
return ns;
|
||||||
@ -57,7 +64,7 @@ export class Handshake {
|
|||||||
async exchange(isInitiator: boolean, session: NoiseSession) : Promise<void> {
|
async exchange(isInitiator: boolean, session: NoiseSession) : Promise<void> {
|
||||||
if (isInitiator) {
|
if (isInitiator) {
|
||||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||||
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
|
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
||||||
} else {
|
} else {
|
||||||
// create payload as responder
|
// create payload as responder
|
||||||
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||||
@ -65,7 +72,7 @@ export class Handshake {
|
|||||||
|
|
||||||
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
||||||
const messageBuffer = await this.xx.sendMessage(session, message);
|
const messageBuffer = await this.xx.sendMessage(session, message);
|
||||||
this.connection.writeLP(messageBuffer);
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,10 +80,10 @@ export class Handshake {
|
|||||||
async finish(isInitiator: boolean, session: NoiseSession) : Promise<void> {
|
async finish(isInitiator: boolean, session: NoiseSession) : Promise<void> {
|
||||||
if (isInitiator) {
|
if (isInitiator) {
|
||||||
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
|
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
|
||||||
this.connection.writeLP(messageBuffer);
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
} else {
|
} else {
|
||||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||||
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
|
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import { bytes } from "./@types/basic";
|
|||||||
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
||||||
import { Duplex } from "./@types/it-pair";
|
import { Duplex } from "./@types/it-pair";
|
||||||
|
|
||||||
type WrappedConnection = ReturnType<typeof Wrap>;
|
export type WrappedConnection = ReturnType<typeof Wrap>;
|
||||||
|
|
||||||
export class Noise implements NoiseConnection {
|
export class Noise implements NoiseConnection {
|
||||||
public protocol = "/noise";
|
public protocol = "/noise";
|
||||||
|
15
src/utils.ts
15
src/utils.ts
@ -1,9 +1,10 @@
|
|||||||
import { x25519, ed25519 } from 'bcrypto';
|
import { x25519, ed25519 } from 'bcrypto';
|
||||||
import protobuf from "protobufjs";
|
import protobuf from "protobufjs";
|
||||||
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
import { KeyPair } from "./@types/libp2p";
|
import { KeyPair } from "./@types/libp2p";
|
||||||
import { bytes } from "./@types/basic";
|
import { bytes } from "./@types/basic";
|
||||||
import {Buffer} from "buffer";
|
import { MessageBuffer } from "./xx";
|
||||||
|
|
||||||
export async function loadPayloadProto () {
|
export async function loadPayloadProto () {
|
||||||
const payloadProtoBuf = await protobuf.load("protos/payload.proto");
|
const payloadProtoBuf = await protobuf.load("protos/payload.proto");
|
||||||
@ -58,3 +59,15 @@ function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function encodeMessageBuffer(message: MessageBuffer) : bytes {
|
||||||
|
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodeMessageBuffer(message: bytes) : MessageBuffer {
|
||||||
|
return {
|
||||||
|
ne: message.slice(0, 32),
|
||||||
|
ns: message.slice(32, 80),
|
||||||
|
ciphertext: message.slice(80, message.length),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import { KeyPair } from './@types/libp2p'
|
|||||||
import { generateKeypair } from './utils';
|
import { generateKeypair } from './utils';
|
||||||
|
|
||||||
|
|
||||||
interface MessageBuffer {
|
export interface MessageBuffer {
|
||||||
ne: bytes32,
|
ne: bytes32,
|
||||||
ns: bytes,
|
ns: bytes,
|
||||||
ciphertext: bytes
|
ciphertext: bytes
|
||||||
@ -277,6 +277,7 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async readMessageA(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
private async readMessageA(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
||||||
|
console.log("publci key: ", message.ne)
|
||||||
if (x25519.publicKeyVerify(message.ne)) {
|
if (x25519.publicKeyVerify(message.ne)) {
|
||||||
hs.re = message.ne;
|
hs.re = message.ne;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,39 @@
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import DuplexPair from 'it-pair/duplex';
|
import Duplex from 'it-pair/duplex';
|
||||||
|
import {Buffer} from "buffer";
|
||||||
|
import Wrap from "it-pb-rpc";
|
||||||
|
|
||||||
import { Noise } from "../src";
|
import {Handshake} from "../src/handshake";
|
||||||
import {generateEd25519Keys} from "./utils";
|
import {generateKeypair} from "../src/utils";
|
||||||
|
|
||||||
describe("Handshake", () => {
|
describe("Handshake", () => {
|
||||||
it("should propose, exchange and finish handshake", async() => {
|
it("should propose, exchange and finish handshake", async() => {
|
||||||
|
const duplex = Duplex();
|
||||||
|
const connectionFrom = Wrap(duplex[0]);
|
||||||
|
const connectionTo = Wrap(duplex[1]);
|
||||||
|
|
||||||
|
const prologue = Buffer.from('/noise');
|
||||||
|
const staticKeysInitiator = generateKeypair();
|
||||||
|
const staticKeysResponder = generateKeypair();
|
||||||
|
|
||||||
|
const handshakeInitator = new Handshake('XX', staticKeysResponder.publicKey, prologue, staticKeysInitiator, connectionFrom);
|
||||||
|
const handshakeResponder = new Handshake('XX', staticKeysInitiator.publicKey, prologue, staticKeysResponder, connectionTo);
|
||||||
|
|
||||||
|
console.log("Going to start with the handshake process...")
|
||||||
|
|
||||||
|
const sessionInitator = await handshakeInitator.propose(true);
|
||||||
|
const sessionResponder = await handshakeResponder.propose(false);
|
||||||
|
|
||||||
|
console.log("Propose finished")
|
||||||
|
|
||||||
|
await handshakeInitator.exchange(true, sessionInitator);
|
||||||
|
await handshakeResponder.exchange(false, sessionResponder);
|
||||||
|
|
||||||
|
console.log("exchange finished")
|
||||||
|
|
||||||
|
await handshakeInitator.finish(true, sessionInitator);
|
||||||
|
await handshakeResponder.finish(false, sessionResponder);
|
||||||
|
|
||||||
|
console.log("finish finished")
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user