@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 4.37 kB
Source Map (JSON)
{"version":3,"file":"state.mjs","sourceRoot":"","sources":["../../../../src/methods/hooks/permitted/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,8BAA8B;AAGlD,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,2BAA2B;AAGjD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,iCAAuB;AAEhE;;;;;;;GAOG;AACH,QAAQ,CAAC,CAAC,0BAA0B,CAAC,SAAkB;IACrD,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;IAChD,6CAA6C;IAC7C,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,4CAA4C,CAC1D,OAAwB;IAExB,OAAO,KAAK,EAAE,GAAG,IAAmD,EAAE,EAAE;QACtE,OAAO,MAAM,OAAO,CAAC,0BAA0B,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC;IACxE,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,QAAQ,CAAC,CAAC,6BAA6B,CACrC,QAA8B,EAC9B,SAAkB;IAElB,6CAA6C;IAC7C,MAAM,GAAG,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;AACtE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,+CAA+C,CAC7D,OAAwB;IAExB,OAAO,KAAK,EAAE,GAAG,IAAsD,EAAE,EAAE;QACzE,OAAO,MAAM,OAAO,CAAC,6BAA6B,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC;IAC3E,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,QAAQ,CAAC,CAAC,4BAA4B,CAAC,SAAkB;IACvD,MAAM,GAAG,CAAC,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;AACvC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,8CAA8C,CAC5D,OAAwB;IAExB,OAAO,KAAK,EAAE,GAAG,IAAqD,EAAE,EAAE;QACxE,OAAO,CAAC,4BAA4B,EAAE,GAAG,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC;IAC1D,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { parseJson } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\nimport type { SagaIterator } from 'redux-saga';\nimport { put, select } from 'redux-saga/effects';\n\nimport type { RunSagaFunction } from '../../../store';\nimport { clearState, getState, setState } from '../../../store';\n\n/**\n * Get the Snap state from the store.\n *\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @returns The state of the Snap.\n * @yields Selects the state from the store.\n */\nfunction* getSnapStateImplementation(encrypted: boolean): SagaIterator<string> {\n const state = yield select(getState(encrypted));\n // TODO: Use actual decryption implementation\n return parseJson(state);\n}\n\n/**\n * Get the implementation of the `getSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `getSnapState` hook.\n */\nexport function getPermittedGetSnapStateMethodImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (...args: Parameters<typeof getSnapStateImplementation>) => {\n return await runSaga(getSnapStateImplementation, ...args).toPromise();\n };\n}\n\n/**\n * Update the Snap state in the store.\n *\n * @param newState - The new state.\n * @param encrypted - Whether to update the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @yields Puts the new state in the store.\n */\nfunction* updateSnapStateImplementation(\n newState: Record<string, Json>,\n encrypted: boolean,\n): SagaIterator<void> {\n // TODO: Use actual encryption implementation\n yield put(setState({ state: JSON.stringify(newState), encrypted }));\n}\n\n/**\n * Get the implementation of the `updateSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `updateSnapState` hook.\n */\nexport function getPermittedUpdateSnapStateMethodImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (...args: Parameters<typeof updateSnapStateImplementation>) => {\n return await runSaga(updateSnapStateImplementation, ...args).toPromise();\n };\n}\n\n/**\n * Clear the Snap state in the store.\n *\n * @param encrypted - Whether to clear the encrypted or unencrypted state.\n * Defaults to encrypted state.\n * @yields Puts the new state in the store.\n */\nfunction* clearSnapStateImplementation(encrypted: boolean): SagaIterator<void> {\n yield put(clearState({ encrypted }));\n}\n\n/**\n * Get the implementation of the `clearSnapState` hook.\n *\n * @param runSaga - The function to run a saga outside the usual Redux flow.\n * @returns The implementation of the `clearSnapState` hook.\n */\nexport function getPermittedClearSnapStateMethodImplementation(\n runSaga: RunSagaFunction,\n) {\n return async (...args: Parameters<typeof clearSnapStateImplementation>) => {\n runSaga(clearSnapStateImplementation, ...args).result();\n };\n}\n"]}