diff --git a/src/encoder.ts b/src/encoder.ts new file mode 100644 index 0000000..ea946ef --- /dev/null +++ b/src/encoder.ts @@ -0,0 +1,27 @@ +import {Buffer} from "buffer"; +import {bytes} from "./@types/basic"; +import {MessageBuffer} from "./xx"; + +export const int16BEEncode = (value, target, offset) => { + target = target || Buffer.allocUnsafe(2); + return target.writeInt16BE(value, offset); +}; +int16BEEncode.bytes = 2; + +export const int16BEDecode = data => { + if (data.length < 2) throw RangeError('Could not decode int16BE'); + return data.readInt16BE(0); +}; +int16BEDecode.bytes = 2; + +export function encodeMessageBuffer(message: MessageBuffer): bytes { + return Buffer.concat([message.ne, message.ns, message.ciphertext]); +} + +export function decodeMessageBuffer(message: bytes): MessageBuffer { + return { + ne: message.slice(0, 32), + ns: message.slice(32, 64), + ciphertext: message.slice(64, message.length), + } +} diff --git a/src/handshake.ts b/src/handshake.ts index 255327e..88eaaeb 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -5,14 +5,13 @@ import { NoiseSession, XXHandshake } from "./xx"; import { KeyPair, PeerId } from "./@types/libp2p"; import { createHandshakePayload, - decodeMessageBuffer, - encodeMessageBuffer, getHandshakePayload, logger, signEarlyDataPayload, signPayload, verifySignedPayload, } from "./utils"; +import { decodeMessageBuffer, encodeMessageBuffer } from "./encoder"; import { WrappedConnection } from "./noise"; export class Handshake { diff --git a/src/noise.ts b/src/noise.ts index 179389c..83b313e 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -7,7 +7,8 @@ import pipe from 'it-pipe'; import lp from 'it-length-prefixed'; import { Handshake } from "./handshake"; -import { generateKeypair, int16BEDecode, int16BEEncode } from "./utils"; +import { generateKeypair } from "./utils"; +import { int16BEDecode, int16BEEncode } from "./encoder"; import { decryptStream, encryptStream } from "./crypto"; import { bytes } from "./@types/basic"; import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p"; diff --git a/src/utils.ts b/src/utils.ts index 8ea0752..8e8faf6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,7 +7,6 @@ import * as crypto from 'libp2p-crypto'; import { KeyPair } from "./@types/libp2p"; import { bytes } from "./@types/basic"; -import { MessageBuffer } from "./xx"; export const logger = debug('libp2p:noise'); @@ -72,18 +71,6 @@ export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer. export const getEarlyDataPayload = (earlyData: bytes) => Buffer.concat([Buffer.from("noise-libp2p-early-data:"), earlyData]); -export function encodeMessageBuffer(message: MessageBuffer): bytes { - return Buffer.concat([message.ne, message.ns, message.ciphertext]); -} - -export function decodeMessageBuffer(message: bytes): MessageBuffer { - return { - ne: message.slice(0, 32), - ns: message.slice(32, 64), - ciphertext: message.slice(64, message.length), - } -} - async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) { const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf); return generatedPeerId.id.equals(peerId); @@ -104,15 +91,3 @@ export async function verifySignedPayload(noiseStaticKey: bytes, plaintext: byte throw new Error("Static key doesn't match to peer that signed payload!"); } } - -export const int16BEEncode = (value, target, offset) => { - target = target || Buffer.allocUnsafe(2); - return target.writeInt16BE(value, offset); -}; -int16BEEncode.bytes = 2; - -export const int16BEDecode = data => { - if (data.length < 2) throw RangeError('Could not decode int16BE'); - return data.readInt16BE(0); -}; -int16BEDecode.bytes = 2; diff --git a/test/noise.test.ts b/test/noise.test.ts index 3acd8e2..6d42cd5 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -7,12 +7,11 @@ import Wrap from "it-pb-rpc"; import {Handshake} from "../src/handshake"; import { createHandshakePayload, - decodeMessageBuffer, - encodeMessageBuffer, generateKeypair, getHandshakePayload, signPayload } from "../src/utils"; +import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder"; import {XXHandshake} from "../src/xx"; import {Buffer} from "buffer"; import {getKeyPairFromPeerId} from "./utils";