mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-18 01:21:35 +00:00
Write functions for verification
This commit is contained in:
16
src/utils.ts
16
src/utils.ts
@ -2,6 +2,7 @@ import { x25519, ed25519 } from 'bcrypto';
|
||||
import protobuf from "protobufjs";
|
||||
import { Buffer } from "buffer";
|
||||
import debug from "debug";
|
||||
import PeerId from "peer-id";
|
||||
|
||||
import { KeyPair } from "./@types/libp2p";
|
||||
import { bytes } from "./@types/basic";
|
||||
@ -82,6 +83,21 @@ export function decodeMessageBuffer(message: bytes): MessageBuffer {
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyPeerId(peerId: bytes, publicKey: bytes) {
|
||||
const generatedPeerId = await PeerId.createFromPubKey(publicKey);
|
||||
if (!generatedPeerId.equals(peerId)) {
|
||||
Promise.reject("Peer ID doesn't match libp2p public key.");
|
||||
}
|
||||
}
|
||||
|
||||
export function verifySignedPayload(noiseStaticKey: bytes, plaintext: bytes, libp2pPublicKey: bytes) {
|
||||
const generatedPayload = getHandshakePayload(noiseStaticKey);
|
||||
|
||||
if (!ed25519.verify(generatedPayload, signature, libp2pPublicKey)) {
|
||||
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);
|
||||
|
Reference in New Issue
Block a user