mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 08:22:35 +00:00
Create handshake handler files
This commit is contained in:
parent
b084207c52
commit
2bb63e3e91
@ -1,5 +1,5 @@
|
||||
import { Buffer } from "buffer";
|
||||
import { Handshake } from "./handshake";
|
||||
import { Handshake } from "./handshake-xx";
|
||||
|
||||
interface ReturnEncryptionWrapper {
|
||||
(source: Iterable<Uint8Array>): AsyncIterableIterator<Uint8Array>;
|
||||
|
44
src/handshake-ik.ts
Normal file
44
src/handshake-ik.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import {NoiseSession} from "./@types/handshake";
|
||||
import {bytes, bytes32} from "./@types/basic";
|
||||
import {KeyPair, PeerId} from "./@types/libp2p";
|
||||
import {WrappedConnection} from "./noise";
|
||||
import {IKHandshake} from "./handshakes/ik";
|
||||
|
||||
export class Handshake {
|
||||
public isInitiator: boolean;
|
||||
public session: NoiseSession;
|
||||
|
||||
private libp2pPrivateKey: bytes;
|
||||
private libp2pPublicKey: bytes;
|
||||
private prologue: bytes32;
|
||||
private staticKeys: KeyPair;
|
||||
private connection: WrappedConnection;
|
||||
private remotePeer: PeerId;
|
||||
private ik: IKHandshake;
|
||||
|
||||
constructor(
|
||||
isInitiator: boolean,
|
||||
libp2pPrivateKey: bytes,
|
||||
libp2pPublicKey: bytes,
|
||||
prologue: bytes32,
|
||||
staticKeys: KeyPair,
|
||||
connection: WrappedConnection,
|
||||
remotePeer: PeerId,
|
||||
handshake?: IKHandshake,
|
||||
) {
|
||||
this.isInitiator = isInitiator;
|
||||
this.libp2pPrivateKey = libp2pPrivateKey;
|
||||
this.libp2pPublicKey = libp2pPublicKey;
|
||||
this.prologue = prologue;
|
||||
this.staticKeys = staticKeys;
|
||||
this.connection = connection;
|
||||
this.remotePeer = remotePeer;
|
||||
|
||||
this.ik = handshake || new IKHandshake();
|
||||
|
||||
// Dummy data
|
||||
// TODO: Load remote static keys if found
|
||||
const remoteStaticKeys = this.staticKeys;
|
||||
this.session = this.ik.initSession(this.isInitiator, this.prologue, this.staticKeys, remoteStaticKeys.publicKey);
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import ensureBuffer from 'it-buffer';
|
||||
import pipe from 'it-pipe';
|
||||
import lp from 'it-length-prefixed';
|
||||
|
||||
import { Handshake } from "./handshake";
|
||||
import { Handshake } from "./handshake-xx";
|
||||
import { generateKeypair } from "./utils";
|
||||
import { uint16BEDecode, uint16BEEncode } from "./encoder";
|
||||
import { decryptStream, encryptStream } from "./crypto";
|
||||
@ -48,7 +48,7 @@ export class Noise implements NoiseConnection {
|
||||
public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||
const wrappedConnection = Wrap(connection);
|
||||
const libp2pPublicKey = localPeer.marshalPubKey();
|
||||
const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer);
|
||||
const handshake = await this.performXXHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer);
|
||||
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
||||
|
||||
return {
|
||||
@ -67,7 +67,7 @@ export class Noise implements NoiseConnection {
|
||||
public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise<SecureOutbound> {
|
||||
const wrappedConnection = Wrap(connection);
|
||||
const libp2pPublicKey = localPeer.marshalPubKey();
|
||||
const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer);
|
||||
const handshake = await this.performXXHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer);
|
||||
const conn = await this.createSecureConnection(wrappedConnection, handshake);
|
||||
|
||||
return {
|
||||
@ -76,7 +76,7 @@ export class Noise implements NoiseConnection {
|
||||
};
|
||||
}
|
||||
|
||||
private async performHandshake(
|
||||
private async performXXHandshake(
|
||||
connection: WrappedConnection,
|
||||
isInitiator: boolean,
|
||||
libp2pPublicKey: bytes,
|
||||
|
@ -3,7 +3,7 @@ import Duplex from 'it-pair/duplex';
|
||||
import {Buffer} from "buffer";
|
||||
import Wrap from "it-pb-rpc";
|
||||
|
||||
import {Handshake} from "../src/handshake";
|
||||
import {Handshake} from "../src/handshake-xx";
|
||||
import {generateKeypair} from "../src/utils";
|
||||
import {createPeerIdsFromFixtures} from "./fixtures/peer";
|
||||
import {getKeyPairFromPeerId} from "./utils";
|
||||
|
@ -5,7 +5,7 @@ import { Noise } from "../src";
|
||||
import {createPeerIdsFromFixtures} from "./fixtures/peer";
|
||||
import Wrap from "it-pb-rpc";
|
||||
import { random } from "bcrypto";
|
||||
import {Handshake} from "../src/handshake";
|
||||
import {Handshake} from "../src/handshake-xx";
|
||||
import {
|
||||
createHandshakePayload,
|
||||
generateKeypair,
|
||||
|
Loading…
x
Reference in New Issue
Block a user