mirror of
https://github.com/waku-org/js-waku.git
synced 2025-01-13 05:54:54 +00:00
Provide hex/bytes utils as part of js-waku
This commit is contained in:
parent
9ff1d87a8d
commit
8cd5a52eba
@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Added
|
||||
- `WakuRelay.deleteObserver` to allow removal of observers, useful when a React component add observers when mounting and needs to delete it when unmounting.
|
||||
- Keep alive feature that pings host regularly, reducing the chance of connections being dropped due to idle.
|
||||
Can be disabled or default frequency (10s) can be changed when calling `Waku.create`.
|
||||
Can be disabled or default frequency (10s) can be changed when calling `Waku.create`.
|
||||
- New `lib/utils` module for easy, dependency-less hex/bytes conversions.
|
||||
|
||||
### Changed
|
||||
- **Breaking**: Auto select peer if none provided for store and light push protocols.
|
||||
|
@ -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, equalByteArrays, hexToBuf } from './utils';
|
||||
import { hexToBuf, equalByteArrays, bufToHex } from 'js-waku/lib/utils';
|
||||
|
||||
export interface KeyPair {
|
||||
privateKey: string;
|
||||
@ -69,7 +69,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
|
||||
*/
|
||||
function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string {
|
||||
return JSON.stringify({
|
||||
ethDmPublicKey: byteArrayToHex(ethDmPublicKey),
|
||||
ethDmPublicKey: bufToHex(ethDmPublicKey),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,29 +0,0 @@
|
||||
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');
|
||||
}
|
||||
|
||||
export function equalByteArrays(
|
||||
a: Uint8Array | Buffer | string,
|
||||
b: Uint8Array | Buffer | string
|
||||
): boolean {
|
||||
let aBuf: Buffer;
|
||||
let bBuf: Buffer;
|
||||
if (typeof a === 'string') {
|
||||
aBuf = hexToBuf(a);
|
||||
} else {
|
||||
aBuf = Buffer.from(a);
|
||||
}
|
||||
|
||||
if (typeof b === 'string') {
|
||||
bBuf = hexToBuf(b);
|
||||
} else {
|
||||
bBuf = Buffer.from(b);
|
||||
}
|
||||
|
||||
return aBuf.compare(bBuf) === 0;
|
||||
}
|
@ -3,7 +3,7 @@ import { getStatusFleetNodes, Waku, WakuMessage } from 'js-waku';
|
||||
import { decode, DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { decryptMessage, validatePublicKeyMessage } from './crypto';
|
||||
import { Message } from './messaging/Messages';
|
||||
import { byteArrayToHex, equalByteArrays } from './utils';
|
||||
import { bufToHex, equalByteArrays } from 'js-waku/lib/utils';
|
||||
|
||||
export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto';
|
||||
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
||||
@ -41,7 +41,7 @@ export function handlePublicKeyMessage(
|
||||
if (!msg.payload) return;
|
||||
const publicKeyMsg = PublicKeyMessage.decode(msg.payload);
|
||||
if (!publicKeyMsg) return;
|
||||
const ethDmPublicKey = byteArrayToHex(publicKeyMsg.ethDmPublicKey);
|
||||
const ethDmPublicKey = bufToHex(publicKeyMsg.ethDmPublicKey);
|
||||
console.log(ethDmPublicKey, myAddress);
|
||||
if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return;
|
||||
|
||||
@ -50,7 +50,7 @@ export function handlePublicKeyMessage(
|
||||
|
||||
if (res) {
|
||||
setter((prevPks: Map<string, string>) => {
|
||||
prevPks.set(byteArrayToHex(publicKeyMsg.ethAddress), ethDmPublicKey);
|
||||
prevPks.set(bufToHex(publicKeyMsg.ethAddress), ethDmPublicKey);
|
||||
return new Map(prevPks);
|
||||
});
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
export { getStatusFleetNodes, Environment, Protocol } from './lib/discover';
|
||||
|
||||
export * as utils from './lib/utils';
|
||||
|
||||
export { Waku } from './lib/waku';
|
||||
export { WakuMessage } from './lib/waku_message';
|
||||
|
||||
|
@ -1,3 +1,29 @@
|
||||
export function hexToBuf(str: string): Buffer {
|
||||
return Buffer.from(str.replace(/0x/i, ''), 'hex');
|
||||
return Buffer.from(str.replace(/^0x/i, ''), 'hex');
|
||||
}
|
||||
|
||||
export function bufToHex(buf: Uint8Array | Buffer): string {
|
||||
const _buf = Buffer.from(buf);
|
||||
return _buf.toString('hex');
|
||||
}
|
||||
|
||||
export function equalByteArrays(
|
||||
a: Uint8Array | Buffer | string,
|
||||
b: Uint8Array | Buffer | string
|
||||
): boolean {
|
||||
let aBuf: Buffer;
|
||||
let bBuf: Buffer;
|
||||
if (typeof a === 'string') {
|
||||
aBuf = hexToBuf(a);
|
||||
} else {
|
||||
aBuf = Buffer.from(a);
|
||||
}
|
||||
|
||||
if (typeof b === 'string') {
|
||||
bBuf = hexToBuf(b);
|
||||
} else {
|
||||
bBuf = Buffer.from(b);
|
||||
}
|
||||
|
||||
return aBuf.compare(bBuf) === 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user