|
| 1 | +import { parseJson } from '@metamask/snaps-utils'; |
| 2 | +import type { Json } from '@metamask/utils'; |
| 3 | +import type { SagaIterator } from 'redux-saga'; |
| 4 | +import { put, select } from 'redux-saga/effects'; |
| 5 | + |
| 6 | +import type { RunSagaFunction } from '../../../store'; |
| 7 | +import { clearState, getState, setState } from '../../../store'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Get the Snap state from the store. |
| 11 | + * |
| 12 | + * @param encrypted - Whether to get the encrypted or unencrypted state. |
| 13 | + * Defaults to encrypted state. |
| 14 | + * @returns The state of the Snap. |
| 15 | + * @yields Selects the state from the store. |
| 16 | + */ |
| 17 | +function* getSnapStateImplementation(encrypted: boolean): SagaIterator<string> { |
| 18 | + const state = yield select(getState(encrypted)); |
| 19 | + // TODO: Use actual decryption implementation |
| 20 | + return parseJson(state); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Get the implementation of the `getSnapState` hook. |
| 25 | + * |
| 26 | + * @param runSaga - The function to run a saga outside the usual Redux flow. |
| 27 | + * @returns The implementation of the `getSnapState` hook. |
| 28 | + */ |
| 29 | +export function getPermittedGetSnapStateMethodImplementation( |
| 30 | + runSaga: RunSagaFunction, |
| 31 | +) { |
| 32 | + return async (...args: Parameters<typeof getSnapStateImplementation>) => { |
| 33 | + return await runSaga(getSnapStateImplementation, ...args).toPromise(); |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Update the Snap state in the store. |
| 39 | + * |
| 40 | + * @param newState - The new state. |
| 41 | + * @param encrypted - Whether to update the encrypted or unencrypted state. |
| 42 | + * Defaults to encrypted state. |
| 43 | + * @yields Puts the new state in the store. |
| 44 | + */ |
| 45 | +function* updateSnapStateImplementation( |
| 46 | + newState: Record<string, Json>, |
| 47 | + encrypted: boolean, |
| 48 | +): SagaIterator<void> { |
| 49 | + // TODO: Use actual encryption implementation |
| 50 | + yield put(setState({ state: JSON.stringify(newState), encrypted })); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Get the implementation of the `updateSnapState` hook. |
| 55 | + * |
| 56 | + * @param runSaga - The function to run a saga outside the usual Redux flow. |
| 57 | + * @returns The implementation of the `updateSnapState` hook. |
| 58 | + */ |
| 59 | +export function getPermittedUpdateSnapStateMethodImplementation( |
| 60 | + runSaga: RunSagaFunction, |
| 61 | +) { |
| 62 | + return async (...args: Parameters<typeof updateSnapStateImplementation>) => { |
| 63 | + return await runSaga(updateSnapStateImplementation, ...args).toPromise(); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Clear the Snap state in the store. |
| 69 | + * |
| 70 | + * @param encrypted - Whether to clear the encrypted or unencrypted state. |
| 71 | + * Defaults to encrypted state. |
| 72 | + * @yields Puts the new state in the store. |
| 73 | + */ |
| 74 | +function* clearSnapStateImplementation(encrypted: boolean): SagaIterator<void> { |
| 75 | + yield put(clearState({ encrypted })); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Get the implementation of the `clearSnapState` hook. |
| 80 | + * |
| 81 | + * @param runSaga - The function to run a saga outside the usual Redux flow. |
| 82 | + * @returns The implementation of the `clearSnapState` hook. |
| 83 | + */ |
| 84 | +export function getPermittedClearSnapStateMethodImplementation( |
| 85 | + runSaga: RunSagaFunction, |
| 86 | +) { |
| 87 | + return async (...args: Parameters<typeof clearSnapStateImplementation>) => { |
| 88 | + runSaga(clearSnapStateImplementation, ...args).result(); |
| 89 | + }; |
| 90 | +} |
0 commit comments