@@ -3,6 +3,11 @@ import type {
3
3
ControllerStateChangeEvent ,
4
4
RestrictedMessenger ,
5
5
} from '@metamask/base-controller' ;
6
+ import {
7
+ SignatureRequestType ,
8
+ type SignatureRequest ,
9
+ type SignatureStateChange ,
10
+ } from '@metamask/signature-controller' ;
6
11
import type {
7
12
TransactionControllerStateChangeEvent ,
8
13
TransactionMeta ,
@@ -82,7 +87,9 @@ type AllowedActions = never;
82
87
/**
83
88
* The external events available to the ShieldController.
84
89
*/
85
- type AllowedEvents = TransactionControllerStateChangeEvent ;
90
+ type AllowedEvents =
91
+ | SignatureStateChange
92
+ | TransactionControllerStateChangeEvent ;
86
93
87
94
/**
88
95
* The messenger of the {@link ShieldController}.
@@ -138,6 +145,11 @@ export class ShieldController extends BaseController<
138
145
previousTransactions : TransactionMeta [ ] | undefined ,
139
146
) => void ;
140
147
148
+ readonly #signatureControllerStateChangeHandler: (
149
+ signatureRequests : Record < string , SignatureRequest > ,
150
+ previousSignatureRequests : Record < string , SignatureRequest > | undefined ,
151
+ ) => void ;
152
+
141
153
constructor ( options : ShieldControllerOptions ) {
142
154
const {
143
155
messenger,
@@ -161,6 +173,8 @@ export class ShieldController extends BaseController<
161
173
this . #transactionHistoryLimit = transactionHistoryLimit ;
162
174
this . #transactionControllerStateChangeHandler =
163
175
this . #handleTransactionControllerStateChange. bind ( this ) ;
176
+ this . #signatureControllerStateChangeHandler =
177
+ this . #handleSignatureControllerStateChange. bind ( this ) ;
164
178
}
165
179
166
180
start ( ) {
@@ -169,13 +183,54 @@ export class ShieldController extends BaseController<
169
183
this . #transactionControllerStateChangeHandler,
170
184
( state ) => state . transactions ,
171
185
) ;
186
+
187
+ this . messagingSystem . subscribe (
188
+ 'SignatureController:stateChange' ,
189
+ this . #signatureControllerStateChangeHandler,
190
+ ( state ) => state . signatureRequests ,
191
+ ) ;
172
192
}
173
193
174
194
stop ( ) {
175
195
this . messagingSystem . unsubscribe (
176
196
'TransactionController:stateChange' ,
177
197
this . #transactionControllerStateChangeHandler,
178
198
) ;
199
+
200
+ this . messagingSystem . unsubscribe (
201
+ 'SignatureController:stateChange' ,
202
+ this . #signatureControllerStateChangeHandler,
203
+ ) ;
204
+ }
205
+
206
+ #handleSignatureControllerStateChange(
207
+ signatureRequests : Record < string , SignatureRequest > ,
208
+ previousSignatureRequests : Record < string , SignatureRequest > | undefined ,
209
+ ) {
210
+ const signatureRequestsArray = Object . values ( signatureRequests ) ;
211
+ const previousSignatureRequestsArray = Object . values (
212
+ previousSignatureRequests ?? { } ,
213
+ ) ;
214
+ const previousSignatureRequestsById = new Map < string , SignatureRequest > (
215
+ previousSignatureRequestsArray . map ( ( request ) => [ request . id , request ] ) ,
216
+ ) ;
217
+ for ( const signatureRequest of signatureRequestsArray ) {
218
+ const previousSignatureRequest = previousSignatureRequestsById . get (
219
+ signatureRequest . id ,
220
+ ) ;
221
+
222
+ // Check coverage if the signature request is new and has type
223
+ // `personal_sign`.
224
+ if (
225
+ ! previousSignatureRequest &&
226
+ signatureRequest . type === SignatureRequestType . PersonalSign
227
+ ) {
228
+ this . checkSignatureCoverage ( signatureRequest ) . catch (
229
+ // istanbul ignore next
230
+ ( error ) => log ( 'Error checking coverage:' , error ) ,
231
+ ) ;
232
+ }
233
+ }
179
234
}
180
235
181
236
#handleTransactionControllerStateChange(
@@ -212,7 +267,7 @@ export class ShieldController extends BaseController<
212
267
*/
213
268
async checkCoverage ( txMeta : TransactionMeta ) : Promise < CoverageResult > {
214
269
// Check coverage
215
- const coverageResult = await this . #fetchCoverageResult ( txMeta ) ;
270
+ const coverageResult = await this . #backend . checkCoverage ( txMeta ) ;
216
271
217
272
// Publish coverage result
218
273
this . messagingSystem . publish (
@@ -226,8 +281,29 @@ export class ShieldController extends BaseController<
226
281
return coverageResult ;
227
282
}
228
283
229
- async #fetchCoverageResult( txMeta : TransactionMeta ) : Promise < CoverageResult > {
230
- return this . #backend. checkCoverage ( txMeta ) ;
284
+ /**
285
+ * Checks the coverage of a signature request.
286
+ *
287
+ * @param signatureRequest - The signature request to check coverage for.
288
+ * @returns The coverage result.
289
+ */
290
+ async checkSignatureCoverage (
291
+ signatureRequest : SignatureRequest ,
292
+ ) : Promise < CoverageResult > {
293
+ // Check coverage
294
+ const coverageResult =
295
+ await this . #backend. checkSignatureCoverage ( signatureRequest ) ;
296
+
297
+ // Publish coverage result
298
+ this . messagingSystem . publish (
299
+ `${ controllerName } :coverageResultReceived` ,
300
+ coverageResult ,
301
+ ) ;
302
+
303
+ // Update state
304
+ this . #addCoverageResult( signatureRequest . id , coverageResult ) ;
305
+
306
+ return coverageResult ;
231
307
}
232
308
233
309
#addCoverageResult( txId : string , coverageResult : CoverageResult ) {
0 commit comments