Skip to content

Commit

Permalink
add missing logic for guardians 2fa hook
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Dec 28, 2023
1 parent 6d5841f commit 1c4883d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### [0.15.1](https://github.com/useElven/core/releases/tag/v0.15.1) (2023-12-29)
- add missing transaction id logic for guardian 2FA hooks

### [0.15.0](https://github.com/useElven/core/releases/tag/v0.15.0) (2023-12-28)
- change the way we're getting the smart contract address in the useScDeploy hook
- add optional id for all hooks that use useTransaction. It helps with signing providers' logic based on redirections to diferentiate and track which hook triggered the redirection
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useelven/core",
"version": "0.15.0",
"version": "0.15.1",
"description": "Core React hooks for MultiversX DApps",
"license": "MIT",
"author": "Julian Ćwirko <julian.io>",
Expand Down
53 changes: 31 additions & 22 deletions src/hooks/common-helpers/signAndSendTxOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,36 @@ export const checkNeedsGuardianSigning = (
return true;
};

const getCallbackUrl = (
ongoingTxId?: string,
webWalletRedirectUrl?: string
) => {
const currentUrl = window?.location?.href;
const clbck =
webWalletRedirectUrl && window
? `${window.location.origin}${webWalletRedirectUrl}`
: currentUrl;
if (!ongoingTxId) return clbck;

const alteredCallbackUrl = new URL(clbck);
alteredCallbackUrl.searchParams.set('ongoingTx', ongoingTxId);

return alteredCallbackUrl.toString();
};

export const sendTxToGuardian = async (
signedTx: Transaction,
walletAddress?: string,
webWalletRedirectUrl?: string
webWalletRedirectUrl?: string,
ongoingTxId?: string
) => {
const webWalletProvider = new WalletProvider(
`${walletAddress}${DAPP_INIT_ROUTE}`
);
const currentUrl = window?.location.href;
const callbackUrl =
webWalletRedirectUrl && window
? `${window.location.origin}${webWalletRedirectUrl}`
: currentUrl;

const alteredCallbackUrl = new URL(callbackUrl);
const clbck = getCallbackUrl(ongoingTxId, webWalletRedirectUrl);

const alteredCallbackUrl = new URL(clbck);
alteredCallbackUrl.searchParams.set(
WebWalletUrlParamsEnum.hasWebWalletGuardianSign,
'true'
Expand Down Expand Up @@ -148,21 +163,10 @@ export const signAndSendTxOperations = async (

try {
if (dappProvider instanceof WalletProvider) {
const currentUrl = window?.location?.href;
const getCallback = () => {
const clbck =
webWalletRedirectUrl && window
? `${window.location.origin}${webWalletRedirectUrl}`
: currentUrl;
if (!ongoingTxId) return clbck;
if (webWalletRedirectUrl?.includes('?')) {
return `${clbck}&ongoingTx=${ongoingTxId}`;
}
return `${clbck}?ongoingTx=${ongoingTxId}`;
};

await dappProvider.signTransaction(tx, {
callbackUrl: encodeURIComponent(getCallback()),
callbackUrl: encodeURIComponent(
getCallbackUrl(ongoingTxId, webWalletRedirectUrl)
),
});
}
if (dappProvider instanceof ExtensionProvider) {
Expand All @@ -185,7 +189,12 @@ export const signAndSendTxOperations = async (
);

if (needsGuardianSign) {
await sendTxToGuardian(signedTx, walletAddress, webWalletRedirectUrl);
await sendTxToGuardian(
signedTx,
walletAddress,
webWalletRedirectUrl,
ongoingTxId
);

return;
}
Expand Down

0 comments on commit 1c4883d

Please sign in to comment.