From 71a55112100186891a8d5f301a3a6ba1daca5d8c Mon Sep 17 00:00:00 2001 From: Steven Straatemans Date: Mon, 18 Nov 2024 11:54:13 +0100 Subject: [PATCH] check if user is agent --- .../AccountProvider/AccountProvider.tsx | 33 ++++++++++++++--- .../src/components/AgentsList/AgentsList.tsx | 2 -- packages/apps/rwa-demo/src/hooks/getAgents.ts | 1 - .../apps/rwa-demo/src/services/fetchAgents.ts | 1 - .../apps/rwa-demo/src/services/isAgent.ts | 35 +++++++++++++++++++ .../apps/rwa-demo/src/services/removeAgent.ts | 4 +-- .../CompactTable/CompactTable.stories.tsx | 1 - 7 files changed, 66 insertions(+), 11 deletions(-) delete mode 100644 packages/apps/rwa-demo/src/services/fetchAgents.ts create mode 100644 packages/apps/rwa-demo/src/services/isAgent.ts diff --git a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx index 9d414540d2..fc20f8e8f1 100644 --- a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx +++ b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx @@ -1,7 +1,8 @@ 'use client'; +import { useNetwork } from '@/hooks/networks'; +import { isAgent } from '@/services/isAgent'; import { getAccountCookieName } from '@/utils/getAccountCookieName'; import type { ICommand, IUnsignedCommand } from '@kadena/client'; -import type { ConnectedAccount } from '@kadena/spirekey-sdk'; import { useRouter } from 'next/navigation'; import type { FC, PropsWithChildren } from 'react'; import { createContext, useCallback, useEffect, useState } from 'react'; @@ -13,12 +14,13 @@ interface IAccountError { } export interface IAccountContext { - account?: ConnectedAccount; + account?: IWalletAccount; error?: IAccountError; isMounted: boolean; login: () => void; logout: () => void; sign: (tx: IUnsignedCommand) => Promise; + isAgent: boolean; } export const AccountContext = createContext({ @@ -27,13 +29,25 @@ export const AccountContext = createContext({ login: () => {}, logout: () => {}, sign: async () => undefined, + isAgent: false, }); export const AccountProvider: FC = ({ children }) => { - const [account, setAccount] = useState(); + const [account, setAccount] = useState(); const [isMounted, setIsMounted] = useState(false); + const [isAgentState, setIsAgentState] = useState(false); + const { activeNetwork } = useNetwork(); const router = useRouter(); + const checkIsAgent = async (account: IWalletAccount) => { + const resIsAgent = await isAgent( + { agent: account.address }, + activeNetwork, + account, + ); + setIsAgentState(!!resIsAgent); + }; + const login = useCallback(async () => { const { message, focus, close } = await getWalletConnection(); focus(); @@ -49,6 +63,7 @@ export const AccountProvider: FC = ({ children }) => { }); const account = (payload as IState).accounts[0] as IWalletAccount; + localStorage.setItem(getAccountCookieName(), JSON.stringify(account)!); close(); }, [router]); @@ -73,6 +88,16 @@ export const AccountProvider: FC = ({ children }) => { setIsMounted(true); }, []); + useEffect(() => { + if (!account) { + setIsAgentState(false); + return; + } + + // eslint-disable-next-line @typescript-eslint/no-floating-promises + checkIsAgent(account); + }, [account]); + const sign = async (tx: IUnsignedCommand): Promise => { const { message, close } = await getWalletConnection(); const response = await message('SIGN_REQUEST', tx as any); @@ -87,7 +112,7 @@ export const AccountProvider: FC = ({ children }) => { return ( {children} diff --git a/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx b/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx index b8980bdb5c..052feb3034 100644 --- a/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx +++ b/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx @@ -24,12 +24,10 @@ export const AgentsList: FC = () => { const signedTransaction = await sign(tx); if (!signedTransaction) return; - console.log({ cmd: JSON.parse(signedTransaction.cmd) }); const client = getClient(); const res = await client.submit(signedTransaction); await client.listen(res); - console.log(111, { res }); console.log('DONE'); } catch (e: any) {} }; diff --git a/packages/apps/rwa-demo/src/hooks/getAgents.ts b/packages/apps/rwa-demo/src/hooks/getAgents.ts index 60b1ab1bae..a1d98752a6 100644 --- a/packages/apps/rwa-demo/src/hooks/getAgents.ts +++ b/packages/apps/rwa-demo/src/hooks/getAgents.ts @@ -20,7 +20,6 @@ export const useGetAgents = () => { }, }); - console.log(data?.events.edges); const agents = data?.events.edges.map((edge: any) => { console.log(edge); diff --git a/packages/apps/rwa-demo/src/services/fetchAgents.ts b/packages/apps/rwa-demo/src/services/fetchAgents.ts deleted file mode 100644 index ebe79b956a..0000000000 --- a/packages/apps/rwa-demo/src/services/fetchAgents.ts +++ /dev/null @@ -1 +0,0 @@ -export const fetchAgents = () => {}; diff --git a/packages/apps/rwa-demo/src/services/isAgent.ts b/packages/apps/rwa-demo/src/services/isAgent.ts new file mode 100644 index 0000000000..f7455b1ebd --- /dev/null +++ b/packages/apps/rwa-demo/src/services/isAgent.ts @@ -0,0 +1,35 @@ +import type { IWalletAccount } from '@/components/AccountProvider/utils'; +import type { INetwork } from '@/components/NetworkProvider/NetworkProvider'; +import { ADMIN } from '@/constants'; +import { getClient } from '@/utils/client'; +import { Pact } from '@kadena/client'; + +export interface IIsAgentProps { + agent: string; +} + +export const isAgent = async ( + data: IIsAgentProps, + network: INetwork, + account: IWalletAccount, +) => { + const client = getClient(); + + const transaction = Pact.builder + .execution(`(RWA.agent-role.is-agent (read-string 'agent))`) + .setMeta({ + senderAccount: ADMIN.account, + chainId: network.chainId, + }) + .addData('agent', data.agent) + .setNetworkId(network.networkId) + .createTransaction(); + + const { result } = await client.local(transaction, { + preflight: false, + signatureVerification: false, + }); + console.log({ result }); + + return result.status === 'success' ? result.data : undefined; +}; diff --git a/packages/apps/rwa-demo/src/services/removeAgent.ts b/packages/apps/rwa-demo/src/services/removeAgent.ts index 00f3680193..69551603b9 100644 --- a/packages/apps/rwa-demo/src/services/removeAgent.ts +++ b/packages/apps/rwa-demo/src/services/removeAgent.ts @@ -1,7 +1,7 @@ +import type { IWalletAccount } from '@/components/AccountProvider/utils'; import type { INetwork } from '@/components/NetworkProvider/NetworkProvider'; import { ADMIN } from '@/constants'; import { Pact } from '@kadena/client'; -import type { ConnectedAccount } from '@kadena/spirekey-sdk'; export interface IRemoveAgentProps { agent: string; @@ -10,7 +10,7 @@ export interface IRemoveAgentProps { export const removeAgent = async ( data: IRemoveAgentProps, network: INetwork, - account: ConnectedAccount, + account: IWalletAccount, ) => { return Pact.builder .execution(`(RWA.agent-role.remove-agent (read-string 'agent))`) diff --git a/packages/libs/kode-ui/src/patterns/CompactTable/CompactTable.stories.tsx b/packages/libs/kode-ui/src/patterns/CompactTable/CompactTable.stories.tsx index 71326fe9ba..32ce797d31 100644 --- a/packages/libs/kode-ui/src/patterns/CompactTable/CompactTable.stories.tsx +++ b/packages/libs/kode-ui/src/patterns/CompactTable/CompactTable.stories.tsx @@ -313,7 +313,6 @@ export const FormatWithAction: Story = { }, render: ({ isLoading }) => { const callAction = (value: any) => { - console.log({ value }); alert(value); }; return (