From 39360de85dcbf740adaa52e6998893ce1a3d9c0c Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 29 Aug 2023 09:25:27 +0200 Subject: [PATCH] add function to component --- .../components/popups/SignMessagePopup.svelte | 10 +++-- .../src/lib/core/wallet/actions/index.ts | 1 + .../lib/core/wallet/actions/signMessage.ts | 45 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 packages/shared/src/lib/core/wallet/actions/signMessage.ts diff --git a/packages/desktop/components/popups/SignMessagePopup.svelte b/packages/desktop/components/popups/SignMessagePopup.svelte index 446fc3382f..857ee6246d 100644 --- a/packages/desktop/components/popups/SignMessagePopup.svelte +++ b/packages/desktop/components/popups/SignMessagePopup.svelte @@ -6,10 +6,11 @@ import { onMount } from 'svelte' import { selectedAccount } from '@core/account/stores' import { JsonRpcResponse } from '@walletconnect/jsonrpc-types' - import { sleep, truncateString } from '@core/utils' + import { truncateString } from '@core/utils' import { IConnectedDapp } from '@auxiliary/wallet-connect/interface' import { IAccountState } from '@core/account' import { IChain } from '@core/network' + import { signMessage } from '@core/wallet/actions' export let _onMount: (..._: any[]) => Promise = async () => {} export let requestId: number @@ -37,9 +38,10 @@ } async function sign(): Promise { - // TODO: Replace this with the correct signing implementation - await sleep(500) - return '0x3123' + const { chainId, coinType } = chain.getConfiguration() + const signedMessage = await signMessage(message, chainId, coinType, account) + + return signedMessage } function onCancelClick(): void { diff --git a/packages/shared/src/lib/core/wallet/actions/index.ts b/packages/shared/src/lib/core/wallet/actions/index.ts index e8d94a0e5a..85e08b9471 100644 --- a/packages/shared/src/lib/core/wallet/actions/index.ts +++ b/packages/shared/src/lib/core/wallet/actions/index.ts @@ -10,5 +10,6 @@ export * from './mintNft' export * from './rejectActivity' export * from './sendOutput' export * from './unwrapStardustAsset' +export * from './signMessage' export * from './send' diff --git a/packages/shared/src/lib/core/wallet/actions/signMessage.ts b/packages/shared/src/lib/core/wallet/actions/signMessage.ts new file mode 100644 index 0000000000..1aa589bfa5 --- /dev/null +++ b/packages/shared/src/lib/core/wallet/actions/signMessage.ts @@ -0,0 +1,45 @@ +import { IAccountState } from '@core/account' +import { updateSelectedAccount } from '@core/account/stores' +import { handleError } from '@core/error/handlers' +import { EvmChainId } from '@core/network/enums' +import { isActiveLedgerProfile, isSoftwareProfile } from '@core/profile/stores' +import { get } from 'svelte/store' +import { closePopup } from '../../../../../../desktop/lib/auxiliary/popup' +import { signMessageWithStronghold } from '@core/stronghold/utils' + +export async function signMessage( + message: string, + chainId: EvmChainId, + coinType: number, + account: IAccountState +): Promise { + try { + updateSelectedAccount({ isTransferring: true }) + + const bip44Path = { + coinType, + account: account.index, + change: 0, + addressIndex: 0, + } + let signedMessage: string | undefined + if (get(isSoftwareProfile)) { + signedMessage = await signMessageWithStronghold(message, 'eth_sign', bip44Path, chainId, account) + // } else if (get(isActiveLedgerProfile)) { + // signedMessage = await Ledger.signEvmTransaction(txData, chainId, bip44Path) + } + + if (!signedMessage) { + if (get(isActiveLedgerProfile)) { + closePopup(true) + } + throw new Error('No signature provided') + } + + return signedMessage + } catch (err) { + handleError(err) + } finally { + updateSelectedAccount({ isTransferring: false }) + } +}