Skip to content

feat: allow for multiple keys in snap_getState - #4125

Open
hmalik88 wants to merge 7 commits into
mainfrom
hm/update-get-state
Open

hmalik88 wants to merge 7 commits into
mainfrom
hm/update-get-state

Conversation

@hmalik88

@hmalik88 hmalik88 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Description

Updates snap_getState to accept an array of keys in the key parameter, allowing a Snap to fetch multiple state values in a single call.

Before

// Only one key at a time
const value = await snap.request({
  method: 'snap_getState',
  params: { key: 'user.name' },
});
// → "Alice"

After

// Multiple keys in one call
const values = await snap.request({
  method: 'snap_getState',
  params: { key: ['user.name', 'user.age', 'settings.theme'] },
});
// → { 'user.name': 'Alice', 'user.age': 42, 'settings.theme': 'dark' }

Behavior

  • When key is a string[], the result is a Record<string, Json> mapping each key to its resolved value. Keys not found in state map to null.
  • All existing validation rules apply to each element in the array (dot-path syntax, forbidden keys).

Note

Medium Risk
Changes permitted state-read RPC and validation for encrypted Snap storage, but behavior is additive and reuses existing single-key lookup and security checks.

Overview
Extends snap_getState so params.key can be a string[], not just a single path. With an array, the RPC returns a Record<string, Json> keyed by each requested path; paths that are missing resolve to null. Single-key and whole-state behavior is unchanged.

Implementation updates get() in getState.ts to map over array keys (including empty array → {}), adds StateKeysStruct validation, and keeps existing dot-path and forbidden-segment rules per element. GetStateParams in the SDK is updated accordingly.

Example Snap, RPC tests, manage-state integration tests, and the test-snaps GetState UI (comma-separated keys) exercise the new behavior; changelogs note the addition.

Reviewed by Cursor Bugbot for commit 7ab29d5. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.59%. Comparing base (2c8a299) to head (7ab29d5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4125   +/-   ##
=======================================
  Coverage   98.59%   98.59%           
=======================================
  Files         429      429           
  Lines       12495    12498    +3     
  Branches     1976     1977    +1     
=======================================
+ Hits        12320    12323    +3     
  Misses        175      175           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hmalik88
hmalik88 marked this pull request as ready for review September 17, 2026 16:58
@hmalik88
hmalik88 requested a review from a team as a code owner September 17, 2026 16:58

const GetStateParametersStruct = object({
key: optional(StateKeyStruct),
key: optional(union([StateKeyStruct, StateKeysStruct])),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use a selective union so we get better error messaging? We can branch on isArray()

}

if (Array.isArray(key)) {
return Object.fromEntries(key.map((k) => [k, get(value, k)]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't remember why we chose to use a classic loop below, this will likely be slower. Can you @Mrtenz

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.

2 participants