Skip to content

Commit

Permalink
Add method for adding params to transaction meta (#1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuri authored Nov 8, 2023
1 parent e56b975 commit bab7ec0
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 3 deletions.
101 changes: 100 additions & 1 deletion packages/transaction-controller/src/TransactionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ describe('TransactionController', () => {
const mockDeviceConfirmedOn = WalletDevice.OTHER;
const mockOrigin = 'origin';
const mockSecurityAlertResponse = {
resultType: 'Malicious',
result_type: 'Malicious',
reason: 'blur_farming',
description:
'A SetApprovalForAll request was made on {contract}. We found the operator {operator} to be malicious',
Expand Down Expand Up @@ -3418,4 +3418,103 @@ describe('TransactionController', () => {
);
});
});

describe('updateSecurityAlertResponse', () => {
const mockSendFlowHistory = [
{
entry:
'sendFlow - user selected transfer to my accounts on recipient screen',
timestamp: 1650663928211,
},
];

it('add securityAlertResponse to transaction meta', async () => {
const transactionMetaId = '123';
const status = TransactionStatus.submitted;
const controller = newController();
controller.state.transactions.push({
id: transactionMetaId,
status,
txParams: {
from: ACCOUNT_MOCK,
to: ACCOUNT_2_MOCK,
},
history: mockSendFlowHistory,
} as any);
expect(controller.state.transactions[0]).toBeDefined();
controller.updateSecurityAlertResponse(transactionMetaId, {
reason: 'NA',
result_type: 'Benign',
});

expect(
controller.state.transactions[0].securityAlertResponse,
).toBeDefined();
});

it('should throw error if transactionMetaId is not defined', async () => {
const transactionMetaId = '123';
const status = TransactionStatus.submitted;
const controller = newController();
controller.state.transactions.push({
id: transactionMetaId,
status,
} as any);
expect(controller.state.transactions[0]).toBeDefined();

expect(() =>
controller.updateSecurityAlertResponse(undefined as any, {
reason: 'NA',
result_type: 'Benign',
}),
).toThrow(
'Cannot update security alert response as no transaction metadata found',
);
});

it('should throw error if securityAlertResponse is not defined', async () => {
const transactionMetaId = '123';
const status = TransactionStatus.submitted;
const controller = newController();
controller.state.transactions.push({
id: transactionMetaId,
status,
} as any);
expect(controller.state.transactions[0]).toBeDefined();

expect(() =>
controller.updateSecurityAlertResponse(
transactionMetaId,
undefined as any,
),
).toThrow(
'updateSecurityAlertResponse: securityAlertResponse should not be null',
);
});

it('should throw error if transaction with given id does not exist', async () => {
const transactionMetaId = '123';
const status = TransactionStatus.submitted;
const controller = newController();
controller.state.transactions.push({
id: transactionMetaId,
status,
txParams: {
from: ACCOUNT_MOCK,
to: ACCOUNT_2_MOCK,
},
history: mockSendFlowHistory,
} as any);
expect(controller.state.transactions[0]).toBeDefined();

expect(() =>
controller.updateSecurityAlertResponse('456', {
reason: 'NA',
result_type: 'Benign',
}),
).toThrow(
'Cannot update security alert response as no transaction metadata found',
);
});
});
});
31 changes: 30 additions & 1 deletion packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
TransactionMeta,
TransactionReceipt,
WalletDevice,
SecurityAlertResponse,
} from './types';
import {
TransactionEnvelopeType,
Expand Down Expand Up @@ -595,7 +596,7 @@ export class TransactionController extends BaseController<
method?: string;
origin?: string;
requireApproval?: boolean | undefined;
securityAlertResponse?: Record<string, unknown>;
securityAlertResponse?: SecurityAlertResponse;
sendFlowHistory?: SendFlowHistoryEntry[];
swaps?: {
hasApproveTx?: boolean;
Expand Down Expand Up @@ -999,6 +1000,34 @@ export class TransactionController extends BaseController<
this.update({ transactions: this.trimTransactionsForState(transactions) });
}

/**
* Update the security alert response for a transaction.
*
* @param transactionId - ID of the transaction.
* @param securityAlertResponse - The new security alert response for the transaction.
*/
updateSecurityAlertResponse(
transactionId: string,
securityAlertResponse: SecurityAlertResponse,
) {
if (!securityAlertResponse) {
throw new Error(
'updateSecurityAlertResponse: securityAlertResponse should not be null',
);
}
const transactionMeta = this.getTransaction(transactionId);
if (!transactionMeta) {
throw new Error(
`Cannot update security alert response as no transaction metadata found`,
);
}
const updatedMeta = merge(transactionMeta, { securityAlertResponse });
this.updateTransaction(
updatedMeta,
'TransactionController:updatesecurityAlertResponse - securityAlertResponse updated',
);
}

/**
* Removes all transactions from state, optionally based on the current network.
*
Expand Down
12 changes: 11 additions & 1 deletion packages/transaction-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ type TransactionMetaBase = {
/**
* Response from security validator.
*/
securityAlertResponse?: Record<string, unknown>;
securityAlertResponse?: SecurityAlertResponse;

/**
* Response from security provider.
Expand Down Expand Up @@ -878,3 +878,13 @@ export type TransactionError = {
*/
rpc?: unknown;
};

/**
* Type for security alert response from transaction validator.
*/
export type SecurityAlertResponse = {
reason: string;
features?: string[];
result_type: string;
providerRequestsCount?: Record<string, number>;
};

0 comments on commit bab7ec0

Please sign in to comment.