Skip to content

Commit

Permalink
Added transaction and message cancel signing handle
Browse files Browse the repository at this point in the history
  • Loading branch information
DanutIlie committed Jan 26, 2025
1 parent 9b08278 commit d5cc2eb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/core/providers/DappProvider/DappProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Transaction } from '@multiversx/sdk-core/out/transaction';
import { IProvider } from '../types/providerFactory.types';
import { login } from './helpers/login/login';
import { logout } from './helpers/logout/logout';
import { handleSignMessageError } from './helpers/signMessage/handleSignMessageError';
import { signMessage } from './helpers/signMessage/signMessage';
import {
verifyMessage,
VerifyMessageReturnType
} from './helpers/signMessage/verifyMessage';
import { handleSignTransactionError } from './helpers/signTransactions/handleSignTransactionError';
import {
signTransactionsWithProvider,
SignTransactionsOptionsType
Expand Down Expand Up @@ -53,12 +55,17 @@ export class DappProvider {
transactions: Transaction[],
options?: SignTransactionsOptionsType
): Promise<Transaction[]> {
const signedTransactions = await signTransactionsWithProvider({
provider: this.provider,
transactions,
options
});
return signedTransactions;
try {
const signedTransactions = await signTransactionsWithProvider({
provider: this.provider,
transactions,
options
});
return signedTransactions;
} catch (error) {
const errorMessage = handleSignTransactionError(error);
throw new Error(errorMessage);
}
}

async signMessage(
Expand All @@ -67,12 +74,17 @@ export class DappProvider {
hasConsentPopup?: boolean;
}
): Promise<Message | null> {
const signedMessage = await signMessage({
provider: this.provider,
message,
options
});
return signedMessage;
try {
const signedMessage = await signMessage({
provider: this.provider,
message,
options
});
return signedMessage;
} catch (error) {
const errorMessage = handleSignMessageError(error);
throw new Error(errorMessage);
}
}

verifyMessage(signedMessage: string): VerifyMessageReturnType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ERROR_SIGNING } from 'constants/errorMessages.constants';
import {
CANCEL_TRANSACTION_TOAST_DEFAULT_DURATION,
CANCEL_TRANSACTION_TOAST_ID
} from 'constants/transactions.constants';
import { createCustomToast } from 'store/actions';

export function handleSignMessageError(error?: unknown) {
const errorMessage =
(error as Error)?.message || (error as string) || ERROR_SIGNING;
console.error(errorMessage);

createCustomToast({
toastId: `${CANCEL_TRANSACTION_TOAST_ID}-${Date.now()}`,
duration: CANCEL_TRANSACTION_TOAST_DEFAULT_DURATION,
icon: 'warning',
iconClassName: 'warning',
message: ERROR_SIGNING
});

return errorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
ERROR_SIGNING,
TRANSACTION_CANCELLED
} from 'constants/errorMessages.constants';
import {
CANCEL_TRANSACTION_TOAST_DEFAULT_DURATION,
CANCEL_TRANSACTION_TOAST_ID
} from 'constants/transactions.constants';
import { createCustomToast } from 'store/actions';

export function handleSignTransactionError(error?: unknown) {
const errorMessage = (error as Error)?.message || ERROR_SIGNING;
const isTxCancelled = errorMessage.includes(TRANSACTION_CANCELLED);
if (isTxCancelled) {
createCustomToast({
toastId: `${CANCEL_TRANSACTION_TOAST_ID}-${Date.now()}`,
duration: CANCEL_TRANSACTION_TOAST_DEFAULT_DURATION,
icon: 'warning',
iconClassName: 'warning',
message: TRANSACTION_CANCELLED
});
}

return errorMessage;
}

0 comments on commit d5cc2eb

Please sign in to comment.