mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-26 02:32:34 +00:00
Expose noise class
This commit is contained in:
parent
b69769f8dd
commit
8cf420fd88
42
src/noise.ts
42
src/noise.ts
@ -1,17 +1,55 @@
|
|||||||
|
import { x25519 } from 'bcrypto';
|
||||||
|
|
||||||
import { bytes } from "./types/basic";
|
import { bytes } from "./types/basic";
|
||||||
import { Connection } from "./types/libp2p";
|
import { Connection } from "./types/libp2p";
|
||||||
|
import { KeyPair, XXHandshake } from "./xx";
|
||||||
|
import { signPayload } from "../test/utils";
|
||||||
|
import {Buffer} from "buffer";
|
||||||
|
|
||||||
export class Noise {
|
export class Noise {
|
||||||
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
|
private readonly privateKey: bytes;
|
||||||
|
private staticKeys?: KeyPair;
|
||||||
|
private earlyData?: bytes;
|
||||||
|
|
||||||
|
constructor(privateKey: bytes, staticNoiseKey?: bytes, earlyData?: bytes) {
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
this.earlyData = earlyData;
|
||||||
|
|
||||||
|
if (staticNoiseKey) {
|
||||||
|
const publicKey = x25519.publicKeyCreate(staticNoiseKey);
|
||||||
|
this.staticKeys = {
|
||||||
|
privateKey: staticNoiseKey,
|
||||||
|
publicKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public tag() {
|
public tag() {
|
||||||
return '/noise';
|
return '/noise';
|
||||||
}
|
}
|
||||||
|
|
||||||
public encrypt(InsecureConnection: Connection, remotePublicKey: bytes) {
|
public async encrypt(InsecureConnection: Connection, remotePublicKey: bytes) {
|
||||||
|
const isInitiator = InsecureConnection.stats.direction === "outbound";
|
||||||
|
const secretKey = await this.doHandshake(isInitiator, remotePublicKey);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async doHandshake(isInitiator: boolean, remotePublicKey: bytes) : Promise<bytes> {
|
||||||
|
const xx = new XXHandshake();
|
||||||
|
if (!this.staticKeys) {
|
||||||
|
this.staticKeys = await xx.generateKeypair();
|
||||||
|
}
|
||||||
|
|
||||||
|
let signedPayload;
|
||||||
|
if (this.earlyData) {
|
||||||
|
const payload = Buffer.concat([this.earlyData, this.staticKeys.publicKey])
|
||||||
|
signedPayload = await signPayload(this.privateKey, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
const prologue = Buffer.from(this.tag());
|
||||||
|
const nsInit = await xx.initSession(isInitiator, prologue, this.staticKeys, remotePublicKey);
|
||||||
|
// TODO: Send messages, confirm handshake and return shared key
|
||||||
|
return Buffer.alloc(0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
|
import { bytes } from "./basic";
|
||||||
|
|
||||||
type PeerId = {
|
type PeerId = {
|
||||||
id: string,
|
id: string,
|
||||||
privKey: string,
|
privKey: string,
|
||||||
pubKey: string,
|
pubKey: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
type ConnectionStat = {
|
type ConnectionStats = {
|
||||||
direction: "inbound" | "outbound",
|
direction: "inbound" | "outbound",
|
||||||
|
encryption: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Connection {
|
export interface Connection {
|
||||||
localPeer: PeerId,
|
localPeer: PeerId,
|
||||||
remotePeer: PeerId,
|
remotePeer: PeerId,
|
||||||
stat: ConnectionStat,
|
stats: ConnectionStats,
|
||||||
}
|
}
|
||||||
|
12
test/noise.test.ts
Normal file
12
test/noise.test.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
import { Noise } from "../src";
|
||||||
|
import {generateEd25519Keys} from "./utils";
|
||||||
|
|
||||||
|
describe("Noise", () => {
|
||||||
|
it("should encrypt", async() => {
|
||||||
|
const libp2pKeys = await generateEd25519Keys();
|
||||||
|
|
||||||
|
const noise = new Noise(libp2pKeys._key);
|
||||||
|
await noise.encrypt();
|
||||||
|
})
|
||||||
|
});
|
@ -1,5 +1,7 @@
|
|||||||
import protobuf from "protobufjs";
|
import protobuf from "protobufjs";
|
||||||
import * as crypto from 'libp2p-crypto';
|
import * as crypto from 'libp2p-crypto';
|
||||||
|
import { ed25519 } from 'bcrypto';
|
||||||
|
import { bytes } from "../src/types/basic";
|
||||||
|
|
||||||
export async function loadPayloadProto () {
|
export async function loadPayloadProto () {
|
||||||
const payloadProtoBuf = await protobuf.load("protos/payload.proto");
|
const payloadProtoBuf = await protobuf.load("protos/payload.proto");
|
||||||
@ -9,3 +11,9 @@ export async function loadPayloadProto () {
|
|||||||
export async function generateEd25519Keys() {
|
export async function generateEd25519Keys() {
|
||||||
return await crypto.keys.generateKeyPair('ed25519');
|
return await crypto.keys.generateKeyPair('ed25519');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function signPayload(privateKey: bytes, payload: bytes) {
|
||||||
|
const Ed25519PrivateKey = crypto.keys.supportedKeys.ed25519.Ed25519PrivateKey;
|
||||||
|
// const ed25519 = Ed25519PrivateKey(privateKey, "need-to-get-public-key");
|
||||||
|
// return ed25519.sign(privateKey, payload);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user