Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cw3): adds bulk sigdata #2758

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/apps/dev-wallet/src/App/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ImportData } from '@/pages/settings/import-data/import-data';
import { RevealPhrase } from '@/pages/settings/reveal-phrase/reveal-phrase';
import { Settings } from '@/pages/settings/settings';
import { SignatureBuilder } from '@/pages/signature-builder/signature-builder';
import { TransactionGroupPage } from '@/pages/transaction-group/TransactionGroup';
import { TransactionPage } from '@/pages/transaction/Transaction';
import { Transactions } from '@/pages/transactions/transactions';
import { Transfer } from '@/pages/transfer/transfer';
Expand Down Expand Up @@ -120,6 +121,10 @@ export const Routes: FC = () => {
<Route path="/key-management/:tab" element={<KeysPage />} />
<Route path="/create-account" element={<CreateAccount />} />
<Route path="/transaction/:groupId" element={<TransactionPage />} />
<Route
path="/transaction-group/:groupId"
element={<TransactionGroupPage />}
/>
<Route path="/transactions" element={<Transactions />} />
<Route path="/keyset/:keysetId" element={<Keyset />} />
<Route path="/fungible/:contract" element={<FungiblePage />} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const UnlockPrompt: React.FC<{
type="password"
{...register('password')}
label="Password"
autoFocus
/>
)}
{storePassword && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ import {
} from '@kadena/client';

import { SideBarBreadcrumbs } from '@/Components/SideBarBreadcrumbs/SideBarBreadcrumbs';
import { transactionRepository } from '@/modules/transaction/transaction.repository';
import {
ITransaction,
transactionRepository,
} from '@/modules/transaction/transaction.repository';
import * as transactionService from '@/modules/transaction/transaction.service';
import { useWallet } from '@/modules/wallet/wallet.hook';
import { normalizeTx } from '@/utils/normalizeSigs';
import { browse, readContent } from '@/utils/select-file';
import {
determineSchema,
RequestScheme,
signingRequestToPactCommand,
} from '@/utils/transaction-scheme';
import { usePatchedNavigate } from '@/utils/usePatchedNavigate';
import { base64UrlDecodeArr } from '@kadena/cryptography-utils';
import { MonoDashboardCustomize } from '@kadena/kode-icons/system';
import {
MonoDashboardCustomize,
MonoRemove,
MonoRemoveCircleOutline,
MonoRestoreFromTrash,
MonoSignature,
} from '@kadena/kode-icons/system';
import {
Box,
Button,
Expand All @@ -32,8 +42,9 @@ import { SideBarBreadcrumbsItem } from '@kadena/kode-ui/patterns';
import { execCodeParser } from '@kadena/pactjs-generator';
import classNames from 'classnames';
import yaml from 'js-yaml';
import { useEffect, useMemo, useState } from 'react';
import { FC, useEffect, useMemo, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { TxTileGeneric } from '../transaction-group/components/TxTileGeneric';
import { codeArea } from './style.css';

const getTxFromUrlHash = () =>
Expand All @@ -45,12 +56,18 @@ export function SignatureBuilder() {
const [error, setError] = useState<string>();
const [schema, setSchema] = useState<RequestScheme>();
const [input, setInput] = useState<string>('');
const [fileContent, setFileContent] = useState<any>();
const [pactCommand, setPactCommand] = useState<IPartialPactCommand>();
const [unsignedTx, setUnsignedTx] = useState<IUnsignedCommand>();
const [signed, setSignedTx] = useState<IUnsignedCommand>();
const [capsWithoutSigners, setCapsWithoutSigners] = useState<
ISigningRequest['caps']
>([]);
const [groupId] = useState<string>(crypto.randomUUID());
const [transactions, setTransactions] = useState<ITransaction[]>([]);
const [existingTransactions, setExistingTransactions] = useState<
ITransaction[]
>([]);
const { profile, activeNetwork, networks, setActiveNetwork } = useWallet();
const navigate = usePatchedNavigate();

Expand All @@ -75,11 +92,27 @@ export function SignatureBuilder() {
if (urlTransaction) {
const data = new TextDecoder().decode(base64UrlDecodeArr(urlTransaction));
setInput(data);
processSig(data);
processSigData(data);
addTransactionToGroup(groupId);
}
}, [urlTransaction]);

function processSig(inputData: string) {
useEffect(() => {
async function addTransaction() {
if (unsignedTx && profile) {
await addTransactionToGroup(groupId);
setTransactions(
await transactionRepository.getTransactionsByGroup(
groupId,
profile.uuid,
),
);
}
}
addTransaction();
}, [unsignedTx]);

function processSigData(inputData: string) {
setInput(inputData);
let schema = determineSchema(inputData);
if (schema === 'base64') {
Expand Down Expand Up @@ -121,18 +154,44 @@ export function SignatureBuilder() {
setSchema(schema);
}

const reviewTransaction = async () => {
const removeTransactionToAdd = async (tx: ITransaction) => {
if (!profile) return;

// remove transaction from existingTransactions
if (
existingTransactions.find((item) => item.uuid === tx.uuid) !== undefined
) {
setExistingTransactions((prev) =>
prev.filter((item) => item.uuid !== tx.uuid),
);
}

// remove transaction from group
if (transactions.find((item) => item.uuid === tx.uuid) !== undefined) {
await transactionRepository.deleteTransaction(tx.uuid);
setTransactions(
await transactionRepository.getTransactionsByGroup(
groupId,
profile.uuid,
),
);
}
};

const addTransactionToGroup = async (groupId: string) => {
if (!unsignedTx || !profile || !activeNetwork) return;

const command: IPactCommand = JSON.parse(unsignedTx.cmd);
let networkUUID = activeNetwork.uuid;

if (command.networkId && activeNetwork.networkId !== command.networkId) {
const network = networks.filter(
({ networkId }) => networkId === command.networkId,
);
if (network.length === 0) {
setError(
command.networkId === 'mainnet01'
? 'MANNET_IS_DISABLED: Mainnet is disabled since the wallet is not fully tested; you can try other networks'
? 'MAINNET_IS_DISABLED: Mainnet is disabled since the wallet is not fully tested; you can try other networks'
: 'NETWORK_NOT_FOUND: This network is not found',
);
throw new Error('Network not found');
Expand All @@ -144,7 +203,6 @@ export function SignatureBuilder() {
// switch network
setActiveNetwork(network[0]);
}
const groupId = crypto.randomUUID();

// check if transaction already exists
const tx = await transactionRepository.getTransactionByHashNetworkProfile(
Expand All @@ -162,12 +220,23 @@ export function SignatureBuilder() {
pubKey?: string;
}>),
);

await transactionRepository.updateTransaction({
...tx,
sigs: updatedTx.sigs,
});

if (
existingTransactions.find((item) => item.uuid === tx.uuid) ===
undefined
) {
setExistingTransactions((prev) => [...prev, tx]);
}
}
navigate(`/transaction/${tx.groupId}`);
setPactCommand(undefined);
setUnsignedTx(undefined);
setCapsWithoutSigners([]);
setInput('');
return;
}

Expand All @@ -177,7 +246,15 @@ export function SignatureBuilder() {
networkUUID: networkUUID,
groupId,
});
navigate(`/transaction/${groupId}`);

setPactCommand(undefined);
setUnsignedTx(undefined);
setCapsWithoutSigners([]);
setInput('');
};

const reviewTransaction = () => {
navigate(`/transaction-group/${groupId}`);
};

return (
Expand All @@ -189,17 +266,20 @@ export function SignatureBuilder() {
</SideBarBreadcrumbs>

<Stack flexDirection={'column'} gap={'md'} width="100%">
<Heading as="h4">Paste SigData, CommandSigData, or Payload</Heading>
<Heading as="h4">
Paste or Upload SigData, CommandSigData, or Payload
</Heading>
<textarea
value={input}
className={classNames(codeArea)}
onChange={(e) => {
e.preventDefault();
setError(undefined);
processSig(e.target.value);
processSigData(e.target.value);
}}
/>
<Box>{schema && <Text>{`Schema: ${schema}`}</Text>}</Box>

<Box>{schema && <Text>{`Schema: ${schema}`}</Text>} </Box>
{schema === 'signingRequest' && (
<Notification intent="info" role="status">
SigningRequest is not supported yet, We are working on it.
Expand All @@ -210,13 +290,94 @@ export function SignatureBuilder() {
{error}
</Notification>
)}

{/* <Box>
<Button
variant="outlined"
onClick={async () => {
setError(undefined);
setFileContent(undefined);
const file = await browse(true);
if (file && file instanceof File) {
const content = await readContent(file);
try {
const json = JSON.parse(content, (_key, value) => {
if (typeof value !== 'string') return value;
if (value.startsWith('Uint8Array:')) {
const base64Url = value.split('Uint8Array:')[1];
return base64UrlDecodeArr(base64Url);
}
if (value.startsWith('ArrayBuffer:')) {
const base64Url = value.split('ArrayBuffer:')[1];
return base64UrlDecodeArr(base64Url).buffer;
}
return value;
});
setFileContent(json);
} catch (e) {
setError('Invalid file format');
}
}
}}
>
Add From File(s)
</Button>
</Box> */}

<Box>
{transactions.length > 0 && (
<>
<Text>Transactions to review</Text>
{transactions.map((tx) => (
<TxTileGeneric
tx={tx}
buttons={[
{
label: 'Discard',
Icon: () => <MonoRemoveCircleOutline />,
onClick: () => removeTransactionToAdd(tx),
position: 'flex-start',
},
]}
/>
))}
</>
)}
</Box>

<Box>
<Box>
{['PactCommand', 'quickSignRequest'].includes(schema!) && (
<Button onPress={reviewTransaction}>Review Transaction</Button>
)}
{transactions.length > 0 &&
['PactCommand', 'quickSignRequest'].includes(schema!) && (
<Button onPress={reviewTransaction}>
Review Transaction(s)
</Button>
)}
</Box>
</Box>

<Box>
{existingTransactions.length > 0 &&
existingTransactions.map((tx) => (
<TxTileGeneric
tx={tx}
buttons={[
{
label: 'Discard',
Icon: () => <MonoRemoveCircleOutline />,
onClick: () => removeTransactionToAdd(tx),
position: 'flex-start',
},
{
label: 'Open',
onClick: () => navigate(`/transaction-group/${tx.groupId}`),
Icon: () => <MonoSignature />,
position: 'flex-end',
},
]}
/>
))}
</Box>
</Stack>
</>
);
Expand Down
Loading
Loading