Skip to content

Commit

Permalink
Support alph_signAndSubmitUnsignedTx in desktop wallet
Browse files Browse the repository at this point in the history
Related to #1083
  • Loading branch information
nop33 committed Jan 9, 2025
1 parent 0821549 commit 1fce4f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion apps/desktop-wallet/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -511,5 +511,7 @@
"Welcome to your Ledger!": "Welcome to your Ledger!",
"Would you like to scan for active addresses?": "Would you like to scan for active addresses?",
"Scan": "Scan",
"Active address discovery completed. Addresses added: {{ count }}": "Active address discovery completed. Addresses added: {{ count }}"
"Active address discovery completed. Addresses added: {{ count }}": "Active address discovery completed. Addresses added: {{ count }}",
"Sign and Send Unsigned Transaction": "Sign and Send Unsigned Transaction",
"Sign and Send": "Sign and Send"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { keyring } from '@alephium/keyring'
import { getHumanReadableError, throttledClient, WALLETCONNECT_ERRORS } from '@alephium/shared'
import { SignUnsignedTxResult } from '@alephium/web3'
import { usePostHog } from 'posthog-js/react'
import { memo, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

Expand All @@ -16,21 +17,24 @@ import { SignUnsignedTxData } from '@/features/walletConnect/walletConnectTypes'
import { useAppDispatch } from '@/hooks/redux'
import CenteredModal, { ModalContent, ModalFooterButton, ModalFooterButtons } from '@/modals/CenteredModal'
import {
transactionSent,
unsignedTransactionDecodingFailed,
unsignedTransactionSignFailed,
unsignedTransactionSignSucceeded
} from '@/storage/transactions/transactionsActions'

export interface SignUnsignedTxModalProps {
txData: SignUnsignedTxData
submit?: boolean
}

const SignUnsignedTxModal = memo(({ id, txData }: ModalBaseProp & SignUnsignedTxModalProps) => {
const SignUnsignedTxModal = memo(({ id, txData, submit = false }: ModalBaseProp & SignUnsignedTxModalProps) => {
const { t } = useTranslation()
const { sendAnalytics } = useAnalytics()
const dispatch = useAppDispatch()
const { sendUserRejectedResponse, sendSuccessResponse, sendFailureResponse } = useWalletConnectContext()
const { isLedger, onLedgerError } = useLedger()
const posthog = usePostHog()

const [isLoading, setIsLoading] = useState(false)
const [decodedUnsignedTx, setDecodedUnsignedTx] = useState<Omit<SignUnsignedTxResult, 'signature'> | undefined>(
Expand Down Expand Up @@ -89,6 +93,27 @@ const SignUnsignedTxModal = memo(({ id, txData }: ModalBaseProp & SignUnsignedTx
}

const signResult: SignUnsignedTxResult = { signature, ...decodedUnsignedTx }

if (submit) {
const data = await throttledClient.node.transactions.postTransactionsSubmit({
unsignedTx: decodedUnsignedTx.unsignedTx,
signature
})

dispatch(
transactionSent({
hash: data.txId,
fromAddress: txData.fromAddress.hash,
toAddress: '',
timestamp: new Date().getTime(),
type: 'transfer',
status: 'sent'
})
)

posthog.capture('Signed and submitted unsigned transaction')
}

await sendSuccessResponse(signResult, true)

dispatch(unsignedTransactionSignSucceeded())
Expand Down Expand Up @@ -116,7 +141,7 @@ const SignUnsignedTxModal = memo(({ id, txData }: ModalBaseProp & SignUnsignedTx
return (
<CenteredModal
id={id}
title={t('Sign Unsigned Transaction')}
title={t(submit ? 'Sign and Send Unsigned Transaction' : 'Sign Unsigned Transaction')}
onClose={rejectAndClose}
isLoading={isLoading}
dynamicContent
Expand All @@ -134,7 +159,7 @@ const SignUnsignedTxModal = memo(({ id, txData }: ModalBaseProp & SignUnsignedTx
{t('Reject')}
</ModalFooterButton>
<ModalFooterButton onClick={handleSign} disabled={isLoading || !decodedUnsignedTx}>
{t('Sign')}
{submit ? t('Sign and Send') : t('Sign')}
</ModalFooterButton>
</ModalFooterButtons>
</ModalContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,19 @@ const WalletConnectSessionRequestEventHandler = ({
dispatch(openModal({ name: 'SignMessageModal', props: { txData } }))
break
}
case 'alph_signUnsignedTx': {
case 'alph_signUnsignedTx':
case 'alph_signAndSubmitUnsignedTx': {
const { unsignedTx, signerAddress } = request.params as SignUnsignedTxParams
const txData: SignUnsignedTxData = {
fromAddress: getSignerAddressByHash(signerAddress),
unsignedTx
}
dispatch(openModal({ name: 'SignUnsignedTxModal', props: { txData } }))
dispatch(
openModal({
name: 'SignUnsignedTxModal',
props: { txData, submit: request.method === 'alph_signAndSubmitUnsignedTx' }
})
)
break
}
case 'alph_requestNodeApi': {
Expand Down

0 comments on commit 1fce4f8

Please sign in to comment.