From daf1a6055de19c2fef9f19d493b4fac5c59de74f Mon Sep 17 00:00:00 2001 From: rcoderdev Date: Thu, 9 Jan 2025 15:26:39 +0400 Subject: [PATCH] feat: add recordFromItems utility and refactor useKeysignMutation - Introduced a new utility function `recordFromItems` to convert an array of items into a record based on a provided key extraction function. - Refactored `useKeysignMutation` to utilize `recordFromItems` for creating a signatures record, improving code clarity and maintainability. - Updated `ScanQrView` to include error message extraction using `extractErrorMsg`, enhancing error handling in the component. --- frontend/src/lib/utils/record/recordFromItems.ts | 12 ++++++++++++ .../keysign/shared/mutations/useKeysignMutation.ts | 6 ++++-- frontend/src/vault/qr/upload/ScanQrView.tsx | 4 +++- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 frontend/src/lib/utils/record/recordFromItems.ts diff --git a/frontend/src/lib/utils/record/recordFromItems.ts b/frontend/src/lib/utils/record/recordFromItems.ts new file mode 100644 index 000000000..f06a3a4be --- /dev/null +++ b/frontend/src/lib/utils/record/recordFromItems.ts @@ -0,0 +1,12 @@ +export function recordFromItems( + items: readonly T[], + getKey: (item: T) => K +): Record { + const record = {} as Record; + + items.forEach(item => { + record[getKey(item)] = item; + }); + + return record; +} diff --git a/frontend/src/vault/keysign/shared/mutations/useKeysignMutation.ts b/frontend/src/vault/keysign/shared/mutations/useKeysignMutation.ts index 574f2510d..e0a0af35b 100644 --- a/frontend/src/vault/keysign/shared/mutations/useKeysignMutation.ts +++ b/frontend/src/vault/keysign/shared/mutations/useKeysignMutation.ts @@ -4,10 +4,10 @@ import { Keysign } from '../../../../../wailsjs/go/tss/TssService'; import { getPreSigningHashes } from '../../../../chain/tx/utils/getPreSigningHashes'; import { getCoinType } from '../../../../chain/walletCore/getCoinType'; import { hexEncode } from '../../../../chain/walletCore/hexEncode'; -import { arraysToRecord } from '../../../../lib/utils/array/arraysToRecord'; import { getLastItem } from '../../../../lib/utils/array/getLastItem'; import { chainPromises } from '../../../../lib/utils/promise/chainPromises'; import { pick } from '../../../../lib/utils/record/pick'; +import { recordFromItems } from '../../../../lib/utils/record/recordFromItems'; import { Chain } from '../../../../model/chain'; import { useAssertWalletCore } from '../../../../providers/WalletCoreProvider'; import { BlockchainServiceFactory } from '../../../../services/Blockchain/BlockchainServiceFactory'; @@ -70,7 +70,9 @@ export const useKeysignMutation = () => { tssType.toString().toLowerCase() ); - const signaturesRecord = arraysToRecord(msgs, signatures); + const signaturesRecord = recordFromItems(signatures, ({ msg }) => + Buffer.from(msg, 'base64').toString('hex') + ); const publicKey = await getVaultPublicKey({ vault, diff --git a/frontend/src/vault/qr/upload/ScanQrView.tsx b/frontend/src/vault/qr/upload/ScanQrView.tsx index e7afe387c..ecf0646fd 100644 --- a/frontend/src/vault/qr/upload/ScanQrView.tsx +++ b/frontend/src/vault/qr/upload/ScanQrView.tsx @@ -7,6 +7,7 @@ import { Button } from '../../../lib/ui/buttons/Button'; import { takeWholeSpaceAbsolutely } from '../../../lib/ui/css/takeWholeSpaceAbsolutely'; import { MatchQuery } from '../../../lib/ui/query/components/MatchQuery'; import { attempt } from '../../../lib/utils/attempt'; +import { extractErrorMsg } from '../../../lib/utils/error/extractErrorMsg'; import { FlowErrorPageContent } from '../../../ui/flow/FlowErrorPageContent'; import { FlowPendingPageContent } from '../../../ui/flow/FlowPendingPageContent'; import { PageContent } from '../../../ui/page/PageContent'; @@ -114,9 +115,10 @@ export const ScanQrView = ({ pending={() => ( )} - error={() => ( + error={error => ( {