mirror of https://github.com/waku-org/js-waku.git
Provide hex/bytes utils as part of js-waku
This commit is contained in:
parent
9ff1d87a8d
commit
8cd5a52eba
|
@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- `WakuRelay.deleteObserver` to allow removal of observers, useful when a React component add observers when mounting and needs to delete it when unmounting.
|
- `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.
|
- 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
|
### Changed
|
||||||
- **Breaking**: Auto select peer if none provided for store and light push protocols.
|
- **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 { ethers } from 'ethers';
|
||||||
import { Signer } from '@ethersproject/abstract-signer';
|
import { Signer } from '@ethersproject/abstract-signer';
|
||||||
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
|
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||||
import { byteArrayToHex, equalByteArrays, hexToBuf } from './utils';
|
import { hexToBuf, equalByteArrays, bufToHex } from 'js-waku/lib/utils';
|
||||||
|
|
||||||
export interface KeyPair {
|
export interface KeyPair {
|
||||||
privateKey: string;
|
privateKey: string;
|
||||||
|
@ -69,7 +69,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
|
||||||
*/
|
*/
|
||||||
function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string {
|
function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string {
|
||||||
return JSON.stringify({
|
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 { decode, DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||||
import { decryptMessage, validatePublicKeyMessage } from './crypto';
|
import { decryptMessage, validatePublicKeyMessage } from './crypto';
|
||||||
import { Message } from './messaging/Messages';
|
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 PublicKeyContentTopic = '/eth-dm/1/public-key/proto';
|
||||||
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
|
||||||
|
@ -41,7 +41,7 @@ export function handlePublicKeyMessage(
|
||||||
if (!msg.payload) return;
|
if (!msg.payload) return;
|
||||||
const publicKeyMsg = PublicKeyMessage.decode(msg.payload);
|
const publicKeyMsg = PublicKeyMessage.decode(msg.payload);
|
||||||
if (!publicKeyMsg) return;
|
if (!publicKeyMsg) return;
|
||||||
const ethDmPublicKey = byteArrayToHex(publicKeyMsg.ethDmPublicKey);
|
const ethDmPublicKey = bufToHex(publicKeyMsg.ethDmPublicKey);
|
||||||
console.log(ethDmPublicKey, myAddress);
|
console.log(ethDmPublicKey, myAddress);
|
||||||
if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return;
|
if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return;
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ export function handlePublicKeyMessage(
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
setter((prevPks: Map<string, string>) => {
|
setter((prevPks: Map<string, string>) => {
|
||||||
prevPks.set(byteArrayToHex(publicKeyMsg.ethAddress), ethDmPublicKey);
|
prevPks.set(bufToHex(publicKeyMsg.ethAddress), ethDmPublicKey);
|
||||||
return new Map(prevPks);
|
return new Map(prevPks);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
export { getStatusFleetNodes, Environment, Protocol } from './lib/discover';
|
export { getStatusFleetNodes, Environment, Protocol } from './lib/discover';
|
||||||
|
|
||||||
|
export * as utils from './lib/utils';
|
||||||
|
|
||||||
export { Waku } from './lib/waku';
|
export { Waku } from './lib/waku';
|
||||||
export { WakuMessage } from './lib/waku_message';
|
export { WakuMessage } from './lib/waku_message';
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,29 @@
|
||||||
export function hexToBuf(str: string): Buffer {
|
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…
Reference in New Issue