Skip to content

Commit

Permalink
Improve signing error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Oct 24, 2024
1 parent 01274d7 commit 5b6077c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/extension/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
"Transaction building failed": "Transaction building failed",
"Sending transaction failed": "Sending transaction failed",
"Sign unsigned tx failed": "Sign unsigned tx failed",
"Sign message failed": "Sign message failed",
"Add Network": "Add Network",
"Switch Network": "Switch Network",
"Network ID": "Network ID",
Expand Down
38 changes: 24 additions & 14 deletions packages/extension/src/background/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,32 @@ export const handleActionApproval = async (
}

case "ALPH_SIGN_MESSAGE": {
const account = await wallet.getAccount({
address: action.payload.signerAddress,
networkId: action.payload.networkId,
})
if (!account) {
throw Error("No selected account")
}
try {
const account = await wallet.getAccount({
address: action.payload.signerAddress,
networkId: action.payload.networkId,
})
if (!account) {
throw Error("No selected account")
}
if (account.signer.type === 'ledger') {
throw Error("Signing messages with Ledger accounts is not supported")
}

const result = await wallet.signMessage(account, action.payload)
const result = await wallet.signMessage(account, action.payload)

return {
type: "ALPH_SIGN_MESSAGE_SUCCESS",
data: {
signature: result.signature,
actionHash,
},
return {
type: "ALPH_SIGN_MESSAGE_SUCCESS",
data: {
signature: result.signature,
actionHash,
},
}
} catch (error) {
return {
type: "ALPH_SIGN_MESSAGE_FAILURE",
data: { actionHash, error: `${error}` },
}
}
}

Expand Down
29 changes: 20 additions & 9 deletions packages/extension/src/ui/features/actions/ActionScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,26 @@ export const ActionScreen: FC = () => {
onSubmit={async () => {
await approveAction(action)
useAppState.setState({ isLoading: true })
await waitForMessage(
"ALPH_SIGN_MESSAGE_SUCCESS",
({ data }) => data.actionHash === action.meta.hash,
)
await analytics.track("signedMessage", {
networkId: selectedAccount?.networkId || t("unknown"),
})
closePopupIfLastAction()
useAppState.setState({ isLoading: false })
const result = await Promise.race([
waitForMessage(
'ALPH_SIGN_MESSAGE_SUCCESS',
({ data }) => data.actionHash === action.meta.hash,
),
waitForMessage(
'ALPH_SIGN_MESSAGE_FAILURE',
({ data }) => data.actionHash === action.meta.hash,
),
])
if ("error" in result) {
useAppState.setState({
error: `${t('Sign message failed')}: ${result.error}`,
isLoading: false,
})
navigate(routes.error())
} else {
closePopupIfLastAction()
useAppState.setState({ isLoading: false })
}
}}
onReject={onReject}
selectedAccount={signerAccount}
Expand Down

0 comments on commit 5b6077c

Please sign in to comment.