From a7089193c8c969ee581585cd08300c8b16bb8f73 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Wed, 29 Jul 2020 19:50:20 -0230 Subject: [PATCH] Use async/await for getRestrictedMethods (#9099) --- .../permissions/restrictedMethods.js | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/app/scripts/controllers/permissions/restrictedMethods.js b/app/scripts/controllers/permissions/restrictedMethods.js index 074cbb5a5..0e1ae4b9a 100644 --- a/app/scripts/controllers/permissions/restrictedMethods.js +++ b/app/scripts/controllers/permissions/restrictedMethods.js @@ -1,34 +1,30 @@ export default function getRestrictedMethods ({ getIdentities, getKeyringAccounts }) { return { 'eth_accounts': { - method: (_, res, __, end) => { - getKeyringAccounts() - .then((accounts) => { - const identities = getIdentities() - res.result = accounts - .sort((firstAddress, secondAddress) => { - if (!identities[firstAddress]) { - throw new Error(`Missing identity for address ${firstAddress}`) - } else if (!identities[secondAddress]) { - throw new Error(`Missing identity for address ${secondAddress}`) - } else if (identities[firstAddress].lastSelected === identities[secondAddress].lastSelected) { - return 0 - } else if (identities[firstAddress].lastSelected === undefined) { - return 1 - } else if (identities[secondAddress].lastSelected === undefined) { - return -1 - } + method: async (_, res, __, end) => { + try { + const accounts = await getKeyringAccounts() + const identities = getIdentities() + res.result = accounts.sort((firstAddress, secondAddress) => { + if (!identities[firstAddress]) { + throw new Error(`Missing identity for address ${firstAddress}`) + } else if (!identities[secondAddress]) { + throw new Error(`Missing identity for address ${secondAddress}`) + } else if (identities[firstAddress].lastSelected === identities[secondAddress].lastSelected) { + return 0 + } else if (identities[firstAddress].lastSelected === undefined) { + return 1 + } else if (identities[secondAddress].lastSelected === undefined) { + return -1 + } - return identities[secondAddress].lastSelected - identities[firstAddress].lastSelected - }) - end() + return identities[secondAddress].lastSelected - identities[firstAddress].lastSelected }) - .catch( - (err) => { - res.error = err - end(err) - }, - ) + end() + } catch (err) { + res.error = err + end(err) + } }, }, }