Skip to content

Commit

Permalink
Remove edited
Browse files Browse the repository at this point in the history
  • Loading branch information
arhtudormorar committed Dec 12, 2024
1 parent b318ef0 commit 295a7b0
Showing 1 changed file with 1 addition and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Transaction } from '@multiversx/sdk-core/out';
import { isBrowserWithPopupConfirmation } from 'constants/browser.constants';
import { safeWindow } from 'constants/window.constants';
import { getAddress } from 'core/methods/account/getAddress';
import { defineCustomElements, SignTransactionsModal } from 'lib/sdkDappCoreUi';
import { defineCustomElements } from 'lib/sdkDappCoreUi';
import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider';
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';
import { SignEventsEnum } from '../components/SignTransactionsModal/signTransactionsModal.types';
import { SignTransactionsStateManager } from '../components/SignTransactionsModal/SignTransactionsStateManager';

interface ICreateCrossWindowProviderProps {
address?: string;
Expand All @@ -31,80 +27,5 @@ export async function createCrossWindowProvider({
provider.setShouldShowConsentPopup(true);
}

provider.signTransactions = async (
transactions: Transaction[]
): Promise<Transaction[]> => {
// Create sign transactions modal web component
const signModalElement = document.createElement(
'sign-transactions-modal'
) as SignTransactionsModal;

document.body.appendChild(signModalElement);

await customElements.whenDefined('sign-transactions-modal');

const eventBus = await signModalElement.getEventBus();

const manager = SignTransactionsStateManager.getInstance(eventBus);
if (!manager) {
throw new Error('Unable to establish connection with sign screens');
}

return new Promise<Transaction[]>(async (resolve, reject) => {
const signedTransactions: Transaction[] = [];
let currentTransactionIndex = 0;

const localProvider = new CrossWindowProvider();
await localProvider.init();
localProvider.setWalletUrl(walletAddress || network.walletAddress);
localProvider.setAddress(getAddress());

const signNextTransaction = async () => {
// If all transactions are signed, resolve

const currentTransaction = transactions[currentTransactionIndex];

manager.updateTransaction({
transaction: currentTransaction.toPlainObject()
});

const onCancel = () => {
reject(new Error('Transaction signing cancelled by user'));
signModalElement.remove();
};

const onSign = async () => {
try {
// TODO: check if it's a real transaction or multitransfer step
const [signedTransaction] = await localProvider.signTransactions([
currentTransaction
]);

signedTransactions.push(signedTransaction);

if (signedTransactions.length == transactions.length) {
signModalElement.remove();
eventBus.unsubscribe(SignEventsEnum.SIGN_TRANSACTION, onSign);
eventBus.unsubscribe(SignEventsEnum.CLOSE, onCancel);

resolve(signedTransactions);
} else {
currentTransactionIndex++;
signNextTransaction();
}
} catch (error) {
reject('Error signing transactions: ' + error);
signModalElement.remove();
}
};

eventBus.subscribe(SignEventsEnum.SIGN_TRANSACTION, onSign);
eventBus.subscribe(SignEventsEnum.CLOSE, onCancel);
};

signNextTransaction();
});
};

return provider;
}

0 comments on commit 295a7b0

Please sign in to comment.