mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-14 08:01:42 +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 { Buffer } from "buffer";
|
||||||
import { Duplex } from "it-pair";
|
import { Duplex } from "it-pair";
|
||||||
type WrappedDuplex = {
|
type WrappedDuplex = {
|
||||||
read(bytes?: number): Promise<Buffer>,
|
read(bytes?: number): Promise<Buffer>;
|
||||||
readLP(): Promise<Buffer>,
|
readLP(): Promise<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,28 +2,28 @@ 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: bytes,
|
id: bytes;
|
||||||
privKey: {
|
privKey: {
|
||||||
marshal(): bytes,
|
marshal(): bytes;
|
||||||
},
|
};
|
||||||
pubKey: {
|
pubKey: {
|
||||||
marshal(): bytes,
|
marshal(): bytes;
|
||||||
},
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
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,12 @@ import { Duplex } from "it-pair";
|
|||||||
import { NoiseSession } from "./xx";
|
import { NoiseSession } from "./xx";
|
||||||
import { Handshake } from "./handshake";
|
import { Handshake } from "./handshake";
|
||||||
|
|
||||||
interface IReturnEncryptionWrapper {
|
interface ReturnEncryptionWrapper {
|
||||||
(source: any): any;
|
(source: any): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns generator that encrypts payload from the user
|
// 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) {
|
return async function * (source) {
|
||||||
for await (const chunk of source) {
|
for await (const chunk of source) {
|
||||||
const data = await handshake.encrypt(chunk, session);
|
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
|
// 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) {
|
return async function * (source) {
|
||||||
for await (const chunk of source) {
|
for await (const chunk of source) {
|
||||||
const decrypted = await handshake.decrypt(chunk, session);
|
const decrypted = await handshake.decrypt(chunk, session);
|
||||||
|
@ -45,7 +45,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 0
|
// stage 0
|
||||||
async propose(earlyData?: bytes) : Promise<void> {
|
async propose(earlyData?: bytes): Promise<void> {
|
||||||
if (this.isInitiator) {
|
if (this.isInitiator) {
|
||||||
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||||
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
|
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
|
||||||
@ -68,7 +68,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 1
|
// stage 1
|
||||||
async exchange() : Promise<void> {
|
async exchange(): Promise<void> {
|
||||||
if (this.isInitiator) {
|
if (this.isInitiator) {
|
||||||
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
|
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
|
||||||
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
|
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||||
@ -89,7 +89,7 @@ export class Handshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stage 2
|
// stage 2
|
||||||
async finish() : Promise<void> {
|
async finish(): Promise<void> {
|
||||||
if (this.isInitiator) {
|
if (this.isInitiator) {
|
||||||
const messageBuffer = await this.xx.sendMessage(this.session, Buffer.alloc(0));
|
const messageBuffer = await this.xx.sendMessage(this.session, Buffer.alloc(0));
|
||||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
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.
|
* @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 libp2pPublicKey = localPeer.pubKey.marshal();
|
const libp2pPublicKey = localPeer.pubKey.marshal();
|
||||||
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey);
|
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.
|
* @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> {
|
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||||
const wrappedConnection = Wrap(connection);
|
const wrappedConnection = Wrap(connection);
|
||||||
const libp2pPublicKey = localPeer.pubKey.marshal();
|
const libp2pPublicKey = localPeer.pubKey.marshal();
|
||||||
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey);
|
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey);
|
||||||
@ -94,7 +94,7 @@ export class Noise implements NoiseConnection {
|
|||||||
private async createSecureConnection(
|
private async createSecureConnection(
|
||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
handshake: Handshake,
|
handshake: Handshake,
|
||||||
) : Promise<Duplex> {
|
): Promise<Duplex> {
|
||||||
// Create encryption box/unbox wrapper
|
// Create encryption box/unbox wrapper
|
||||||
const [secure, user] = DuplexPair();
|
const [secure, user] = DuplexPair();
|
||||||
const network = connection.unwrap();
|
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");
|
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);
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ export async function createHandshakePayload(
|
|||||||
libp2pPrivateKey: bytes,
|
libp2pPrivateKey: bytes,
|
||||||
signedPayload: bytes,
|
signedPayload: bytes,
|
||||||
signedEarlyData?: EarlyDataPayload,
|
signedEarlyData?: EarlyDataPayload,
|
||||||
) : Promise<bytes> {
|
): Promise<bytes> {
|
||||||
const NoiseHandshakePayload = await loadPayloadProto();
|
const NoiseHandshakePayload = await loadPayloadProto();
|
||||||
const earlyDataPayload = signedEarlyData ?
|
const earlyDataPayload = signedEarlyData ?
|
||||||
{
|
{
|
||||||
@ -56,7 +56,7 @@ type EarlyDataPayload = {
|
|||||||
libp2pDataSignature: bytes;
|
libp2pDataSignature: bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function signEarlyDataPayload(libp2pPrivateKey: bytes, earlyData: bytes) : EarlyDataPayload {
|
export function signEarlyDataPayload(libp2pPrivateKey: bytes, earlyData: bytes): EarlyDataPayload {
|
||||||
const payload = getEarlyDataPayload(earlyData);
|
const payload = getEarlyDataPayload(earlyData);
|
||||||
const signedPayload = signPayload(libp2pPrivateKey, payload);
|
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 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]);
|
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, 64),
|
ns: message.slice(32, 64),
|
||||||
|
@ -150,7 +150,7 @@ export class XXHandshake {
|
|||||||
|
|
||||||
const ck = h;
|
const ck = h;
|
||||||
const key = this.createEmptyKey();
|
const key = this.createEmptyKey();
|
||||||
const cs:CipherState = this.initializeKey(key);
|
const cs: CipherState = this.initializeKey(key);
|
||||||
|
|
||||||
return { cs, ck, h };
|
return { cs, ck, h };
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user