mirror of https://github.com/waku-org/js-waku.git
Define util function for hex<>buf conversions
This commit is contained in:
parent
a4dd8771f6
commit
c5419630fc
|
@ -1,13 +1,9 @@
|
|||
import { Dispatch, SetStateAction, useEffect } from 'react';
|
||||
import { Environment, getStatusFleetNodes, Waku, WakuMessage } from 'js-waku';
|
||||
import {
|
||||
bytesToHexStr,
|
||||
decode,
|
||||
DirectMessage,
|
||||
PublicKeyMessage,
|
||||
} from './messaging/wire';
|
||||
import { decode, DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { decryptMessage, KeyPair, validatePublicKeyMessage } from './crypto';
|
||||
import { Message } from './messaging/Messages';
|
||||
import { byteArrayToHex } from './utils';
|
||||
|
||||
export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto';
|
||||
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
||||
|
@ -123,7 +119,7 @@ function handlePublicKeyMessage(
|
|||
if (!msg.payload) return;
|
||||
const publicKeyMsg = PublicKeyMessage.decode(msg.payload);
|
||||
if (!publicKeyMsg) return;
|
||||
const ethDmPublicKey = bytesToHexStr(publicKeyMsg.ethDmPublicKey);
|
||||
const ethDmPublicKey = byteArrayToHex(publicKeyMsg.ethDmPublicKey);
|
||||
if (ethDmPublicKey === myPublicKey) return;
|
||||
|
||||
const res = validatePublicKeyMessage(publicKeyMsg);
|
||||
|
@ -131,7 +127,7 @@ function handlePublicKeyMessage(
|
|||
|
||||
if (res) {
|
||||
setter((prevPks: Map<string, string>) => {
|
||||
prevPks.set(bytesToHexStr(publicKeyMsg.ethAddress), ethDmPublicKey);
|
||||
prevPks.set(byteArrayToHex(publicKeyMsg.ethAddress), ethDmPublicKey);
|
||||
return new Map(prevPks);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,11 +3,8 @@ import '@ethersproject/shims';
|
|||
import * as EthCrypto from 'eth-crypto';
|
||||
import { ethers } from 'ethers';
|
||||
import { Signer } from '@ethersproject/abstract-signer';
|
||||
import {
|
||||
bytesToHexStr,
|
||||
DirectMessage,
|
||||
PublicKeyMessage,
|
||||
} from './messaging/wire';
|
||||
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { byteArrayToHex, hexToBuf } from './utils';
|
||||
|
||||
export interface KeyPair {
|
||||
privateKey: string;
|
||||
|
@ -33,21 +30,15 @@ export async function createPublicKeyMessage(
|
|||
ethDmPublicKey: string
|
||||
): Promise<PublicKeyMessage> {
|
||||
const ethAddress = await web3Signer.getAddress();
|
||||
const bytesEthDmPublicKey = Buffer.from(
|
||||
ethDmPublicKey.replace(/0x/, ''),
|
||||
'hex'
|
||||
);
|
||||
const bytesEthDmPublicKey = hexToBuf(ethDmPublicKey);
|
||||
const signature = await web3Signer.signMessage(
|
||||
formatPublicKeyForSignature(bytesEthDmPublicKey)
|
||||
);
|
||||
|
||||
const bytesEthAddress = Buffer.from(ethAddress.replace(/0x/, ''), 'hex');
|
||||
const bytesSignature = Buffer.from(signature.replace(/0x/, ''), 'hex');
|
||||
|
||||
return new PublicKeyMessage({
|
||||
ethDmPublicKey: bytesEthDmPublicKey,
|
||||
ethAddress: bytesEthAddress,
|
||||
signature: bytesSignature,
|
||||
ethAddress: hexToBuf(ethAddress),
|
||||
signature: hexToBuf(signature),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -58,7 +49,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
|
|||
const formattedMsg = formatPublicKeyForSignature(msg.ethDmPublicKey);
|
||||
try {
|
||||
const sigAddress = ethers.utils.verifyMessage(formattedMsg, msg.signature);
|
||||
const sigAddressBytes = Buffer.from(sigAddress.replace(/0x/, ''), 'hex');
|
||||
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(
|
||||
|
@ -86,7 +77,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
|
|||
*/
|
||||
function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string {
|
||||
return JSON.stringify({
|
||||
ethDmPublicKey: bytesToHexStr(ethDmPublicKey),
|
||||
ethDmPublicKey: byteArrayToHex(ethDmPublicKey),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -56,11 +56,6 @@ export class PublicKeyMessage {
|
|||
}
|
||||
}
|
||||
|
||||
export function bytesToHexStr(bytes: Uint8Array): string {
|
||||
const buf = new Buffer(bytes);
|
||||
return buf.toString('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct Encrypted Message used for private communication over the Waku network.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
export function byteArrayToHex(bytes: Uint8Array): string {
|
||||
const buf = new Buffer(bytes);
|
||||
return buf.toString('hex');
|
||||
}
|
||||
|
||||
export function hexToBuf(str: string): Buffer {
|
||||
return Buffer.from(str.replace(/0x/, ''), 'hex');
|
||||
}
|
Loading…
Reference in New Issue