From cd0fcbbf900012d67742bf92f9180a44a9357fc7 Mon Sep 17 00:00:00 2001 From: Hunter Thornsberry Date: Tue, 6 Aug 2024 00:50:17 -0400 Subject: [PATCH] initial working version --- src/components/Form/FormPasswordGenerator.tsx | 10 ++-- src/components/PageComponents/Channel.tsx | 29 ++++++++--- src/components/UI/Generator.tsx | 52 ++++++++----------- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/components/Form/FormPasswordGenerator.tsx b/src/components/Form/FormPasswordGenerator.tsx index 762be7b0..7dc7281f 100644 --- a/src/components/Form/FormPasswordGenerator.tsx +++ b/src/components/Form/FormPasswordGenerator.tsx @@ -3,11 +3,11 @@ import type { GenericFormElementProps, } from "@components/Form/DynamicForm.js"; import { Generator } from "@components/UI/Generator.js"; -import { useState } from "react"; import { Controller, type FieldValues } from "react-hook-form"; export interface PasswordGeneratorProps extends BaseFormBuilderProps { type: "passwordGenerator"; + devicePSKBitCount: number; } export function PasswordGenerator({ @@ -20,11 +20,13 @@ export function PasswordGenerator({ control={control} render={({ field: { value, onChange, ...rest } }) => ( + {...rest} /> )} /> ); diff --git a/src/components/PageComponents/Channel.tsx b/src/components/PageComponents/Channel.tsx index b9c76612..942b6666 100644 --- a/src/components/PageComponents/Channel.tsx +++ b/src/components/PageComponents/Channel.tsx @@ -4,6 +4,8 @@ import { useToast } from "@core/hooks/useToast.js"; import { useDevice } from "@core/stores/deviceStore.js"; import { Protobuf } from "@meshtastic/js"; import { fromByteArray, toByteArray } from "base64-js"; +import cryptoRandomString from "crypto-random-string"; +import { useState } from "react"; export interface SettingsPanelProps { channel: Protobuf.Channel.Channel; @@ -13,12 +15,15 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => { const { config, connection, addChannel } = useDevice(); const { toast } = useToast(); + const [pass, setPass] = useState(fromByteArray(channel?.settings?.psk ?? new Uint8Array(0))); + const [bitCount, setBits] = useState(channel?.settings?.psk.length ?? 16); + const onSubmit = (data: ChannelValidation) => { const channel = new Protobuf.Channel.Channel({ ...data, settings: { ...data.settings, - psk: toByteArray(data.settings.psk ?? ""), + psk: toByteArray(pass), moduleSettings: { positionPrecision: data.settings.positionEnabled ? data.settings.preciseLocation @@ -36,6 +41,18 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => { }); }; + + const clickEventCb = (e) => { + setPass( + btoa( + cryptoRandomString({ + length: bitCount ?? 0, + type: "alphanumeric", + }), + ), + ); + } + return ( onSubmit={onSubmit} @@ -46,7 +63,7 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => { ...{ settings: { ...channel?.settings, - psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)), + psk: pass, positionEnabled: channel?.settings?.moduleSettings?.positionPrecision !== undefined && @@ -80,11 +97,11 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => { name: "settings.psk", label: "pre-Shared Key", description: "256, 128, or 8 bit PSKs allowed", + devicePSKBitCount: bitCount ?? 0, properties: { - passwordValue: fromByteArray( - channel?.settings?.psk ?? new Uint8Array(0), - ), - devicePSKBitCount: channel?.settings?.psk.length, + value: pass, + onClick: clickEventCb, + changeEvent: (e: string) => setBits(parseInt(e)), }, }, { diff --git a/src/components/UI/Generator.tsx b/src/components/UI/Generator.tsx index bcc78c59..dc057336 100644 --- a/src/components/UI/Generator.tsx +++ b/src/components/UI/Generator.tsx @@ -10,9 +10,9 @@ import { SelectTrigger, SelectValue, } from "@components/UI/Select.js"; -import { cn } from "@core/utils/cn.js"; import cryptoRandomString from "crypto-random-string"; import { useState } from "react"; +import { fromByteArray, toByteArray } from "base64-js"; const generatorVariants = cva( "inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors focus:outline-none focus:ring-2", @@ -49,36 +49,41 @@ export interface GeneratorProps extends React.BaseHTMLAttributes, VariantProps { devicePSKBitCount?: number; - passwordValue?: string; - textValue?: string; + value: string; + buttonText?: string; + changeEvent: Function; + } -const Generator = React.forwardRef( +const getBitString = (bitcount?: number) => { + if (bitcount == 32) { + return "32" + } + if (bitcount == 1) { + return "1" + } + return "16" +} + +const Generator = React.forwardRef( ( { devicePSKBitCount, - passwordValue, - textValue, - className, + value, + buttonText, variant, - size, + changeEvent, ...props }, ref, ) => { - const [pass, setPass] = useState(passwordValue ?? ""); - const [bitCount, setBits] = useState( - devicePSKBitCount?.toString() ?? "", - ); return ( <> - +