Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #465 from PureStake/jan/enable-custom-networks
Browse files Browse the repository at this point in the history
Fix Custom Networks for 'enable' calls
  • Loading branch information
PureBrent authored Jan 6, 2023
2 parents 3a23290 + 0b334b1 commit a13ea60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
14 changes: 6 additions & 8 deletions packages/extension/src/background/messaging/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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++) {
Expand Down
29 changes: 7 additions & 22 deletions packages/extension/src/background/transaction/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
Expand All @@ -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']);
Expand Down

0 comments on commit a13ea60

Please sign in to comment.