-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for state methods to snaps-simulation
- Loading branch information
Showing
14 changed files
with
506 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './get-preferences'; | ||
export * from './interface'; | ||
export * from './notifications'; | ||
export * from './permitted'; | ||
export * from './request-user-approval'; | ||
export * from './state'; | ||
export * from './interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './state'; |
119 changes: 119 additions & 0 deletions
119
packages/snaps-simulation/src/methods/hooks/permitted/state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { createStore, getState, setState } from '../../../store'; | ||
import { getMockOptions } from '../../../test-utils'; | ||
import { | ||
getPermittedClearSnapStateMethodImplementation, | ||
getPermittedGetSnapStateMethodImplementation, | ||
getPermittedUpdateSnapStateMethodImplementation, | ||
} from './state'; | ||
|
||
describe('getPermittedGetSnapStateMethodImplementation', () => { | ||
it('returns the implementation of the `getSnapState` hook', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedGetSnapStateMethodImplementation(runSaga); | ||
|
||
expect(await fn(true)).toBeNull(); | ||
|
||
store.dispatch( | ||
setState({ | ||
state: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
encrypted: true, | ||
}), | ||
); | ||
|
||
expect(await fn(true)).toStrictEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
|
||
it('returns the implementation of the `getSnapState` hook for unencrypted state', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedGetSnapStateMethodImplementation(runSaga); | ||
|
||
expect(await fn(false)).toBeNull(); | ||
|
||
store.dispatch( | ||
setState({ | ||
state: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
encrypted: false, | ||
}), | ||
); | ||
|
||
expect(await fn(false)).toStrictEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getPermittedUpdateSnapStateMethodImplementation', () => { | ||
it('returns the implementation of the `updateSnapState` hook', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedUpdateSnapStateMethodImplementation(runSaga); | ||
|
||
expect(getState(true)(store.getState())).toBeNull(); | ||
|
||
await fn({ foo: 'bar' }, true); | ||
|
||
expect(getState(true)(store.getState())).toStrictEqual( | ||
JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
); | ||
}); | ||
|
||
it('returns the implementation of the `updateSnapState` hook for unencrypted state', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedUpdateSnapStateMethodImplementation(runSaga); | ||
|
||
expect(getState(false)(store.getState())).toBeNull(); | ||
|
||
await fn({ foo: 'bar' }, false); | ||
|
||
expect(getState(false)(store.getState())).toStrictEqual( | ||
JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
); | ||
}); | ||
}); | ||
|
||
describe('getPermittedClearSnapStateMethodImplementation', () => { | ||
it('returns the implementation of the `clearSnapState` hook', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedClearSnapStateMethodImplementation(runSaga); | ||
|
||
store.dispatch( | ||
setState({ | ||
state: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
encrypted: true, | ||
}), | ||
); | ||
|
||
await fn(true); | ||
|
||
expect(getState(true)(store.getState())).toBeNull(); | ||
}); | ||
|
||
it('returns the implementation of the `clearSnapState` hook for unencrypted state', async () => { | ||
const { store, runSaga } = createStore(getMockOptions()); | ||
const fn = getPermittedClearSnapStateMethodImplementation(runSaga); | ||
|
||
store.dispatch( | ||
setState({ | ||
state: JSON.stringify({ | ||
foo: 'bar', | ||
}), | ||
encrypted: false, | ||
}), | ||
); | ||
|
||
await fn(false); | ||
|
||
expect(getState(false)(store.getState())).toBeNull(); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
packages/snaps-simulation/src/methods/hooks/permitted/state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { parseJson } from '@metamask/snaps-utils'; | ||
import type { Json } from '@metamask/utils'; | ||
import type { SagaIterator } from 'redux-saga'; | ||
import { put, select } from 'redux-saga/effects'; | ||
|
||
import type { RunSagaFunction } from '../../../store'; | ||
import { clearState, getState, setState } from '../../../store'; | ||
|
||
/** | ||
* 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: boolean): SagaIterator<string> { | ||
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 getPermittedGetSnapStateMethodImplementation( | ||
runSaga: RunSagaFunction, | ||
) { | ||
return async (...args: Parameters<typeof getSnapStateImplementation>) => { | ||
return await runSaga(getSnapStateImplementation, ...args).toPromise(); | ||
}; | ||
} | ||
|
||
/** | ||
* 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: Record<string, Json>, | ||
encrypted: boolean, | ||
): SagaIterator<void> { | ||
// 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 getPermittedUpdateSnapStateMethodImplementation( | ||
runSaga: RunSagaFunction, | ||
) { | ||
return async (...args: Parameters<typeof updateSnapStateImplementation>) => { | ||
return await runSaga(updateSnapStateImplementation, ...args).toPromise(); | ||
}; | ||
} | ||
|
||
/** | ||
* 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: boolean): SagaIterator<void> { | ||
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 getPermittedClearSnapStateMethodImplementation( | ||
runSaga: RunSagaFunction, | ||
) { | ||
return async (...args: Parameters<typeof clearSnapStateImplementation>) => { | ||
runSaga(clearSnapStateImplementation, ...args).result(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.