@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
84 lines • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClearSnapStateMethodImplementation = exports.getUpdateSnapStateMethodImplementation = exports.getGetSnapStateMethodImplementation = 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 _snapId - The ID of the Snap to get the state for. This is ignored
* because the simulator only supports one Snap.
* @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(_snapId, encrypted = true) {
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 getGetSnapStateMethodImplementation(runSaga) {
return (...args) => {
return runSaga(getSnapStateImplementation, ...args).result();
};
}
exports.getGetSnapStateMethodImplementation = getGetSnapStateMethodImplementation;
/**
* Update the Snap state in the store.
*
* @param _snapId - The ID of the Snap to update the state for. This is ignored
* because the simulator only supports one Snap.
* @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(_snapId, newState, encrypted = true) {
// 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 getUpdateSnapStateMethodImplementation(runSaga) {
return (...args) => {
runSaga(updateSnapStateImplementation, ...args).result();
};
}
exports.getUpdateSnapStateMethodImplementation = getUpdateSnapStateMethodImplementation;
/**
* Clear the Snap state in the store.
*
* @param _snapId - The ID of the Snap to clear the state for. This is ignored
* because the simulator only supports one Snap.
* @param encrypted - Whether to clear the encrypted or unencrypted state.
* Defaults to encrypted state.
* @yields Puts the new state in the store.
*/
function* clearSnapStateImplementation(_snapId, encrypted = true) {
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 getClearSnapStateMethodImplementation(runSaga) {
return async (...args) => {
runSaga(clearSnapStateImplementation, ...args).result();
};
}
exports.getClearSnapStateMethodImplementation = getClearSnapStateMethodImplementation;
//# sourceMappingURL=state.cjs.map