mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-05-30 02:31:20 +00:00
Fix tslint
This commit is contained in:
parent
b2d058291c
commit
f5888f6405
@ -15,7 +15,7 @@ export type PeerId = {
|
|||||||
export interface NoiseConnection {
|
export interface NoiseConnection {
|
||||||
remoteEarlyData?(): bytes,
|
remoteEarlyData?(): bytes,
|
||||||
secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
||||||
secureInbound(remotePeer: PeerId, insecure: any): Promise<SecureOutbound>,
|
secureInbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SecureOutbound = {
|
export type SecureOutbound = {
|
||||||
|
@ -53,6 +53,7 @@ export class Handshake {
|
|||||||
return ns;
|
return ns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stage 1
|
||||||
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();
|
||||||
@ -68,6 +69,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stage 2
|
||||||
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));
|
||||||
|
23
src/noise.ts
23
src/noise.ts
@ -3,18 +3,20 @@ import { Buffer } from "buffer";
|
|||||||
import Wrap from 'it-pb-rpc';
|
import Wrap from 'it-pb-rpc';
|
||||||
|
|
||||||
import { Handshake } from "./handshake";
|
import { Handshake } from "./handshake";
|
||||||
import { createHandshakePayload, generateKeypair, getHandshakePayload, signPayload } from "./utils";
|
import { generateKeypair } from "./utils";
|
||||||
import { decryptStreams, encryptStreams } from "./crypto";
|
import { decryptStreams, encryptStreams } from "./crypto";
|
||||||
import { bytes } from "./@types/basic";
|
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 class Noise implements NoiseConnection {
|
export class Noise implements NoiseConnection {
|
||||||
public protocol = "/noise";
|
public protocol = "/noise";
|
||||||
|
|
||||||
private readonly privateKey: bytes;
|
private readonly privateKey: bytes;
|
||||||
private staticKeys: KeyPair;
|
private readonly staticKeys: KeyPair;
|
||||||
private earlyData?: bytes;
|
private readonly earlyData?: bytes;
|
||||||
|
|
||||||
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
|
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
|
||||||
this.privateKey = privateKey;
|
this.privateKey = privateKey;
|
||||||
@ -27,7 +29,7 @@ export class Noise implements NoiseConnection {
|
|||||||
publicKey,
|
publicKey,
|
||||||
}
|
}
|
||||||
} else {
|
} 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.
|
* @param {PeerId} remotePeer - optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.
|
||||||
* @returns {Promise<SecureOutbound>}
|
* @returns {Promise<SecureOutbound>}
|
||||||
*/
|
*/
|
||||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer?: PeerId) : Promise<SecureOutbound> {
|
// tslint:disable-next-line
|
||||||
|
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId) : Promise<SecureOutbound> {
|
||||||
|
return {
|
||||||
|
conn: undefined,
|
||||||
|
remotePeer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createSecureConnection(
|
private async createSecureConnection(
|
||||||
connection,
|
connection: WrappedConnection,
|
||||||
remotePublicKey: bytes,
|
remotePublicKey: bytes,
|
||||||
isInitiator: boolean,
|
isInitiator: boolean,
|
||||||
) : Promise<Duplex> {
|
) : Promise<Duplex> {
|
||||||
if (!this.staticKeys) {
|
|
||||||
this.staticKeys = await generateKeypair();
|
|
||||||
}
|
|
||||||
|
|
||||||
const prologue = Buffer.from(this.protocol);
|
const prologue = Buffer.from(this.protocol);
|
||||||
const handshake = new Handshake('XX', remotePublicKey, prologue, this.staticKeys, connection);
|
const handshake = new Handshake('XX', remotePublicKey, prologue, this.staticKeys, connection);
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ export async function loadPayloadProto () {
|
|||||||
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateKeypair() : Promise<KeyPair> {
|
export function generateKeypair() : KeyPair {
|
||||||
const privateKey = x25519.privateKeyGenerate();
|
const privateKey = x25519.privateKeyGenerate();
|
||||||
const publicKey = x25519.publicKeyCreate(privateKey);
|
const publicKey = x25519.publicKeyCreate(privateKey);
|
||||||
|
|
||||||
|
@ -226,7 +226,7 @@ export class XXHandshake {
|
|||||||
|
|
||||||
private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
||||||
let ns = Buffer.alloc(0);
|
let ns = Buffer.alloc(0);
|
||||||
hs.e = await generateKeypair();
|
hs.e = generateKeypair();
|
||||||
if (!hs.e) {
|
if (!hs.e) {
|
||||||
throw new Error("Handshake state has keypair missing.");
|
throw new Error("Handshake state has keypair missing.");
|
||||||
}
|
}
|
||||||
@ -239,7 +239,7 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
||||||
hs.e = await generateKeypair();
|
hs.e = generateKeypair();
|
||||||
if (!hs.e) {
|
if (!hs.e) {
|
||||||
throw new Error("Handshake state has keypair missing.");
|
throw new Error("Handshake state has keypair missing.");
|
||||||
}
|
}
|
||||||
|
11
test/handshake.test.ts
Normal file
11
test/handshake.test.ts
Normal file
@ -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() => {
|
||||||
|
|
||||||
|
})
|
||||||
|
});
|
@ -33,8 +33,8 @@ describe("Index", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function doHandshake(xx) {
|
async function doHandshake(xx) {
|
||||||
const kpInit = await xx.generateKeypair();
|
const kpInit = await generateKeypair();
|
||||||
const kpResp = await xx.generateKeypair();
|
const kpResp = await generateKeypair();
|
||||||
|
|
||||||
// initiator setup
|
// initiator setup
|
||||||
const libp2pInitKeys = await generateEd25519Keys();
|
const libp2pInitKeys = await generateEd25519Keys();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user