@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
78 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPermittedClearSnapStateMethodImplementation = exports.getPermittedUpdateSnapStateMethodImplementation = exports.getPermittedGetSnapStateMethodImplementation = void 0;
const snaps_utils_1 = require("@metamask/snaps-utils");
const effects_1 = require("redux-saga/effects");
const store_1 = require("../../../store/index.cjs");
/**
* Get the Snap state from the store.
*
* @param encrypted - Whether to get the encrypted or unencrypted state.
* Defaults to encrypted state.
* @returns The state of the Snap.
* @yields Selects the state from the store.
*/
function* getSnapStateImplementation(encrypted) {
const state = yield (0, effects_1.select)((0, store_1.getState)(encrypted));
// TODO: Use actual decryption implementation
return (0, snaps_utils_1.parseJson)(state);
}
/**
* Get the implementation of the `getSnapState` hook.
*
* @param runSaga - The function to run a saga outside the usual Redux flow.
* @returns The implementation of the `getSnapState` hook.
*/
function getPermittedGetSnapStateMethodImplementation(runSaga) {
return async (...args) => {
return await runSaga(getSnapStateImplementation, ...args).toPromise();
};
}
exports.getPermittedGetSnapStateMethodImplementation = getPermittedGetSnapStateMethodImplementation;
/**
* Update the Snap state in the store.
*
* @param newState - The new state.
* @param encrypted - Whether to update the encrypted or unencrypted state.
* Defaults to encrypted state.
* @yields Puts the new state in the store.
*/
function* updateSnapStateImplementation(newState, encrypted) {
// TODO: Use actual encryption implementation
yield (0, effects_1.put)((0, store_1.setState)({ state: JSON.stringify(newState), encrypted }));
}
/**
* Get the implementation of the `updateSnapState` hook.
*
* @param runSaga - The function to run a saga outside the usual Redux flow.
* @returns The implementation of the `updateSnapState` hook.
*/
function getPermittedUpdateSnapStateMethodImplementation(runSaga) {
return async (...args) => {
return await runSaga(updateSnapStateImplementation, ...args).toPromise();
};
}
exports.getPermittedUpdateSnapStateMethodImplementation = getPermittedUpdateSnapStateMethodImplementation;
/**
* Clear the Snap state in the store.
*
* @param encrypted - Whether to clear the encrypted or unencrypted state.
* Defaults to encrypted state.
* @yields Puts the new state in the store.
*/
function* clearSnapStateImplementation(encrypted) {
yield (0, effects_1.put)((0, store_1.clearState)({ encrypted }));
}
/**
* Get the implementation of the `clearSnapState` hook.
*
* @param runSaga - The function to run a saga outside the usual Redux flow.
* @returns The implementation of the `clearSnapState` hook.
*/
function getPermittedClearSnapStateMethodImplementation(runSaga) {
return async (...args) => {
runSaga(clearSnapStateImplementation, ...args).result();
};
}
exports.getPermittedClearSnapStateMethodImplementation = getPermittedClearSnapStateMethodImplementation;
//# sourceMappingURL=state.cjs.map