mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-12 08:51:38 +00:00
adjust code to new proto
This commit is contained in:
44
src/utils.ts
44
src/utils.ts
@ -1,17 +1,13 @@
|
|||||||
import { x25519, HKDF, SHA256 } from 'bcrypto';
|
import {HKDF, SHA256, x25519} from 'bcrypto';
|
||||||
import protobuf from "protobufjs";
|
|
||||||
import {Buffer} from "buffer";
|
import {Buffer} from "buffer";
|
||||||
import PeerId from "peer-id";
|
import PeerId from "peer-id";
|
||||||
import * as crypto from 'libp2p-crypto';
|
import * as crypto from 'libp2p-crypto';
|
||||||
import {KeyPair} from "./@types/libp2p";
|
import {KeyPair} from "./@types/libp2p";
|
||||||
import {bytes, bytes32} from "./@types/basic";
|
import {bytes, bytes32} from "./@types/basic";
|
||||||
import {Hkdf, INoisePayload} from "./@types/handshake";
|
import {Hkdf, INoisePayload} from "./@types/handshake";
|
||||||
import payloadProto from "./proto/payload.json";
|
import {pb} from "./proto/payload";
|
||||||
|
|
||||||
async function loadPayloadProto () {
|
const NoiseHandshakePayloadProto = pb.NoiseHandshakePayload;
|
||||||
const payloadProtoBuf = await protobuf.Root.fromJSON(payloadProto);
|
|
||||||
return payloadProtoBuf.lookupType("NoiseHandshakePayload");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateKeypair(): KeyPair {
|
export function generateKeypair(): KeyPair {
|
||||||
const privateKey = x25519.privateKeyGenerate();
|
const privateKey = x25519.privateKeyGenerate();
|
||||||
@ -43,19 +39,14 @@ export async function createHandshakePayload(
|
|||||||
signedPayload: bytes,
|
signedPayload: bytes,
|
||||||
earlyData?: bytes,
|
earlyData?: bytes,
|
||||||
): Promise<bytes> {
|
): Promise<bytes> {
|
||||||
const NoiseHandshakePayload = await loadPayloadProto();
|
|
||||||
const earlyDataPayload = earlyData ?
|
|
||||||
{
|
|
||||||
data: earlyData,
|
|
||||||
} : {};
|
|
||||||
|
|
||||||
const payloadInit = NoiseHandshakePayload.create({
|
const payloadInit = NoiseHandshakePayloadProto.create({
|
||||||
identityKey: libp2pPublicKey,
|
identityKey: libp2pPublicKey,
|
||||||
identitySig: signedPayload,
|
identitySig: signedPayload,
|
||||||
...earlyDataPayload,
|
data: earlyData || null,
|
||||||
});
|
});
|
||||||
|
|
||||||
return Buffer.from(NoiseHandshakePayload.encode(payloadInit).finish());
|
return Buffer.from(NoiseHandshakePayloadProto.encode(payloadInit).finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -63,14 +54,13 @@ export async function signPayload(peerId: PeerId, payload: bytes): Promise<bytes
|
|||||||
return peerId.privKey.sign(payload);
|
return peerId.privKey.sign(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPeerIdFromPayload(payload: INoisePayload): Promise<PeerId> {
|
export async function getPeerIdFromPayload(payload: pb.INoiseHandshakePayload): Promise<PeerId> {
|
||||||
return await PeerId.createFromPubKey(Buffer.from(payload.identityKey));
|
return await PeerId.createFromPubKey(Buffer.from(payload.identityKey as Uint8Array));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function decodePayload(payload: bytes): Promise<INoisePayload> {
|
export async function decodePayload(payload: bytes): Promise<pb.INoiseHandshakePayload> {
|
||||||
const NoiseHandshakePayload = await loadPayloadProto();
|
return NoiseHandshakePayloadProto.toObject(
|
||||||
return NoiseHandshakePayload.toObject(
|
NoiseHandshakePayloadProto.decode(payload)
|
||||||
NoiseHandshakePayload.decode(payload)
|
|
||||||
) as INoisePayload;
|
) as INoisePayload;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,19 +82,11 @@ async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) {
|
|||||||
*/
|
*/
|
||||||
export async function verifySignedPayload(
|
export async function verifySignedPayload(
|
||||||
noiseStaticKey: bytes,
|
noiseStaticKey: bytes,
|
||||||
payload: INoisePayload,
|
payload: pb.INoiseHandshakePayload,
|
||||||
remotePeer: PeerId
|
remotePeer: PeerId
|
||||||
): Promise<PeerId> {
|
): Promise<PeerId> {
|
||||||
try {
|
|
||||||
//temporary fix until protobufsjs conversion options starts working
|
|
||||||
//by default it ends up as Uint8Array
|
|
||||||
payload.identityKey = Buffer.from(payload.identityKey);
|
|
||||||
payload.identitySig = Buffer.from(payload.identitySig);
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error("Failed to decode received payload. Reason: " + e.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(await isValidPeerId(remotePeer.id, payload.identityKey)) ) {
|
if (!(await isValidPeerId(remotePeer.id, Buffer.from(payload.identityKey as Uint8Array)))) {
|
||||||
throw new Error("Peer ID doesn't match libp2p public key.");
|
throw new Error("Peer ID doesn't match libp2p public key.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user