@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
78 lines • 2.9 kB
JavaScript
import { parseJson } from "@metamask/snaps-utils";
import { put, select } from "redux-saga/effects";
import { clearState, getState, setState } from "../../store/index.mjs";
/**
* 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 select(getState(encrypted));
// TODO: Use actual decryption implementation
return 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.
*/
export function getGetSnapStateMethodImplementation(runSaga) {
return (...args) => {
return runSaga(getSnapStateImplementation, ...args).result();
};
}
/**
* 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 put(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.
*/
export function getUpdateSnapStateMethodImplementation(runSaga) {
return (...args) => {
runSaga(updateSnapStateImplementation, ...args).result();
};
}
/**
* 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 put(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.
*/
export function getClearSnapStateMethodImplementation(runSaga) {
return async (...args) => {
runSaga(clearSnapStateImplementation, ...args).result();
};
}
//# sourceMappingURL=state.mjs.map