diff --git a/packages/starknet-snap/src/rpcs/executeTxn.ts b/packages/starknet-snap/src/rpcs/executeTxn.ts index dcb55a00..3b89ff93 100644 --- a/packages/starknet-snap/src/rpcs/executeTxn.ts +++ b/packages/starknet-snap/src/rpcs/executeTxn.ts @@ -25,6 +25,7 @@ import { confirmDialog, UniversalDetailsStruct, CallsStruct, + mapDeprecatedParams, } from '../utils'; import { logger } from '../utils/logger'; import { @@ -76,6 +77,21 @@ export class ExecuteTxnRpc extends AccountRpcController< this.tokenStateManager = new TokenStateManager(); } + protected async preExecute(params: ExecuteTxnParams): Promise { + // 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 = { + senderAddress: 'address', + txnInvocation: 'calls', + invocationsDetails: 'details', + }; + + // Apply the mappings to params + mapDeprecatedParams(params, paramMappings); + await super.preExecute(params); + } + /** * Execute the transaction request handler. * diff --git a/packages/starknet-snap/src/rpcs/sign-declare-transaction.ts b/packages/starknet-snap/src/rpcs/sign-declare-transaction.ts index b6496553..f9f784a4 100644 --- a/packages/starknet-snap/src/rpcs/sign-declare-transaction.ts +++ b/packages/starknet-snap/src/rpcs/sign-declare-transaction.ts @@ -16,6 +16,7 @@ import { BaseRequestStruct, AccountRpcController, DeclareSignDetailsStruct, + mapDeprecatedParams, } from '../utils'; import { signDeclareTransaction as signDeclareTransactionUtil } from '../utils/starknetUtils'; @@ -48,6 +49,22 @@ export class SignDeclareTransactionRpc extends AccountRpcController< protected responseStruct = SignDeclareTransactionResponseStruct; + protected async preExecute( + params: SignDeclareTransactionParams, + ): Promise { + // 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 = { + 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. diff --git a/packages/starknet-snap/src/rpcs/signMessage.ts b/packages/starknet-snap/src/rpcs/signMessage.ts index a04b9adc..896bcfed 100644 --- a/packages/starknet-snap/src/rpcs/signMessage.ts +++ b/packages/starknet-snap/src/rpcs/signMessage.ts @@ -16,6 +16,7 @@ import { AuthorizableStruct, BaseRequestStruct, AccountRpcController, + mapDeprecatedParams, } from '../utils'; import { signMessage as signMessageUtil } from '../utils/starknetUtils'; @@ -45,6 +46,19 @@ export class SignMessageRpc extends AccountRpcController< protected responseStruct = SignMessageResponseStruct; + protected async preExecute(params: SignMessageParams): Promise { + // 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 = { + 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. diff --git a/packages/starknet-snap/src/rpcs/signTransaction.ts b/packages/starknet-snap/src/rpcs/signTransaction.ts index c7f79b57..6f66350e 100644 --- a/packages/starknet-snap/src/rpcs/signTransaction.ts +++ b/packages/starknet-snap/src/rpcs/signTransaction.ts @@ -17,6 +17,7 @@ import { AccountRpcController, CallDataStruct, toJson, + mapDeprecatedParams, } from '../utils'; import { signTransactions } from '../utils/starknetUtils'; @@ -49,6 +50,19 @@ export class SignTransactionRpc extends AccountRpcController< protected responseStruct = SignTransactionResponseStruct; + protected async preExecute(params: SignTransactionParams): Promise { + // 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 = { + 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. diff --git a/packages/starknet-snap/src/utils/formatterUtils.test.ts b/packages/starknet-snap/src/utils/formatterUtils.test.ts new file mode 100644 index 00000000..0ea9b941 --- /dev/null +++ b/packages/starknet-snap/src/utils/formatterUtils.test.ts @@ -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); + }); +}); diff --git a/packages/starknet-snap/src/utils/formatterUtils.ts b/packages/starknet-snap/src/utils/formatterUtils.ts index ebfdf475..c91e1488 100644 --- a/packages/starknet-snap/src/utils/formatterUtils.ts +++ b/packages/starknet-snap/src/utils/formatterUtils.ts @@ -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 = ( + requestParams: Params, + mappings: Record, +) => { + 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 + } + }); +};