2023-03-14 11:57:05 +01:00
|
|
|
import log from 'loglevel';
|
|
|
|
import { ThunkAction } from 'redux-thunk';
|
|
|
|
import { AnyAction } from 'redux';
|
|
|
|
import {
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
displayWarning,
|
|
|
|
hideLoadingIndication,
|
|
|
|
showLoadingIndication,
|
|
|
|
} from '../actions';
|
|
|
|
import {
|
|
|
|
callBackgroundMethod,
|
|
|
|
submitRequestToBackground,
|
|
|
|
} from '../action-queue';
|
|
|
|
import { MetaMaskReduxState } from '../store';
|
|
|
|
import { isErrorWithMessage } from '../../../shared/modules/error';
|
|
|
|
|
2023-04-27 10:45:37 +02:00
|
|
|
export function showInteractiveReplacementTokenBanner({
|
|
|
|
url,
|
|
|
|
oldRefreshToken,
|
|
|
|
}: {
|
|
|
|
url: string;
|
|
|
|
oldRefreshToken: string;
|
|
|
|
}): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
try {
|
|
|
|
await submitRequestToBackground('showInteractiveReplacementTokenBanner', [
|
|
|
|
url,
|
|
|
|
oldRefreshToken,
|
|
|
|
]);
|
|
|
|
} catch (err: any) {
|
|
|
|
if (err) {
|
|
|
|
dispatch(displayWarning(err.message));
|
|
|
|
throw new Error(err.message);
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 11:57:05 +01:00
|
|
|
};
|
|
|
|
}
|
2023-05-24 13:41:21 +02:00
|
|
|
|
|
|
|
export function setTypedMessageInProgress(msgId: string) {
|
|
|
|
return async (dispatch: any) => {
|
|
|
|
dispatch(showLoadingIndication());
|
|
|
|
try {
|
|
|
|
await submitRequestToBackground('setTypedMessageInProgress', [msgId]);
|
|
|
|
} catch (error: any) {
|
|
|
|
log.error(error);
|
|
|
|
dispatch(displayWarning(error.message));
|
|
|
|
} finally {
|
|
|
|
await forceUpdateMetamaskState(dispatch);
|
|
|
|
dispatch(hideLoadingIndication());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-14 11:57:05 +01:00
|
|
|
/**
|
|
|
|
* A factory that contains all MMI actions ready to use
|
|
|
|
* Example usage:
|
|
|
|
* const mmiActions = mmiActionsFactory();
|
|
|
|
* mmiActions.connectCustodyAddresses(...)
|
|
|
|
*/
|
|
|
|
export function mmiActionsFactory() {
|
|
|
|
function createAsyncAction(
|
|
|
|
name: string,
|
|
|
|
params: any,
|
|
|
|
useForceUpdateMetamaskState?: any,
|
|
|
|
loadingText?: string,
|
|
|
|
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
|
|
|
|
log.debug(`background.${name}`);
|
2023-05-24 13:41:21 +02:00
|
|
|
return async (dispatch: any) => {
|
2023-03-14 11:57:05 +01:00
|
|
|
if (loadingText) {
|
|
|
|
dispatch(showLoadingIndication(loadingText));
|
|
|
|
}
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = await submitRequestToBackground(name, [...params]);
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(displayWarning(error));
|
|
|
|
if (isErrorWithMessage(error)) {
|
|
|
|
throw new Error(error.message);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadingText) {
|
|
|
|
dispatch(hideLoadingIndication());
|
|
|
|
}
|
|
|
|
if (useForceUpdateMetamaskState) {
|
|
|
|
await forceUpdateMetamaskState(dispatch);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:41:28 +02:00
|
|
|
function createAction(name: string, payload: any): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
callBackgroundMethod(name, [payload], (error) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
2023-03-14 11:57:05 +01:00
|
|
|
}
|
2023-06-30 12:41:28 +02:00
|
|
|
|
|
|
|
resolve();
|
2023-03-14 11:57:05 +01:00
|
|
|
});
|
2023-06-30 12:41:28 +02:00
|
|
|
});
|
2023-03-14 11:57:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
connectCustodyAddresses: (
|
|
|
|
custodianType: string,
|
|
|
|
custodianName: string,
|
|
|
|
newAccounts: string[],
|
|
|
|
) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'connectCustodyAddresses',
|
|
|
|
[custodianType, custodianName, newAccounts],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
'Looking for your custodian account...',
|
|
|
|
),
|
|
|
|
getCustodianAccounts: (
|
|
|
|
token: string,
|
|
|
|
apiUrl: string,
|
|
|
|
custody: string,
|
|
|
|
getNonImportedAccounts: boolean,
|
|
|
|
) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianAccounts',
|
|
|
|
[token, apiUrl, custody, getNonImportedAccounts],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
'Getting custodian accounts...',
|
|
|
|
),
|
|
|
|
getCustodianAccountsByAddress: (
|
|
|
|
jwt: string,
|
|
|
|
apiUrl: string,
|
|
|
|
address: string,
|
|
|
|
custody: string,
|
|
|
|
) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianAccountsByAddress',
|
|
|
|
[jwt, apiUrl, address, custody],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
'Getting custodian accounts...',
|
|
|
|
),
|
|
|
|
getCustodianTransactionDeepLink: (address: string, txId: string) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianTransactionDeepLink',
|
|
|
|
[address, txId],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
),
|
|
|
|
getCustodianConfirmDeepLink: (txId: string) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianConfirmDeepLink',
|
|
|
|
[txId],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
),
|
|
|
|
getCustodianSignMessageDeepLink: (from: string, custodyTxId: string) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianSignMessageDeepLink',
|
|
|
|
[from, custodyTxId],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
),
|
|
|
|
getCustodianToken: (custody: string) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianToken',
|
|
|
|
[custody],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
),
|
|
|
|
getCustodianJWTList: (custody: string) =>
|
|
|
|
createAsyncAction(
|
|
|
|
'getCustodianJWTList',
|
|
|
|
[custody],
|
|
|
|
forceUpdateMetamaskState,
|
|
|
|
),
|
|
|
|
setWaitForConfirmDeepLinkDialog: (waitForConfirmDeepLinkDialog: boolean) =>
|
|
|
|
createAction(
|
|
|
|
'setWaitForConfirmDeepLinkDialog',
|
|
|
|
waitForConfirmDeepLinkDialog,
|
|
|
|
),
|
|
|
|
setComplianceAuthData: (clientId: string, projectId: string) =>
|
|
|
|
createAsyncAction('setComplianceAuthData', [{ clientId, projectId }]),
|
|
|
|
deleteComplianceAuthData: () =>
|
|
|
|
createAsyncAction('deleteComplianceAuthData', []),
|
|
|
|
generateComplianceReport: (address: string) =>
|
|
|
|
createAction('generateComplianceReport', address),
|
|
|
|
getComplianceHistoricalReportsByAddress: (
|
|
|
|
address: string,
|
|
|
|
projectId: string,
|
|
|
|
) =>
|
|
|
|
createAsyncAction('getComplianceHistoricalReportsByAddress', [
|
|
|
|
address,
|
|
|
|
projectId,
|
|
|
|
]),
|
|
|
|
syncReportsInProgress: (address: string, historicalReports: []) =>
|
|
|
|
createAction('syncReportsInProgress', { address, historicalReports }),
|
|
|
|
removeConnectInstitutionalFeature: (origin: string, projectId: string) =>
|
|
|
|
createAction('removeConnectInstitutionalFeature', { origin, projectId }),
|
2023-06-30 12:41:28 +02:00
|
|
|
removeAddTokenConnectRequest: ({
|
|
|
|
origin,
|
|
|
|
apiUrl,
|
|
|
|
token,
|
|
|
|
}: {
|
|
|
|
origin: string;
|
|
|
|
apiUrl: string;
|
|
|
|
token: string;
|
|
|
|
}) =>
|
2023-03-14 11:57:05 +01:00
|
|
|
createAction('removeAddTokenConnectRequest', { origin, apiUrl, token }),
|
2023-06-30 12:41:28 +02:00
|
|
|
setCustodianConnectRequest: ({
|
|
|
|
token,
|
|
|
|
apiUrl,
|
|
|
|
custodianType,
|
|
|
|
custodianName,
|
|
|
|
}: {
|
|
|
|
token: string;
|
|
|
|
apiUrl: string;
|
|
|
|
custodianType: string;
|
|
|
|
custodianName: string;
|
|
|
|
}) =>
|
2023-03-14 11:57:05 +01:00
|
|
|
createAsyncAction('setCustodianConnectRequest', [
|
|
|
|
{ token, apiUrl, custodianType, custodianName },
|
|
|
|
]),
|
|
|
|
getCustodianConnectRequest: () =>
|
|
|
|
createAsyncAction('getCustodianConnectRequest', []),
|
|
|
|
getMmiConfiguration: () => createAsyncAction('getMmiConfiguration', []),
|
|
|
|
getAllCustodianAccountsWithToken: (custodyType: string, token: string) =>
|
|
|
|
createAsyncAction('getAllCustodianAccountsWithToken', [
|
|
|
|
custodyType,
|
|
|
|
token,
|
|
|
|
]),
|
|
|
|
setCustodianNewRefreshToken: (
|
|
|
|
address: string,
|
|
|
|
oldAuthDetails: string,
|
|
|
|
oldApiUrl: string,
|
|
|
|
newAuthDetails: string,
|
|
|
|
newApiUrl: string,
|
|
|
|
) =>
|
|
|
|
createAsyncAction('setCustodianNewRefreshToken', [
|
|
|
|
address,
|
|
|
|
oldAuthDetails,
|
|
|
|
oldApiUrl,
|
|
|
|
newAuthDetails,
|
|
|
|
newApiUrl,
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
}
|