From 89cf04bf3ea80ce4cfaee7a27766d2a5686fe3d9 Mon Sep 17 00:00:00 2001 From: Matias Benary Date: Fri, 9 Aug 2024 19:20:20 -0300 Subject: [PATCH 1/2] feat: added key utilities --- src/components/wallet-utilities/KeyTable.tsx | 111 +++++++++++++++++++ src/pages/wallet-utilities.tsx | 12 +- 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/components/wallet-utilities/KeyTable.tsx diff --git a/src/components/wallet-utilities/KeyTable.tsx b/src/components/wallet-utilities/KeyTable.tsx new file mode 100644 index 000000000..64baaeec2 --- /dev/null +++ b/src/components/wallet-utilities/KeyTable.tsx @@ -0,0 +1,111 @@ +import { Button, Table, Text, Tooltip } from '@near-pagoda/ui'; +import type { DeleteKeyAction } from '@near-wallet-selector/core'; +import { useEffect, useState } from 'react'; + +import { useAuthStore } from '@/stores/auth'; +import { useVmStore } from '@/stores/vm'; + +type AccessKey = { + public_key: string; + access_key: { + permission: { + FunctionCall?: { + receiver_id: string; + allowance?: string; + }; + }; + }; +}; + +const KeyTable: React.FC = () => { + const near = useVmStore((store) => store.near); + const accountId = useAuthStore((store) => store.accountId); + const wallet = useAuthStore((store) => store.wallet); + + const [keys, setKeys] = useState([]); + const [selectedKeys, setSelectedKeys] = useState([]); + + useEffect(() => { + if (!near || !accountId) return; + + const getInfo = async () => { + const { nearConnection } = near; + const account = await nearConnection.account(accountId); + const accessKeys = (await account.getAccessKeys()) as AccessKey[]; + setKeys(accessKeys.filter((accessKey) => accessKey.access_key.permission.FunctionCall)); + }; + + getInfo(); + }, [near, accountId]); + + const truncatePublicKey = (key: string): string => `${key.slice(0, 20)}...`; + + const handleSelectAll = (): void => { + if (selectedKeys.length === keys.length) { + setSelectedKeys([]); + } else { + setSelectedKeys(keys.map((key) => key.public_key)); + } + }; + + const handleSelect = (id: string): void => { + setSelectedKeys((prev) => (prev.includes(id) ? prev.filter((keyId) => keyId !== id) : [...prev, id])); + }; + + const handleDeauthorizeAll = async (): Promise => { + if (!accountId || !wallet) return; + + const actions: DeleteKeyAction[] = selectedKeys.map((publicKey) => { + return { + type: 'DeleteKey', + params: { + publicKey: publicKey, + }, + }; + }); + + await wallet.signAndSendTransaction({ receiverId: accountId, actions }); + }; + + return ( + <> +