Skip to content
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

Add isDecodeSignatureRequestEnabled to SignatureController constructor #4903

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions packages/signature-controller/src/SignatureController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,10 @@ describe('SignatureController', () => {
},
],
};
const { controller } = createController();
const { controller } = createController({
decodingApiUrl: 'www.test.com',
getFeatureFlags: () => ({ disableDecodingApi: false }),
});

jest
.spyOn(DecodingDataUtils, 'decodeSignature')
Expand All @@ -948,8 +951,74 @@ describe('SignatureController', () => {
).toStrictEqual(MOCK_STATE_CHANGES);
});

it('does not invoke decodeSignature if decodingApiUrl is not defined', async () => {
const { controller } = createController({
decodingApiUrl: undefined,
getFeatureFlags: () => ({ disableDecodingApi: false }),
});

await controller.newUnsignedTypedMessage(
PERMIT_PARAMS_MOCK,
PERMIT_REQUEST_MOCK,
SignTypedDataVersion.V4,
{ parseJsonData: false },
);

expect(
controller.state.signatureRequests[ID_MOCK].decodingLoading,
).toBeUndefined();
expect(
controller.state.signatureRequests[ID_MOCK].decodingData,
).toBeUndefined();
});

it('does not invoke decodeSignature if featureFLag disableDecodingApi is true', async () => {
const { controller } = createController({
decodingApiUrl: 'www.test.com',
getFeatureFlags: () => ({ disableDecodingApi: true }),
});

await controller.newUnsignedTypedMessage(
PERMIT_PARAMS_MOCK,
PERMIT_REQUEST_MOCK,
SignTypedDataVersion.V4,
{ parseJsonData: false },
);

expect(
controller.state.signatureRequests[ID_MOCK].decodingLoading,
).toBeUndefined();
expect(
controller.state.signatureRequests[ID_MOCK].decodingData,
).toBeUndefined();
});

it('does not invoke decodeSignature if getFeatureFlags is not defined', async () => {
const { controller } = createController({
decodingApiUrl: 'www.test.com',
getFeatureFlags: undefined,
});

await controller.newUnsignedTypedMessage(
PERMIT_PARAMS_MOCK,
PERMIT_REQUEST_MOCK,
SignTypedDataVersion.V4,
{ parseJsonData: false },
);

expect(
controller.state.signatureRequests[ID_MOCK].decodingLoading,
).toBeUndefined();
expect(
controller.state.signatureRequests[ID_MOCK].decodingData,
).toBeUndefined();
});

it('correctly set decoding data if decodeSignature fails', async () => {
const { controller } = createController();
const { controller } = createController({
decodingApiUrl: 'www.test.com',
getFeatureFlags: () => ({ disableDecodingApi: false }),
});

jest
.spyOn(DecodingDataUtils, 'decodeSignature')
Expand All @@ -973,7 +1042,10 @@ describe('SignatureController', () => {
});

it('set decodingLoading to true while api request is in progress', async () => {
const { controller } = createController();
const { controller } = createController({
decodingApiUrl: 'www.test.com',
getFeatureFlags: () => ({ disableDecodingApi: false }),
});

jest
.spyOn(DecodingDataUtils, 'decodeSignature')
Expand Down
21 changes: 20 additions & 1 deletion packages/signature-controller/src/SignatureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export type SignatureControllerMessenger = RestrictedControllerMessenger<
never
>;

type FeatureFlags = { disableDecodingApi: boolean };

export type SignatureControllerOptions = {
/**
* Restricted controller messenger required by the signature controller.
Expand All @@ -161,6 +163,11 @@ export type SignatureControllerOptions = {
*/
decodingApiUrl?: string;

/**
* Function to get features flag information
*/
getFeatureFlags?: () => FeatureFlags;

/**
* Initial state of the controller.
*/
Expand All @@ -184,19 +191,23 @@ export class SignatureController extends BaseController<

#decodingApiUrl?: string;

#getFeatureFlags?: () => { disableDecodingApi: boolean };

#trace: TraceCallback;

/**
* Construct a Sign controller.
*
* @param options - The controller options.
* @param options.decodingApiUrl - Api used to get decoded data for permits.
* @param options.getFeatureFlags - Function to get required feature flags.
* @param options.messenger - The restricted controller messenger for the sign controller.
* @param options.state - Initial state to set on this controller.
* @param options.trace - Callback to generate trace information.
* @param options.decodingApiUrl - Api used to get decoded data for permits.
*/
constructor({
decodingApiUrl,
getFeatureFlags,
messenger,
state,
trace,
Expand All @@ -214,6 +225,7 @@ export class SignatureController extends BaseController<
this.hub = new EventEmitter();
this.#trace = trace ?? (((_request, fn) => fn?.()) as TraceCallback);
this.#decodingApiUrl = decodingApiUrl;
this.#getFeatureFlags = getFeatureFlags;
}

/**
Expand Down Expand Up @@ -902,6 +914,13 @@ export class SignatureController extends BaseController<
request: OriginalRequest,
chainId: string,
) {
if (
!this.#getFeatureFlags ||
this.#getFeatureFlags().disableDecodingApi ||
!this.#decodingApiUrl
) {
return;
}
this.#updateMetadata(signatureRequestId, (draftMetadata) => {
draftMetadata.decodingLoading = true;
});
Expand Down
5 changes: 1 addition & 4 deletions packages/signature-controller/src/utils/decoding-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ export const DECODING_API_ERRORS = {
export async function decodeSignature(
request: OriginalRequest,
chainId: string,
decodingApiUrl?: string,
decodingApiUrl: string,
) {
if (!decodingApiUrl) {
return undefined;
}
try {
const { method, origin, params } = request;
if (request.method === EthMethod.SignTypedDataV4) {
Expand Down
Loading