diff --git a/packages/starknet-snap/src/utils/formatterUtils.ts b/packages/starknet-snap/src/utils/formatterUtils.ts new file mode 100644 index 00000000..de0aabb8 --- /dev/null +++ b/packages/starknet-snap/src/utils/formatterUtils.ts @@ -0,0 +1,9 @@ +export const hexToString = (hex) => { + let str = ''; + for (let i = 0; i < hex.length; i += 2) { + const hexValue = hex.substr(i, 2); + const decimalValue = parseInt(hexValue, 16); + str += String.fromCharCode(decimalValue); + } + return str; +}; diff --git a/packages/starknet-snap/src/utils/starknetUtils.ts b/packages/starknet-snap/src/utils/starknetUtils.ts index 43944463..2cb59311 100644 --- a/packages/starknet-snap/src/utils/starknetUtils.ts +++ b/packages/starknet-snap/src/utils/starknetUtils.ts @@ -46,6 +46,7 @@ import { getAddressKey } from './keyPair'; import { getAccount, getAccounts, getTransactionFromVoyagerUrl, getTransactionsFromVoyagerUrl } from './snapUtils'; import { logger } from './logger'; import { RpcV4GetTransactionReceiptResponse } from '../types/snapApi'; +import { hexToString } from './formatterUtils'; export const getCallDataArray = (callDataStr: string): string[] => { return (callDataStr ?? '') @@ -593,13 +594,16 @@ export const findAddressIndex = async ( export const isUpgradeRequired = async (network: Network, address: string) => { try { logger.log(`isUpgradeRequired: address = ${address}`); - const version = await getVersion(address, network); + const hexResp = await getVersion(address, network); + const version = hexToString(hexResp); + logger.log(`isUpgradeRequired: hexResp = ${hexResp}, version = ${version}`); const versionArr = version.split('.'); return Number(versionArr[1]) < MIN_ACC_CONTRACT_VERSION[1]; } catch (err) { if (!err.message.includes('Contract not found')) { throw err; } + logger.error(`isUpgradeRequired: error:`, err); //[TODO] if address is cario0 but not deployed we should throw error return false; } diff --git a/packages/starknet-snap/test/utils/starknetUtils.test.ts b/packages/starknet-snap/test/utils/starknetUtils.test.ts index 4835126d..769ea889 100644 --- a/packages/starknet-snap/test/utils/starknetUtils.test.ts +++ b/packages/starknet-snap/test/utils/starknetUtils.test.ts @@ -193,13 +193,13 @@ describe('Test function: isUpgradeRequired', function () { }); it('should return true when upgrade is required', async function () { - sandbox.stub(utils, 'getVersion').callsFake(async () => '0.2.3'); + sandbox.stub(utils, 'getVersion').callsFake(async () => '0x302e322e33'); const result = await utils.isUpgradeRequired(STARKNET_TESTNET_NETWORK, userAddress); expect(result).to.be.eq(true); }); it('should return false when upgrade is not required', async function () { - sandbox.stub(utils, 'getVersion').callsFake(async () => '0.3.0'); + sandbox.stub(utils, 'getVersion').callsFake(async () => '0x302e332e30'); const result = await utils.isUpgradeRequired(STARKNET_TESTNET_NETWORK, userAddress); expect(result).to.be.eq(false); });