import type { SlDialog } from '@shoelace-style/shoelace' import type { Accessor, Component } from 'solid-js' import { buf2hex } from '@wovin/utils' import { Logger } from 'besonders-logger' import { createMnemonic, encHashedMnemonic, getECDHkeypairFromHashArray, testStats } from 'mnemkey' import { createEffect, createSignal, For, Show } from 'solid-js' import { getMnemonicFromIDB } from '../../data/agent/AgentCrypto' import { useAgent } from '../../data/agent/AgentState' import { notifyToast } from '../../ui/utils-ui' import { Iconify } from '../mini-components' const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.INFO) // eslint-disable-line unused-imports/no-unused-vars const inputStyle = { '--sl-input-font-size-small': '11px', } const exampleMnem = 'basket,chief,toad,chemical,little,analysis,admonish,cupcake,diatribe,obedient,craving,nightcap,hopeful,street,raggedy,stymie,mechanic,papyrus,sorcerer,plushy,rickshaw,taffy,nocturne,unicycle' export const SingleWord: Component<{ index: number mnemArray: Accessor }> = function SingleWord(props) { const [myWord, setMyword] = createSignal(props.mnemArray()[props.index]) const onInput = (evt: InputEvent) => { setMyword((evt.target as HTMLInputElement).value) } return ( ) } export const MnemDialog: Component<{ // mnemDialogRef: any }> = function MnemDialog() { const [storedMnemonic, setStoredMnemonic] = createSignal() const [isKeyMatching, setMatching] = createSignal(false) const [mnemToTest, setMnem] = createSignal('') const [mnemArray, setMnemArr] = createSignal([]) createEffect(() => { const cleanedMnemString = mnemToTest().replaceAll(' ', ',').replaceAll(',,,', ',').replaceAll(',,', ',') setMnemArr(cleanedMnemString.split(',')) VERBOSE('cleaned array', mnemArray()) }) const onInput = (evt: InputEvent) => { setMnem((evt.target as HTMLInputElement).value) } const compareDerivedKeys = async () => { const { publicKey } = await getECDHkeypairFromHashArray(encHashedMnemonic(mnemArray())) DEBUG({ publicKey }, mnemArray()) const isMatching = await comparePubDerivationKey(buf2hex(await globalThis.crypto.subtle.exportKey('raw', publicKey))) if (isMatching) { setMatching(true) notifyToast(`Looking good, your public key in IDB matches the one derived from the mnemonic you entered`, 'success', 10000) } else { notifyToast(`UhOh, your pubkey DOES NOT MATCH the one derived from the mnemonic`, 'danger', 10000) } } const comparePubDerivationKey = async (hexPubKey, newMnemArr?: string[]) => { const agent = useAgent() const { publicKey } = await agent.getDerivationKeypair(newMnemArr) const exportedPubKeyHex = buf2hex(await globalThis.crypto.subtle.exportKey('raw', publicKey)) const isMatching = !!(hexPubKey === exportedPubKeyHex) DEBUG({ isMatching, hexPubKey, exportedPubKeyHex }) return isMatching } const afterhide = async (hideargs) => { DEBUG({ hideargs }) } const dealWithStoredMnem = async () => { const mnemFromIDB = await getMnemonicFromIDB(useAgent()) DEBUG({ mnemFromIDB }) setStoredMnemonic(mnemFromIDB) setMnem(mnemFromIDB) notifyToast('You should really save the mnemonic so we can delete it from IDB (even if it is encrypted)') } const importDerivationKey = async (newMnemArr?: string[]) => { const { publicKey } = await useAgent().getDerivationKeypair(newMnemArr) const exportedPubKeyHex = buf2hex(await globalThis.crypto.subtle.exportKey('raw', publicKey)) DEBUG({ exportedPubKeyHex }) } let mnemDialogRef: SlDialog return ( <> mnemDialogRef?.show()} size='small' >
Mnemonic Key

Enter a valid 24 word mnemonic:

1}>
{ if (index % 2 === 0) { result.push(array.slice(index, index + 2)) } return result }, [])} > {(_item, eachIndex) => { return (
) }}
Check
{ setMnem(createMnemonic(24).join(',')) testStats(256, 10) }} size='small' >
New
{ importDerivationKey(mnemArray()) testStats(256, 10) }} size='small' >
Import
Export
) }