2022-06-17 10:48:15 +10:00
|
|
|
import { Button } from "@material-ui/core";
|
|
|
|
|
import React, { useState } from "react";
|
|
|
|
|
import {
|
|
|
|
|
createPublicKeyMessage,
|
|
|
|
|
KeyPair,
|
|
|
|
|
PublicKeyMessageEncryptionKey,
|
|
|
|
|
} from "./crypto";
|
|
|
|
|
import { PublicKeyMessage } from "./messaging/wire";
|
2022-12-21 21:22:23 +01:00
|
|
|
import type { RelayNode } from "@waku/interfaces";
|
|
|
|
|
import { createEncoder } from "@waku/message-encryption/symmetric";
|
2022-06-17 10:48:15 +10:00
|
|
|
import { PublicKeyContentTopic } from "./waku";
|
2022-08-29 15:16:41 +10:00
|
|
|
import type { TypedDataSigner } from "@ethersproject/abstract-signer";
|
2022-06-17 10:48:15 +10:00
|
|
|
|
|
|
|
|
interface Props {
|
2022-09-20 14:21:52 +10:00
|
|
|
encryptionKeyPair: KeyPair | undefined;
|
2022-12-21 21:22:23 +01:00
|
|
|
waku: RelayNode | undefined;
|
2022-06-17 10:48:15 +10:00
|
|
|
address: string | undefined;
|
2022-08-29 15:16:41 +10:00
|
|
|
signer: TypedDataSigner | undefined;
|
2022-06-17 10:48:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function BroadcastPublicKey({
|
2022-09-20 14:21:52 +10:00
|
|
|
encryptionKeyPair,
|
2022-06-17 10:48:15 +10:00
|
|
|
waku,
|
|
|
|
|
address,
|
2022-08-29 15:16:41 +10:00
|
|
|
signer,
|
2022-06-17 10:48:15 +10:00
|
|
|
}: Props) {
|
|
|
|
|
const [publicKeyMsg, setPublicKeyMsg] = useState<PublicKeyMessage>();
|
|
|
|
|
|
2022-09-20 14:21:52 +10:00
|
|
|
const broadcastPublicKey = async () => {
|
|
|
|
|
if (!encryptionKeyPair) return;
|
2022-06-17 10:48:15 +10:00
|
|
|
if (!address) return;
|
|
|
|
|
if (!waku) return;
|
2022-08-29 15:16:41 +10:00
|
|
|
if (!signer) return;
|
2022-06-17 10:48:15 +10:00
|
|
|
|
2022-09-20 14:21:52 +10:00
|
|
|
const _publicKeyMessage = await (async () => {
|
|
|
|
|
if (!publicKeyMsg) {
|
|
|
|
|
const pkm = await createPublicKeyMessage(
|
|
|
|
|
address,
|
|
|
|
|
encryptionKeyPair.publicKey,
|
|
|
|
|
signer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setPublicKeyMsg(pkm);
|
|
|
|
|
return pkm;
|
|
|
|
|
}
|
|
|
|
|
return publicKeyMsg;
|
|
|
|
|
})();
|
|
|
|
|
const payload = _publicKeyMessage.encode();
|
|
|
|
|
|
2022-12-21 21:22:23 +01:00
|
|
|
const publicKeyMessageEncoder = createEncoder(
|
2022-09-20 14:21:52 +10:00
|
|
|
PublicKeyContentTopic,
|
2023-01-19 14:28:41 +05:30
|
|
|
PublicKeyMessageEncryptionKey,
|
|
|
|
|
undefined,
|
|
|
|
|
true
|
2022-09-20 14:21:52 +10:00
|
|
|
);
|
|
|
|
|
|
2022-11-07 16:16:59 +05:30
|
|
|
await waku.relay.send(publicKeyMessageEncoder, { payload });
|
2022-06-17 10:48:15 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
onClick={broadcastPublicKey}
|
2022-09-20 14:21:52 +10:00
|
|
|
disabled={!encryptionKeyPair || !waku || !address || !signer}
|
2022-06-17 10:48:15 +10:00
|
|
|
>
|
|
|
|
|
Broadcast Encryption Public Key
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|