mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:32:18 +00:00
use built in peer id types
This commit is contained in:
parent
725a1e180d
commit
2f2476b146
@ -1,23 +1,12 @@
|
||||
import { bytes, bytes32 } from "./basic";
|
||||
import { Duplex } from "it-pair";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
export type KeyPair = {
|
||||
publicKey: bytes32;
|
||||
privateKey: bytes32;
|
||||
}
|
||||
|
||||
export type PeerId = {
|
||||
id: bytes;
|
||||
privKey: {
|
||||
marshal(): bytes;
|
||||
};
|
||||
pubKey: {
|
||||
marshal(): bytes;
|
||||
};
|
||||
marshalPubKey(): bytes;
|
||||
marshalPrivKey(): bytes;
|
||||
};
|
||||
|
||||
export interface INoiseConnection {
|
||||
remoteEarlyData?(): bytes;
|
||||
secureOutbound(localPeer: PeerId, insecure: any, remotePeer: PeerId): Promise<SecureOutbound>;
|
||||
|
@ -2,13 +2,14 @@ import {WrappedConnection} from "./noise";
|
||||
import {IK} from "./handshakes/ik";
|
||||
import {NoiseSession} from "./@types/handshake";
|
||||
import {bytes, bytes32} from "./@types/basic";
|
||||
import {KeyPair, PeerId} from "./@types/libp2p";
|
||||
import {KeyPair} from "./@types/libp2p";
|
||||
import {IHandshake} from "./@types/handshake-interface";
|
||||
import {Buffer} from "buffer";
|
||||
import {decode0, decode1, encode0, encode1} from "./encoder";
|
||||
import {verifySignedPayload} from "./utils";
|
||||
import {FailedIKError} from "./errors";
|
||||
import {logger} from "./logger";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
export class IKHandshake implements IHandshake {
|
||||
public isInitiator: boolean;
|
||||
|
@ -1,15 +1,13 @@
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
import { XXHandshake } from "./handshake-xx";
|
||||
import { XX } from "./handshakes/xx";
|
||||
import { KeyPair, PeerId } from "./@types/libp2p";
|
||||
import { bytes, bytes32 } from "./@types/basic";
|
||||
import {
|
||||
verifySignedPayload,
|
||||
} from "./utils";
|
||||
import { logger } from "./logger";
|
||||
import { WrappedConnection } from "./noise";
|
||||
import {decode0, decode1, encode1} from "./encoder";
|
||||
import {Buffer} from "buffer";
|
||||
import {XXHandshake} from "./handshake-xx";
|
||||
import {XX} from "./handshakes/xx";
|
||||
import {KeyPair} from "./@types/libp2p";
|
||||
import {bytes, bytes32} from "./@types/basic";
|
||||
import {verifySignedPayload,} from "./utils";
|
||||
import {logger} from "./logger";
|
||||
import {WrappedConnection} from "./noise";
|
||||
import {decode0, decode1} from "./encoder";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
export class XXFallbackHandshake extends XXHandshake {
|
||||
private ephemeralKeys?: KeyPair;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
import { XX } from "./handshakes/xx";
|
||||
import { KeyPair, PeerId } from "./@types/libp2p";
|
||||
import { KeyPair } from "./@types/libp2p";
|
||||
import { bytes, bytes32 } from "./@types/basic";
|
||||
import { NoiseSession } from "./@types/handshake";
|
||||
import {IHandshake} from "./@types/handshake-interface";
|
||||
@ -11,6 +11,7 @@ import {
|
||||
import { logger } from "./logger";
|
||||
import { decode0, decode1, encode0, encode1 } from "./encoder";
|
||||
import { WrappedConnection } from "./noise";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
export class XXHandshake implements IHandshake {
|
||||
public isInitiator: boolean;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import {PeerId} from "./@types/libp2p";
|
||||
import {bytes, bytes32} from "./@types/basic";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
/**
|
||||
* Storage for static keys of previously connected peers.
|
||||
|
16
src/noise.ts
16
src/noise.ts
@ -13,11 +13,12 @@ import { generateKeypair, getPayload } from "./utils";
|
||||
import { uint16BEDecode, uint16BEEncode } from "./encoder";
|
||||
import { decryptStream, encryptStream } from "./crypto";
|
||||
import { bytes } from "./@types/basic";
|
||||
import { INoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
||||
import { INoiseConnection, KeyPair, SecureOutbound } from "./@types/libp2p";
|
||||
import { Duplex } from "./@types/it-pair";
|
||||
import {IHandshake} from "./@types/handshake-interface";
|
||||
import {KeyCache} from "./keycache";
|
||||
import {logger} from "./logger";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
export type WrappedConnection = ReturnType<typeof Wrap>;
|
||||
|
||||
@ -36,12 +37,18 @@ export class Noise implements INoiseConnection {
|
||||
private readonly earlyData?: bytes;
|
||||
private useNoisePipes: boolean;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param staticNoiseKey
|
||||
* @param earlyData
|
||||
* @param useNoisePipes
|
||||
*/
|
||||
constructor(staticNoiseKey?: bytes, earlyData?: bytes, useNoisePipes = true) {
|
||||
this.earlyData = earlyData || Buffer.alloc(0);
|
||||
this.useNoisePipes = useNoisePipes;
|
||||
|
||||
if (staticNoiseKey) {
|
||||
const publicKey = x25519.publicKeyCreate(staticNoiseKey); // TODO: verify this
|
||||
const publicKey = x25519.publicKeyCreate(staticNoiseKey);
|
||||
this.staticKeys = {
|
||||
privateKey: staticNoiseKey,
|
||||
publicKey,
|
||||
@ -100,10 +107,7 @@ export class Noise implements INoiseConnection {
|
||||
/**
|
||||
* If Noise pipes supported, tries IK handshake first with XX as fallback if it fails.
|
||||
* If noise pipes disabled or remote peer static key is unknown, use XX.
|
||||
* @param connection
|
||||
* @param isInitiator
|
||||
* @param libp2pPublicKey
|
||||
* @param remotePeer
|
||||
* @param params
|
||||
*/
|
||||
private async performHandshake(params: HandshakeParams): Promise<IHandshake> {
|
||||
const payload = await getPayload(params.localPeer, this.staticKeys.publicKey, this.earlyData);
|
||||
|
Loading…
x
Reference in New Issue
Block a user