Use util function

This commit is contained in:
Franck Royer 2021-07-09 15:03:23 +10:00
parent 4f50ca1732
commit 9ff1d87a8d
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 2 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import * as EthCrypto from 'eth-crypto';
import { ethers } from 'ethers';
import { Signer } from '@ethersproject/abstract-signer';
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
import { byteArrayToHex, hexToBuf } from './utils';
import { byteArrayToHex, equalByteArrays, hexToBuf } from './utils';
export interface KeyPair {
privateKey: string;
@ -49,15 +49,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
const formattedMsg = formatPublicKeyForSignature(msg.ethDmPublicKey);
try {
const sigAddress = ethers.utils.verifyMessage(formattedMsg, msg.signature);
const sigAddressBytes = hexToBuf(sigAddress);
// Compare the actual byte arrays instead of strings that may differ in casing or prefixing.
const cmp = sigAddressBytes.compare(new Buffer(msg.ethAddress));
console.log(
`Buffer comparison result: ${cmp} for (signature address, message address)`,
sigAddressBytes,
msg.ethAddress
);
return cmp === 0;
return equalByteArrays(sigAddress, msg.ethAddress);
} catch (e) {
console.log(
'Failed to verify signature for Public Key Message',