mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
handshake propose
This commit is contained in:
parent
af95dc2fcd
commit
f6bc40baf4
@ -1,7 +1,7 @@
|
|||||||
import { bytes, bytes32 } from "./@types/basic";
|
import { bytes, bytes32 } from "./@types/basic";
|
||||||
import { NoiseSession, XXHandshake } from "./xx";
|
import { NoiseSession, XXHandshake } from "./xx";
|
||||||
import { KeyPair, PeerId } from "./@types/libp2p";
|
import { KeyPair } from "./@types/libp2p";
|
||||||
import {Buffer} from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
type handshakeType = "XX";
|
type handshakeType = "XX";
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ export class Handshake {
|
|||||||
private signedPayload: bytes;
|
private signedPayload: bytes;
|
||||||
private prologue: bytes32;
|
private prologue: bytes32;
|
||||||
private staticKeys: KeyPair;
|
private staticKeys: KeyPair;
|
||||||
|
private connection: any;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
type: handshakeType,
|
type: handshakeType,
|
||||||
@ -18,23 +19,32 @@ export class Handshake {
|
|||||||
prologue: bytes32,
|
prologue: bytes32,
|
||||||
signedPayload: bytes,
|
signedPayload: bytes,
|
||||||
staticKeys: KeyPair,
|
staticKeys: KeyPair,
|
||||||
|
connection,
|
||||||
) {
|
) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.remotePublicKey = remotePublicKey;
|
this.remotePublicKey = remotePublicKey;
|
||||||
this.signedPayload = signedPayload;
|
this.signedPayload = signedPayload;
|
||||||
this.prologue = prologue;
|
this.prologue = prologue;
|
||||||
this.staticKeys = staticKeys;
|
this.staticKeys = staticKeys;
|
||||||
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stage 0
|
||||||
async propose(isInitiator: boolean) : Promise<NoiseSession> {
|
async propose(isInitiator: boolean) : Promise<NoiseSession> {
|
||||||
const xx = new XXHandshake();
|
const xx = new XXHandshake();
|
||||||
|
|
||||||
const nsInit = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
const ns = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
||||||
|
|
||||||
if (isInitiator) {
|
if (isInitiator) {
|
||||||
const message = Buffer.concat([Buffer.alloc(0), this.signedPayload]);
|
const message = Buffer.concat([Buffer.alloc(0), this.signedPayload]);
|
||||||
const messageBuffer = await xx.sendMessage(nsInit, message);
|
const messageBuffer = await xx.sendMessage(ns, message);
|
||||||
|
this.connection.writeLP(messageBuffer);
|
||||||
|
} else {
|
||||||
|
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||||
|
const plaintext = await xx.recvMessage(ns, receivedMessageBuffer);
|
||||||
}
|
}
|
||||||
return nsInit;
|
|
||||||
|
return ns;
|
||||||
}
|
}
|
||||||
|
|
||||||
async exchange() : Promise<NoiseSession> {
|
async exchange() : Promise<NoiseSession> {
|
||||||
|
@ -3,7 +3,7 @@ import { Buffer } from "buffer";
|
|||||||
import Wrap from 'it-pb-rpc';
|
import Wrap from 'it-pb-rpc';
|
||||||
|
|
||||||
import { Handshake } from "./handshake";
|
import { Handshake } from "./handshake";
|
||||||
import { createHandshakePayload, generateKeypair, getHandshakePayload } from "./utils";
|
import { createHandshakePayload, generateKeypair, getHandshakePayload, signPayload } from "./utils";
|
||||||
import { decryptStreams, encryptStreams } from "./crypto";
|
import { decryptStreams, encryptStreams } from "./crypto";
|
||||||
import { bytes } from "./@types/basic";
|
import { bytes } from "./@types/basic";
|
||||||
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
||||||
@ -69,10 +69,11 @@ export class Noise implements NoiseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const payload = getHandshakePayload(this.staticKeys.publicKey);
|
const payload = getHandshakePayload(this.staticKeys.publicKey);
|
||||||
const signedPayload = signHandshakePayload(this.staticKeys.privateKey, payload);
|
const signedPayload = signPayload(this.staticKeys.privateKey, payload);
|
||||||
const handshakePayload = await createHandshakePayload(this.staticKeys, signedPayload);
|
const handshakePayload = await createHandshakePayload(this.staticKeys, signedPayload);
|
||||||
|
|
||||||
const prologue = Buffer.from(this.protocol);
|
const prologue = Buffer.from(this.protocol);
|
||||||
const handshake = new Handshake('XX', remotePublicKey, prologue, handshakePayload, this.staticKeys);
|
const handshake = new Handshake('XX', remotePublicKey, prologue, handshakePayload, this.staticKeys, connection);
|
||||||
const session = await handshake.propose(isInitiator);
|
const session = await handshake.propose(isInitiator);
|
||||||
|
|
||||||
return await encryptStreams(connection, session);
|
return await encryptStreams(connection, session);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user