Skip to content

Commit

Permalink
Fix/sign request (#2802)
Browse files Browse the repository at this point in the history
* fix(dw): sign request

* fix(dw): sign request
  • Loading branch information
javadkh2 authored Jan 17, 2025
1 parent 2e520f2 commit b62fd47
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 32 deletions.
2 changes: 2 additions & 0 deletions packages/apps/dev-wallet/src/App/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { KeepPasswordPolicy } from '@/pages/settings/keep-password-policy/keep-p
import { RevealPhrase } from '@/pages/settings/reveal-phrase/reveal-phrase';
import { Settings } from '@/pages/settings/settings';
import { SignatureBuilder } from '@/pages/signature-builder/signature-builder';
import { SignRequest } from '@/pages/transaction/sign-request';
import { TransactionPage } from '@/pages/transaction/Transaction';
import { Transfer } from '@/pages/transfer/transfer';
import { ImportChainweaverExport } from '@/pages/wallet-recovery/import-chainweaver-export/import-chainweaver-export';
Expand Down Expand Up @@ -143,6 +144,7 @@ export const Routes: FC = () => {
path="/settings/keep-password-policy"
element={<KeepPasswordPolicy />}
/>
<Route path="/sign-request/:requestId" element={<SignRequest />} />
</Route>
</Route>
<Route element={<LayoutFull />}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useGlobalState } from '@/App/providers/globalState';
import { usePatchedNavigate } from '@/utils/usePatchedNavigate';
import { IPactCommand, IUnsignedCommand } from '@kadena/client';
import {
FC,
PropsWithChildren,
Expand All @@ -9,7 +8,6 @@ import {
useEffect,
useState,
} from 'react';
import { addTransaction } from '../transaction/transaction.service';
import { useWallet } from '../wallet/wallet.hook';

type Message = {
Expand Down Expand Up @@ -95,6 +93,7 @@ export const CommunicationProvider: FC<PropsWithChildren> = ({ children }) => {
const handlers = [
handleRequest('CONNECTION_REQUEST', '/connect'),
handleRequest('PAYMENT_REQUEST', '/payment'),
handleRequest('SIGN_REQUEST', '/sign-request'),
handle('GET_STATUS', async () => {
return {
payload: {
Expand All @@ -103,36 +102,6 @@ export const CommunicationProvider: FC<PropsWithChildren> = ({ children }) => {
},
};
}),
handle('SIGN_REQUEST', async (data) => {
const { id, payload } = data as {
id: string;
payload: IUnsignedCommand;
};
console.log('SIGN_REQUEST', id);
console.log('payload', payload);
const cmd = JSON.parse(payload.cmd) as IPactCommand;
const networkUUID =
networks.find(({ networkId }) => networkId === cmd.networkId)?.uuid ??
activeNetwork?.uuid;

if (!networkUUID) {
throw new Error('Network not found');
}
if (!profile?.uuid) {
throw new Error('Profile not found');
}

const tx = await addTransaction({
transaction: payload as IUnsignedCommand,
profileId: profile?.uuid,
networkUUID: networkUUID,
groupId: id,
});
const request = createRequest(data);
setOrigin(`transaction/${tx.uuid}?request=${id}`);
navigate(`transaction/${tx.uuid}?request=${id}`);
return request;
}),
];
return () => {
handlers.forEach((unsubscribe) => unsubscribe());
Expand Down
103 changes: 103 additions & 0 deletions packages/apps/dev-wallet/src/pages/transaction/sign-request.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { ITransaction } from '@/modules/transaction/transaction.repository';

import { SideBarBreadcrumbs } from '@/Components/SideBarBreadcrumbs/SideBarBreadcrumbs';
import { useRequests } from '@/modules/communication/communication.provider';
import { addTransaction } from '@/modules/transaction/transaction.service';
import { useWallet } from '@/modules/wallet/wallet.hook';
import { usePatchedNavigate } from '@/utils/usePatchedNavigate';
import { IPactCommand, IUnsignedCommand } from '@kadena/client';
import { MonoSwapHoriz } from '@kadena/kode-icons/system';
import { Heading, Notification, Stack, Text } from '@kadena/kode-ui';
import { SideBarBreadcrumbsItem } from '@kadena/kode-ui/patterns';
import { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import { TxList } from './components/TxList';

export const SignRequest = () => {
const navigate = usePatchedNavigate();
const { profile, networks, activeNetwork } = useWallet();
const { requestId } = useParams();
const requests = useRequests();
const [tx, setTx] = useState<ITransaction>();
const [error, setError] = useState<string | undefined>();

useEffect(() => {
const run = async () => {
if (profile?.uuid && requestId) {
const tx = requests.get(requestId)?.payload as IUnsignedCommand;
if (!tx) {
setError('No transaction');
return;
}
const cmd: IPactCommand = JSON.parse(tx.cmd);
const networkUUID =
networks.find(({ networkId }) => networkId === cmd.networkId)?.uuid ??
activeNetwork?.uuid;

if (!networkUUID) {
setError(`Network not found for ${cmd.networkId}`);
return;
}

const transaction = await addTransaction({
transaction: tx as IUnsignedCommand,
profileId: profile?.uuid,
networkUUID: networkUUID,
groupId: requestId,
});

setTx(transaction);
}
};
run();
}, [
requestId,
navigate,
profile?.uuid,
requests,
networks,
activeNetwork?.uuid,
]);

return (
<>
<SideBarBreadcrumbs icon={<MonoSwapHoriz />}>
<SideBarBreadcrumbsItem href={`/transactions`}>
Activities
</SideBarBreadcrumbsItem>
<SideBarBreadcrumbsItem href={`/sign-request/${requestId}`}>
Sign Request
</SideBarBreadcrumbsItem>
</SideBarBreadcrumbs>

{error ? (
<Notification intent="negative" role="alert">
{error}
</Notification>
) : (
<Stack flexDirection={'column'} gap={'lg'} overflow="auto">
<Stack flexDirection={'column'} gap={'sm'}>
<Heading>Transaction</Heading>
{!tx && <Text>No transaction</Text>}
</Stack>
<TxList
onDone={() => {
console.log('done');
}}
txIds={tx ? [tx.uuid] : []}
showExpanded={true}
onSign={(tx) => {
if (requestId) {
const request = requests.get(requestId);
if (request) {
console.log('resolving request', request);
request.resolve({ status: 'signed', transaction: tx });
}
}
}}
/>
</Stack>
)}
</>
);
};

0 comments on commit b62fd47

Please sign in to comment.