Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON storage limit utilities #14

Merged
merged 33 commits into from
Jul 12, 2022
Merged

Add JSON storage limit utilities #14

merged 33 commits into from
Jul 12, 2022

Conversation

david0xd
Copy link
Contributor

@david0xd david0xd commented Jun 24, 2022

This PR will add utility functions to handle limits and validation of JSON storage used by Snaps.

This is still work in progress.

The main goal of this PR is to implement the following function:

export function validateJsonAndGetSize(
  jsObject: unknown,
  skipSizingProcess = false,
): [isValid: boolean, plainTextSizeInBytes: number]

This function has two capabilities:

  1. Validate JavaScript object serialization capabilities (validate if JavaScript object can be serialized to JSON by using JSON.stringify(...))
  2. Calculate the potential size in Bytes after JavaScript object is serialized to plain text in JSON format.

Size calculation can be skipped by setting skipSizingProcess=true in which case only the validation process will be done and 0 will be returned for a size.

Fixes https://app.zenhub.com/workspaces/snaps-platform-615b3a7c08d2b20015eb6c4e/issues/metamask/snaps-skunkworks/571
Fixes MetaMask/snaps#571

Related to: MetaMask/snaps#209

Copy link
Member

@FrederikBolding FrederikBolding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! Left some comments

src/misc.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/misc.ts Show resolved Hide resolved
src/test.data.ts Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/test.data.ts Show resolved Hide resolved
src/misc.ts Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/misc.ts Outdated Show resolved Hide resolved
src/test.data.ts Show resolved Hide resolved
Copy link
Member

@Mrtenz Mrtenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should prefer avoiding any whenever possible.

src/json.test.ts Outdated Show resolved Hide resolved
src/json.test.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
Copy link
Member

@Mrtenz Mrtenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

src/json.ts Show resolved Hide resolved
src/json.ts Outdated Show resolved Hide resolved
Copy link
Member

@FrederikBolding FrederikBolding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Good work on this!

@david0xd david0xd marked this pull request as ready for review July 12, 2022 14:53
@david0xd david0xd requested a review from a team as a code owner July 12, 2022 14:53
@david0xd david0xd merged commit aeb62e7 into main Jul 12, 2022
@david0xd david0xd deleted the dd/snaps-storage-limit branch July 12, 2022 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit Snaps storage to ensure they can't break Metamask storage
3 participants