@@ -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}.
@@ -134,6 +141,11 @@ export class ShieldController extends BaseController<
134
141
previousTransactions : TransactionMeta [ ] | undefined ,
135
142
) => void ;
136
143
144
+ readonly #signatureControllerStateChangeHandler: (
145
+ signatureRequests : Record < string , SignatureRequest > ,
146
+ previousSignatureRequests : Record < string , SignatureRequest > | undefined ,
147
+ ) => void ;
148
+
137
149
constructor ( options : ShieldControllerOptions ) {
138
150
const {
139
151
messenger,
@@ -157,6 +169,8 @@ export class ShieldController extends BaseController<
157
169
this . #transactionHistoryLimit = transactionHistoryLimit ;
158
170
this . #transactionControllerStateChangeHandler =
159
171
this . #handleTransactionControllerStateChange. bind ( this ) ;
172
+ this . #signatureControllerStateChangeHandler =
173
+ this . #handleSignatureControllerStateChange. bind ( this ) ;
160
174
}
161
175
162
176
start ( ) {
@@ -165,13 +179,54 @@ export class ShieldController extends BaseController<
165
179
this . #transactionControllerStateChangeHandler,
166
180
( state ) => state . transactions ,
167
181
) ;
182
+
183
+ this . messagingSystem . subscribe (
184
+ 'SignatureController:stateChange' ,
185
+ this . #signatureControllerStateChangeHandler,
186
+ ( state ) => state . signatureRequests ,
187
+ ) ;
168
188
}
169
189
170
190
stop ( ) {
171
191
this . messagingSystem . unsubscribe (
172
192
'TransactionController:stateChange' ,
173
193
this . #transactionControllerStateChangeHandler,
174
194
) ;
195
+
196
+ this . messagingSystem . unsubscribe (
197
+ 'SignatureController:stateChange' ,
198
+ this . #signatureControllerStateChangeHandler,
199
+ ) ;
200
+ }
201
+
202
+ #handleSignatureControllerStateChange(
203
+ signatureRequests : Record < string , SignatureRequest > ,
204
+ previousSignatureRequests : Record < string , SignatureRequest > | undefined ,
205
+ ) {
206
+ const signatureRequestsArray = Object . values ( signatureRequests ) ;
207
+ const previousSignatureRequestsArray = Object . values (
208
+ previousSignatureRequests ?? { } ,
209
+ ) ;
210
+ const previousSignatureRequestsById = new Map < string , SignatureRequest > (
211
+ previousSignatureRequestsArray . map ( ( request ) => [ request . id , request ] ) ,
212
+ ) ;
213
+ for ( const signatureRequest of signatureRequestsArray ) {
214
+ const previousSignatureRequest = previousSignatureRequestsById . get (
215
+ signatureRequest . id ,
216
+ ) ;
217
+
218
+ // Check coverage if the signature request is new and has type
219
+ // `personal_sign`.
220
+ if (
221
+ ! previousSignatureRequest &&
222
+ signatureRequest . type === SignatureRequestType . PersonalSign
223
+ ) {
224
+ this . checkSignatureCoverage ( signatureRequest ) . catch (
225
+ // istanbul ignore next
226
+ ( error ) => log ( 'Error checking coverage:' , error ) ,
227
+ ) ;
228
+ }
229
+ }
175
230
}
176
231
177
232
#handleTransactionControllerStateChange(
@@ -208,7 +263,7 @@ export class ShieldController extends BaseController<
208
263
*/
209
264
async checkCoverage ( txMeta : TransactionMeta ) : Promise < CoverageResult > {
210
265
// Check coverage
211
- const coverageResult = await this . #fetchCoverageResult ( txMeta ) ;
266
+ const coverageResult = await this . #backend . checkCoverage ( txMeta ) ;
212
267
213
268
// Publish coverage result
214
269
this . messagingSystem . publish (
@@ -222,8 +277,29 @@ export class ShieldController extends BaseController<
222
277
return coverageResult ;
223
278
}
224
279
225
- async #fetchCoverageResult( txMeta : TransactionMeta ) : Promise < CoverageResult > {
226
- return this . #backend. checkCoverage ( txMeta ) ;
280
+ /**
281
+ * Checks the coverage of a signature request.
282
+ *
283
+ * @param signatureRequest - The signature request to check coverage for.
284
+ * @returns The coverage result.
285
+ */
286
+ async checkSignatureCoverage (
287
+ signatureRequest : SignatureRequest ,
288
+ ) : Promise < CoverageResult > {
289
+ // Check coverage
290
+ const coverageResult =
291
+ await this . #backend. checkSignatureCoverage ( signatureRequest ) ;
292
+
293
+ // Publish coverage result
294
+ this . messagingSystem . publish (
295
+ `${ controllerName } :coverageResultReceived` ,
296
+ coverageResult ,
297
+ ) ;
298
+
299
+ // Update state
300
+ this . #addCoverageResult( signatureRequest . id , coverageResult ) ;
301
+
302
+ return coverageResult ;
227
303
}
228
304
229
305
#addCoverageResult( txId : string , coverageResult : CoverageResult ) {
0 commit comments