Skip to content

Commit b62ffe0

Browse files
fix: Prevent infinite recursion in crypto.subtle polyfill (#133)
## Explanation The `ed25519` polyfill in the Solana Snap is broken when anything but the `ed25519` algorithm is used. This prevents us from using the newest version of `key-tree` which has a bunch of performance improvements. This PR corrects the polyfill so it no longer infinitely recurses and bumps `key-tree` back to the latest version.
1 parent eec15e0 commit b62ffe0

6 files changed

Lines changed: 20 additions & 26 deletions

File tree

packages/solana-wallet-snap/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- **BREAKING:** Removed the legacy snap-hosted send dialog (`startSendTransactionFlow` RPC and `features/send` UI). Send is now handled exclusively through the unified send flow client methods (`confirmSend`, `onAmountInput`, `onAddressInput`). ([#130](https://github.com/MetaMask/internal-snaps/pull/130))
1313
- **BREAKING:** Removed the deprecated `getFeeForTransaction` RPC. Use the `computeFee` client request instead. ([#130](https://github.com/MetaMask/internal-snaps/pull/130))
1414

15+
### Fixed
16+
17+
- Bump `@metamask/key-tree` from `9.1.2` to `^10.1.1` ([#133](https://github.com/MetaMask/internal-snaps/pull/133))
18+
- This should make account creation faster.
19+
1520
## [5.0.1]
1621

1722
### Fixed

packages/solana-wallet-snap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"devDependencies": {
5454
"@jest/globals": "^29.5.0",
5555
"@metamask/auto-changelog": "^6.1.1",
56-
"@metamask/key-tree": "9.1.2",
56+
"@metamask/key-tree": "^10.1.1",
5757
"@metamask/keyring-api": "^23.7.0",
5858
"@metamask/keyring-snap-sdk": "^9.2.1",
5959
"@metamask/snaps-cli": "^8.4.1",

packages/solana-wallet-snap/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/internal-snaps.git"
88
},
99
"source": {
10-
"shasum": "EmQFdIbfOGX+rDDiptegsY6jedzz3nsP2O/st22VoDo=",
10+
"shasum": "DgyBv5EPBAdrofMhGZPNViHmj/LWVPLw6q25d8fY9jw=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/solana-wallet-snap/src/polyfills/ed25519/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import { isEd25519Algorithm } from './utils/is-ed25519-algorithm';
1818
export function install() {
1919
const { subtle } = globalThis.crypto;
2020

21+
const originalGenerateKey = subtle.generateKey.bind(subtle);
22+
const originalExportKey = subtle.exportKey.bind(subtle);
23+
const originalSign = subtle.sign.bind(subtle);
24+
const originalVerify = subtle.verify.bind(subtle);
25+
const originalImportKey = subtle.importKey.bind(subtle);
26+
2127
Object.defineProperty(globalThis, 'isSecureContext', {
2228
value: true,
2329
writable: true,
@@ -32,7 +38,7 @@ export function install() {
3238
const algorithm = args[0];
3339

3440
if (!isEd25519Algorithm(algorithm)) {
35-
return await globalThis.crypto.subtle.generateKey(...args);
41+
return await originalGenerateKey(...args);
3642
}
3743

3844
return await generateKeyPolyfill(...args);
@@ -49,7 +55,7 @@ export function install() {
4955
const key = args[1];
5056

5157
if (!isEd25519Algorithm(key.algorithm)) {
52-
return await globalThis.crypto.subtle.exportKey(...args);
58+
return await originalExportKey(...args);
5359
}
5460

5561
return await exportKeyPolyfill(...args);
@@ -69,7 +75,7 @@ export function install() {
6975
!isEd25519Algorithm(algorithm) ||
7076
!isEd25519Algorithm(key.algorithm)
7177
) {
72-
return await globalThis.crypto.subtle.sign(...args);
78+
return await originalSign(...args);
7379
}
7480

7581
return await signPolyfill(...args);
@@ -89,7 +95,7 @@ export function install() {
8995
!isEd25519Algorithm(algorithm) ||
9096
!isEd25519Algorithm(key.algorithm)
9197
) {
92-
return await globalThis.crypto.subtle.verify(...args);
98+
return await originalVerify(...args);
9399
}
94100

95101
return await verifyPolyfill(...args);
@@ -106,7 +112,7 @@ export function install() {
106112
const algorithm = args[2];
107113

108114
if (!isEd25519Algorithm(algorithm)) {
109-
return await globalThis.crypto.subtle.importKey(...args);
115+
return await originalImportKey(...args);
110116
}
111117

112118
return await importKeyPolyfill(...args);

yarn.config.cjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ const { inspect } = require('util');
2323
* Only intended as temporary measures to faciliate upgrades and releases.
2424
* This should trend towards empty.
2525
*/
26-
const ALLOWED_INCONSISTENT_DEPENDENCIES = {
27-
// `@metamask/solana-wallet-snap` remains on 9.1.2 because 10.1.1 changes
28-
// key-tree behavior that the Snap currently relies on.
29-
'@metamask/key-tree': ['9.1.2', '^10.1.1'],
30-
};
26+
const ALLOWED_INCONSISTENT_DEPENDENCIES = {};
3127

3228
/**
3329
* These packages are allowed as peer dependencies without requiring installation as

yarn.lock

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,19 +2758,6 @@ __metadata:
27582758
languageName: node
27592759
linkType: hard
27602760

2761-
"@metamask/key-tree@npm:9.1.2":
2762-
version: 9.1.2
2763-
resolution: "@metamask/key-tree@npm:9.1.2"
2764-
dependencies:
2765-
"@metamask/scure-bip39": "npm:^2.1.1"
2766-
"@metamask/utils": "npm:^9.0.0"
2767-
"@noble/curves": "npm:^1.2.0"
2768-
"@noble/hashes": "npm:^1.3.2"
2769-
"@scure/base": "npm:^1.0.0"
2770-
checksum: 10/9b178a4156b2f36bf630564dd0530c41c6356492971d2bcc8f979c79c81144945823a5b770e4097e12b89b42133b81f00c95a7b8fe9931ea1dd928989ee3c406
2771-
languageName: node
2772-
linkType: hard
2773-
27742761
"@metamask/key-tree@npm:^10.0.2, @metamask/key-tree@npm:^10.1.1":
27752762
version: 10.1.1
27762763
resolution: "@metamask/key-tree@npm:10.1.1"
@@ -3678,7 +3665,7 @@ __metadata:
36783665
dependencies:
36793666
"@jest/globals": "npm:^29.5.0"
36803667
"@metamask/auto-changelog": "npm:^6.1.1"
3681-
"@metamask/key-tree": "npm:9.1.2"
3668+
"@metamask/key-tree": "npm:^10.1.1"
36823669
"@metamask/keyring-api": "npm:^23.7.0"
36833670
"@metamask/keyring-snap-sdk": "npm:^9.2.1"
36843671
"@metamask/snaps-cli": "npm:^8.4.1"

0 commit comments

Comments
 (0)