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