@metamask-previews/eth-json-rpc-middleware 21.0.0-preview-1f85e85a → 21.0.0-preview-536ed51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/dist/block-cache.cjs +41 -32
- package/dist/block-cache.cjs.map +1 -1
- package/dist/block-cache.d.cts +12 -3
- package/dist/block-cache.d.cts.map +1 -1
- package/dist/block-cache.d.mts +12 -3
- package/dist/block-cache.d.mts.map +1 -1
- package/dist/block-cache.mjs +41 -32
- package/dist/block-cache.mjs.map +1 -1
- package/dist/block-ref-rewrite.cjs +12 -8
- package/dist/block-ref-rewrite.cjs.map +1 -1
- package/dist/block-ref-rewrite.d.cts +3 -3
- package/dist/block-ref-rewrite.d.cts.map +1 -1
- package/dist/block-ref-rewrite.d.mts +3 -3
- package/dist/block-ref-rewrite.d.mts.map +1 -1
- package/dist/block-ref-rewrite.mjs +12 -8
- package/dist/block-ref-rewrite.mjs.map +1 -1
- package/dist/block-ref.cjs +17 -10
- package/dist/block-ref.cjs.map +1 -1
- package/dist/block-ref.d.cts +12 -3
- package/dist/block-ref.d.cts.map +1 -1
- package/dist/block-ref.d.mts +12 -3
- package/dist/block-ref.d.mts.map +1 -1
- package/dist/block-ref.mjs +17 -10
- package/dist/block-ref.mjs.map +1 -1
- package/dist/block-tracker-inspector.cjs +28 -25
- package/dist/block-tracker-inspector.cjs.map +1 -1
- package/dist/block-tracker-inspector.d.cts +3 -3
- package/dist/block-tracker-inspector.d.cts.map +1 -1
- package/dist/block-tracker-inspector.d.mts +3 -3
- package/dist/block-tracker-inspector.d.mts.map +1 -1
- package/dist/block-tracker-inspector.mjs +28 -25
- package/dist/block-tracker-inspector.mjs.map +1 -1
- package/dist/fetch.cjs +8 -12
- package/dist/fetch.cjs.map +1 -1
- package/dist/fetch.d.cts +5 -3
- package/dist/fetch.d.cts.map +1 -1
- package/dist/fetch.d.mts +5 -3
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +8 -12
- package/dist/fetch.mjs.map +1 -1
- package/dist/inflight-cache.cjs +61 -45
- package/dist/inflight-cache.cjs.map +1 -1
- package/dist/inflight-cache.d.cts +12 -3
- package/dist/inflight-cache.d.cts.map +1 -1
- package/dist/inflight-cache.d.mts +12 -3
- package/dist/inflight-cache.d.mts.map +1 -1
- package/dist/inflight-cache.mjs +61 -45
- package/dist/inflight-cache.mjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.cjs +20 -9
- package/dist/methods/wallet-request-execution-permissions.cjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.cts +14 -3
- package/dist/methods/wallet-request-execution-permissions.d.cts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.mts +14 -3
- package/dist/methods/wallet-request-execution-permissions.d.mts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.mjs +18 -7
- package/dist/methods/wallet-request-execution-permissions.mjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.cjs +23 -11
- package/dist/methods/wallet-revoke-execution-permission.cjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.cts +19 -5
- package/dist/methods/wallet-revoke-execution-permission.d.cts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.mts +19 -5
- package/dist/methods/wallet-revoke-execution-permission.d.mts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.mjs +20 -8
- package/dist/methods/wallet-revoke-execution-permission.mjs.map +1 -1
- package/dist/providerAsMiddleware.cjs +18 -1
- package/dist/providerAsMiddleware.cjs.map +1 -1
- package/dist/providerAsMiddleware.d.cts +18 -3
- package/dist/providerAsMiddleware.d.cts.map +1 -1
- package/dist/providerAsMiddleware.d.mts +18 -3
- package/dist/providerAsMiddleware.d.mts.map +1 -1
- package/dist/providerAsMiddleware.mjs +16 -0
- package/dist/providerAsMiddleware.mjs.map +1 -1
- package/dist/retryOnEmpty.cjs +8 -11
- package/dist/retryOnEmpty.cjs.map +1 -1
- package/dist/retryOnEmpty.d.cts +3 -3
- package/dist/retryOnEmpty.d.cts.map +1 -1
- package/dist/retryOnEmpty.d.mts +3 -3
- package/dist/retryOnEmpty.d.mts.map +1 -1
- package/dist/retryOnEmpty.mjs +8 -11
- package/dist/retryOnEmpty.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +0 -5
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +0 -5
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/error.cjs +7 -0
- package/dist/utils/error.cjs.map +1 -1
- package/dist/utils/error.d.cts +7 -0
- package/dist/utils/error.d.cts.map +1 -1
- package/dist/utils/error.d.mts +7 -0
- package/dist/utils/error.d.mts.map +1 -1
- package/dist/utils/error.mjs +7 -0
- package/dist/utils/error.mjs.map +1 -1
- package/dist/utils/validation.cjs +34 -2
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +29 -3
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +29 -3
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +34 -2
- package/dist/utils/validation.mjs.map +1 -1
- package/dist/wallet.cjs +199 -92
- package/dist/wallet.cjs.map +1 -1
- package/dist/wallet.d.cts +35 -8
- package/dist/wallet.d.cts.map +1 -1
- package/dist/wallet.d.mts +35 -8
- package/dist/wallet.d.mts.map +1 -1
- package/dist/wallet.mjs +200 -93
- package/dist/wallet.mjs.map +1 -1
- package/package.json +2 -1
package/dist/wallet.cjs
CHANGED
|
@@ -25,7 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.createWalletMiddleware = void 0;
|
|
27
27
|
const sigUtil = __importStar(require("@metamask/eth-sig-util"));
|
|
28
|
-
const
|
|
28
|
+
const v2_1 = require("@metamask/json-rpc-engine/v2");
|
|
29
29
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
30
30
|
const utils_1 = require("@metamask/utils");
|
|
31
31
|
const wallet_request_execution_permissions_1 = require("./methods/wallet-request-execution-permissions.cjs");
|
|
@@ -33,93 +33,150 @@ const wallet_revoke_execution_permission_1 = require("./methods/wallet-revoke-ex
|
|
|
33
33
|
const common_1 = require("./utils/common.cjs");
|
|
34
34
|
const normalize_1 = require("./utils/normalize.cjs");
|
|
35
35
|
const validation_1 = require("./utils/validation.cjs");
|
|
36
|
+
/**
|
|
37
|
+
* Creates a JSON-RPC middleware that handles "wallet"-related JSON-RPC methods.
|
|
38
|
+
* "Wallet" may have had a specific meaning at some point in the distant past,
|
|
39
|
+
* but at this point it's just an arbitrary label.
|
|
40
|
+
*
|
|
41
|
+
* @param options - The options for the middleware.
|
|
42
|
+
* @param options.getAccounts - The function to get the accounts for the origin.
|
|
43
|
+
* @param options.processDecryptMessage - The function to process the decrypt message request.
|
|
44
|
+
* @param options.processEncryptionPublicKey - The function to process the encryption public key request.
|
|
45
|
+
* @param options.processPersonalMessage - The function to process the personal message request.
|
|
46
|
+
* @param options.processTransaction - The function to process the transaction request.
|
|
47
|
+
* @param options.processSignTransaction - The function to process the sign transaction request.
|
|
48
|
+
* @param options.processTypedMessage - The function to process the typed message request.
|
|
49
|
+
* @param options.processTypedMessageV3 - The function to process the typed message v3 request.
|
|
50
|
+
* @param options.processTypedMessageV4 - The function to process the typed message v4 request.
|
|
51
|
+
* @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.
|
|
52
|
+
* @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.
|
|
53
|
+
* @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.
|
|
54
|
+
*/
|
|
36
55
|
function createWalletMiddleware({ getAccounts, processDecryptMessage, processEncryptionPublicKey, processPersonalMessage, processTransaction, processSignTransaction, processTypedMessage, processTypedMessageV3, processTypedMessageV4, processRequestExecutionPermissions, processRevokeExecutionPermission, }) {
|
|
37
56
|
if (!getAccounts) {
|
|
38
57
|
throw new Error('opts.getAccounts is required');
|
|
39
58
|
}
|
|
40
|
-
return (0,
|
|
59
|
+
return (0, v2_1.createScaffoldMiddleware)({
|
|
41
60
|
// account lookups
|
|
42
|
-
eth_accounts:
|
|
43
|
-
eth_coinbase:
|
|
61
|
+
eth_accounts: lookupAccounts,
|
|
62
|
+
eth_coinbase: lookupDefaultAccount,
|
|
44
63
|
// tx signatures
|
|
45
|
-
eth_sendTransaction:
|
|
46
|
-
eth_signTransaction:
|
|
64
|
+
eth_sendTransaction: sendTransaction,
|
|
65
|
+
eth_signTransaction: signTransaction,
|
|
47
66
|
// message signatures
|
|
48
|
-
eth_signTypedData:
|
|
49
|
-
eth_signTypedData_v3:
|
|
50
|
-
eth_signTypedData_v4:
|
|
51
|
-
personal_sign:
|
|
52
|
-
eth_getEncryptionPublicKey:
|
|
53
|
-
eth_decrypt:
|
|
54
|
-
personal_ecRecover:
|
|
67
|
+
eth_signTypedData: signTypedData,
|
|
68
|
+
eth_signTypedData_v3: signTypedDataV3,
|
|
69
|
+
eth_signTypedData_v4: signTypedDataV4,
|
|
70
|
+
personal_sign: personalSign,
|
|
71
|
+
eth_getEncryptionPublicKey: encryptionPublicKey,
|
|
72
|
+
eth_decrypt: decryptMessage,
|
|
73
|
+
personal_ecRecover: personalRecover,
|
|
55
74
|
// EIP-7715
|
|
56
|
-
wallet_requestExecutionPermissions: (0,
|
|
75
|
+
wallet_requestExecutionPermissions: (0, wallet_request_execution_permissions_1.createWalletRequestExecutionPermissionsHandler)({
|
|
57
76
|
processRequestExecutionPermissions,
|
|
58
|
-
})
|
|
59
|
-
wallet_revokeExecutionPermission: (0,
|
|
77
|
+
}),
|
|
78
|
+
wallet_revokeExecutionPermission: (0, wallet_revoke_execution_permission_1.createWalletRevokeExecutionPermissionHandler)({
|
|
60
79
|
processRevokeExecutionPermission,
|
|
61
|
-
})
|
|
80
|
+
}),
|
|
62
81
|
});
|
|
63
82
|
//
|
|
64
83
|
// account lookups
|
|
65
84
|
//
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Gets the accounts for the origin.
|
|
87
|
+
*
|
|
88
|
+
* @param options - Options bag.
|
|
89
|
+
* @param options.context - The context of the request.
|
|
90
|
+
* @returns The accounts for the origin.
|
|
91
|
+
*/
|
|
92
|
+
async function lookupAccounts({ context, }) {
|
|
93
|
+
return await getAccounts(context.assertGet('origin'));
|
|
68
94
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Gets the default account (i.e. first in the list) for the origin.
|
|
97
|
+
*
|
|
98
|
+
* @param options - Options bag.
|
|
99
|
+
* @param options.context - The context of the request.
|
|
100
|
+
* @returns The default account for the origin.
|
|
101
|
+
*/
|
|
102
|
+
async function lookupDefaultAccount({ context, }) {
|
|
103
|
+
const accounts = await getAccounts(context.assertGet('origin'));
|
|
104
|
+
return accounts[0] || null;
|
|
72
105
|
}
|
|
73
106
|
//
|
|
74
107
|
// transaction signatures
|
|
75
108
|
//
|
|
76
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Sends a transaction.
|
|
111
|
+
*
|
|
112
|
+
* @param options - Options bag.
|
|
113
|
+
* @param options.request - The request.
|
|
114
|
+
* @param options.context - The context of the request.
|
|
115
|
+
* @returns The transaction hash.
|
|
116
|
+
*/
|
|
117
|
+
async function sendTransaction({ request, context, }) {
|
|
77
118
|
if (!processTransaction) {
|
|
78
119
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
79
120
|
}
|
|
80
|
-
if (!
|
|
81
|
-
!Array.isArray(
|
|
82
|
-
!(
|
|
121
|
+
if (!request.params ||
|
|
122
|
+
!Array.isArray(request.params) ||
|
|
123
|
+
!(request.params.length >= 1)) {
|
|
83
124
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
84
125
|
}
|
|
85
|
-
const params =
|
|
126
|
+
const params = request.params[0];
|
|
86
127
|
const txParams = {
|
|
87
128
|
...params,
|
|
88
|
-
from: await validateAndNormalizeKeyholder(params?.from || '',
|
|
129
|
+
from: await validateAndNormalizeKeyholder(params?.from || '', context),
|
|
89
130
|
};
|
|
90
|
-
|
|
131
|
+
return await processTransaction(txParams, request, context);
|
|
91
132
|
}
|
|
92
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Signs a transaction.
|
|
135
|
+
*
|
|
136
|
+
* @param options - Options bag.
|
|
137
|
+
* @param options.request - The request.
|
|
138
|
+
* @param options.context - The context of the request.
|
|
139
|
+
* @returns The signed transaction.
|
|
140
|
+
*/
|
|
141
|
+
async function signTransaction({ request, context, }) {
|
|
93
142
|
if (!processSignTransaction) {
|
|
94
143
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
95
144
|
}
|
|
96
|
-
if (!
|
|
97
|
-
!Array.isArray(
|
|
98
|
-
!(
|
|
145
|
+
if (!request.params ||
|
|
146
|
+
!Array.isArray(request.params) ||
|
|
147
|
+
!(request.params.length >= 1)) {
|
|
99
148
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
100
149
|
}
|
|
101
|
-
const params =
|
|
150
|
+
const params = request.params[0];
|
|
102
151
|
const txParams = {
|
|
103
152
|
...params,
|
|
104
|
-
from: await validateAndNormalizeKeyholder(params?.from || '',
|
|
153
|
+
from: await validateAndNormalizeKeyholder(params?.from || '', context),
|
|
105
154
|
};
|
|
106
|
-
|
|
155
|
+
return await processSignTransaction(txParams, request);
|
|
107
156
|
}
|
|
108
157
|
//
|
|
109
158
|
// message signatures
|
|
110
159
|
//
|
|
111
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Signs a `eth_signTypedData` message.
|
|
162
|
+
*
|
|
163
|
+
* @param options - Options bag.
|
|
164
|
+
* @param options.request - The request.
|
|
165
|
+
* @param options.context - The context of the request.
|
|
166
|
+
* @returns The signed message.
|
|
167
|
+
*/
|
|
168
|
+
async function signTypedData({ request, context, }) {
|
|
112
169
|
if (!processTypedMessage) {
|
|
113
170
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
114
171
|
}
|
|
115
|
-
if (!
|
|
116
|
-
!Array.isArray(
|
|
117
|
-
!(
|
|
172
|
+
if (!request.params ||
|
|
173
|
+
!Array.isArray(request.params) ||
|
|
174
|
+
!(request.params.length >= 2)) {
|
|
118
175
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
119
176
|
}
|
|
120
|
-
const params =
|
|
177
|
+
const params = request.params;
|
|
121
178
|
const message = params[0];
|
|
122
|
-
const address = await validateAndNormalizeKeyholder(params[1],
|
|
179
|
+
const address = await validateAndNormalizeKeyholder(params[1], context);
|
|
123
180
|
const version = 'V1';
|
|
124
181
|
const extraParams = params[2] || {};
|
|
125
182
|
const msgParams = {
|
|
@@ -129,19 +186,27 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
129
186
|
signatureMethod: 'eth_signTypedData',
|
|
130
187
|
version,
|
|
131
188
|
};
|
|
132
|
-
|
|
189
|
+
return await processTypedMessage(msgParams, request, version);
|
|
133
190
|
}
|
|
134
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Signs a `eth_signTypedData_v3` message.
|
|
193
|
+
*
|
|
194
|
+
* @param options - Options bag.
|
|
195
|
+
* @param options.request - The request.
|
|
196
|
+
* @param options.context - The context of the request.
|
|
197
|
+
* @returns The signed message.
|
|
198
|
+
*/
|
|
199
|
+
async function signTypedDataV3({ request, context, }) {
|
|
135
200
|
if (!processTypedMessageV3) {
|
|
136
201
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
137
202
|
}
|
|
138
|
-
if (!
|
|
139
|
-
!Array.isArray(
|
|
140
|
-
!(
|
|
203
|
+
if (!request.params ||
|
|
204
|
+
!Array.isArray(request.params) ||
|
|
205
|
+
!(request.params.length >= 2)) {
|
|
141
206
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
142
207
|
}
|
|
143
|
-
const params =
|
|
144
|
-
const address = await validateAndNormalizeKeyholder(params[0],
|
|
208
|
+
const params = request.params;
|
|
209
|
+
const address = await validateAndNormalizeKeyholder(params[0], context);
|
|
145
210
|
const message = (0, normalize_1.normalizeTypedMessage)(params[1]);
|
|
146
211
|
validatePrimaryType(message);
|
|
147
212
|
validateVerifyingContract(message);
|
|
@@ -152,19 +217,27 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
152
217
|
version,
|
|
153
218
|
signatureMethod: 'eth_signTypedData_v3',
|
|
154
219
|
};
|
|
155
|
-
|
|
220
|
+
return await processTypedMessageV3(msgParams, request, version);
|
|
156
221
|
}
|
|
157
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Signs a `eth_signTypedData_v4` message.
|
|
224
|
+
*
|
|
225
|
+
* @param options - Options bag.
|
|
226
|
+
* @param options.request - The request.
|
|
227
|
+
* @param options.context - The context of the request.
|
|
228
|
+
* @returns The signed message.
|
|
229
|
+
*/
|
|
230
|
+
async function signTypedDataV4({ request, context, }) {
|
|
158
231
|
if (!processTypedMessageV4) {
|
|
159
232
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
160
233
|
}
|
|
161
|
-
if (!
|
|
162
|
-
!Array.isArray(
|
|
163
|
-
!(
|
|
234
|
+
if (!request.params ||
|
|
235
|
+
!Array.isArray(request.params) ||
|
|
236
|
+
!(request.params.length >= 2)) {
|
|
164
237
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
165
238
|
}
|
|
166
|
-
const params =
|
|
167
|
-
const address = await validateAndNormalizeKeyholder(params[0],
|
|
239
|
+
const params = request.params;
|
|
240
|
+
const address = await validateAndNormalizeKeyholder(params[0], context);
|
|
168
241
|
const message = (0, normalize_1.normalizeTypedMessage)(params[1]);
|
|
169
242
|
validatePrimaryType(message);
|
|
170
243
|
validateVerifyingContract(message);
|
|
@@ -175,18 +248,26 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
175
248
|
version,
|
|
176
249
|
signatureMethod: 'eth_signTypedData_v4',
|
|
177
250
|
};
|
|
178
|
-
|
|
251
|
+
return await processTypedMessageV4(msgParams, request, version);
|
|
179
252
|
}
|
|
180
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Signs a `personal_sign` message.
|
|
255
|
+
*
|
|
256
|
+
* @param options - Options bag.
|
|
257
|
+
* @param options.request - The request.
|
|
258
|
+
* @param options.context - The context of the request.
|
|
259
|
+
* @returns The signed message.
|
|
260
|
+
*/
|
|
261
|
+
async function personalSign({ request, context, }) {
|
|
181
262
|
if (!processPersonalMessage) {
|
|
182
263
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
183
264
|
}
|
|
184
|
-
if (!
|
|
185
|
-
!Array.isArray(
|
|
186
|
-
!(
|
|
265
|
+
if (!request.params ||
|
|
266
|
+
!Array.isArray(request.params) ||
|
|
267
|
+
!(request.params.length >= 2)) {
|
|
187
268
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
188
269
|
}
|
|
189
|
-
const params =
|
|
270
|
+
const params = request.params;
|
|
190
271
|
// process normally
|
|
191
272
|
const firstParam = params[0];
|
|
192
273
|
const secondParam = params[1];
|
|
@@ -201,11 +282,6 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
201
282
|
// and the second param is definitely not, but is hex.
|
|
202
283
|
let address, message;
|
|
203
284
|
if ((0, validation_1.resemblesAddress)(firstParam) && !(0, validation_1.resemblesAddress)(secondParam)) {
|
|
204
|
-
let warning = `The eth_personalSign method requires params ordered `;
|
|
205
|
-
warning += `[message, address]. This was previously handled incorrectly, `;
|
|
206
|
-
warning += `and has been corrected automatically. `;
|
|
207
|
-
warning += `Please switch this param order for smooth behavior in the future.`;
|
|
208
|
-
res.warning = warning;
|
|
209
285
|
address = firstParam;
|
|
210
286
|
message = secondParam;
|
|
211
287
|
}
|
|
@@ -213,62 +289,93 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
213
289
|
message = firstParam;
|
|
214
290
|
address = secondParam;
|
|
215
291
|
}
|
|
216
|
-
address = await validateAndNormalizeKeyholder(address,
|
|
292
|
+
address = await validateAndNormalizeKeyholder(address, context);
|
|
217
293
|
const msgParams = {
|
|
218
294
|
...extraParams,
|
|
219
295
|
from: address,
|
|
220
296
|
data: message,
|
|
221
297
|
signatureMethod: 'personal_sign',
|
|
222
298
|
};
|
|
223
|
-
|
|
299
|
+
return await processPersonalMessage(msgParams, request);
|
|
224
300
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Recovers the signer address from a `personal_sign` message.
|
|
303
|
+
*
|
|
304
|
+
* @param options - Options bag.
|
|
305
|
+
* @param options.request - The request.
|
|
306
|
+
* @returns The recovered signer address.
|
|
307
|
+
*/
|
|
308
|
+
async function personalRecover({ request, }) {
|
|
309
|
+
if (!request.params ||
|
|
310
|
+
!Array.isArray(request.params) ||
|
|
311
|
+
!(request.params.length >= 2)) {
|
|
229
312
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
230
313
|
}
|
|
231
|
-
const params =
|
|
314
|
+
const params = request.params;
|
|
232
315
|
const message = params[0];
|
|
233
316
|
const signature = params[1];
|
|
234
317
|
const signerAddress = sigUtil.recoverPersonalSignature({
|
|
235
318
|
data: message,
|
|
236
319
|
signature,
|
|
237
320
|
});
|
|
238
|
-
|
|
321
|
+
return signerAddress;
|
|
239
322
|
}
|
|
240
|
-
|
|
323
|
+
/**
|
|
324
|
+
* Gets the encryption public key for an address.
|
|
325
|
+
*
|
|
326
|
+
* @param options - Options bag.
|
|
327
|
+
* @param options.request - The request.
|
|
328
|
+
* @param options.context - The context of the request.
|
|
329
|
+
* @returns The encryption public key.
|
|
330
|
+
*/
|
|
331
|
+
async function encryptionPublicKey({ request, context, }) {
|
|
241
332
|
if (!processEncryptionPublicKey) {
|
|
242
333
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
243
334
|
}
|
|
244
|
-
if (!
|
|
245
|
-
!Array.isArray(
|
|
246
|
-
!(
|
|
335
|
+
if (!request.params ||
|
|
336
|
+
!Array.isArray(request.params) ||
|
|
337
|
+
!(request.params.length >= 1)) {
|
|
247
338
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
248
339
|
}
|
|
249
|
-
const params =
|
|
250
|
-
const address = await validateAndNormalizeKeyholder(params[0],
|
|
251
|
-
|
|
340
|
+
const params = request.params;
|
|
341
|
+
const address = await validateAndNormalizeKeyholder(params[0], context);
|
|
342
|
+
return await processEncryptionPublicKey(address, {
|
|
343
|
+
id: request.id,
|
|
344
|
+
origin: context.assertGet('origin'),
|
|
345
|
+
securityAlertResponse: context.assertGet('securityAlertResponse'),
|
|
346
|
+
});
|
|
252
347
|
}
|
|
253
|
-
|
|
348
|
+
/**
|
|
349
|
+
* Decrypts a message.
|
|
350
|
+
*
|
|
351
|
+
* @param options - Options bag.
|
|
352
|
+
* @param options.request - The request.
|
|
353
|
+
* @param options.context - The context of the request.
|
|
354
|
+
* @returns The decrypted message.
|
|
355
|
+
*/
|
|
356
|
+
async function decryptMessage({ request, context, }) {
|
|
254
357
|
if (!processDecryptMessage) {
|
|
255
358
|
throw rpc_errors_1.rpcErrors.methodNotSupported();
|
|
256
359
|
}
|
|
257
|
-
if (!
|
|
258
|
-
!Array.isArray(
|
|
259
|
-
!(
|
|
360
|
+
if (!request.params ||
|
|
361
|
+
!Array.isArray(request.params) ||
|
|
362
|
+
!(request.params.length >= 1)) {
|
|
260
363
|
throw rpc_errors_1.rpcErrors.invalidInput();
|
|
261
364
|
}
|
|
262
|
-
const params =
|
|
365
|
+
const params = request.params;
|
|
263
366
|
const ciphertext = params[0];
|
|
264
|
-
const address = await validateAndNormalizeKeyholder(params[1],
|
|
367
|
+
const address = await validateAndNormalizeKeyholder(params[1], context);
|
|
265
368
|
const extraParams = params[2] || {};
|
|
266
369
|
const msgParams = {
|
|
267
370
|
...extraParams,
|
|
268
371
|
from: address,
|
|
269
372
|
data: ciphertext,
|
|
270
373
|
};
|
|
271
|
-
|
|
374
|
+
return await processDecryptMessage(msgParams, {
|
|
375
|
+
id: request.id,
|
|
376
|
+
origin: context.assertGet('origin'),
|
|
377
|
+
securityAlertResponse: context.assertGet('securityAlertResponse'),
|
|
378
|
+
});
|
|
272
379
|
}
|
|
273
380
|
//
|
|
274
381
|
// utility
|
|
@@ -278,12 +385,12 @@ function createWalletMiddleware({ getAccounts, processDecryptMessage, processEnc
|
|
|
278
385
|
* copy of it.
|
|
279
386
|
*
|
|
280
387
|
* @param address - The address to validate and normalize.
|
|
281
|
-
* @param
|
|
388
|
+
* @param context - The context of the request.
|
|
282
389
|
* @returns The normalized address, if valid. Otherwise, throws
|
|
283
390
|
* an error
|
|
284
391
|
*/
|
|
285
|
-
async function validateAndNormalizeKeyholder(address,
|
|
286
|
-
return (0, validation_1.validateAndNormalizeKeyholder)(address,
|
|
392
|
+
async function validateAndNormalizeKeyholder(address, context) {
|
|
393
|
+
return (0, validation_1.validateAndNormalizeKeyholder)(address, context, { getAccounts });
|
|
287
394
|
}
|
|
288
395
|
}
|
|
289
396
|
exports.createWalletMiddleware = createWalletMiddleware;
|
package/dist/wallet.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet.cjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAAkD;AAElD,+DAGmC;AACnC,qDAAiD;AACjD,2CAAoD;AAQpD,6GAGwD;AACxD,yGAGsD;AAEtD,+CAAyD;AACzD,qDAA6E;AAC7E,uDAG4B;AAyE5B,SAAgB,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,kCAAkC,EAClC,gCAAgC,GAEX;IACrB,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;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AAC,IAAA,yBAAiB,EAAC,iBAAiB,CAAC,EACrC;QACA,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;KAChC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport {\n createAsyncMiddleware,\n createScaffoldMiddleware,\n} from '@metamask/json-rpc-engine';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type {\n JsonRpcRequest,\n PendingJsonRpcResponse,\n Json,\n Hex,\n} from '@metamask/utils';\n\nimport {\n type ProcessRequestExecutionPermissionsHook,\n walletRequestExecutionPermissions,\n} from './methods/wallet-request-execution-permissions';\nimport {\n type ProcessRevokeExecutionPermissionHook,\n walletRevokeExecutionPermission,\n} from './methods/wallet-revoke-execution-permission';\nimport type { Block } from './types';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\n/*\nexport type TransactionParams = {\n [prop: string]: Json;\n from: string;\n}\n*/\n\n/*\nexport type TransactionParams = JsonRpcParams & {\n from: string;\n}\n*/\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (req: JsonRpcRequest) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: // }: WalletMiddlewareOptions): JsonRpcMiddleware<string, Block> {\nWalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware({\n // account lookups\n eth_accounts: createAsyncMiddleware(lookupAccounts),\n eth_coinbase: createAsyncMiddleware(lookupDefaultAccount),\n\n // tx signatures\n eth_sendTransaction: createAsyncMiddleware(sendTransaction),\n eth_signTransaction: createAsyncMiddleware(signTransaction),\n\n // message signatures\n eth_signTypedData: createAsyncMiddleware(signTypedData),\n eth_signTypedData_v3: createAsyncMiddleware(signTypedDataV3),\n eth_signTypedData_v4: createAsyncMiddleware(signTypedDataV4),\n personal_sign: createAsyncMiddleware(personalSign),\n eth_getEncryptionPublicKey: createAsyncMiddleware(encryptionPublicKey),\n eth_decrypt: createAsyncMiddleware(decryptMessage),\n personal_ecRecover: createAsyncMiddleware(personalRecover),\n\n // EIP-7715\n wallet_requestExecutionPermissions: createAsyncMiddleware(\n async (req, res) =>\n walletRequestExecutionPermissions(req, res, {\n processRequestExecutionPermissions,\n }),\n ),\n wallet_revokeExecutionPermission: createAsyncMiddleware(async (req, res) =>\n walletRevokeExecutionPermission(req, res, {\n processRevokeExecutionPermission,\n }),\n ),\n });\n\n //\n // account lookups\n //\n\n async function lookupAccounts(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n res.result = await getAccounts(req);\n }\n\n async function lookupDefaultAccount(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n const accounts = await getAccounts(req);\n res.result = accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n async function sendTransaction(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', req),\n };\n res.result = await processTransaction(txParams, req);\n }\n\n async function signTransaction(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', req),\n };\n res.result = await processSignTransaction(txParams, req);\n }\n\n //\n // message signatures\n //\n async function signTypedData(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], req);\n const version = 'V1';\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n res.result = await processTypedMessage(msgParams, req, version);\n }\n\n async function signTypedDataV3(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], req);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n res.result = await processTypedMessageV3(msgParams, req, version);\n }\n\n async function signTypedDataV4(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], req);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n res.result = await processTypedMessageV4(msgParams, req, version);\n }\n\n async function personalSign(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n let warning = `The eth_personalSign method requires params ordered `;\n warning += `[message, address]. This was previously handled incorrectly, `;\n warning += `and has been corrected automatically. `;\n warning += `Please switch this param order for smooth behavior in the future.`;\n (res as any).warning = warning;\n\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, req);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n res.result = await processPersonalMessage(msgParams, req);\n }\n\n async function personalRecover(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n res.result = signerAddress;\n }\n\n async function encryptionPublicKey(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = req.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], req);\n\n res.result = await processEncryptionPublicKey(address, req);\n }\n\n async function decryptMessage(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n ): Promise<void> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !req?.params ||\n !Array.isArray(req.params) ||\n !(req.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = req.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(params[1], req);\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n res.result = await processDecryptMessage(msgParams, req);\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param req - The request object.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n req: JsonRpcRequest,\n ): Promise<string> {\n return validateKeyholder(address as Hex, req, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string) {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string) {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"wallet.cjs","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAAkD;AAMlD,qDAAwE;AAExE,qDAAiD;AACjD,2CAAoD;AAGpD,6GAGwD;AACxD,yGAGsD;AACtD,+CAAyD;AACzD,qDAA6E;AAC7E,uDAG4B;AA4E5B;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,sBAAsB,CAAC,EACrC,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,qBAAqB,EACrB,kCAAkC,EAClC,gCAAgC,GACR;IAKxB,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,OAAO,IAAA,6BAAwB,EAA0B;QACvD,kBAAkB;QAClB,YAAY,EAAE,cAAc;QAC5B,YAAY,EAAE,oBAAoB;QAElC,gBAAgB;QAChB,mBAAmB,EAAE,eAAe;QACpC,mBAAmB,EAAE,eAAe;QAEpC,qBAAqB;QACrB,iBAAiB,EAAE,aAAa;QAChC,oBAAoB,EAAE,eAAe;QACrC,oBAAoB,EAAE,eAAe;QACrC,aAAa,EAAE,YAAY;QAC3B,0BAA0B,EAAE,mBAAmB;QAC/C,WAAW,EAAE,cAAc;QAC3B,kBAAkB,EAAE,eAAe;QAEnC,WAAW;QACX,kCAAkC,EAChC,IAAA,qFAA8C,EAAC;YAC7C,kCAAkC;SACnC,CAAC;QACJ,gCAAgC,EAC9B,IAAA,iFAA4C,EAAC;YAC3C,gCAAgC;SACjC,CAAC;KACL,CAAC,CAAC;IAEH,EAAE;IACF,kBAAkB;IAClB,EAAE;IAEF;;;;;;OAMG;IACH,KAAK,UAAU,cAAc,CAAC,EAC5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CAAC;IAC5C,IAAI,CAAC,mBAAmB,EAAE;QACxB,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;KAChC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,yBAAyB,CAAC,IAAY;IAC7C,MAAM,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,GAAG,EAAE,EAAE,GAAG,IAAA,6BAAiB,EAAC,IAAI,CAAC,CAAC;IACvE,sEAAsE;IACtE,iEAAiE;IACjE,IACE,iBAAiB;QAChB,iBAA4B,KAAK,QAAQ;QAC1C,CAAC,IAAA,yBAAiB,EAAC,iBAAiB,CAAC,EACrC;QACA,MAAM,sBAAS,CAAC,YAAY,EAAE,CAAC;KAChC;AACH,CAAC","sourcesContent":["import * as sigUtil from '@metamask/eth-sig-util';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n MiddlewareParams,\n} from '@metamask/json-rpc-engine/v2';\nimport { createScaffoldMiddleware } from '@metamask/json-rpc-engine/v2';\nimport type { MessageRequest } from '@metamask/message-manager';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { isValidHexAddress } from '@metamask/utils';\nimport type { JsonRpcRequest, Json, Hex } from '@metamask/utils';\n\nimport {\n createWalletRequestExecutionPermissionsHandler,\n type ProcessRequestExecutionPermissionsHook,\n} from './methods/wallet-request-execution-permissions';\nimport {\n type ProcessRevokeExecutionPermissionHook,\n createWalletRevokeExecutionPermissionHandler,\n} from './methods/wallet-revoke-execution-permission';\nimport { stripArrayTypeIfPresent } from './utils/common';\nimport { normalizeTypedMessage, parseTypedMessage } from './utils/normalize';\nimport {\n resemblesAddress,\n validateAndNormalizeKeyholder as validateKeyholder,\n} from './utils/validation';\n\nexport type TransactionParams = {\n from: string;\n};\n\nexport type MessageParams = TransactionParams & {\n data: string;\n signatureMethod?: string;\n};\n\nexport type TypedMessageParams = MessageParams & {\n version: string;\n};\n\nexport type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {\n data: Record<string, unknown>[];\n};\n\nexport type WalletMiddlewareOptions = {\n getAccounts: (origin: string) => Promise<string[]>;\n processDecryptMessage?: (\n msgParams: MessageParams,\n req: MessageRequest,\n ) => Promise<string>;\n processEncryptionPublicKey?: (\n address: string,\n req: MessageRequest,\n ) => Promise<string>;\n processPersonalMessage?: (\n msgParams: MessageParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n context: WalletMiddlewareContext,\n ) => Promise<string>;\n processSignTransaction?: (\n txParams: TransactionParams,\n req: JsonRpcRequest,\n ) => Promise<string>;\n processTypedMessage?: (\n msgParams: TypedMessageV1Params,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processTypedMessageV3?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processTypedMessageV4?: (\n msgParams: TypedMessageParams,\n req: JsonRpcRequest,\n version: string,\n ) => Promise<string>;\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;\n};\n\nexport type WalletMiddlewareKeyValues = {\n networkClientId: string;\n origin: string;\n securityAlertResponse?: Record<string, Json>;\n traceContext?: unknown;\n};\n\nexport type WalletMiddlewareContext =\n MiddlewareContext<WalletMiddlewareKeyValues>;\n\nexport type WalletMiddlewareParams = MiddlewareParams<\n JsonRpcRequest,\n WalletMiddlewareContext\n>;\n\n/**\n * Creates a JSON-RPC middleware that handles \"wallet\"-related JSON-RPC methods.\n * \"Wallet\" may have had a specific meaning at some point in the distant past,\n * but at this point it's just an arbitrary label.\n *\n * @param options - The options for the middleware.\n * @param options.getAccounts - The function to get the accounts for the origin.\n * @param options.processDecryptMessage - The function to process the decrypt message request.\n * @param options.processEncryptionPublicKey - The function to process the encryption public key request.\n * @param options.processPersonalMessage - The function to process the personal message request.\n * @param options.processTransaction - The function to process the transaction request.\n * @param options.processSignTransaction - The function to process the sign transaction request.\n * @param options.processTypedMessage - The function to process the typed message request.\n * @param options.processTypedMessageV3 - The function to process the typed message v3 request.\n * @param options.processTypedMessageV4 - The function to process the typed message v4 request.\n * @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.\n * @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.\n * @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.\n */\nexport function createWalletMiddleware({\n getAccounts,\n processDecryptMessage,\n processEncryptionPublicKey,\n processPersonalMessage,\n processTransaction,\n processSignTransaction,\n processTypedMessage,\n processTypedMessageV3,\n processTypedMessageV4,\n processRequestExecutionPermissions,\n processRevokeExecutionPermission,\n}: WalletMiddlewareOptions): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n WalletMiddlewareContext\n> {\n if (!getAccounts) {\n throw new Error('opts.getAccounts is required');\n }\n\n return createScaffoldMiddleware<WalletMiddlewareContext>({\n // account lookups\n eth_accounts: lookupAccounts,\n eth_coinbase: lookupDefaultAccount,\n\n // tx signatures\n eth_sendTransaction: sendTransaction,\n eth_signTransaction: signTransaction,\n\n // message signatures\n eth_signTypedData: signTypedData,\n eth_signTypedData_v3: signTypedDataV3,\n eth_signTypedData_v4: signTypedDataV4,\n personal_sign: personalSign,\n eth_getEncryptionPublicKey: encryptionPublicKey,\n eth_decrypt: decryptMessage,\n personal_ecRecover: personalRecover,\n\n // EIP-7715\n wallet_requestExecutionPermissions:\n createWalletRequestExecutionPermissionsHandler({\n processRequestExecutionPermissions,\n }),\n wallet_revokeExecutionPermission:\n createWalletRevokeExecutionPermissionHandler({\n processRevokeExecutionPermission,\n }),\n });\n\n //\n // account lookups\n //\n\n /**\n * Gets the accounts for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The accounts for the origin.\n */\n async function lookupAccounts({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n return await getAccounts(context.assertGet('origin'));\n }\n\n /**\n * Gets the default account (i.e. first in the list) for the origin.\n *\n * @param options - Options bag.\n * @param options.context - The context of the request.\n * @returns The default account for the origin.\n */\n async function lookupDefaultAccount({\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n const accounts = await getAccounts(context.assertGet('origin'));\n return accounts[0] || null;\n }\n\n //\n // transaction signatures\n //\n\n /**\n * Sends a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The transaction hash.\n */\n async function sendTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processTransaction(txParams, request, context);\n }\n\n /**\n * Signs a transaction.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed transaction.\n */\n async function signTransaction({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processSignTransaction) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params[0] as TransactionParams | undefined;\n const txParams: TransactionParams = {\n ...params,\n from: await validateAndNormalizeKeyholder(params?.from || '', context),\n };\n return await processSignTransaction(txParams, request);\n }\n\n //\n // message signatures\n //\n\n /**\n * Signs a `eth_signTypedData` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedData({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [\n Record<string, unknown>[],\n string,\n Record<string, string>?,\n ];\n const message = params[0];\n const address = await validateAndNormalizeKeyholder(params[1], context);\n const version = 'V1';\n const extraParams = params[2] || {};\n const msgParams: TypedMessageV1Params = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'eth_signTypedData',\n version,\n };\n\n return await processTypedMessage(msgParams, request, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v3` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV3({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV3) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V3';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v3',\n };\n\n return await processTypedMessageV3(msgParams, request, version);\n }\n\n /**\n * Signs a `eth_signTypedData_v4` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function signTypedDataV4({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processTypedMessageV4) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n const message = normalizeTypedMessage(params[1]);\n validatePrimaryType(message);\n validateVerifyingContract(message);\n const version = 'V4';\n const msgParams: TypedMessageParams = {\n data: message,\n from: address,\n version,\n signatureMethod: 'eth_signTypedData_v4',\n };\n\n return await processTypedMessageV4(msgParams, request, version);\n }\n\n /**\n * Signs a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The signed message.\n */\n async function personalSign({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processPersonalMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string, TransactionParams?];\n\n // process normally\n const firstParam = params[0];\n const secondParam = params[1];\n // non-standard \"extraParams\" to be appended to our \"msgParams\" obj\n const extraParams = params[2] || {};\n\n // We initially incorrectly ordered these parameters.\n // To gracefully respect users who adopted this API early,\n // we are currently gracefully recovering from the wrong param order\n // when it is clearly identifiable.\n //\n // That means when the first param is definitely an address,\n // and the second param is definitely not, but is hex.\n let address: string, message: string;\n if (resemblesAddress(firstParam) && !resemblesAddress(secondParam)) {\n address = firstParam;\n message = secondParam;\n } else {\n message = firstParam;\n address = secondParam;\n }\n address = await validateAndNormalizeKeyholder(address, context);\n\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: message,\n signatureMethod: 'personal_sign',\n };\n\n return await processPersonalMessage(msgParams, request);\n }\n\n /**\n * Recovers the signer address from a `personal_sign` message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @returns The recovered signer address.\n */\n async function personalRecover({\n request,\n }: WalletMiddlewareParams): Promise<Json> {\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 2)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string, string];\n const message = params[0];\n const signature = params[1];\n const signerAddress = sigUtil.recoverPersonalSignature({\n data: message,\n signature,\n });\n\n return signerAddress;\n }\n\n /**\n * Gets the encryption public key for an address.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The encryption public key.\n */\n async function encryptionPublicKey({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processEncryptionPublicKey) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n\n const params = request.params as [string];\n\n const address = await validateAndNormalizeKeyholder(params[0], context);\n\n return await processEncryptionPublicKey(address, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.assertGet('securityAlertResponse'),\n });\n }\n\n /**\n * Decrypts a message.\n *\n * @param options - Options bag.\n * @param options.request - The request.\n * @param options.context - The context of the request.\n * @returns The decrypted message.\n */\n async function decryptMessage({\n request,\n context,\n }: WalletMiddlewareParams): Promise<Json> {\n if (!processDecryptMessage) {\n throw rpcErrors.methodNotSupported();\n }\n if (\n !request.params ||\n !Array.isArray(request.params) ||\n !(request.params.length >= 1)\n ) {\n throw rpcErrors.invalidInput();\n }\n const params = request.params as [string, string, Record<string, Json>?];\n\n const ciphertext: string = params[0];\n const address: string = await validateAndNormalizeKeyholder(\n params[1],\n context,\n );\n const extraParams = params[2] || {};\n const msgParams: MessageParams = {\n ...extraParams,\n from: address,\n data: ciphertext,\n };\n\n return await processDecryptMessage(msgParams, {\n id: request.id as string | number,\n origin: context.assertGet('origin'),\n securityAlertResponse: context.assertGet('securityAlertResponse'),\n });\n }\n\n //\n // utility\n //\n\n /**\n * Validates the keyholder address, and returns a normalized (i.e. lowercase)\n * copy of it.\n *\n * @param address - The address to validate and normalize.\n * @param context - The context of the request.\n * @returns The normalized address, if valid. Otherwise, throws\n * an error\n */\n async function validateAndNormalizeKeyholder(\n address: string,\n context: WalletMiddlewareContext,\n ): Promise<string> {\n return validateKeyholder(address as Hex, context, { getAccounts });\n }\n}\n\n/**\n * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.\n *\n * @param data - The data passed in typedSign request.\n */\nfunction validatePrimaryType(data: string) {\n const { primaryType, types } = parseTypedMessage(data);\n if (!types) {\n throw rpcErrors.invalidInput();\n }\n\n // Primary type can be an array.\n const baseType = stripArrayTypeIfPresent(primaryType);\n\n // Return if the base type is not defined in the types\n const baseTypeDefinitions = types[baseType];\n if (!baseTypeDefinitions) {\n throw rpcErrors.invalidInput();\n }\n}\n\n/**\n * Validates verifyingContract of typedSignMessage.\n *\n * @param data - The data passed in typedSign request.\n * This function allows the verifyingContract to be either:\n * - A valid hex address\n * - The string \"cosmos\" (as it is hard-coded in some Cosmos ecosystem's EVM adapters)\n * - An empty string\n */\nfunction validateVerifyingContract(data: string) {\n const { domain: { verifyingContract } = {} } = parseTypedMessage(data);\n // Explicit check for cosmos here has been added to address this issue\n // https://github.com/MetaMask/eth-json-rpc-middleware/issues/337\n if (\n verifyingContract &&\n (verifyingContract as string) !== 'cosmos' &&\n !isValidHexAddress(verifyingContract)\n ) {\n throw rpcErrors.invalidInput();\n }\n}\n"]}
|
package/dist/wallet.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
-
import type {
|
|
1
|
+
import type { JsonRpcMiddleware, MiddlewareContext, MiddlewareParams } from "@metamask/json-rpc-engine/v2";
|
|
2
|
+
import type { MessageRequest } from "@metamask/message-manager";
|
|
3
|
+
import type { JsonRpcRequest, Json } from "@metamask/utils";
|
|
3
4
|
import { type ProcessRequestExecutionPermissionsHook } from "./methods/wallet-request-execution-permissions.cjs";
|
|
4
5
|
import { type ProcessRevokeExecutionPermissionHook } from "./methods/wallet-revoke-execution-permission.cjs";
|
|
5
|
-
import type { Block } from "./types.cjs";
|
|
6
6
|
export type TransactionParams = {
|
|
7
7
|
from: string;
|
|
8
8
|
};
|
|
@@ -17,11 +17,11 @@ export type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {
|
|
|
17
17
|
data: Record<string, unknown>[];
|
|
18
18
|
};
|
|
19
19
|
export type WalletMiddlewareOptions = {
|
|
20
|
-
getAccounts: (
|
|
21
|
-
processDecryptMessage?: (msgParams: MessageParams, req:
|
|
22
|
-
processEncryptionPublicKey?: (address: string, req:
|
|
20
|
+
getAccounts: (origin: string) => Promise<string[]>;
|
|
21
|
+
processDecryptMessage?: (msgParams: MessageParams, req: MessageRequest) => Promise<string>;
|
|
22
|
+
processEncryptionPublicKey?: (address: string, req: MessageRequest) => Promise<string>;
|
|
23
23
|
processPersonalMessage?: (msgParams: MessageParams, req: JsonRpcRequest) => Promise<string>;
|
|
24
|
-
processTransaction?: (txParams: TransactionParams, req: JsonRpcRequest) => Promise<string>;
|
|
24
|
+
processTransaction?: (txParams: TransactionParams, req: JsonRpcRequest, context: WalletMiddlewareContext) => Promise<string>;
|
|
25
25
|
processSignTransaction?: (txParams: TransactionParams, req: JsonRpcRequest) => Promise<string>;
|
|
26
26
|
processTypedMessage?: (msgParams: TypedMessageV1Params, req: JsonRpcRequest, version: string) => Promise<string>;
|
|
27
27
|
processTypedMessageV3?: (msgParams: TypedMessageParams, req: JsonRpcRequest, version: string) => Promise<string>;
|
|
@@ -29,5 +29,32 @@ export type WalletMiddlewareOptions = {
|
|
|
29
29
|
processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;
|
|
30
30
|
processRevokeExecutionPermission?: ProcessRevokeExecutionPermissionHook;
|
|
31
31
|
};
|
|
32
|
-
export
|
|
32
|
+
export type WalletMiddlewareKeyValues = {
|
|
33
|
+
networkClientId: string;
|
|
34
|
+
origin: string;
|
|
35
|
+
securityAlertResponse?: Record<string, Json>;
|
|
36
|
+
traceContext?: unknown;
|
|
37
|
+
};
|
|
38
|
+
export type WalletMiddlewareContext = MiddlewareContext<WalletMiddlewareKeyValues>;
|
|
39
|
+
export type WalletMiddlewareParams = MiddlewareParams<JsonRpcRequest, WalletMiddlewareContext>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a JSON-RPC middleware that handles "wallet"-related JSON-RPC methods.
|
|
42
|
+
* "Wallet" may have had a specific meaning at some point in the distant past,
|
|
43
|
+
* but at this point it's just an arbitrary label.
|
|
44
|
+
*
|
|
45
|
+
* @param options - The options for the middleware.
|
|
46
|
+
* @param options.getAccounts - The function to get the accounts for the origin.
|
|
47
|
+
* @param options.processDecryptMessage - The function to process the decrypt message request.
|
|
48
|
+
* @param options.processEncryptionPublicKey - The function to process the encryption public key request.
|
|
49
|
+
* @param options.processPersonalMessage - The function to process the personal message request.
|
|
50
|
+
* @param options.processTransaction - The function to process the transaction request.
|
|
51
|
+
* @param options.processSignTransaction - The function to process the sign transaction request.
|
|
52
|
+
* @param options.processTypedMessage - The function to process the typed message request.
|
|
53
|
+
* @param options.processTypedMessageV3 - The function to process the typed message v3 request.
|
|
54
|
+
* @param options.processTypedMessageV4 - The function to process the typed message v4 request.
|
|
55
|
+
* @param options.processRequestExecutionPermissions - The function to process the request execution permissions request.
|
|
56
|
+
* @param options.processRevokeExecutionPermission - The function to process the revoke execution permission request.
|
|
57
|
+
* @returns A JSON-RPC middleware that handles wallet-related JSON-RPC methods.
|
|
58
|
+
*/
|
|
59
|
+
export declare function createWalletMiddleware({ getAccounts, processDecryptMessage, processEncryptionPublicKey, processPersonalMessage, processTransaction, processSignTransaction, processTypedMessage, processTypedMessageV3, processTypedMessageV4, processRequestExecutionPermissions, processRevokeExecutionPermission, }: WalletMiddlewareOptions): JsonRpcMiddleware<JsonRpcRequest, Json, WalletMiddlewareContext>;
|
|
33
60
|
//# sourceMappingURL=wallet.d.cts.map
|