mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 18:42:32 +00:00
Split encoder functions from utils
This commit is contained in:
parent
ac950d1419
commit
acb9b87537
27
src/encoder.ts
Normal file
27
src/encoder.ts
Normal file
@ -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),
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
@ -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";
|
||||
|
25
src/utils.ts
25
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;
|
||||
|
@ -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";
|
||||
|
Loading…
x
Reference in New Issue
Block a user