mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-19 13:41:40 +00:00
Add types and encryption methods
This commit is contained in:
13
src/crypto.ts
Normal file
13
src/crypto.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Duplex } from "./types/libp2p";
|
||||
import { NoiseSession } from "./xx";
|
||||
|
||||
// Send encrypted payload from the user to stream
|
||||
export async function encryptStreams(streams: Duplex, session: NoiseSession) : Promise<void> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Decrypt received payload from the stream and pipe to user
|
||||
export async function decryptStreams(streams: Duplex, session: NoiseSession) : Promise<void> {
|
||||
|
||||
}
|
19
src/noise.ts
19
src/noise.ts
@ -6,10 +6,11 @@ import { InsecureConnection, NoiseConnection, PeerId, SecureConnection, KeyPair
|
||||
|
||||
import { Handshake } from "./handshake";
|
||||
import { generateKeypair, signPayload } from "./utils";
|
||||
import {encryptStream} from "./crypto";
|
||||
|
||||
export class Noise implements NoiseConnection {
|
||||
private readonly privateKey: bytes;
|
||||
private staticKeys?: KeyPair;
|
||||
private staticKeys: KeyPair;
|
||||
private earlyData?: bytes;
|
||||
|
||||
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
|
||||
@ -22,6 +23,8 @@ export class Noise implements NoiseConnection {
|
||||
privateKey: staticNoiseKey,
|
||||
publicKey,
|
||||
}
|
||||
} else {
|
||||
// todo: generate new static key
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,14 +46,6 @@ export class Noise implements NoiseConnection {
|
||||
public async secureInbound(connection: InsecureConnection) : Promise<SecureConnection> {
|
||||
}
|
||||
|
||||
private async read(ciphertext: bytes) {
|
||||
|
||||
}
|
||||
|
||||
private async write(plaintext: bytes) {
|
||||
|
||||
}
|
||||
|
||||
private async createSecureConnection(
|
||||
connection: InsecureConnection,
|
||||
remotePublicKey: bytes,
|
||||
@ -69,13 +64,13 @@ export class Noise implements NoiseConnection {
|
||||
const prologue = Buffer.from(this.protocol());
|
||||
const session = await Handshake.runXX(isInitiator, remotePublicKey, prologue, signedPayload, this.staticKeys);
|
||||
|
||||
await encryptStream(connection.streams, session);
|
||||
|
||||
return {
|
||||
insecure: connection,
|
||||
...connection,
|
||||
initiator: isInitiator,
|
||||
prologue,
|
||||
// localKey: get public key,
|
||||
localPeer: connection.localPeer,
|
||||
remotePeer: connection.remotePeer,
|
||||
local: {
|
||||
noiseKey: this.staticKeys.publicKey,
|
||||
// libp2pKey:
|
||||
|
@ -22,16 +22,23 @@ type ConnectionStats = {
|
||||
encryption: string,
|
||||
}
|
||||
|
||||
type Stream = {
|
||||
sink(),
|
||||
|
||||
// Also seen as Pair
|
||||
export type Stream = {
|
||||
sink(source: Iterable<any>),
|
||||
source: Object,
|
||||
}
|
||||
|
||||
export type Duplex = [Stream, Stream];
|
||||
|
||||
export interface InsecureConnection {
|
||||
localPeer: PeerId,
|
||||
remotePeer: PeerId,
|
||||
local: PeerInfo,
|
||||
remote: PeerInfo,
|
||||
stats: ConnectionStats,
|
||||
streams(): [Stream],
|
||||
|
||||
streams(): Duplex,
|
||||
addStream(muxedStream: any) : Stream,
|
||||
}
|
||||
|
||||
@ -42,14 +49,9 @@ export interface NoiseConnection {
|
||||
}
|
||||
|
||||
export interface SecureConnection {
|
||||
insecure: InsecureConnection,
|
||||
initiator: boolean,
|
||||
prologue: bytes32,
|
||||
localKey: bytes,
|
||||
localPeer: PeerId,
|
||||
remotePeer: PeerId,
|
||||
local: PeerInfo,
|
||||
remote: PeerInfo,
|
||||
|
||||
xxNoiseSession: NoiseSession,
|
||||
xxComplete: boolean,
|
||||
|
@ -227,6 +227,9 @@ export class XXHandshake {
|
||||
private async writeMessageA(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
||||
let ns = Buffer.alloc(0);
|
||||
hs.e = await generateKeypair();
|
||||
if (!hs.e) {
|
||||
throw new Error("Handshake state has keypair missing.");
|
||||
}
|
||||
const ne = hs.e.publicKey;
|
||||
|
||||
this.mixHash(hs.ss, ne);
|
||||
@ -237,6 +240,9 @@ export class XXHandshake {
|
||||
|
||||
private async writeMessageB(hs: HandshakeState, payload: bytes) : Promise<MessageBuffer> {
|
||||
hs.e = await generateKeypair();
|
||||
if (!hs.e) {
|
||||
throw new Error("Handshake state has keypair missing.");
|
||||
}
|
||||
const ne = hs.e.publicKey;
|
||||
this.mixHash(hs.ss, ne);
|
||||
|
||||
|
Reference in New Issue
Block a user