diff --git a/components/badges/actions/Action.tsx b/components/badges/actions/Action.tsx index 0921f8e..4349123 100644 --- a/components/badges/actions/Action.tsx +++ b/components/badges/actions/Action.tsx @@ -5,29 +5,22 @@ import { dispatchExecute, isEitherType, previewExecutePayload } from 'components import { ActionsCombobox } from 'components/badges/actions/Combobox' import { useActionsComboboxState } from 'components/badges/actions/Combobox.hooks' import { Button } from 'components/Button' -import { Conditional } from 'components/Conditional' import { FormControl } from 'components/FormControl' -// import { FormGroup } from 'components/FormGroup' -import { AddressInput, NumberInput } from 'components/forms/FormInput' import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks' import { MetadataAttributes } from 'components/forms/MetadataAttributes' import { useMetadataAttributesState } from 'components/forms/MetadataAttributes.hooks' -import { InputDateTime } from 'components/InputDateTime' import { JsonPreview } from 'components/JsonPreview' import { TransactionHash } from 'components/TransactionHash' import { useWallet } from 'contexts/wallet' import type { Badge, BadgeHubInstance } from 'contracts/badgeHub' import * as crypto from 'crypto' -import { toPng } from 'html-to-image' import sizeof from 'object-sizeof' -import { QRCodeCanvas } from 'qrcode.react' import type { FormEvent } from 'react' -import { useEffect, useRef, useState } from 'react' +import { useEffect, useState } from 'react' import { toast } from 'react-hot-toast' -import { FaArrowRight, FaCopy, FaSave } from 'react-icons/fa' +import { FaArrowRight } from 'react-icons/fa' import { useMutation } from 'react-query' import * as secp256k1 from 'secp256k1' -import { NETWORK } from 'utils/constants' import type { AirdropAllocation } from 'utils/isValidAccountsFile' import { resolveAddress } from 'utils/resolveAddress' @@ -54,11 +47,8 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage const [transferrable, setTransferrable] = useState(undefined) const [resolvedOwnerAddress, setResolvedOwnerAddress] = useState('') const [editFee, setEditFee] = useState(undefined) - const [dispatch, setDispatch] = useState(false) - - const [createdBadgeId, setCreatedBadgeId] = useState(undefined) - const [createdBadgeKey, setCreatedBadgeKey] = useState(undefined) - const qrRef = useRef(null) + const [triggerDispatch, setTriggerDispatch] = useState(false) + const [keyPairs, setKeyPairs] = useState([]) const actionComboboxState = useActionsComboboxState() const type = actionComboboxState.value?.id @@ -179,10 +169,7 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage subtitle: 'Number of keys/owners to execute the action for', }) - const showBadgeField = type === 'create_badge' - const showMetadataField = isEitherType(type, ['create_badge', 'edit_badge']) - const showIdField = type === 'edit_badge' - const showNFTField = type === 'set_nft' + const showMetadataField = isEitherType(type, ['edit_badge']) const payload: DispatchExecuteArgs = { badge: { @@ -348,10 +335,10 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage }, []) useEffect(() => { - void dispatchEditBadge().catch((err) => { + void dispatchEditBadgeMessage().catch((err) => { toast.error(String(err), { style: { maxWidth: 'none' } }) }) - }, [dispatch]) + }, [triggerDispatch]) useEffect(() => { const addresses: string[] = [] @@ -402,18 +389,20 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage }, ) .then((currentBadge) => { + // TODO - Go over the calculation const currentBadgeMetadataSize = - Number(sizeof(currentBadge.metadata)) + Number(sizeof(currentBadge.metadata.attributes)) + Number(sizeof(currentBadge.metadata)) + Number(sizeof(currentBadge.metadata.attributes) * 2) console.log('Current badge metadata size: ', currentBadgeMetadataSize) - const newBadgeMetadataSize = Number(sizeof(badge?.metadata)) + Number(sizeof(badge?.metadata.attributes)) + const newBadgeMetadataSize = + Number(sizeof(badge?.metadata)) + Number(sizeof(badge?.metadata.attributes)) * 2 console.log('New badge metadata size: ', newBadgeMetadataSize) if (newBadgeMetadataSize > currentBadgeMetadataSize) { const calculatedFee = ((newBadgeMetadataSize - currentBadgeMetadataSize) * Number(feeRate.metadata)) / 2 setEditFee(calculatedFee) - setDispatch(!dispatch) + setTriggerDispatch(!triggerDispatch) } else { setEditFee(undefined) - setDispatch(!dispatch) + setTriggerDispatch(!triggerDispatch) } }) .catch((error) => { @@ -437,7 +426,7 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage }, ) - const dispatchEditBadge = async () => { + const dispatchEditBadgeMessage = async () => { if (type) { const txHash = await toast.promise(dispatchExecute(payload), { error: `${type.charAt(0).toUpperCase() + type.slice(1)} execute failed!`, @@ -454,36 +443,17 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage setAirdropAllocationArray(data) } - const handleGenerateKey = () => { - let privKey: Buffer - do { - privKey = crypto.randomBytes(32) - } while (!secp256k1.privateKeyVerify(privKey)) + const handleGenerateKeys = (amount: number) => { + for (let i = 0; i < amount; i++) { + let privKey: Buffer + do { + privKey = crypto.randomBytes(32) + } while (!secp256k1.privateKeyVerify(privKey)) - const privateKey = privKey.toString('hex') - setCreatedBadgeKey(privateKey) - console.log('Private Key: ', privateKey) - - const publicKey = Buffer.from(secp256k1.publicKeyCreate(privKey)).toString('hex') - - keyState.onChange(publicKey) - } - - const handleDownloadQr = async () => { - const qrElement = qrRef.current - await toPng(qrElement as HTMLElement).then((dataUrl) => { - const link = document.createElement('a') - link.download = `badge-${createdBadgeId as string}.png` - link.href = dataUrl - link.click() - }) - } - - const copyClaimURL = async () => { - const baseURL = NETWORK === 'testnet' ? 'https://badges.publicawesome.dev' : 'https://badges.stargaze.zone' - const claimURL = `${baseURL}/?id=${createdBadgeId as string}&key=${createdBadgeKey as string}` - await navigator.clipboard.writeText(claimURL) - toast.success('Copied claim URL to clipboard') + const privateKey = privKey.toString('hex') + const publicKey = Buffer.from(secp256k1.publicKeyCreate(privKey)).toString('hex') + keyPairs.push(publicKey.concat(',', privateKey)) + } } return ( @@ -491,43 +461,8 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage
- {showBadgeField && createdBadgeId && createdBadgeKey && ( -
-
- -
- {/*
*/} -
- - -
-
- )} - - {showBadgeField && } - {showBadgeField && } - {showBadgeField && } {showMetadataField && ( -
+
Metadata @@ -548,7 +483,6 @@ export const BadgeActions = ({ badgeHubContractAddress, badgeId, badgeHubMessage
)} - {showNFTField && } {/* {showAirdropFileField && ( )} */} - - - setTimestamp(date)} value={timestamp} /> - - - - - - setTimestamp(date)} value={timestamp} /> - - - {showBadgeField && } - {showBadgeField && ( -
- -
- )}