From 3ac36a8a87972d477b950d39fcf05bd53edd2f9e Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Thu, 9 Dec 2021 20:14:52 +1100 Subject: [PATCH] Channel PSK generator --- src/components/Channel.tsx | 67 ++++++++++++++++++++++++++------- src/pages/settings/Channels.tsx | 2 +- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/components/Channel.tsx b/src/components/Channel.tsx index d2b0249f..9fabda58 100644 --- a/src/components/Channel.tsx +++ b/src/components/Channel.tsx @@ -1,13 +1,16 @@ import React from 'react'; -import { useForm } from 'react-hook-form'; +import { fromByteArray, toByteArray } from 'base64-js'; +import { useForm, useWatch } from 'react-hook-form'; import { FaQrcode } from 'react-icons/fa'; import { FiEdit3, FiSave } from 'react-icons/fi'; +import { MdRefresh, MdVisibility, MdVisibilityOff } from 'react-icons/md'; import QRCode from 'react-qr-code'; import { Card } from '@components/generic/Card'; import { Checkbox } from '@components/generic/form/Checkbox'; import { Input } from '@components/generic/form/Input'; +import { Select } from '@components/generic/form/Select'; import { IconButton } from '@components/generic/IconButton'; import { Loading } from '@components/generic/Loading'; import { Modal } from '@components/generic/Modal'; @@ -16,18 +19,17 @@ import { Protobuf } from '@meshtastic/meshtasticjs'; export interface ChannelProps { channel: Protobuf.Channel; - hideEnabled?: boolean; + isPrimary?: boolean; } -export const Channel = ({ - channel, - hideEnabled, -}: ChannelProps): JSX.Element => { +export const Channel = ({ channel, isPrimary }: ChannelProps): JSX.Element => { const [edit, setEdit] = React.useState(false); const [loading, setLoading] = React.useState(false); const [showQr, setShowQr] = React.useState(false); + const [keySize, setKeySize] = React.useState<128 | 256>(256); + const [pskHidden, setPskHidden] = React.useState(true); - const { register, handleSubmit } = useForm<{ + const { register, handleSubmit, setValue, control } = useForm<{ enabled: boolean; settings: { name: string; @@ -55,11 +57,17 @@ export const Channel = ({ downlinkEnabled: channel.settings?.downlinkEnabled, uplinkEnabled: channel.settings?.uplinkEnabled, txPower: channel.settings?.txPower, - psk: new TextDecoder().decode(channel.settings?.psk), + psk: fromByteArray(channel.settings?.psk ?? new Uint8Array(0)), }, }, }); + const watchPsk = useWatch({ + control, + name: 'settings.psk', + defaultValue: '', + }); + const onSubmit = handleSubmit(async (data) => { setLoading(true); const adminChannel = Protobuf.Channel.create({ @@ -72,7 +80,7 @@ export const Channel = ({ index: channel.index, settings: { ...data.settings, - psk: new TextEncoder().encode(data.settings.psk), + psk: toByteArray(data.settings.psk ?? ''), }, }); @@ -91,26 +99,57 @@ export const Channel = ({ }} > - + {edit ? ( <> {loading && }
- {/* TODO: get gap working */}
- {/* @todo: change to disable & make primary buttons */} - {!hideEnabled && ( + {!isPrimary && ( )} + + { + setPskHidden(!setPskHidden); + }} + icon={pskHidden ? : } + /> + { + const key = new Uint8Array(keySize); + crypto.getRandomValues(key); + setValue('settings.psk', fromByteArray(key)); + }} + icon={} + /> + + } {...register('settings.psk')} /> ))}