Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added transaction and message cancel signing handle #71

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Added transaction and message cancel signing handle](https://github.com/multiversx/mx-sdk-dapp-core/pull/71)
- [Added custom guard transactions support](https://github.com/multiversx/mx-sdk-dapp-core/pull/68)
- [Added custom toast support](https://github.com/multiversx/mx-sdk-dapp-core/pull/67)
- [Fixed guarded transactions](https://github.com/multiversx/mx-sdk-dapp-core/pull/66)
Expand Down
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;
}
Loading