-
-
Notifications
You must be signed in to change notification settings - Fork 262
feat: Shield: log transaction and signature #6633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ccaa25b
010e0c4
c24668d
71e30cd
c26de78
e16226d
bd647c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,15 @@ import type { | |
| RestrictedMessenger, | ||
| } from '@metamask/base-controller'; | ||
| import { | ||
| SignatureRequestStatus, | ||
| SignatureRequestType, | ||
| type SignatureRequest, | ||
| type SignatureStateChange, | ||
| } from '@metamask/signature-controller'; | ||
| import type { | ||
| TransactionControllerStateChangeEvent, | ||
| TransactionMeta, | ||
| import { | ||
| TransactionStatus, | ||
| type TransactionControllerStateChangeEvent, | ||
| type TransactionMeta, | ||
| } from '@metamask/transaction-controller'; | ||
|
|
||
| import { controllerName } from './constants'; | ||
|
|
@@ -230,6 +232,17 @@ export class ShieldController extends BaseController< | |
| (error) => log('Error checking coverage:', error), | ||
| ); | ||
| } | ||
|
|
||
| // Log signature once the signature request has been fulfilled. | ||
| if ( | ||
| signatureRequest.status === SignatureRequestStatus.Signed && | ||
| signatureRequest.status !== previousSignatureRequest?.status | ||
| ) { | ||
| this.#logSignature(signatureRequest).catch( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand the |
||
| // istanbul ignore next | ||
| (error) => log('Error logging signature:', error), | ||
| ); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Async Logging Fails Due to Race ConditionRace condition between coverage checking and signature logging. When a new signature request is created and immediately signed, both Additional Locations (1)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice find. I'm aware of this. The reasoning here is: If the state is not updated yet, it means we don't have a coverage result yet. In this case it's OK to not log as far as I understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Premature Logging for New ItemsThe signature and transaction logging conditions trigger prematurely for new items initialized with a Additional Locations (1) |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -256,6 +269,17 @@ export class ShieldController extends BaseController< | |
| (error) => log('Error checking coverage:', error), | ||
| ); | ||
| } | ||
|
|
||
| // Log transaction once it has been submitted. | ||
| if ( | ||
| transaction.status === TransactionStatus.submitted && | ||
| transaction.status !== previousTransaction?.status | ||
| ) { | ||
| this.#logTransaction(transaction).catch( | ||
| // istanbul ignore next | ||
| (error) => log('Error logging transaction:', error), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -346,4 +370,49 @@ export class ShieldController extends BaseController< | |
| } | ||
| }); | ||
| } | ||
|
|
||
| async #logSignature(signatureRequest: SignatureRequest) { | ||
| const coverageId = this.#getLatestCoverageId(signatureRequest.id); | ||
| if (!coverageId) { | ||
| throw new Error('Coverage ID not found'); | ||
| } | ||
|
|
||
| const sig = signatureRequest.rawSig; | ||
| if (!sig) { | ||
| throw new Error('Signature not found'); | ||
| } | ||
|
|
||
| await this.#backend.logSignature({ | ||
| coverageId, | ||
| signature: sig, | ||
| // Status is 'shown' because the coverageId can only be retrieved after | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand the |
||
| // the result is in the state. If the result is in the state, we assume | ||
| // that it has been shown. | ||
| status: 'shown', | ||
| }); | ||
| } | ||
|
|
||
| async #logTransaction(txMeta: TransactionMeta) { | ||
| const coverageId = this.#getLatestCoverageId(txMeta.id); | ||
| if (!coverageId) { | ||
| throw new Error('Coverage ID not found'); | ||
| } | ||
|
|
||
| const txHash = txMeta.hash; | ||
| if (!txHash) { | ||
| throw new Error('Transaction hash not found'); | ||
| } | ||
| await this.#backend.logTransaction({ | ||
| coverageId, | ||
| transactionHash: txHash, | ||
| // Status is 'shown' because the coverageId can only be retrieved after | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // the result is in the state. If the result is in the state, we assume | ||
| // that it has been shown. | ||
| status: 'shown', | ||
| }); | ||
| } | ||
|
|
||
| #getLatestCoverageId(itemId: string) { | ||
| return this.state.coverageResults[itemId]?.results[0]?.coverageId; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanna verify, this might change when we wanna do
logon the confirmation reject, right?