mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 17:32:25 +00:00
Fix eslint
This commit is contained in:
parent
9c898043b8
commit
79d81c3117
4
src/@types/it-pair/index.d.ts
vendored
4
src/@types/it-pair/index.d.ts
vendored
@ -1,6 +1,6 @@
|
|||||||
export type Duplex = [Stream, Stream];
|
export type Duplex = [Stream, Stream];
|
||||||
|
|
||||||
type Stream = {
|
type Stream = {
|
||||||
sink(source: Iterable<any>),
|
sink(source: Iterable<any>);
|
||||||
source: Object,
|
source: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
10
src/@types/it-pb-rpc/index.d.ts
vendored
10
src/@types/it-pb-rpc/index.d.ts
vendored
@ -2,11 +2,11 @@ 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(bytes: number): Buffer,
|
read(bytes: number): Buffer;
|
||||||
readLP(): Buffer,
|
readLP(): Buffer;
|
||||||
write(input: Buffer): void,
|
write(input: Buffer): void;
|
||||||
writeLP(input: Buffer): void,
|
writeLP(input: Buffer): void;
|
||||||
unwrap(): Duplex
|
unwrap(): Duplex;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Wrap (duplex: any): WrappedDuplex;
|
function Wrap (duplex: any): WrappedDuplex;
|
||||||
|
@ -2,24 +2,24 @@ import { bytes, bytes32 } from "./basic";
|
|||||||
import { Duplex } from "it-pair";
|
import { Duplex } from "it-pair";
|
||||||
|
|
||||||
export interface KeyPair {
|
export interface KeyPair {
|
||||||
publicKey: bytes32,
|
publicKey: bytes32;
|
||||||
privateKey: bytes32,
|
privateKey: bytes32;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PeerId = {
|
export type PeerId = {
|
||||||
id: string,
|
id: string;
|
||||||
privKey: string,
|
privKey: string;
|
||||||
pubKey: string,
|
pubKey: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
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(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
secureInbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SecureOutbound = {
|
export type SecureOutbound = {
|
||||||
conn: Duplex,
|
conn: Duplex;
|
||||||
remotePeer: PeerId,
|
remotePeer: PeerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,14 @@ import { Duplex } from "it-pair";
|
|||||||
import { NoiseSession } from "./xx";
|
import { NoiseSession } from "./xx";
|
||||||
|
|
||||||
// Send encrypted payload from the user to stream
|
// Send encrypted payload from the user to stream
|
||||||
export async function encryptStreams(streams: Duplex, session: NoiseSession) : Promise<Duplex> {
|
export async function encryptStreams(streams: Duplex, session: NoiseSession): Promise<Duplex> {
|
||||||
|
// TODO: implement
|
||||||
|
return streams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Decrypt received payload from the stream and pipe to user
|
// Decrypt received payload from the stream and pipe to user
|
||||||
export async function decryptStreams(streams: Duplex, session: NoiseSession) : Promise<Duplex> {
|
export async function decryptStreams(streams: Duplex, session: NoiseSession): Promise<Duplex> {
|
||||||
|
// TODO: implement
|
||||||
|
return streams;
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 0
|
// stage 0
|
||||||
async propose(isInitiator: boolean, earlyData?: bytes) : Promise<NoiseSession> {
|
async propose(isInitiator: boolean, earlyData?: bytes): Promise<NoiseSession> {
|
||||||
const ns = await this.xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
const ns = await this.xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
||||||
|
|
||||||
if (isInitiator) {
|
if (isInitiator) {
|
||||||
@ -61,7 +61,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 1
|
// 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();
|
||||||
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
||||||
@ -77,7 +77,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 2
|
// 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));
|
||||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
|
@ -40,7 +40,7 @@ export class Noise implements NoiseConnection {
|
|||||||
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
|
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
|
||||||
* @returns {Promise<SecureOutbound>}
|
* @returns {Promise<SecureOutbound>}
|
||||||
*/
|
*/
|
||||||
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId) : Promise<SecureOutbound> {
|
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||||
const wrappedConnection = Wrap(connection);
|
const wrappedConnection = Wrap(connection);
|
||||||
const remotePublicKey = Buffer.from(remotePeer.pubKey);
|
const remotePublicKey = Buffer.from(remotePeer.pubKey);
|
||||||
const session = await this.createSecureConnection(wrappedConnection, remotePublicKey, true);
|
const session = await this.createSecureConnection(wrappedConnection, remotePublicKey, true);
|
||||||
@ -59,7 +59,7 @@ export class Noise implements NoiseConnection {
|
|||||||
* @returns {Promise<SecureOutbound>}
|
* @returns {Promise<SecureOutbound>}
|
||||||
*/
|
*/
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId) : Promise<SecureOutbound> {
|
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||||
return {
|
return {
|
||||||
conn: undefined,
|
conn: undefined,
|
||||||
remotePeer
|
remotePeer
|
||||||
@ -70,7 +70,7 @@ export class Noise implements NoiseConnection {
|
|||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
remotePublicKey: bytes,
|
remotePublicKey: bytes,
|
||||||
isInitiator: boolean,
|
isInitiator: boolean,
|
||||||
) : Promise<Duplex> {
|
): Promise<Duplex> {
|
||||||
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
src/utils.ts
10
src/utils.ts
@ -11,7 +11,7 @@ export async function loadPayloadProto () {
|
|||||||
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateKeypair() : KeyPair {
|
export function generateKeypair(): KeyPair {
|
||||||
const privateKey = x25519.privateKeyGenerate();
|
const privateKey = x25519.privateKeyGenerate();
|
||||||
const publicKey = x25519.publicKeyCreate(privateKey);
|
const publicKey = x25519.publicKeyCreate(privateKey);
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ export async function createHandshakePayload(
|
|||||||
signedPayload: bytes,
|
signedPayload: bytes,
|
||||||
earlyData?: bytes,
|
earlyData?: bytes,
|
||||||
libp2pPrivateKey?: bytes,
|
libp2pPrivateKey?: bytes,
|
||||||
) : Promise<bytes> {
|
): Promise<bytes> {
|
||||||
const NoiseHandshakePayload = await loadPayloadProto();
|
const NoiseHandshakePayload = await loadPayloadProto();
|
||||||
const payloadInit = NoiseHandshakePayload.create({
|
const payloadInit = NoiseHandshakePayload.create({
|
||||||
libp2pKey: libp2pPublicKey,
|
libp2pKey: libp2pPublicKey,
|
||||||
@ -46,7 +46,7 @@ export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.
|
|||||||
|
|
||||||
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
|
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
|
||||||
|
|
||||||
function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object {
|
function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes): Record<string, any> {
|
||||||
if (!earlyData || !privateKey) {
|
if (!earlyData || !privateKey) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@ -59,11 +59,11 @@ function resolveEarlyDataPayload(privateKey?: bytes, earlyData?: bytes) : Object
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encodeMessageBuffer(message: MessageBuffer) : bytes {
|
export function encodeMessageBuffer(message: MessageBuffer): bytes {
|
||||||
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
|
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodeMessageBuffer(message: bytes) : MessageBuffer {
|
export function decodeMessageBuffer(message: bytes): MessageBuffer {
|
||||||
return {
|
return {
|
||||||
ne: message.slice(0, 32),
|
ne: message.slice(0, 32),
|
||||||
ns: message.slice(32, 80),
|
ns: message.slice(32, 80),
|
||||||
|
@ -155,7 +155,7 @@ export class XXHandshake {
|
|||||||
return { cs, ck, h };
|
return { cs, ck, h };
|
||||||
}
|
}
|
||||||
|
|
||||||
private mixKey(ss: SymmetricState, ikm: bytes32) {
|
private mixKey(ss: SymmetricState, ikm: bytes32): void {
|
||||||
const [ ck, tempK ] = this.getHkdf(ss.ck, ikm);
|
const [ ck, tempK ] = this.getHkdf(ss.ck, ikm);
|
||||||
ss.cs = this.initializeKey(tempK) as CipherState;
|
ss.cs = this.initializeKey(tempK) as CipherState;
|
||||||
ss.ck = ck;
|
ss.ck = ck;
|
||||||
@ -183,7 +183,7 @@ export class XXHandshake {
|
|||||||
return [ k1, k2, k3 ];
|
return [ k1, k2, k3 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
private mixHash(ss: SymmetricState, data: bytes) {
|
private mixHash(ss: SymmetricState, data: bytes): void {
|
||||||
ss.h = this.getHash(ss.h, data);
|
ss.h = this.getHash(ss.h, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,7 +215,7 @@ export class XXHandshake {
|
|||||||
return plaintext;
|
return plaintext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private split (ss: SymmetricState) {
|
private split (ss: SymmetricState): void {
|
||||||
const [ tempk1, tempk2 ] = this.getHkdf(ss.ck, Buffer.alloc(0));
|
const [ tempk1, tempk2 ] = this.getHkdf(ss.ck, Buffer.alloc(0));
|
||||||
const cs1 = this.initializeKey(tempk1);
|
const cs1 = this.initializeKey(tempk1);
|
||||||
const cs2 = this.initializeKey(tempk2);
|
const cs2 = this.initializeKey(tempk2);
|
||||||
@ -316,7 +316,7 @@ export class XXHandshake {
|
|||||||
return this.decryptWithAd(cs, Buffer.alloc(0), message.ciphertext);
|
return this.decryptWithAd(cs, Buffer.alloc(0), message.ciphertext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initSession(initiator: boolean, prologue: bytes32, s: KeyPair, rs: bytes32) : Promise<NoiseSession> {
|
public async initSession(initiator: boolean, prologue: bytes32, s: KeyPair, rs: bytes32): Promise<NoiseSession> {
|
||||||
const psk = this.createEmptyKey();
|
const psk = this.createEmptyKey();
|
||||||
let hs;
|
let hs;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user