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

Fix: Missing TransactionIDs issue #697

Open
wants to merge 2 commits into
base: release/0.6.2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 11 additions & 7 deletions source/Modules/Controller/controllerBase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import qs from 'query-string';
import extension from 'extensionizer';

import { checkPendingTransaction, createPendingTransaction, removePendingTransaction } from '@modules/storageManager';
import {
checkPendingTransaction,
createPendingTransaction,
removePendingTransaction,
resetPendingTransactions,
} from '@modules/storageManager';
import ERRORS from '@background/errors';
import SIZES from '../../views/Popup/components/Transfer/constants';

Expand Down Expand Up @@ -30,6 +35,7 @@ export class ControllerModuleBase {
this.keyring = keyring;
this.secureController = secureController;
this.backgroundController = backgroundController;
resetPendingTransactions();
}

secureWrapper({ args, handlerObject }) {
Expand All @@ -48,9 +54,8 @@ export class ControllerModuleBase {
return this.secureController(
args[0].callback,
async () => {
createPendingTransaction((transactionId) => {
handlerObject.handler(...newArgs, transactionId);
});
const transactionId = await createPendingTransaction();
await handlerObject.handler(...newArgs, transactionId);
},
);
}
Expand All @@ -66,9 +71,8 @@ export class ControllerModuleBase {
return this.secureController(
methodArgs[0].callback,
async () => {
handlerObject.handler(...methodArgs);
removePendingTransaction(transactionId, () => {
});
await handlerObject.handler(...methodArgs);
await removePendingTransaction(transactionId);
},
);
});
Expand Down
41 changes: 31 additions & 10 deletions source/Modules/storageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,51 @@ export const setDabContacts = (dabContacts = [], cb = () => ([])) => {
secureSetWrapper({ dabContacts }, [], cb);
};

