mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-19 22:51:24 +00:00
Update handling keys, refactoring
This commit is contained in:
@ -8,93 +8,98 @@ import {
|
||||
decodeMessageBuffer,
|
||||
encodeMessageBuffer,
|
||||
getHandshakePayload,
|
||||
logger,
|
||||
logger, signEarlyDataPayload,
|
||||
signPayload,
|
||||
} from "./utils";
|
||||
import { WrappedConnection } from "./noise";
|
||||
|
||||
type handshakeType = "XX";
|
||||
|
||||
export class Handshake {
|
||||
public isInitiator: boolean;
|
||||
public session: NoiseSession;
|
||||
|
||||
private type: handshakeType;
|
||||
private remotePublicKey: bytes;
|
||||
private remotePublicKey?: bytes;
|
||||
private libp2pPrivateKey: bytes;
|
||||
private libp2pPublicKey: bytes;
|
||||
private prologue: bytes32;
|
||||
private staticKeys: KeyPair;
|
||||
private connection: WrappedConnection;
|
||||
private xx: XXHandshake;
|
||||
|
||||
constructor(
|
||||
type: handshakeType,
|
||||
isInitiator: boolean,
|
||||
remotePublicKey: bytes,
|
||||
libp2pPrivateKey: bytes,
|
||||
libp2pPublicKey: bytes,
|
||||
prologue: bytes32,
|
||||
staticKeys: KeyPair,
|
||||
connection: WrappedConnection,
|
||||
handshake?: XXHandshake,
|
||||
) {
|
||||
this.type = type;
|
||||
this.isInitiator = isInitiator;
|
||||
this.remotePublicKey = remotePublicKey;
|
||||
this.libp2pPrivateKey = libp2pPrivateKey;
|
||||
this.libp2pPublicKey = libp2pPublicKey;
|
||||
this.prologue = prologue;
|
||||
this.staticKeys = staticKeys;
|
||||
this.connection = connection;
|
||||
|
||||
this.xx = handshake || new XXHandshake();
|
||||
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys);
|
||||
}
|
||||
|
||||
// stage 0
|
||||
async propose(earlyData?: bytes) : Promise<NoiseSession> {
|
||||
const ns = await this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
||||
|
||||
async propose(earlyData?: bytes) : Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||
const signedEarlyDataPayload = signEarlyDataPayload(this.libp2pPrivateKey, earlyData || Buffer.alloc(0));
|
||||
const handshakePayload = await createHandshakePayload(
|
||||
this.staticKeys.publicKey,
|
||||
this.libp2pPublicKey,
|
||||
this.libp2pPrivateKey,
|
||||
signedPayload,
|
||||
earlyData,
|
||||
this.staticKeys.privateKey
|
||||
signedEarlyDataPayload
|
||||
);
|
||||
const messageBuffer = await this.xx.sendMessage(ns, handshakePayload);
|
||||
const messageBuffer = await this.xx.sendMessage(this.session, handshakePayload);
|
||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||
|
||||
logger("Stage 0 - Initiator finished proposing");
|
||||
logger("Stage 0 - Initiator finished proposing, sent signed NoiseHandshake payload.");
|
||||
} else {
|
||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||
const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer));
|
||||
logger("Stage 0 - Responder received proposed message.");
|
||||
}
|
||||
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
|
||||
this.remotePublicKey = receivedMessageBuffer.ne;
|
||||
|
||||
return ns;
|
||||
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||
logger("Stage 0 - Responder received proposed message and remote static public key.");
|
||||
}
|
||||
}
|
||||
|
||||
// stage 1
|
||||
async exchange(session: NoiseSession) : Promise<void> {
|
||||
async exchange() : Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
||||
const receivedMessageBuffer = decodeMessageBuffer((await this.connection.readLP()).slice());
|
||||
this.remotePublicKey = receivedMessageBuffer.ne;
|
||||
const plaintext = await this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||
logger('Stage 1 - Initiator received the message.');
|
||||
} else {
|
||||
// create payload as responder
|
||||
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
|
||||
const signedPayload = signPayload(this.libp2pPrivateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||
const handshakePayload = await createHandshakePayload(
|
||||
this.libp2pPublicKey,
|
||||
this.libp2pPrivateKey,
|
||||
signedPayload,
|
||||
);
|
||||
|
||||
const messageBuffer = await this.xx.sendMessage(session, handshakePayload);
|
||||
const messageBuffer = await this.xx.sendMessage(this.session, handshakePayload);
|
||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||
logger('Stage 1 - Responder sent the message.')
|
||||
}
|
||||
}
|
||||
|
||||
// stage 2
|
||||
async finish(session: NoiseSession) : Promise<void> {
|
||||
async finish() : Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
|
||||
const messageBuffer = await this.xx.sendMessage(this.session, Buffer.alloc(0));
|
||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||
logger('Stage 2 - Initiator sent message.');
|
||||
} else {
|
||||
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
||||
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
|
||||
const plaintext = await this.xx.recvMessage(this.session, decodeMessageBuffer(receivedMessageBuffer));
|
||||
logger('Stage 2 - Responder received the message, finished handshake.')
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user