examples.waku.org/examples/eth-pm/src/BroadcastPublicKey.tsx

71 lines
1.8 KiB
TypeScript
Raw Normal View History

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";
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";
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;
waku: RelayNode | undefined;
2022-06-17 10:48:15 +10:00
address: string | undefined;
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,
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;
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();
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
);
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>
);
}