From 0b334b14a614a09f479dddadf283892394aa67d5 Mon Sep 17 00:00:00 2001 From: Jan Marcano Date: Thu, 5 Jan 2023 16:21:44 -0500 Subject: [PATCH] Fix Custom Networks for 'enable' calls --- .../src/background/messaging/task.ts | 14 ++++----- .../src/background/transaction/actions.ts | 29 +++++-------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/packages/extension/src/background/messaging/task.ts b/packages/extension/src/background/messaging/task.ts index 0e52f730..d5e65af9 100644 --- a/packages/extension/src/background/messaging/task.ts +++ b/packages/extension/src/background/messaging/task.ts @@ -613,8 +613,7 @@ export class Task { let networkSpecifiedType = 0; if (genesisID && genesisHash) { networkSpecifiedType = 1; - } - else if (genesisID || genesisHash) { + } else if (genesisID || genesisHash) { networkSpecifiedType = 2; } d.body.params.networkSpecifiedType = networkSpecifiedType; @@ -634,15 +633,14 @@ export class Task { setTimeout(() => { MessageApi.send(d); }, 2000); - } - else { + } else { // Get ledger/hash/id from the genesisID and/or hash const ledgerTemplate = getLedgerFromMixedGenesis(genesisID, genesisHash); // Validate that the genesis id and hash if provided match the resulting one // This is because a dapp may request an id and hash from different ledgers if ((genesisID && genesisID !== ledgerTemplate.genesisId) - || (genesisHash && genesisHash !== ledgerTemplate.genesisHash)) { + || (genesisHash && ledgerTemplate.genesisHash && genesisHash !== ledgerTemplate.genesisHash)) { d.error = RequestError.UnsupportedLedger; setTimeout(() => { MessageApi.send(d); @@ -699,9 +697,10 @@ export class Task { // Failure means we won't auto authorize, but we can sink the error as we are re-prompting } } + // We haven't immediately failed and don't have preAuthorization so we need to prompt accounts. const promptedAccounts = []; - + // Add any requested accounts so they can be in the proper order to start if (accounts) { for (let i = 0; i < accounts.length; i++) { @@ -1058,7 +1057,6 @@ export class Task { }); }, // Accounts - /* eslint-disable-next-line no-unused-vars */ [JsonRpcMethod.Accounts]: (d: any, resolve: Function, reject: Function) => { const session = InternalMethods.getHelperSession(); // If we don't have a ledger requested, respond with an error giving available ledgers @@ -1635,7 +1633,7 @@ export class Task { // Setup new prompted accounts which will be the return values const newPromptedAccounts = []; - + // Add any requested accounts so they can be in the proper order to start if (promptedAccounts) { for (let i = 0; i < promptedAccounts.length; i++) { diff --git a/packages/extension/src/background/transaction/actions.ts b/packages/extension/src/background/transaction/actions.ts index 63c62a2e..8822f991 100644 --- a/packages/extension/src/background/transaction/actions.ts +++ b/packages/extension/src/background/transaction/actions.ts @@ -144,7 +144,7 @@ export function getLedgerFromGenesisId(genesisId: string) { return defaultLedger; } -export function getLedgerFromMixedGenesis(genesisId: string, genesisHash: string): LedgerTemplate { +export function getLedgerFromMixedGenesis(genesisId: string, genesisHash?: string): LedgerTemplate { // Default the ledger to mainnet const defaultLedger = 'MainNet'; @@ -160,17 +160,12 @@ export function getLedgerFromMixedGenesis(genesisId: string, genesisHash: string } } - // Injected networks may have additional information, multiples, or additional checks - // so we will check them separately + // Injected networks may have additional validations so we check them separately const injectedNetworks = Settings.getCleansedInjectedNetworks(); - injectedNetworks.forEach(network => { - if (network['genesisId'] === genesisId) { - // Found genesisId, make sure the hash matches - if (!genesisHash || genesisHash === network.genesisHash) { - return network; - } - } - }); + ledger = injectedNetworks.find((network) => network['genesisId'] === genesisId); + if (ledger) { + return ledger; + } } // We didn't match on the genesis id so check the hashes @@ -183,17 +178,7 @@ export function getLedgerFromMixedGenesis(genesisId: string, genesisHash: string } } - // Injected networks may have additional information, multiples, or additional checks - // so we will check them separately - const injectedNetworks = Settings.getCleansedInjectedNetworks(); - injectedNetworks.forEach(network => { - if (network['genesisHash'] === genesisHash) { - // Found genesisHash, make sure the id matches - if (!genesisId || genesisId === ledger.genesisId) { - return network; - } - } - }); + // We don't currently store the genesisHash of the custom networks } return defaultLedgers.find((l) => defaultLedger === l['name']);