use built in peer id types

This commit is contained in:
Marin Petrunić 2020-02-07 12:59:52 +01:00
parent 725a1e180d
commit 2f2476b146
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
6 changed files with 26 additions and 33 deletions

View File

@ -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>;

View File

@ -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;

View File

@ -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 {KeyPair} from "./@types/libp2p";
import {bytes, bytes32} from "./@types/basic";
import {
verifySignedPayload,
} from "./utils";
import {verifySignedPayload,} from "./utils";
import {logger} from "./logger";
import {WrappedConnection} from "./noise";
import {decode0, decode1, encode1} from "./encoder";
import {decode0, decode1} from "./encoder";
import PeerId from "peer-id";
export class XXFallbackHandshake extends XXHandshake {
private ephemeralKeys?: KeyPair;

View File

@ -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;

View File

@ -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.

View File

@ -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);