@@ -62,11 +62,6 @@ export function getHandshakePayload (publicKey: bytes): bytes {
6262 return uint8ArrayConcat ( [ prefix , publicKey ] , prefix . length + publicKey . length )
6363}
6464
65- async function isValidPeerId ( peerId : PeerId , publicKeyProtobuf : bytes ) : Promise < boolean > {
66- const generatedPeerId = await peerIdFromKeys ( publicKeyProtobuf )
67- return generatedPeerId . equals ( peerId )
68- }
69-
7065/**
7166 * Verifies signed payload, throws on any irregularities.
7267 *
@@ -80,31 +75,30 @@ export async function verifySignedPayload (
8075 payload : pb . NoiseHandshakePayload ,
8176 remotePeer : PeerId
8277) : Promise < PeerId > {
83- const identityKey = payload . identityKey
84- if ( ! ( await isValidPeerId ( remotePeer , identityKey ) ) ) {
78+ // Unmarshaling from PublicKey protobuf
79+ const payloadPeerId = await peerIdFromKeys ( payload . identityKey )
80+ if ( ! payloadPeerId . equals ( remotePeer ) ) {
8581 throw new Error ( "Peer ID doesn't match libp2p public key." )
8682 }
8783 const generatedPayload = getHandshakePayload ( noiseStaticKey )
88- // Unmarshaling from PublicKey protobuf
89- const peerId = await peerIdFromKeys ( identityKey )
9084
91- if ( peerId . publicKey == null ) {
85+ if ( payloadPeerId . publicKey == null ) {
9286 throw new Error ( 'PublicKey was missing from PeerId' )
9387 }
9488
9589 if ( payload . identitySig == null ) {
9690 throw new Error ( 'Signature was missing from message' )
9791 }
9892
99- const publicKey = unmarshalPublicKey ( peerId . publicKey )
93+ const publicKey = unmarshalPublicKey ( payloadPeerId . publicKey )
10094
10195 const valid = await publicKey . verify ( generatedPayload , payload . identitySig )
10296
10397 if ( ! valid ) {
10498 throw new Error ( "Static key doesn't match to peer that signed payload!" )
10599 }
106100
107- return peerId
101+ return payloadPeerId
108102}
109103
110104export function isValidPublicKey ( pk : bytes ) : boolean {
0 commit comments