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

chore: ensure backward compatibility with remoteEntry.js of snap v2.9.0 #355

Merged
merged 11 commits into from
Sep 19, 2024
16 changes: 16 additions & 0 deletions packages/starknet-snap/src/rpcs/executeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
confirmDialog,
UniversalDetailsStruct,
CallsStruct,
mapDeprecatedParams,
} from '../utils';
import { logger } from '../utils/logger';
import {
Expand Down Expand Up @@ -76,6 +77,21 @@ export class ExecuteTxnRpc extends AccountRpcController<
this.tokenStateManager = new TokenStateManager();
}

protected async preExecute(params: ExecuteTxnParams): Promise<void> {
// Define mappings to ensure backward compatibility with previous versions of the API.
// These mappings replace deprecated parameter names with the updated equivalents,
// allowing older integrations to function without changes
const paramMappings: Record<string, string> = {
senderAddress: 'address',
txnInvocation: 'calls',
invocationsDetails: 'details',
};

// Apply the mappings to params
mapDeprecatedParams(params, paramMappings);
await super.preExecute(params);
}

/**
* Execute the transaction request handler.
*
Expand Down
17 changes: 17 additions & 0 deletions packages/starknet-snap/src/rpcs/sign-declare-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
BaseRequestStruct,
AccountRpcController,
DeclareSignDetailsStruct,
mapDeprecatedParams,
} from '../utils';
import { signDeclareTransaction as signDeclareTransactionUtil } from '../utils/starknetUtils';

Expand Down Expand Up @@ -48,6 +49,22 @@ export class SignDeclareTransactionRpc extends AccountRpcController<

protected responseStruct = SignDeclareTransactionResponseStruct;

protected async preExecute(
params: SignDeclareTransactionParams,
): Promise<void> {
// Define mappings to ensure backward compatibility with previous versions of the API.
// These mappings replace deprecated parameter names with the updated equivalents,
// allowing older integrations to function without changes
const paramMappings: Record<string, string> = {
signerAddress: 'address',
transaction: 'details',
};

// Apply the mappings to params
mapDeprecatedParams(params, paramMappings);
await super.preExecute(params);
}

/**
* Execute the sign declare transaction request handler.
* It will show a confirmation dialog to the user before signing the declare transaction.
Expand Down
14 changes: 14 additions & 0 deletions packages/starknet-snap/src/rpcs/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AuthorizableStruct,
BaseRequestStruct,
AccountRpcController,
mapDeprecatedParams,
} from '../utils';
import { signMessage as signMessageUtil } from '../utils/starknetUtils';

Expand Down Expand Up @@ -45,6 +46,19 @@ export class SignMessageRpc extends AccountRpcController<

protected responseStruct = SignMessageResponseStruct;

protected async preExecute(params: SignMessageParams): Promise<void> {
// Define mappings to ensure backward compatibility with previous versions of the API.
// These mappings replace deprecated parameter names with the updated equivalents,
// allowing older integrations to function without changes
const paramMappings: Record<string, string> = {
signerAddress: 'address',
};

// Apply the mappings to params
mapDeprecatedParams(params, paramMappings);
await super.preExecute(params);
}

/**
* Execute the sign message request handler.
* It will show a confirmation dialog to the user before signing the message.
Expand Down
14 changes: 14 additions & 0 deletions packages/starknet-snap/src/rpcs/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
AccountRpcController,
CallDataStruct,
toJson,
mapDeprecatedParams,
} from '../utils';
import { signTransactions } from '../utils/starknetUtils';

Expand Down Expand Up @@ -49,6 +50,19 @@ export class SignTransactionRpc extends AccountRpcController<

protected responseStruct = SignTransactionResponseStruct;

protected async preExecute(params: SignTransactionParams): Promise<void> {
// Define mappings to ensure backward compatibility with previous versions of the API.
// These mappings replace deprecated parameter names with the updated equivalents,
// allowing older integrations to function without changes
const paramMappings: Record<string, string> = {
signerAddress: 'address',
};

// Apply the mappings to params
mapDeprecatedParams(params, paramMappings);
await super.preExecute(params);
}

/**
* Execute the sign transaction request handler.
* It will show a confirmation dialog to the user before signing the transaction.
Expand Down
67 changes: 67 additions & 0 deletions packages/starknet-snap/src/utils/formatterUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { mapDeprecatedParams } from './formatterUtils';

describe('mapDeprecatedParams', () => {
it('maps deprecated parameters to their new equivalents', () => {
const requestParams = {
signerAddress: '0x123',
txnInvocation: 'invoke',
};
const mappings = {
signerAddress: 'address',
txnInvocation: 'calls',
};

const expected = {
address: '0x123',
calls: 'invoke',
};

mapDeprecatedParams(requestParams, mappings);

expect(requestParams).toStrictEqual(expected);
});

it('removes the deprecated parameter after mapping', () => {
const requestParams = {
signerAddress: '0x123',
txnInvocation: 'invoke',
};
const mappings = {
signerAddress: 'address',
txnInvocation: 'calls',
};

mapDeprecatedParams(requestParams, mappings);

expect(requestParams).not.toHaveProperty('signerAddress');
expect(requestParams).not.toHaveProperty('txnInvocation');
});

it('does nothing if the deprecated parameter does not exist', () => {
const requestParams = {
otherParam: 'value',
};
const mappings = {
signerAddress: 'address',
};

const expected = { otherParam: 'value' };

mapDeprecatedParams(requestParams, mappings);

expect(requestParams).toStrictEqual(expected);
});

it('does nothing if the mapping is empty', () => {
const requestParams = {
signerAddress: '0x123',
};
const mappings = {};

const expected = { signerAddress: '0x123' };

mapDeprecatedParams(requestParams, mappings);

expect(requestParams).toStrictEqual(expected);
});
});
30 changes: 30 additions & 0 deletions packages/starknet-snap/src/utils/formatterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,33 @@ export const hexToString = (hexStr) => {
}
return str;
};

/**
* Maps deprecated parameters to their new equivalents in the requestParams object
* and removes the deprecated parameters afterward.
*
* @param requestParams - The object containing the API request parameters.
* @param mappings - A record of key-value pairs where the key is the old parameter
* and the value is the new parameter.
* @example
* const paramMappings = {
* signerAddress: 'address',
* senderAddress: 'address',
* txnInvocation: 'calls',
* invocationsDetails: 'details',
* transaction: 'details'
* };
* mapDeprecatedParams(apiParams.requestParams, paramMappings);
*/
export const mapDeprecatedParams = <Params>(
requestParams: Params,
mappings: Record<string, string>,
) => {
Object.keys(mappings).forEach((oldParam) => {
const newParam = mappings[oldParam] as unknown as keyof Params;
if (Object.prototype.hasOwnProperty.call(requestParams, oldParam)) {
requestParams[newParam] = requestParams[oldParam];
delete requestParams[oldParam]; // Remove old param after mapping
}
});
};
Loading