-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Improve Snaps execution performance (#13420)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Improve Snaps execution performance by bumping the Snaps packages to latest. The latest packages have improved caching for Snap state and changes the Snaps execution on mobile to run each Snap in a separate WebView. This PR adapts `SnapsExecutionWebView` to support this new execution architecture and also attempts to speed up key derivation by using a native implementation of `hmacSha512`. Furthermore, the idle timeout for Snaps is bumped to 5 minutes. ## **Related issues** Fixes: MetaMask/snaps#2873 ## **Manual testing steps** Run any Snap and make sure it still works!
- Loading branch information
1 parent
8886b52
commit a9cd3af
Showing
11 changed files
with
354 additions
and
181 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { NativeModules } from 'react-native'; | ||
import { hmacSha512 } from './hmac'; | ||
import { hmac as nobleHmac } from '@noble/hashes/hmac'; | ||
import { sha512 as nobleSha512 } from '@noble/hashes/sha512'; | ||
import { bytesToHex, hexToBytes, stringToBytes } from '@metamask/utils'; | ||
|
||
describe('hmacSha512', () => { | ||
NativeModules.Aes.hmac512 = jest | ||
.fn() | ||
.mockImplementation((data, key) => | ||
bytesToHex(nobleHmac(nobleSha512, hexToBytes(key), stringToBytes(data))), | ||
); | ||
|
||
it('returns hash from native module', async () => { | ||
const key = new Uint8Array(32); | ||
const data = new Uint8Array(32); | ||
|
||
const result = await hmacSha512(key, data); | ||
expect(bytesToHex(result)).toBe( | ||
'0xbae46cebebbb90409abc5acf7ac21fdb339c01ce15192c52fb9e8aa11a8de9a4ea15a045f2be245fbb98916a9ae81b353e33b9c42a55380c5158241daeb3c6dd', | ||
); | ||
}); | ||
}); |
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,16 @@ | ||
import { | ||
bytesToHex, | ||
bytesToString, | ||
hexToBytes, | ||
remove0x, | ||
} from '@metamask/utils'; | ||
import { NativeModules } from 'react-native'; | ||
|
||
export async function hmacSha512(key: Uint8Array, data: Uint8Array) { | ||
const Aes = NativeModules.Aes; | ||
const bytes = await Aes.hmac512( | ||
bytesToString(data), | ||
remove0x(bytesToHex(key)), | ||
); | ||
return hexToBytes(bytes); | ||
} |
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 |
---|---|---|
|
@@ -14,3 +14,4 @@ export { | |
DERIVATION_OPTIONS_DEFAULT_OWASP2023, | ||
pbkdf2, | ||
}; | ||
export * from './hmac'; |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { | ||
createWebView, | ||
removeWebView, | ||
SnapsExecutionWebView, | ||
} from './SnapsExecutionWebView'; | ||
|
||
describe('SnapsExecutionWebView', () => { | ||
it('should render correctly', () => { | ||
const wrapper = render(<SnapsExecutionWebView />); | ||
expect(wrapper).toMatchInlineSnapshot(` | ||
<RCTScrollView> | ||
<View> | ||
<View | ||
style={ | ||
{ | ||
"height": 0, | ||
} | ||
} | ||
/> | ||
</View> | ||
</RCTScrollView> | ||
`); | ||
}); | ||
|
||
it('should create and remove WebViews correctly', async () => { | ||
const wrapper = render(<SnapsExecutionWebView />); | ||
createWebView('foo'); | ||
createWebView('bar'); | ||
wrapper.rerender(<SnapsExecutionWebView />); | ||
expect(await wrapper.queryByTestId('foo')).toBeTruthy(); | ||
expect(await wrapper.queryByTestId('bar')).toBeTruthy(); | ||
removeWebView('foo'); | ||
wrapper.rerender(<SnapsExecutionWebView />); | ||
expect(await wrapper.queryByTestId('foo')).toBeNull(); | ||
expect(await wrapper.queryByTestId('bar')).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.