mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-26 11:05:39 +00:00
Rename "Direct" to "Private" in code
This commit is contained in:
parent
0673edacb5
commit
d494558596
@ -17,8 +17,8 @@ import WifiIcon from '@material-ui/icons/Wifi';
|
||||
import BroadcastPublicKey from './BroadcastPublicKey';
|
||||
import Messaging from './messaging/Messaging';
|
||||
import {
|
||||
DirectMessageContentTopic,
|
||||
handleDirectMessage,
|
||||
PrivateMessageContentTopic,
|
||||
handlePrivateMessage,
|
||||
handlePublicKeyMessage,
|
||||
initWaku,
|
||||
PublicKeyContentTopic,
|
||||
@ -122,20 +122,22 @@ function App() {
|
||||
if (!address) return;
|
||||
if (!provider?.provider?.request) return;
|
||||
|
||||
const observerDirectMessage = handleDirectMessage.bind(
|
||||
const observerPrivateMessage = handlePrivateMessage.bind(
|
||||
{},
|
||||
setMessages,
|
||||
address,
|
||||
provider.provider.request
|
||||
);
|
||||
|
||||
waku.relay.addObserver(observerDirectMessage, [DirectMessageContentTopic]);
|
||||
waku.relay.addObserver(observerPrivateMessage, [
|
||||
PrivateMessageContentTopic,
|
||||
]);
|
||||
|
||||
return function cleanUp() {
|
||||
if (!waku) return;
|
||||
if (!observerDirectMessage) return;
|
||||
waku.relay.deleteObserver(observerDirectMessage, [
|
||||
DirectMessageContentTopic,
|
||||
if (!observerPrivateMessage) return;
|
||||
waku.relay.deleteObserver(observerPrivateMessage, [
|
||||
PrivateMessageContentTopic,
|
||||
]);
|
||||
};
|
||||
}, [waku, address, provider?.provider?.request]);
|
||||
@ -178,7 +180,7 @@ function App() {
|
||||
light push
|
||||
</Typography>
|
||||
<Typography variant="h6" className={classes.title}>
|
||||
Ethereum Direct Message
|
||||
Ethereum Private Message with Wallet Encryption
|
||||
</Typography>
|
||||
<Typography>{addressDisplay}</Typography>
|
||||
</Toolbar>
|
||||
|
@ -9,8 +9,8 @@ import {
|
||||
import React, { ChangeEvent, useState, KeyboardEvent } from 'react';
|
||||
import { Waku, WakuMessage } from 'js-waku';
|
||||
import { bufToHex, hexToBuf } from 'js-waku/lib/utils';
|
||||
import { DirectMessage } from './wire';
|
||||
import { DirectMessageContentTopic } from '../waku';
|
||||
import { PrivateMessage } from './wire';
|
||||
import { PrivateMessageContentTopic } from '../waku';
|
||||
import * as sigUtil from 'eth-sig-util';
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
@ -110,12 +110,12 @@ async function encodeEncryptedWakuMessage(
|
||||
publicKey: Uint8Array,
|
||||
address: string
|
||||
): Promise<WakuMessage> {
|
||||
const directMsg = new DirectMessage({
|
||||
const privateMessage = new PrivateMessage({
|
||||
toAddress: hexToBuf(address),
|
||||
message: message,
|
||||
});
|
||||
|
||||
const payload = directMsg.encode();
|
||||
const payload = privateMessage.encode();
|
||||
|
||||
const encObj = sigUtil.encrypt(
|
||||
Buffer.from(publicKey).toString('base64'),
|
||||
@ -124,7 +124,7 @@ async function encodeEncryptedWakuMessage(
|
||||
);
|
||||
|
||||
const encryptedPayload = Buffer.from(JSON.stringify(encObj), 'utf8');
|
||||
return WakuMessage.fromBytes(encryptedPayload, DirectMessageContentTopic);
|
||||
return WakuMessage.fromBytes(encryptedPayload, PrivateMessageContentTopic);
|
||||
}
|
||||
|
||||
function sendMessage(
|
||||
|
@ -59,36 +59,36 @@ export class PublicKeyMessage {
|
||||
}
|
||||
}
|
||||
|
||||
export interface DirectMessagePayload {
|
||||
export interface PrivateMessagePayload {
|
||||
toAddress: Uint8Array;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct Encrypted Message used for private communication over the Waku network.
|
||||
* Encrypted Message used for private communication over the Waku network.
|
||||
*/
|
||||
export class DirectMessage {
|
||||
private static Type = new Type('DirectMessage')
|
||||
export class PrivateMessage {
|
||||
private static Type = new Type('PrivateMessage')
|
||||
.add(new Field('toAddress', 1, 'bytes'))
|
||||
.add(new Field('message', 2, 'string'));
|
||||
private static Root = new Root().define('messages').add(DirectMessage.Type);
|
||||
private static Root = new Root().define('messages').add(PrivateMessage.Type);
|
||||
|
||||
constructor(public payload: DirectMessagePayload) {}
|
||||
constructor(public payload: PrivateMessagePayload) {}
|
||||
|
||||
public encode(): Uint8Array {
|
||||
const message = DirectMessage.Type.create(this.payload);
|
||||
return DirectMessage.Type.encode(message).finish();
|
||||
const message = PrivateMessage.Type.create(this.payload);
|
||||
return PrivateMessage.Type.encode(message).finish();
|
||||
}
|
||||
|
||||
public static decode(bytes: Uint8Array | Buffer): DirectMessage | undefined {
|
||||
const payload = DirectMessage.Type.decode(
|
||||
public static decode(bytes: Uint8Array | Buffer): PrivateMessage | undefined {
|
||||
const payload = PrivateMessage.Type.decode(
|
||||
bytes
|
||||
) as unknown as DirectMessagePayload;
|
||||
) as unknown as PrivateMessagePayload;
|
||||
if (!payload.toAddress || !payload.message) {
|
||||
console.log('Field missing on decoded Direct Message', payload);
|
||||
console.log('Field missing on decoded PrivateMessage', payload);
|
||||
return;
|
||||
}
|
||||
return new DirectMessage(payload);
|
||||
return new PrivateMessage(payload);
|
||||
}
|
||||
|
||||
get toAddress(): Uint8Array {
|
||||
|
@ -1,14 +1,14 @@
|
||||
import { Dispatch, SetStateAction } from 'react';
|
||||
import { Waku, WakuMessage } from 'js-waku';
|
||||
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { PrivateMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { validatePublicKeyMessage } from './crypto';
|
||||
import { Message } from './messaging/Messages';
|
||||
import { bufToHex, equalByteArrays } from 'js-waku/lib/utils';
|
||||
|
||||
export const PublicKeyContentTopic =
|
||||
'/eth-pm-wallet/1/encryption-public-key/proto';
|
||||
export const DirectMessageContentTopic =
|
||||
'/eth-pm-wallet/1/direct-message/proto';
|
||||
export const PrivateMessageContentTopic =
|
||||
'/eth-pm-wallet/1/private-message/proto';
|
||||
|
||||
export async function initWaku(): Promise<Waku> {
|
||||
const waku = await Waku.create({ bootstrap: true });
|
||||
@ -52,7 +52,7 @@ export function handlePublicKeyMessage(
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleDirectMessage(
|
||||
export async function handlePrivateMessage(
|
||||
setter: Dispatch<SetStateAction<Message[]>>,
|
||||
address: string,
|
||||
providerRequest: (request: {
|
||||
@ -61,7 +61,7 @@ export async function handleDirectMessage(
|
||||
}) => Promise<any>,
|
||||
wakuMsg: WakuMessage
|
||||
) {
|
||||
console.log('Direct Message received:', wakuMsg);
|
||||
console.log('Private Message received:', wakuMsg);
|
||||
if (!wakuMsg.payload) return;
|
||||
|
||||
const decryptedPayload = await providerRequest({
|
||||
@ -70,22 +70,22 @@ export async function handleDirectMessage(
|
||||
}).catch((error) => console.log(error.message));
|
||||
|
||||
console.log('Decrypted Payload:', decryptedPayload);
|
||||
const directMessage = DirectMessage.decode(
|
||||
const privateMessage = PrivateMessage.decode(
|
||||
Buffer.from(decryptedPayload, 'hex')
|
||||
);
|
||||
if (!directMessage) {
|
||||
console.log('Failed to decode Direct Message');
|
||||
if (!privateMessage) {
|
||||
console.log('Failed to decode Private Message');
|
||||
return;
|
||||
}
|
||||
if (!equalByteArrays(directMessage.toAddress, address)) return;
|
||||
if (!equalByteArrays(privateMessage.toAddress, address)) return;
|
||||
|
||||
const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date();
|
||||
|
||||
console.log('Message decrypted:', directMessage.message);
|
||||
console.log('Message decrypted:', privateMessage.message);
|
||||
setter((prevMsgs: Message[]) => {
|
||||
const copy = prevMsgs.slice();
|
||||
copy.push({
|
||||
text: directMessage.message,
|
||||
text: privateMessage.message,
|
||||
timestamp: timestamp,
|
||||
});
|
||||
return copy;
|
||||
|
@ -19,8 +19,8 @@ import WifiIcon from '@material-ui/icons/Wifi';
|
||||
import BroadcastPublicKey from './BroadcastPublicKey';
|
||||
import Messaging from './messaging/Messaging';
|
||||
import {
|
||||
DirectMessageContentTopic,
|
||||
handleDirectMessage,
|
||||
PrivateMessageContentTopic,
|
||||
handlePrivateMessage,
|
||||
handlePublicKeyMessage,
|
||||
initWaku,
|
||||
PublicKeyContentTopic,
|
||||
@ -137,19 +137,21 @@ function App() {
|
||||
if (!EncryptionKeyPair) return;
|
||||
if (!address) return;
|
||||
|
||||
const observerDirectMessage = handleDirectMessage.bind(
|
||||
const observerPrivateMessage = handlePrivateMessage.bind(
|
||||
{},
|
||||
setMessages,
|
||||
address
|
||||
);
|
||||
|
||||
waku.relay.addObserver(observerDirectMessage, [DirectMessageContentTopic]);
|
||||
waku.relay.addObserver(observerPrivateMessage, [
|
||||
PrivateMessageContentTopic,
|
||||
]);
|
||||
|
||||
return function cleanUp() {
|
||||
if (!waku) return;
|
||||
if (!observerDirectMessage) return;
|
||||
waku.relay.deleteObserver(observerDirectMessage, [
|
||||
DirectMessageContentTopic,
|
||||
if (!observerPrivateMessage) return;
|
||||
waku.relay.deleteObserver(observerPrivateMessage, [
|
||||
PrivateMessageContentTopic,
|
||||
]);
|
||||
};
|
||||
}, [waku, address, EncryptionKeyPair]);
|
||||
@ -192,7 +194,7 @@ function App() {
|
||||
light push
|
||||
</Typography>
|
||||
<Typography variant="h6" className={classes.title}>
|
||||
Ethereum Direct Message
|
||||
Ethereum Private Message
|
||||
</Typography>
|
||||
<Typography>{addressDisplay}</Typography>
|
||||
</Toolbar>
|
||||
|
@ -9,8 +9,8 @@ import {
|
||||
import React, { ChangeEvent, useState, KeyboardEvent } from 'react';
|
||||
import { Waku, WakuMessage } from 'js-waku';
|
||||
import { hexToBuf } from 'js-waku/lib/utils';
|
||||
import { DirectMessage } from './wire';
|
||||
import { DirectMessageContentTopic } from '../waku';
|
||||
import { PrivateMessage } from './wire';
|
||||
import { PrivateMessageContentTopic } from '../waku';
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
formControl: {
|
||||
@ -109,13 +109,13 @@ async function encodeEncryptedWakuMessage(
|
||||
publicKey: Uint8Array,
|
||||
address: string
|
||||
): Promise<WakuMessage> {
|
||||
const directMsg = new DirectMessage({
|
||||
const privateMessage = new PrivateMessage({
|
||||
toAddress: hexToBuf(address),
|
||||
message: message,
|
||||
});
|
||||
|
||||
const payload = directMsg.encode();
|
||||
return WakuMessage.fromBytes(payload, DirectMessageContentTopic, {
|
||||
const payload = privateMessage.encode();
|
||||
return WakuMessage.fromBytes(payload, PrivateMessageContentTopic, {
|
||||
encPublicKey: publicKey,
|
||||
});
|
||||
}
|
||||
|
@ -59,36 +59,36 @@ export class PublicKeyMessage {
|
||||
}
|
||||
}
|
||||
|
||||
export interface DirectMessagePayload {
|
||||
export interface PrivateMessagePayload {
|
||||
toAddress: Uint8Array;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct Encrypted Message used for private communication over the Waku network.
|
||||
* Encrypted Message used for private communication over the Waku network.
|
||||
*/
|
||||
export class DirectMessage {
|
||||
private static Type = new Type('DirectMessage')
|
||||
export class PrivateMessage {
|
||||
private static Type = new Type('PrivateMessage')
|
||||
.add(new Field('toAddress', 1, 'bytes'))
|
||||
.add(new Field('message', 2, 'string'));
|
||||
private static Root = new Root().define('messages').add(DirectMessage.Type);
|
||||
private static Root = new Root().define('messages').add(PrivateMessage.Type);
|
||||
|
||||
constructor(public payload: DirectMessagePayload) {}
|
||||
constructor(public payload: PrivateMessagePayload) {}
|
||||
|
||||
public encode(): Uint8Array {
|
||||
const message = DirectMessage.Type.create(this.payload);
|
||||
return DirectMessage.Type.encode(message).finish();
|
||||
const message = PrivateMessage.Type.create(this.payload);
|
||||
return PrivateMessage.Type.encode(message).finish();
|
||||
}
|
||||
|
||||
public static decode(bytes: Uint8Array | Buffer): DirectMessage | undefined {
|
||||
const payload = DirectMessage.Type.decode(
|
||||
public static decode(bytes: Uint8Array | Buffer): PrivateMessage | undefined {
|
||||
const payload = PrivateMessage.Type.decode(
|
||||
bytes
|
||||
) as unknown as DirectMessagePayload;
|
||||
) as unknown as PrivateMessagePayload;
|
||||
if (!payload.toAddress || !payload.message) {
|
||||
console.log('Field missing on decoded Direct Message', payload);
|
||||
console.log('Field missing on decoded Private Message', payload);
|
||||
return;
|
||||
}
|
||||
return new DirectMessage(payload);
|
||||
return new PrivateMessage(payload);
|
||||
}
|
||||
|
||||
get toAddress(): Uint8Array {
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { Dispatch, SetStateAction } from 'react';
|
||||
import { Waku, WakuMessage } from 'js-waku';
|
||||
import { DirectMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { PrivateMessage, PublicKeyMessage } from './messaging/wire';
|
||||
import { validatePublicKeyMessage } from './crypto';
|
||||
import { Message } from './messaging/Messages';
|
||||
import { bufToHex, equalByteArrays } from 'js-waku/lib/utils';
|
||||
|
||||
export const PublicKeyContentTopic = '/eth-pm/1/public-key/proto';
|
||||
export const DirectMessageContentTopic = '/eth-pm/1/private-message/proto';
|
||||
export const PrivateMessageContentTopic = '/eth-pm/1/private-message/proto';
|
||||
|
||||
export async function initWaku(): Promise<Waku> {
|
||||
const waku = await Waku.create({ bootstrap: true });
|
||||
@ -50,27 +50,27 @@ export function handlePublicKeyMessage(
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleDirectMessage(
|
||||
export async function handlePrivateMessage(
|
||||
setter: Dispatch<SetStateAction<Message[]>>,
|
||||
address: string,
|
||||
wakuMsg: WakuMessage
|
||||
) {
|
||||
console.log('Direct Message received:', wakuMsg);
|
||||
console.log('Private Message received:', wakuMsg);
|
||||
if (!wakuMsg.payload) return;
|
||||
const directMessage = DirectMessage.decode(wakuMsg.payload);
|
||||
if (!directMessage) {
|
||||
console.log('Failed to decode Direct Message');
|
||||
const privateMessage = PrivateMessage.decode(wakuMsg.payload);
|
||||
if (!privateMessage) {
|
||||
console.log('Failed to decode Private Message');
|
||||
return;
|
||||
}
|
||||
if (!equalByteArrays(directMessage.toAddress, address)) return;
|
||||
if (!equalByteArrays(privateMessage.toAddress, address)) return;
|
||||
|
||||
const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date();
|
||||
|
||||
console.log('Message decrypted:', directMessage.message);
|
||||
console.log('Message decrypted:', privateMessage.message);
|
||||
setter((prevMsgs: Message[]) => {
|
||||
const copy = prevMsgs.slice();
|
||||
copy.push({
|
||||
text: directMessage.message,
|
||||
text: privateMessage.message,
|
||||
timestamp: timestamp,
|
||||
});
|
||||
return copy;
|
||||
|
Loading…
x
Reference in New Issue
Block a user