mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
Better naming
This commit is contained in:
parent
4a9b814e5a
commit
096a30b289
@ -1,5 +1,5 @@
|
|||||||
import {WrappedConnection} from "./noise";
|
import {WrappedConnection} from "./noise";
|
||||||
import {IKHandshake} from "./handshakes/ik";
|
import {IK} from "./handshakes/ik";
|
||||||
import {NoiseSession} from "./@types/handshake";
|
import {NoiseSession} from "./@types/handshake";
|
||||||
import {bytes, bytes32} from "./@types/basic";
|
import {bytes, bytes32} from "./@types/basic";
|
||||||
import {KeyPair, PeerId} from "./@types/libp2p";
|
import {KeyPair, PeerId} from "./@types/libp2p";
|
||||||
@ -16,7 +16,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
private staticKeys: KeyPair;
|
private staticKeys: KeyPair;
|
||||||
private connection: WrappedConnection;
|
private connection: WrappedConnection;
|
||||||
private remotePeer: PeerId;
|
private remotePeer: PeerId;
|
||||||
private ik: IKHandshake;
|
private ik: IK;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
isInitiator: boolean,
|
isInitiator: boolean,
|
||||||
@ -26,7 +26,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
staticKeys: KeyPair,
|
staticKeys: KeyPair,
|
||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
remotePeer: PeerId,
|
remotePeer: PeerId,
|
||||||
handshake?: IKHandshake,
|
handshake?: IK,
|
||||||
) {
|
) {
|
||||||
this.isInitiator = isInitiator;
|
this.isInitiator = isInitiator;
|
||||||
this.libp2pPrivateKey = libp2pPrivateKey;
|
this.libp2pPrivateKey = libp2pPrivateKey;
|
||||||
@ -36,7 +36,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.remotePeer = remotePeer;
|
this.remotePeer = remotePeer;
|
||||||
|
|
||||||
this.ik = handshake || new IKHandshake();
|
this.ik = handshake || new IK();
|
||||||
|
|
||||||
// Dummy data
|
// Dummy data
|
||||||
// TODO: Load remote static keys if found
|
// TODO: Load remote static keys if found
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
import { Handshake as XXHandshake } from "./handshake-xx";
|
import { Handshake as XXHandshake } from "./handshake-xx";
|
||||||
import { XXHandshake as XX } from "./handshakes/xx";
|
import { XX } from "./handshakes/xx";
|
||||||
import { KeyPair, PeerId } from "./@types/libp2p";
|
import { KeyPair, PeerId } from "./@types/libp2p";
|
||||||
import { bytes, bytes32 } from "./@types/basic";
|
import { bytes, bytes32 } from "./@types/basic";
|
||||||
import {
|
import {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
import { XXHandshake } from "./handshakes/xx";
|
import { XX } from "./handshakes/xx";
|
||||||
import { KeyPair, PeerId } from "./@types/libp2p";
|
import { KeyPair, PeerId } from "./@types/libp2p";
|
||||||
import { bytes, bytes32 } from "./@types/basic";
|
import { bytes, bytes32 } from "./@types/basic";
|
||||||
import { NoiseSession } from "./@types/handshake";
|
import { NoiseSession } from "./@types/handshake";
|
||||||
@ -21,7 +21,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
public session: NoiseSession;
|
public session: NoiseSession;
|
||||||
|
|
||||||
protected connection: WrappedConnection;
|
protected connection: WrappedConnection;
|
||||||
protected xx: XXHandshake;
|
protected xx: XX;
|
||||||
|
|
||||||
protected libp2pPrivateKey: bytes;
|
protected libp2pPrivateKey: bytes;
|
||||||
protected libp2pPublicKey: bytes;
|
protected libp2pPublicKey: bytes;
|
||||||
@ -37,7 +37,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
staticKeys: KeyPair,
|
staticKeys: KeyPair,
|
||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
remotePeer: PeerId,
|
remotePeer: PeerId,
|
||||||
handshake?: XXHandshake,
|
handshake?: XX,
|
||||||
) {
|
) {
|
||||||
this.isInitiator = isInitiator;
|
this.isInitiator = isInitiator;
|
||||||
this.libp2pPrivateKey = libp2pPrivateKey;
|
this.libp2pPrivateKey = libp2pPrivateKey;
|
||||||
@ -47,7 +47,7 @@ export class Handshake implements HandshakeInterface {
|
|||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.remotePeer = remotePeer;
|
this.remotePeer = remotePeer;
|
||||||
|
|
||||||
this.xx = handshake || new XXHandshake();
|
this.xx = handshake || new XX();
|
||||||
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys);
|
this.session = this.xx.initSession(this.isInitiator, this.prologue, this.staticKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import {AbstractHandshake} from "./abstract-handshake";
|
|||||||
import {KeyPair} from "../@types/libp2p";
|
import {KeyPair} from "../@types/libp2p";
|
||||||
|
|
||||||
|
|
||||||
export class IKHandshake extends AbstractHandshake {
|
export class IK extends AbstractHandshake {
|
||||||
public initSession(initiator: boolean, prologue: bytes32, s: KeyPair, rs: bytes32): NoiseSession {
|
public initSession(initiator: boolean, prologue: bytes32, s: KeyPair, rs: bytes32): NoiseSession {
|
||||||
const psk = this.createEmptyKey();
|
const psk = this.createEmptyKey();
|
||||||
|
|
||||||
|
@ -3,12 +3,12 @@ import { BN } from 'bn.js';
|
|||||||
|
|
||||||
import { bytes32, bytes } from '../@types/basic'
|
import { bytes32, bytes } from '../@types/basic'
|
||||||
import { KeyPair } from '../@types/libp2p'
|
import { KeyPair } from '../@types/libp2p'
|
||||||
import {generateKeypair, getHkdf, isValidPublicKey} from '../utils';
|
import {generateKeypair, isValidPublicKey} from '../utils';
|
||||||
import { HandshakeState, MessageBuffer, NoiseSession } from "../@types/handshake";
|
import { HandshakeState, MessageBuffer, NoiseSession } from "../@types/handshake";
|
||||||
import {AbstractHandshake} from "./abstract-handshake";
|
import {AbstractHandshake} from "./abstract-handshake";
|
||||||
|
|
||||||
|
|
||||||
export class XXHandshake extends AbstractHandshake {
|
export class XX extends AbstractHandshake {
|
||||||
private initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32): HandshakeState {
|
private initializeInitiator(prologue: bytes32, s: KeyPair, rs: bytes32, psk: bytes32): HandshakeState {
|
||||||
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
const name = "Noise_XX_25519_ChaChaPoly_SHA256";
|
||||||
const ss = this.initializeSymmetric(name);
|
const ss = this.initializeSymmetric(name);
|
||||||
|
@ -15,7 +15,6 @@ import { decryptStream, encryptStream } 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";
|
||||||
import { Duplex } from "./@types/it-pair";
|
import { Duplex } from "./@types/it-pair";
|
||||||
import {XXHandshake} from "./handshakes/xx";
|
|
||||||
import {HandshakeInterface} from "./@types/handshake-interface";
|
import {HandshakeInterface} from "./@types/handshake-interface";
|
||||||
|
|
||||||
export type WrappedConnection = ReturnType<typeof Wrap>;
|
export type WrappedConnection = ReturnType<typeof Wrap>;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {Buffer} from "buffer";
|
import {Buffer} from "buffer";
|
||||||
import {IKHandshake} from "../../src/handshakes/ik";
|
import {IK} from "../../src/handshakes/ik";
|
||||||
import {KeyPair} from "../../src/@types/libp2p";
|
import {KeyPair} from "../../src/@types/libp2p";
|
||||||
import {createHandshakePayload, generateKeypair, getHandshakePayload} from "../../src/utils";
|
import {createHandshakePayload, generateKeypair, getHandshakePayload} from "../../src/utils";
|
||||||
import {assert, expect} from "chai";
|
import {assert, expect} from "chai";
|
||||||
@ -10,8 +10,8 @@ describe("Index", () => {
|
|||||||
|
|
||||||
it("Test complete IK handshake", async () => {
|
it("Test complete IK handshake", async () => {
|
||||||
try {
|
try {
|
||||||
const ikI = new IKHandshake();
|
const ikI = new IK();
|
||||||
const ikR = new IKHandshake();
|
const ikR = new IK();
|
||||||
|
|
||||||
// Generate static noise keys
|
// Generate static noise keys
|
||||||
const kpInitiator: KeyPair = await generateKeypair();
|
const kpInitiator: KeyPair = await generateKeypair();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { expect, assert } from "chai";
|
import { expect, assert } from "chai";
|
||||||
import { Buffer } from 'buffer';
|
import { Buffer } from 'buffer';
|
||||||
|
|
||||||
import { XXHandshake } from "../../src/handshakes/xx";
|
import { XX } from "../../src/handshakes/xx";
|
||||||
import { KeyPair } from "../../src/@types/libp2p";
|
import { KeyPair } from "../../src/@types/libp2p";
|
||||||
import { generateEd25519Keys } from "../utils";
|
import { generateEd25519Keys } from "../utils";
|
||||||
import {createHandshakePayload, generateKeypair, getHandshakePayload, getHkdf} from "../../src/utils";
|
import {createHandshakePayload, generateKeypair, getHandshakePayload, getHkdf} from "../../src/utils";
|
||||||
@ -11,7 +11,7 @@ describe("Index", () => {
|
|||||||
|
|
||||||
it("Test creating new XX session", async () => {
|
it("Test creating new XX session", async () => {
|
||||||
try {
|
try {
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
|
|
||||||
const kpInitiator: KeyPair = await generateKeypair();
|
const kpInitiator: KeyPair = await generateKeypair();
|
||||||
const kpResponder: KeyPair = await generateKeypair();
|
const kpResponder: KeyPair = await generateKeypair();
|
||||||
@ -23,7 +23,7 @@ describe("Index", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("Test get HKDF", async () => {
|
it("Test get HKDF", async () => {
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
const ckBytes = Buffer.from('4e6f6973655f58585f32353531395f58436861436861506f6c795f53484132353600000000000000000000000000000000000000000000000000000000000000', 'hex');
|
const ckBytes = Buffer.from('4e6f6973655f58585f32353531395f58436861436861506f6c795f53484132353600000000000000000000000000000000000000000000000000000000000000', 'hex');
|
||||||
const ikm = Buffer.from('a3eae50ea37a47e8a7aa0c7cd8e16528670536dcd538cebfd724fb68ce44f1910ad898860666227d4e8dd50d22a9a64d1c0a6f47ace092510161e9e442953da3', 'hex');
|
const ikm = Buffer.from('a3eae50ea37a47e8a7aa0c7cd8e16528670536dcd538cebfd724fb68ce44f1910ad898860666227d4e8dd50d22a9a64d1c0a6f47ace092510161e9e442953da3', 'hex');
|
||||||
const ck = Buffer.alloc(32);
|
const ck = Buffer.alloc(32);
|
||||||
@ -104,7 +104,7 @@ describe("Index", () => {
|
|||||||
|
|
||||||
it("Test handshake", async () => {
|
it("Test handshake", async () => {
|
||||||
try {
|
try {
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
await doHandshake(xx);
|
await doHandshake(xx);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
assert(false, e.message);
|
assert(false, e.message);
|
||||||
@ -113,7 +113,7 @@ describe("Index", () => {
|
|||||||
|
|
||||||
it("Test symmetric encrypt and decrypt", async () => {
|
it("Test symmetric encrypt and decrypt", async () => {
|
||||||
try {
|
try {
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
const { nsInit, nsResp } = await doHandshake(xx);
|
const { nsInit, nsResp } = await doHandshake(xx);
|
||||||
const ad = Buffer.from("authenticated");
|
const ad = Buffer.from("authenticated");
|
||||||
const message = Buffer.from("HelloCrypto");
|
const message = Buffer.from("HelloCrypto");
|
||||||
@ -130,7 +130,7 @@ describe("Index", () => {
|
|||||||
|
|
||||||
it("Test multiple messages encryption and decryption", async () => {
|
it("Test multiple messages encryption and decryption", async () => {
|
||||||
try {
|
try {
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
const { nsInit, nsResp } = await doHandshake(xx);
|
const { nsInit, nsResp } = await doHandshake(xx);
|
||||||
const ad = Buffer.from("authenticated");
|
const ad = Buffer.from("authenticated");
|
||||||
const message = Buffer.from("ethereum1");
|
const message = Buffer.from("ethereum1");
|
||||||
|
@ -13,7 +13,7 @@ import {
|
|||||||
signPayload
|
signPayload
|
||||||
} from "../src/utils";
|
} from "../src/utils";
|
||||||
import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder";
|
import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder";
|
||||||
import {XXHandshake} from "../src/handshakes/xx";
|
import {XX} from "../src/handshakes/xx";
|
||||||
import {Buffer} from "buffer";
|
import {Buffer} from "buffer";
|
||||||
import {getKeyPairFromPeerId} from "./utils";
|
import {getKeyPairFromPeerId} from "./utils";
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ describe("Noise", () => {
|
|||||||
const wrapped = Wrap(inboundConnection);
|
const wrapped = Wrap(inboundConnection);
|
||||||
const prologue = Buffer.from('/noise');
|
const prologue = Buffer.from('/noise');
|
||||||
const staticKeys = generateKeypair();
|
const staticKeys = generateKeypair();
|
||||||
const xx = new XXHandshake();
|
const xx = new XX();
|
||||||
const { privateKey: libp2pPrivKey, publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer);
|
const { privateKey: libp2pPrivKey, publicKey: libp2pPubKey } = getKeyPairFromPeerId(remotePeer);
|
||||||
|
|
||||||
const handshake = new Handshake(false, libp2pPrivKey, libp2pPubKey, prologue, staticKeys, wrapped, localPeer, xx);
|
const handshake = new Handshake(false, libp2pPrivKey, libp2pPubKey, prologue, staticKeys, wrapped, localPeer, xx);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user