mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-11 00:41:34 +00:00
Fix eslint
This commit is contained in:
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 { Duplex } from "it-pair";
|
||||
type WrappedDuplex = {
|
||||
read(bytes?: number): Promise<Buffer>,
|
||||
readLP(): Promise<Buffer>,
|
||||
write(input: Buffer): void,
|
||||
writeLP(input: Buffer): void,
|
||||
unwrap(): Duplex
|
||||
read(bytes?: number): Promise<Buffer>;
|
||||
readLP(): Promise<Buffer>;
|
||||
write(input: Buffer): void;
|
||||
writeLP(input: Buffer): void;
|
||||
unwrap(): Duplex;
|
||||
}
|
||||
|
||||
function Wrap (duplex: any): WrappedDuplex;
|
||||
|
@ -2,28 +2,28 @@ import { bytes, bytes32 } from "./basic";
|
||||
import { Duplex } from "it-pair";
|
||||
|
||||
export interface KeyPair {
|
||||
publicKey: bytes32,
|
||||
privateKey: bytes32,
|
||||
publicKey: bytes32;
|
||||
privateKey: bytes32;
|
||||
}
|
||||
|
||||
export type PeerId = {
|
||||
id: bytes,
|
||||
id: bytes;
|
||||
privKey: {
|
||||
marshal(): bytes,
|
||||
},
|
||||
marshal(): bytes;
|
||||
};
|
||||
pubKey: {
|
||||
marshal(): bytes,
|
||||
},
|
||||
marshal(): bytes;
|
||||
};
|
||||
};
|
||||
|
||||
export interface NoiseConnection {
|
||||
remoteEarlyData?(): bytes,
|
||||
secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
||||
secureInbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>,
|
||||
remoteEarlyData?(): bytes;
|
||||
secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>;
|
||||
secureInbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>;
|
||||
}
|
||||
|
||||
export type SecureOutbound = {
|
||||
conn: Duplex,
|
||||
remotePeer: PeerId,
|
||||
conn: Duplex;
|
||||
remotePeer: PeerId;
|
||||
}
|
||||
|
||||
|
@ -2,12 +2,12 @@ import { Duplex } from "it-pair";
|
||||
import { NoiseSession } from "./xx";
|
||||
import { Handshake } from "./handshake";
|
||||
|
||||
interface IReturnEncryptionWrapper {
|
||||
interface ReturnEncryptionWrapper {
|
||||
(source: any): any;
|
||||
}
|
||||
|
||||
// Returns generator that encrypts payload from the user
|
||||
export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
||||
export function encryptStream(handshake: Handshake, session: NoiseSession): ReturnEncryptionWrapper {
|
||||
return async function * (source) {
|
||||
for await (const chunk of source) {
|
||||
const data = await handshake.encrypt(chunk, session);
|
||||
@ -18,7 +18,7 @@ export function encryptStream(handshake: Handshake, session: NoiseSession) : IRe
|
||||
|
||||
|
||||
// Decrypt received payload to the user
|
||||
export function decryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
||||
export function decryptStream(handshake: Handshake, session: NoiseSession): ReturnEncryptionWrapper {
|
||||
return async function * (source) {
|
||||
for await (const chunk of source) {
|
||||
const decrypted = await handshake.decrypt(chunk, session);
|
||||
|
@ -45,7 +45,7 @@ export class Handshake {
|
||||
}
|
||||
|
||||
// stage 0
|
||||
async propose(earlyData?: bytes) : Promise<void> {
|
||||
async propose(earlyData?: bytes): Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
|
||||
@ -68,7 +68,7 @@ export class Handshake {
|
||||
}
|
||||
|
||||
// stage 1
|
||||
async exchange() : Promise<void> {
|
||||
async exchange(): Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
|
||||
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||
@ -89,7 +89,7 @@ export class Handshake {
|
||||
}
|
||||
|
||||
// stage 2
|
||||
async finish() : Promise<void> {
|
||||
async finish(): Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const messageBuffer = await this.xx.sendMessage(this.session, Buffer.alloc(0));
|
||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||
|
@ -45,7 +45,7 @@ export class Noise implements NoiseConnection {
|
||||
* @param {PeerId} remotePeer - PeerId of the remote peer. Used to validate the integrity of the remote peer.
|
||||
* @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 libp2pPublicKey = localPeer.pubKey.marshal();
|
||||
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey);
|
||||
@ -64,7 +64,7 @@ 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<SecureOutbound>}
|
||||
*/
|
||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId) : Promise<SecureOutbound> {
|
||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||
const wrappedConnection = Wrap(connection);
|
||||
const libp2pPublicKey = localPeer.pubKey.marshal();
|
||||
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey);
|
||||
@ -94,7 +94,7 @@ export class Noise implements NoiseConnection {
|
||||
private async createSecureConnection(
|
||||
connection: WrappedConnection,
|
||||
handshake: Handshake,
|
||||
) : Promise<Duplex> {
|
||||
): Promise<Duplex> {
|
||||
// Create encryption box/unbox wrapper
|
||||
const [secure, user] = DuplexPair();
|
||||
const network = connection.unwrap();
|
||||
|
10
src/utils.ts
10
src/utils.ts
@ -14,7 +14,7 @@ export async function loadPayloadProto () {
|
||||
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");
|
||||
}
|
||||
|
||||
export function generateKeypair() : KeyPair {
|
||||
export function generateKeypair(): KeyPair {
|
||||
const privateKey = x25519.privateKeyGenerate();
|
||||
const publicKey = x25519.publicKeyCreate(privateKey);
|
||||
|
||||
@ -29,7 +29,7 @@ export async function createHandshakePayload(
|
||||
libp2pPrivateKey: bytes,
|
||||
signedPayload: bytes,
|
||||
signedEarlyData?: EarlyDataPayload,
|
||||
) : Promise<bytes> {
|
||||
): Promise<bytes> {
|
||||
const NoiseHandshakePayload = await loadPayloadProto();
|
||||
const earlyDataPayload = signedEarlyData ?
|
||||
{
|
||||
@ -56,7 +56,7 @@ type EarlyDataPayload = {
|
||||
libp2pDataSignature: bytes;
|
||||
}
|
||||
|
||||
export function signEarlyDataPayload(libp2pPrivateKey: bytes, earlyData: bytes) : EarlyDataPayload {
|
||||
export function signEarlyDataPayload(libp2pPrivateKey: bytes, earlyData: bytes): EarlyDataPayload {
|
||||
const payload = getEarlyDataPayload(earlyData);
|
||||
const signedPayload = signPayload(libp2pPrivateKey, payload);
|
||||
|
||||
@ -70,11 +70,11 @@ export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.
|
||||
|
||||
export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]);
|
||||
|
||||
export function encodeMessageBuffer(message: MessageBuffer) : bytes {
|
||||
export function encodeMessageBuffer(message: MessageBuffer): bytes {
|
||||
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
|
||||
}
|
||||
|
||||
export function decodeMessageBuffer(message: bytes) : MessageBuffer {
|
||||
export function decodeMessageBuffer(message: bytes): MessageBuffer {
|
||||
return {
|
||||
ne: message.slice(0, 32),
|
||||
ns: message.slice(32, 64),
|
||||
|
Reference in New Issue
Block a user