@@ -3,6 +3,11 @@ import type {
33 ControllerStateChangeEvent ,
44 RestrictedMessenger ,
55} from '@metamask/base-controller' ;
6+ import {
7+ SignatureRequestType ,
8+ type SignatureRequest ,
9+ type SignatureStateChange ,
10+ } from '@metamask/signature-controller' ;
611import type {
712 TransactionControllerStateChangeEvent ,
813 TransactionMeta ,
@@ -82,7 +87,9 @@ type AllowedActions = never;
8287/**
8388 * The external events available to the ShieldController.
8489 */
85- type AllowedEvents = TransactionControllerStateChangeEvent ;
90+ type AllowedEvents =
91+ | SignatureStateChange
92+ | TransactionControllerStateChangeEvent ;
8693
8794/**
8895 * The messenger of the {@link ShieldController}.
@@ -134,6 +141,11 @@ export class ShieldController extends BaseController<
134141 previousTransactions : TransactionMeta [ ] | undefined ,
135142 ) => void ;
136143
144+ readonly #signatureControllerStateChangeHandler: (
145+ signatureRequests : Record < string , SignatureRequest > ,
146+ previousSignatureRequests : Record < string , SignatureRequest > | undefined ,
147+ ) => void ;
148+
137149 constructor ( options : ShieldControllerOptions ) {
138150 const {
139151 messenger,
@@ -157,6 +169,8 @@ export class ShieldController extends BaseController<
157169 this . #transactionHistoryLimit = transactionHistoryLimit ;
158170 this . #transactionControllerStateChangeHandler =
159171 this . #handleTransactionControllerStateChange. bind ( this ) ;
172+ this . #signatureControllerStateChangeHandler =
173+ this . #handleSignatureControllerStateChange. bind ( this ) ;
160174 }
161175
162176 start ( ) {
@@ -165,13 +179,54 @@ export class ShieldController extends BaseController<
165179 this . #transactionControllerStateChangeHandler,
166180 ( state ) => state . transactions ,
167181 ) ;
182+
183+ this . messagingSystem . subscribe (
184+ 'SignatureController:stateChange' ,
185+ this . #signatureControllerStateChangeHandler,
186+ ( state ) => state . signatureRequests ,
187+ ) ;
168188 }
169189
170190 stop ( ) {
171191 this . messagingSystem . unsubscribe (
172192 'TransactionController:stateChange' ,
173193 this . #transactionControllerStateChangeHandler,
174194 ) ;
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+ }
175230 }
176231
177232 #handleTransactionControllerStateChange(
@@ -208,7 +263,7 @@ export class ShieldController extends BaseController<
208263 */
209264 async checkCoverage ( txMeta : TransactionMeta ) : Promise < CoverageResult > {
210265 // Check coverage
211- const coverageResult = await this . #fetchCoverageResult ( txMeta ) ;
266+ const coverageResult = await this . #backend . checkCoverage ( txMeta ) ;
212267
213268 // Publish coverage result
214269 this . messagingSystem . publish (
@@ -222,8 +277,29 @@ export class ShieldController extends BaseController<
222277 return coverageResult ;
223278 }
224279
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 ;
227303 }
228304
229305 #addCoverageResult( txId : string , coverageResult : CoverageResult ) {
0 commit comments