2022-03-18 20:07:05 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2022-10-11 00:10:44 +02:00
|
|
|
import { captureException } from '@sentry/browser';
|
2023-01-23 17:18:22 +01:00
|
|
|
import { checkForLastError } from '../../../shared/modules/browser-runtime.utils';
|
2018-01-24 01:26:50 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* A wrapper around the extension's storage local API
|
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export default class ExtensionStore {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor() {
|
2022-03-18 20:07:05 +01:00
|
|
|
this.isSupported = Boolean(browser.storage.local);
|
2018-01-24 01:26:50 +01:00
|
|
|
if (!this.isSupported) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error('Storage local API not available.');
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2022-10-11 00:10:44 +02:00
|
|
|
// we use this flag to avoid flooding sentry with a ton of errors:
|
|
|
|
// once data persistence fails once and it flips true we don't send further
|
|
|
|
// data persistence errors to sentry
|
|
|
|
this.dataPersistenceFailing = false;
|
2023-08-17 13:59:05 +02:00
|
|
|
this.mostRecentRetrievedState = null;
|
2022-10-11 00:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setMetadata(initMetaData) {
|
|
|
|
this.metadata = initMetaData;
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(state) {
|
|
|
|
if (!this.isSupported) {
|
|
|
|
throw new Error(
|
|
|
|
'Metamask- cannot persist state to local store as this browser does not support this action',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!state) {
|
|
|
|
throw new Error('MetaMask - updated state is missing');
|
|
|
|
}
|
|
|
|
if (!this.metadata) {
|
|
|
|
throw new Error(
|
|
|
|
'MetaMask - metadata must be set on instance of ExtensionStore before calling "set"',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// we format the data for storage as an object with the "data" key for the controller state object
|
|
|
|
// and the "meta" key for a metadata object containing a version number that tracks how the data shape
|
|
|
|
// has changed using migrations to adapt to backwards incompatible changes
|
|
|
|
await this._set({ data: state, meta: this.metadata });
|
|
|
|
if (this.dataPersistenceFailing) {
|
|
|
|
this.dataPersistenceFailing = false;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (!this.dataPersistenceFailing) {
|
|
|
|
this.dataPersistenceFailing = true;
|
|
|
|
captureException(err);
|
|
|
|
}
|
|
|
|
log.error('error setting state in local store:', err);
|
|
|
|
}
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns all of the keys currently saved
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {Promise<*>}
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
async get() {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!this.isSupported) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = await this._get();
|
2018-03-08 23:10:28 +01:00
|
|
|
// extension.storage.local always returns an obj
|
|
|
|
// if the object is empty, treat it as undefined
|
|
|
|
if (isEmpty(result)) {
|
2023-08-17 13:59:05 +02:00
|
|
|
this.mostRecentRetrievedState = null;
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2018-03-08 23:10:28 +01:00
|
|
|
}
|
2023-08-17 13:59:05 +02:00
|
|
|
this.mostRecentRetrievedState = result;
|
2021-02-04 19:15:23 +01:00
|
|
|
return result;
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns all of the keys currently saved
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2018-04-16 17:29:43 +02:00
|
|
|
* @private
|
2022-07-27 15:28:05 +02:00
|
|
|
* @returns {object} the key-value map from local storage
|
2018-04-16 17:29:43 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_get() {
|
2022-03-18 20:07:05 +01:00
|
|
|
const { local } = browser.storage;
|
2018-03-14 18:49:54 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-03-18 20:07:05 +01:00
|
|
|
local.get(null).then((/** @type {any} */ result) => {
|
2023-01-23 17:18:22 +01:00
|
|
|
const err = checkForLastError();
|
2018-03-14 18:49:54 +01:00
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
reject(err);
|
2018-03-14 18:49:54 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve(result);
|
2018-03-14 18:49:54 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-03-14 18:49:54 +01:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Sets the key in local state
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} obj - The key to set
|
2020-01-13 19:36:36 +01:00
|
|
|
* @returns {Promise<void>}
|
2018-04-16 17:29:43 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_set(obj) {
|
2022-03-18 20:07:05 +01:00
|
|
|
const { local } = browser.storage;
|
2018-03-14 18:49:54 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-03-18 20:07:05 +01:00
|
|
|
local.set(obj).then(() => {
|
2023-01-23 17:18:22 +01:00
|
|
|
const err = checkForLastError();
|
2018-03-14 18:49:54 +01:00
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
reject(err);
|
2018-03-14 18:49:54 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve();
|
2018-03-14 18:49:54 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-03-14 18:49:54 +01:00
|
|
|
}
|
2018-01-24 01:26:50 +01:00
|
|
|
}
|
2018-03-08 23:10:28 +01:00
|
|
|
|
2018-04-16 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Returns whether or not the given object contains no keys
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} obj - The object to check
|
2018-04-16 17:29:43 +02:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function isEmpty(obj) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Object.keys(obj).length === 0;
|
2018-03-08 23:10:28 +01:00
|
|
|
}
|