Skip to content

Commit

Permalink
check if user is agent
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Nov 18, 2024
1 parent 4d6d1a3 commit 71a5511
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<ICommand | undefined>;
isAgent: boolean;
}

export const AccountContext = createContext<IAccountContext>({
Expand All @@ -27,13 +29,25 @@ export const AccountContext = createContext<IAccountContext>({
login: () => {},
logout: () => {},
sign: async () => undefined,
isAgent: false,
});

export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
const [account, setAccount] = useState<ConnectedAccount>();
const [account, setAccount] = useState<IWalletAccount>();
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();
Expand All @@ -49,6 +63,7 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
});

const account = (payload as IState).accounts[0] as IWalletAccount;

localStorage.setItem(getAccountCookieName(), JSON.stringify(account)!);
close();
}, [router]);
Expand All @@ -73,6 +88,16 @@ export const AccountProvider: FC<PropsWithChildren> = ({ 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<ICommand | undefined> => {
const { message, close } = await getWalletConnection();
const response = await message('SIGN_REQUEST', tx as any);
Expand All @@ -87,7 +112,7 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {

return (
<AccountContext.Provider
value={{ account, login, logout, sign, isMounted }}
value={{ account, login, logout, sign, isMounted, isAgent: isAgentState }}
>
{children}
</AccountContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
};
Expand Down
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/hooks/getAgents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const useGetAgents = () => {
},
});

console.log(data?.events.edges);
const agents =
data?.events.edges.map((edge: any) => {
console.log(edge);
Expand Down
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/services/fetchAgents.ts

This file was deleted.

35 changes: 35 additions & 0 deletions packages/apps/rwa-demo/src/services/isAgent.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 2 additions & 2 deletions packages/apps/rwa-demo/src/services/removeAgent.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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))`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ export const FormatWithAction: Story = {
},
render: ({ isLoading }) => {
const callAction = (value: any) => {
console.log({ value });
alert(value);
};
return (
Expand Down

0 comments on commit 71a5511

Please sign in to comment.