export const createPendingTransaction = (cb) => {
const setActiveTransactions = (newActiveTransactions) => new Promise((resolve) => {
secureSetWrapper({ activeTransactions: newActiveTransactions }, {}, resolve);
});

const getActiveTransactions = () => new Promise((resolve) => {
const defaultValue = {};
secureGetWrapper('activeTransactions', defaultValue, (state) => {
const activeTransactions = state?.activeTransactions || defaultValue;
resolve(activeTransactions);
});
});

export const createPendingTransaction = async () => {
const id = uuidv4();
secureSetWrapper({ activeTransactions: { [id]: 'pending' } }, {}, () => cb(id));
const activeTransactions = await getActiveTransactions();
const newActiveTransactions = {
...activeTransactions,
[id]: 'pending',
};
await setActiveTransactions(newActiveTransactions);
return id;
};

export const checkPendingTransaction = (transactionId, cb) => {
secureGetWrapper('activeTransactions', 'pending', (entry) => {
cb(entry.activeTransactions[transactionId]);
});
};

export const reviewPendingTransaction = async (transactionId) => {
const activeTransactions = await getActiveTransactions();
const getTxStatus = () => new Promise((resolve) => {
checkPendingTransaction(transactionId, resolve);
});
const setData = () => new Promise((resolve) => {
secureSetWrapper({ activeTransactions: { [transactionId]: 'reviewed' } }, {}, resolve);
});
const newActiveTransactions = { ...activeTransactions, [transactionId]: 'reviewed' };
const txStatus = await getTxStatus();
if (txStatus !== 'pending') throw new Error('Unauthorized review');
const result = await setData();
return result;
if (txStatus === 'pending') {
return setActiveTransactions(newActiveTransactions);
}
return null;
};
export const removePendingTransaction = (transactionId, cb) => {
secureSetWrapper({ activeTransactions: { [transactionId]: undefined } }, {}, cb);
export const removePendingTransaction = async (transactionId) => {
const activeTransactions = await getActiveTransactions();
const newActiveTransactions = { ...activeTransactions, [transactionId]: undefined };
await setActiveTransactions(newActiveTransactions);
};

export const resetPendingTransactions = () => {
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/AllowAgent/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const AllowAgent = ({
const [expand, setExpand] = useState(false);

const handleAllowAgent = async (status) => {
reviewPendingTransaction(transactionId, () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call('handleAllowAgent', [
url,
{ status, whitelist: args.whitelist || {}, metadata },
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/AppConnection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const AppConnection = ({ setOnTimeout, transactionId, metadata }) => {
} = query;

const handleResponse = async () => {
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call(
'handleAllowAgent',
[
Expand Down
4 changes: 2 additions & 2 deletions source/views/Popup/components/BurnXTC/hooks/useRequests.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {
useEffect(async () => {
if (requests.length === 0) {
setLoading(true);
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call('handleRequestBurnXTC', [response, callId, portId, transactionId]);
if (success) {
window.close();
Expand All @@ -57,7 +57,7 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {

const handleDeclineAll = async () => {
const declinedRequests = requests.map((r) => ({ ...r, status: 'declined' }));
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
await portRPC.call('handleRequestBurnXTC', [declinedRequests, callId, portId, transactionId]);
window.close();
};
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/ImportToken/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const ImportToken = ({

const handleImportToken = async (status) => {
setLoading(status);
reviewPendingTransaction(transactionId, () => { });
await reviewPendingTransaction(transactionId);
const success = await portRPC.call('handleRequestImportToken', [
{ status, token: { canisterId, standard, logo } },
callId,
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/LoginProxy/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const LoginProxy = ({
}) => {
useEffect(async () => {
if (metadata && callId && portId) {
await reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
await portRPC.call(handler, [metadata.url, args || {}, callId, portId, transactionId]);
window.close();
}
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/Principal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Principal = ({
}) => {
useEffect(async () => {
if (metadata && callId && portId) {
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
await portRPC.call('handleGetPrincipal', [metadata.url, callId, portId, transactionId]);
window.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const useTransactions = (transactions, callId, portId, transactionId) => {

const processAll = async (accepted) => {
setLoading(true);
reviewPendingTransaction(transactionId, (res) => res);
await reviewPendingTransaction(transactionId);
await portRPC.call('handleBatchTransactions', [
accepted,
transactions,
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/Sign/hooks/useRequest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const useRequests = (incomingRequest, callId, portId, transactionId) => {
const handleResponse = async (status) => {
request.status = status;
const handler = request.type === 'sign' ? 'handleSign' : 'handleCall';
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call(handler, [status, request, callId, portId, transactionId]);
if (success) {
window.close();
Expand Down
4 changes: 2 additions & 2 deletions source/views/Popup/components/Sign/hooks/useRequests.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {
useEffect(async () => {
if (requests.length === 0) {
setLoading(true);
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call('handleSign', [response, callId, portId, transactionId]);
if (success) {
window.close();
Expand All @@ -55,7 +55,7 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {

const handleDeclineAll = async () => {
const declinedRequests = requests.map((r) => ({ ...r, status: 'declined' }));
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
await portRPC.call('handleSign', [declinedRequests, callId, portId, transactionId]);
window.close();
};
Expand Down
2 changes: 1 addition & 1 deletion source/views/Popup/components/SignMessage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const SignMessage = ({

const handleSignMessage = async (status) => {
setLoading(status);
reviewPendingTransaction(transactionId, () => { });
await reviewPendingTransaction(transactionId);
const success = await portRPC.call('handleRequestSignMessage', [
{ status, token: { canisterId, standard, logo }, messageToSign },
callId,
Expand Down
6 changes: 3 additions & 3 deletions source/views/Popup/components/Transfer/hooks/useRequests.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {
if (requests.length === 0) {
setLoading(true);
const handler = response[0].token ? 'handleRequestTransferToken' : 'handleRequestTransfer';
reviewPendingTransaction(transactionId, async () => {});
await reviewPendingTransaction(transactionId);
const success = await portRPC.call(handler, [response, callId, portId, transactionId]);
if (success) {
window.close();
Expand All @@ -71,8 +71,8 @@ const useRequests = (incomingRequests, callId, portId, transactionId) => {
const declinedRequests = requests.map((r) => ({ ...r, status: 'declined' }));
const handler = declinedRequests[0].token ? 'handleRequestTransferToken' : 'handleRequestTransfer';
await Promise.all([
reviewPendingTransaction(transactionId, async () => {}),
portRPC.call(handler, [declinedRequests, callId, portId, transactionId]),
await reviewPendingTransaction(transactionId),
await portRPC.call(handler, [declinedRequests, callId, portId, transactionId]),
]);

window.close();
Expand Down