mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-22 01:51:51 +00:00
Merge branch 'master' into mpetrunic/fix-outbount-remote-peer
# Conflicts: # CHANGELOG.md
This commit is contained in:
@ -25,6 +25,10 @@ export function encode1(message: MessageBuffer): bytes {
|
||||
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
|
||||
}
|
||||
|
||||
export function encode2(message: MessageBuffer): bytes {
|
||||
return Buffer.concat([message.ns, message.ciphertext]);
|
||||
}
|
||||
|
||||
export function decode0(input: bytes): MessageBuffer {
|
||||
if (input.length < 32) {
|
||||
throw new Error("Cannot decode stage 0 MessageBuffer: length less than 32 bytes.");
|
||||
@ -39,7 +43,7 @@ export function decode0(input: bytes): MessageBuffer {
|
||||
|
||||
export function decode1(input: bytes): MessageBuffer {
|
||||
if (input.length < 80) {
|
||||
throw new Error("Cannot decode stage 0 MessageBuffer: length less than 96 bytes.");
|
||||
throw new Error("Cannot decode stage 1 MessageBuffer: length less than 80 bytes.");
|
||||
}
|
||||
|
||||
return {
|
||||
@ -48,3 +52,15 @@ export function decode1(input: bytes): MessageBuffer {
|
||||
ciphertext: input.slice(80, input.length),
|
||||
}
|
||||
}
|
||||
|
||||
export function decode2(input: bytes): MessageBuffer {
|
||||
if (input.length < 48) {
|
||||
throw new Error("Cannot decode stage 2 MessageBuffer: length less than 48 bytes.");
|
||||
}
|
||||
|
||||
return {
|
||||
ne: Buffer.alloc(0),
|
||||
ns: input.slice(0, 48),
|
||||
ciphertext: input.slice(48, input.length),
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import {
|
||||
verifySignedPayload,
|
||||
} from "./utils";
|
||||
import { logger } from "./logger";
|
||||
import { decode0, decode1, encode0, encode1 } from "./encoder";
|
||||
import {decode0, decode1, decode2, encode0, encode1, encode2} from "./encoder";
|
||||
import { WrappedConnection } from "./noise";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
@ -99,11 +99,11 @@ export class XXHandshake implements IHandshake {
|
||||
if (this.isInitiator) {
|
||||
logger('Stage 2 - Initiator sending third handshake message.');
|
||||
const messageBuffer = this.xx.sendMessage(this.session, this.payload);
|
||||
this.connection.writeLP(encode1(messageBuffer));
|
||||
this.connection.writeLP(encode2(messageBuffer));
|
||||
logger('Stage 2 - Initiator sent message with signed payload.');
|
||||
} else {
|
||||
logger('Stage 2 - Responder waiting for third handshake message...');
|
||||
const receivedMessageBuffer = decode1((await this.connection.readLP()).slice());
|
||||
const receivedMessageBuffer = decode2((await this.connection.readLP()).slice());
|
||||
const {plaintext, valid} = this.xx.recvMessage(this.session, receivedMessageBuffer);
|
||||
if(!valid) {
|
||||
throw new Error("xx handshake stage 2 validation fail");
|
||||
|
@ -42,11 +42,11 @@ export class Noise implements INoiseConnection {
|
||||
*
|
||||
* @param staticNoiseKey x25519 private key, reuse for faster handshakes
|
||||
* @param earlyData
|
||||
* @param useNoisePipes enable IK handshake if initiator static key is known
|
||||
*/
|
||||
constructor(staticNoiseKey?: bytes, earlyData?: bytes, useNoisePipes = true) {
|
||||
constructor(staticNoiseKey?: bytes, earlyData?: bytes) {
|
||||
this.earlyData = earlyData || Buffer.alloc(0);
|
||||
this.useNoisePipes = useNoisePipes;
|
||||
//disabled until properly specked
|
||||
this.useNoisePipes = false;
|
||||
|
||||
if (staticNoiseKey) {
|
||||
const publicKey = x25519.publicKeyCreate(staticNoiseKey);
|
||||
|
Reference in New Issue
Block a user