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

69 lines
1.7 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";
2022-09-20 14:21:52 +10:00
import type { WakuLight } from "js-waku/lib/interfaces";
import { SymEncoder } from "js-waku/lib/waku_message/version_1";
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;
2022-09-13 12:23:56 +10:00
waku: WakuLight | 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 = new SymEncoder(
PublicKeyContentTopic,
PublicKeyMessageEncryptionKey
);
waku.lightPush.push(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>
);
}