|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { usePersistStorage } from '@/hooks/use-form-storage'; |
| 4 | +import sanitizeHtml from 'sanitize-html'; |
| 5 | +import { Sha256 } from '@aws-crypto/sha256-browser'; |
| 6 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 7 | +import { useAccount, useLoginInfo, useSignMessage } from '@useelven/core'; |
| 8 | +import { useForm } from 'react-hook-form'; |
| 9 | +import { z } from 'zod'; |
| 10 | +import { Form } from '@/components/ui/form'; |
| 11 | +import { OperationsInputField } from '@/components/operations/operations-input-field'; |
| 12 | +import { OperationsSubmitButton } from '@/components/operations/operations-submit-button'; |
| 13 | +import { useEffect } from 'react'; |
| 14 | +import { Spinner } from '@/components/ui/spinner'; |
| 15 | + |
| 16 | +const formSchema = z.object({ |
| 17 | + inscription: z.string(), |
| 18 | +}); |
| 19 | + |
| 20 | +export const Sign = ({ |
| 21 | + setNextStep, |
| 22 | +}: { |
| 23 | + setNextStep: (state: boolean) => void; |
| 24 | +}) => { |
| 25 | + const { address } = useAccount(); |
| 26 | + const { loginMethod } = useLoginInfo(); |
| 27 | + |
| 28 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 29 | + resolver: zodResolver(formSchema), |
| 30 | + defaultValues: { |
| 31 | + inscription: '', |
| 32 | + }, |
| 33 | + }); |
| 34 | + |
| 35 | + const { signMessage, signature, pending } = useSignMessage(); |
| 36 | + |
| 37 | + const { setItem: saveInscription } = usePersistStorage({ |
| 38 | + update: (inscription) => { |
| 39 | + form.setValue('inscription', inscription); |
| 40 | + }, |
| 41 | + storageItem: 'general-createInscription-inscription', |
| 42 | + withCleanup: false, |
| 43 | + }); |
| 44 | + |
| 45 | + const { setItem: savePayload } = usePersistStorage({ |
| 46 | + storageItem: 'general-createInscription-partialPayload', |
| 47 | + withCleanup: false, |
| 48 | + }); |
| 49 | + |
| 50 | + const { setItem: saveSignature } = usePersistStorage({ |
| 51 | + storageItem: 'general-createInscription-signature', |
| 52 | + withCleanup: false, |
| 53 | + }); |
| 54 | + |
| 55 | + const prepareData = async ({ inscription }: z.infer<typeof formSchema>) => { |
| 56 | + saveInscription(inscription); |
| 57 | + const sanitized = sanitizeHtml( |
| 58 | + inscription.replaceAll('\n', '').trim() || '' |
| 59 | + ); |
| 60 | + |
| 61 | + try { |
| 62 | + JSON.parse(sanitized); |
| 63 | + } catch { |
| 64 | + form.setError('inscription', { |
| 65 | + message: |
| 66 | + "You've provided the wrong JSON format. Could you try again? Beside the structure remember about double quotes (also for keys) and no trailing coma.", |
| 67 | + }); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const sanitizedBase64 = Buffer.from(sanitized).toString('base64'); |
| 72 | + const hash = new Sha256(); |
| 73 | + hash.update(sanitizedBase64); |
| 74 | + const shaValue = await hash.digest(); |
| 75 | + const shaString = Buffer.from(shaValue.buffer).toString('hex'); |
| 76 | + |
| 77 | + await signMessage({ |
| 78 | + message: shaString, |
| 79 | + options: { callbackUrl: '/inscriptions/create' }, |
| 80 | + }); |
| 81 | + |
| 82 | + const payload = { |
| 83 | + identifier: shaString, |
| 84 | + data: sanitizedBase64, |
| 85 | + owner: address, |
| 86 | + }; |
| 87 | + |
| 88 | + savePayload(payload); |
| 89 | + }; |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + if (signature) { |
| 93 | + saveSignature(signature); |
| 94 | + setNextStep(true); |
| 95 | + } |
| 96 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 97 | + }, [signature]); |
| 98 | + |
| 99 | + const getSigningProviderName = () => { |
| 100 | + if (loginMethod === 'walletconnect') { |
| 101 | + return 'xPortal'; |
| 102 | + } |
| 103 | + return loginMethod; |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <> |
| 108 | + <div className="mb-3 sm:px-8"> |
| 109 | + {pending && ( |
| 110 | + <div className="font-bold flex items-center gap-3"> |
| 111 | + <Spinner size={20} /> Transaction pending (confirmation through{' '} |
| 112 | + {getSigningProviderName()})... |
| 113 | + </div> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + <div className="overflow-y-auto py-0 px-0 sm:px-8"> |
| 117 | + <Form {...form}> |
| 118 | + <form |
| 119 | + id="inscription-form" |
| 120 | + onSubmit={form.handleSubmit(prepareData)} |
| 121 | + className="space-y-8" |
| 122 | + > |
| 123 | + <div className="flex-1 overflow-auto p-1"> |
| 124 | + <OperationsInputField |
| 125 | + name="inscription" |
| 126 | + label="Inscription data" |
| 127 | + type="textarea" |
| 128 | + rows={10} |
| 129 | + placeholder='Example: { "myKey1": "myValue1", "myKey2": "myValue2" }' |
| 130 | + description="You can paste JSON data that will be then encoded with base64. You will be signing a sha256 hash of your data." |
| 131 | + /> |
| 132 | + </div> |
| 133 | + </form> |
| 134 | + </Form> |
| 135 | + </div> |
| 136 | + <div className="flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2 py-4 px-8"> |
| 137 | + <OperationsSubmitButton |
| 138 | + formId="inscription-form" |
| 139 | + label="Sign the data first!" |
| 140 | + disabled={Boolean(signature)} |
| 141 | + pending={pending} |
| 142 | + /> |
| 143 | + </div> |
| 144 | + </> |
| 145 | + ); |
| 146 | +}; |
0 commit comments