From cc2fb943236f07985545a2cdec5978d354585a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Wed, 11 Mar 2020 09:43:40 +0100 Subject: [PATCH] fix buffer usage in browser --- src/utils.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 6bd7bdd..a4004f9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -58,9 +58,9 @@ export async function getPeerIdFromPayload(payload: pb.INoiseHandshakePayload): return await PeerId.createFromPubKey(Buffer.from(payload.identityKey as Uint8Array)); } -export async function decodePayload(payload: bytes): Promise { +export async function decodePayload(payload: bytes|Uint8Array): Promise { return NoiseHandshakePayloadProto.toObject( - NoiseHandshakePayloadProto.decode(payload) + NoiseHandshakePayloadProto.decode(Buffer.from(payload)) ) as INoisePayload; } @@ -85,19 +85,16 @@ export async function verifySignedPayload( payload: pb.INoiseHandshakePayload, remotePeer: PeerId ): Promise { - - if (!(await isValidPeerId(remotePeer.id, Buffer.from(payload.identityKey as Uint8Array)))) { + const identityKey = Buffer.from(payload.identityKey as Uint8Array); + if (!(await isValidPeerId(remotePeer.id, identityKey))) { throw new Error("Peer ID doesn't match libp2p public key."); } - const generatedPayload = getHandshakePayload(noiseStaticKey); - // Unmarshaling from PublicKey protobuf - const publicKey = crypto.keys.unmarshalPublicKey(payload.identityKey); + const publicKey = crypto.keys.unmarshalPublicKey(identityKey); if (!publicKey.verify(generatedPayload, payload.identitySig)) { throw new Error("Static key doesn't match to peer that signed payload!"); } - return remotePeer; }