diff --git a/src/@types/handshake.ts b/src/@types/handshake.ts new file mode 100644 index 0000000..265f769 --- /dev/null +++ b/src/@types/handshake.ts @@ -0,0 +1,39 @@ +import {bytes, bytes32, uint32, uint64} from "./basic"; +import {KeyPair} from "./libp2p"; + +export type Hkdf = [bytes, bytes, bytes]; + +export interface MessageBuffer { + ne: bytes32; + ns: bytes; + ciphertext: bytes; +} + +export type CipherState = { + k: bytes32; + n: uint32; +} + +export type SymmetricState = { + cs: CipherState; + ck: bytes32; // chaining key + h: bytes32; // handshake hash +} + +export type HandshakeState = { + ss: SymmetricState; + s: KeyPair; + e?: KeyPair; + rs: bytes32; + re: bytes32; + psk: bytes32; +} + +export type NoiseSession = { + hs: HandshakeState; + h?: bytes32; + cs1?: CipherState; + cs2?: CipherState; + mc: uint64; + i: boolean; +} diff --git a/src/encoder.ts b/src/encoder.ts index 383187b..8837d4f 100644 --- a/src/encoder.ts +++ b/src/encoder.ts @@ -1,6 +1,6 @@ import {Buffer} from "buffer"; import {bytes} from "./@types/basic"; -import {MessageBuffer} from "./handshakes/xx"; +import {MessageBuffer} from "./@types/handshake"; export const int16BEEncode = (value, target, offset) => { target = target || Buffer.allocUnsafe(2); diff --git a/src/handshake.ts b/src/handshake.ts index d530e05..b2d96dc 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -1,8 +1,9 @@ import { Buffer } from "buffer"; -import { bytes, bytes32 } from "./@types/basic"; -import { NoiseSession, XXHandshake } from "./handshakes/xx"; +import { XXHandshake } from "./handshakes/xx"; import { KeyPair, PeerId } from "./@types/libp2p"; +import { bytes, bytes32 } from "./@types/basic"; +import { NoiseSession } from "./@types/handshake"; import { createHandshakePayload, getHandshakePayload, diff --git a/src/handshakes/xx.ts b/src/handshakes/xx.ts index 7e0c9de..63f0dad 100644 --- a/src/handshakes/xx.ts +++ b/src/handshakes/xx.ts @@ -5,42 +5,8 @@ import { BN } from 'bn.js'; import { bytes32, uint32, uint64, bytes } from '../@types/basic' import { KeyPair } from '../@types/libp2p' import { generateKeypair } from '../utils'; +import { CipherState, HandshakeState, Hkdf, MessageBuffer, NoiseSession, SymmetricState } from "../@types/handshake"; -export interface MessageBuffer { - ne: bytes32; - ns: bytes; - ciphertext: bytes; -} - -type CipherState = { - k: bytes32; - n: uint32; -} - -type SymmetricState = { - cs: CipherState; - ck: bytes32; // chaining key - h: bytes32; // handshake hash -} - -type HandshakeState = { - ss: SymmetricState; - s: KeyPair; - e?: KeyPair; - rs: bytes32; - re: bytes32; - psk: bytes32; -} - -export type NoiseSession = { - hs: HandshakeState; - h?: bytes32; - cs1?: CipherState; - cs2?: CipherState; - mc: uint64; - i: boolean; -} -export type Hkdf = [bytes, bytes, bytes]; const minNonce = 0;