1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/app/scripts/controllers/ens/index.js
Olusegun Akintayo 086a7d0483
Keep memstore contents after service worker restarts (#15913)
* Add all controllers in memstore to store
Add methods to controller to reset memstore
Reset memstore when popup or tab is closed.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* When profile is loaded, set isFirstTime to true..
After resetting the controllers, set the flag to false.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Remove console.logs

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* For some reason programmatically computing the store is not working.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Proper check for browser.storage

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* do a list of rest methods instead of reset controllers.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Mock controller resetStates and localstore get/set

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Comments about TLC

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* bind this.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* use globalThis instead of locastore to store first time state.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Test to check that resetStates is not called a second time

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Set init state in GasFeeController and other controllers so that their
state is persisted accross SW restarts

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Revert localstore changes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* wrap the reset states changes in MV3 flag

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Remove localstore from metamask-controller

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Always reset state on MMController start in MV2.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Use relative path for import of isManifestV3

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix unit test

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-11-22 20:56:26 +04:00

105 lines
2.6 KiB
JavaScript

import punycode from 'punycode/punycode';
import { ObservableStore } from '@metamask/obs-store';
import log from 'loglevel';
import { CHAIN_ID_TO_NETWORK_ID_MAP } from '../../../../shared/constants/network';
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
import Ens from './ens';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const ZERO_X_ERROR_ADDRESS = '0x';
export default class EnsController {
constructor({ ens, provider, onNetworkDidChange, getCurrentChainId } = {}) {
const initState = {
ensResolutionsByAddress: {},
};
this._ens = ens;
if (!this._ens) {
const chainId = getCurrentChainId();
const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId];
if (Ens.getNetworkEnsSupport(network)) {
this._ens = new Ens({
network,
provider,
});
}
}
this.store = new ObservableStore(initState);
this.resetState = () => {
this.store.updateState(initState);
};
onNetworkDidChange(() => {
this.store.putState(initState);
const chainId = getCurrentChainId();
const network = CHAIN_ID_TO_NETWORK_ID_MAP[chainId];
if (Ens.getNetworkEnsSupport(network)) {
this._ens = new Ens({
network,
provider,
});
} else {
delete this._ens;
}
});
}
reverseResolveAddress(address) {
return this._reverseResolveAddress(toChecksumHexAddress(address));
}
async _reverseResolveAddress(address) {
if (!this._ens) {
return undefined;
}
const state = this.store.getState();
if (state.ensResolutionsByAddress[address]) {
return state.ensResolutionsByAddress[address];
}
let domain;
try {
domain = await this._ens.reverse(address);
} catch (error) {
log.debug(error);
return undefined;
}
let registeredAddress;
try {
registeredAddress = await this._ens.lookup(domain);
} catch (error) {
log.debug(error);
return undefined;
}
if (
registeredAddress === ZERO_ADDRESS ||
registeredAddress === ZERO_X_ERROR_ADDRESS
) {
return undefined;
}
if (toChecksumHexAddress(registeredAddress) !== address) {
return undefined;
}
this._updateResolutionsByAddress(address, punycode.toASCII(domain));
return domain;
}
_updateResolutionsByAddress(address, domain) {
const oldState = this.store.getState();
this.store.putState({
ensResolutionsByAddress: {
...oldState.ensResolutionsByAddress,
[address]: domain,
},
});
}
}