Skip to content

Commit

Permalink
feat: ensure backward compatibility with remoteEntry.js of snap v2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 committed Sep 17, 2024
1 parent ac41722 commit 55358ed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import type {
} from './types/snapApi';
import type { SnapState } from './types/snapState';
import { upgradeAccContract } from './upgradeAccContract';
import { getDappUrl, isSnapRpcError } from './utils';
import { getDappUrl, isSnapRpcError, mapDeprecatedParams } from './utils';
import {
CAIRO_VERSION_LEGACY,
ETHER_MAINNET,
Expand Down Expand Up @@ -153,6 +153,21 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
saveMutex,
};

// 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, while the new version
// expects parameters such as `calls`, `details`, and `address`.
const paramMappings: Record<string, string> = {
signerAddress: 'address',
senderAddress: 'address',
txnInvocation: 'calls',
invocationsDetails: 'details',
transaction: 'details',
};

// Apply the mappings to apiParams.requestParams
mapDeprecatedParams(apiParams.requestParams, paramMappings);

switch (request.method) {
case 'starkNet_createAccount':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
Expand Down
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 = (
requestParams: any,
mappings: Record<string, string>,
) => {
Object.keys(mappings).forEach((oldParam) => {
const newParam = mappings[oldParam];
if (requestParams[oldParam]) {
requestParams[newParam] = requestParams[oldParam];
delete requestParams[oldParam]; // Remove old param after mapping
}
});
};

0 comments on commit 55358ed

Please sign in to comment.