Skip to content

Commit

Permalink
Handle retro-compatibility in api's messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautBremand committed Oct 23, 2023
1 parent 89a0953 commit dc7e38a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { useFees, useFetchFromSessionStorage, useTransactionStatus } from '../../../hooks';
import { TransactionStatus } from '../../../types';
import { serializeError } from '../../../utils/errors';
import { parseTransactionParam } from '../../../utils/parseParams';
import { TransactionDetails } from '../../organisms';
import { TransactionPage } from '../../templates';

Expand Down Expand Up @@ -120,7 +121,8 @@ export const SignTransaction: FC = () => {
return;
}

const transaction = 'transaction' in fetchedData ? fetchedData.transaction : null;
const transaction =
'transaction' in fetchedData ? parseTransactionParam(fetchedData.transaction) : null;

if (!transaction) {
setIsParamsMissing(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GEM_WALLET,
ReceiveSubmitBulkTransactionsBackgroundMessage,
ResponseType,
SubmitBulkTransactionsRequest,
SubmitBulkTransactionsWithKeysRequest,
TransactionBulkResponse,
TransactionErrorHandling,
TransactionWithID
Expand All @@ -21,11 +21,7 @@ import {
} from '../../../contexts';
import { useFees, useFetchFromSessionStorage, useTransactionStatus } from '../../../hooks';
import { TransactionStatus } from '../../../types';
import {
loadFromChromeLocalStorage,
parseTransactionsBulkMap,
saveInChromeLocalStorage
} from '../../../utils';
import { loadFromChromeLocalStorage, saveInChromeLocalStorage } from '../../../utils';
import { serializeError } from '../../../utils/errors';
import { PermissionRequiredView } from './PermissionRequiredView';
import { RecapView } from './RecapView';
Expand Down Expand Up @@ -70,7 +66,7 @@ export const SubmitBulkTransactions: FC = () => {
const { fetchedData } = useFetchFromSessionStorage(
urlParams.get(STORAGE_MESSAGING_KEY) ?? undefined
) as {
fetchedData: SubmitBulkTransactionsRequest | undefined;
fetchedData: SubmitBulkTransactionsWithKeysRequest | undefined;
};

const sendMessageToBackground = useCallback(
Expand Down Expand Up @@ -165,8 +161,7 @@ export const SubmitBulkTransactions: FC = () => {
return;
}

const parsedTransactionsMap =
'transactions' in fetchedData ? parseTransactionsBulkMap(fetchedData.transactions) : null;
const parsedTransactionsMap = 'transactions' in fetchedData ? fetchedData.transactions : null;

if (!parsedTransactionsMap) {
setIsParamsMissing(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { useFees, useFetchFromSessionStorage, useTransactionStatus } from '../../../hooks';
import { TransactionStatus } from '../../../types';
import { serializeError } from '../../../utils/errors';
import { parseTransactionParam } from '../../../utils/parseParams';
import { TransactionDetails } from '../../organisms';
import { TransactionPage } from '../../templates';

Expand Down Expand Up @@ -115,7 +116,8 @@ export const SubmitTransaction: FC = () => {
return;
}

const transaction = 'transaction' in fetchedData ? fetchedData.transaction : null;
const transaction =
'transaction' in fetchedData ? parseTransactionParam(fetchedData.transaction) : null;

if (!transaction) {
setIsParamsMissing(true);
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/utils/baseParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Memo, Signer } from '@gemwallet/constants';

import { parseMemos, parseSigners } from './parseFromString';
import { parseMemos, parseSigners } from './parseParams';
import { checkFee } from './transaction';

export type BaseTransactionParamsNew = {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from './image';
export * from './link';
export * from './network';
export * from './numbersToSeed';
export * from './parseFromString';
export * from './parseParams';
export * from './storage';
export * from './storageChromeLocal';
export * from './storageChromeSession';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { NFTokenCreateOfferFlags, NFTokenMintFlags, NFTokenMintFlagsInterface } from 'xrpl';
import {
NFTokenCreateOfferFlags,
NFTokenMintFlags,
NFTokenMintFlagsInterface,
Transaction
} from 'xrpl';

import { Signer, TransactionWithID } from '@gemwallet/constants';
import { Signer } from '@gemwallet/constants';

import {
createNFTOfferFlagsToNumber,
Expand All @@ -14,9 +19,9 @@ import {
parsePaymentFlags,
parseSetAccountFlags,
parseSigners,
parseTransactionsBulkMap,
parseTransactionParam,
parseTrustSetFlags
} from './parseFromString';
} from './parseParams';

describe('parseAmount', () => {
test('parse amount in drops', () => {
Expand Down Expand Up @@ -357,21 +362,40 @@ describe('createNFTOfferFlagsToNumber', () => {
});
});

describe('parseTransactionsBulkMap', () => {
it('should return null if input is null', () => {
expect(parseTransactionsBulkMap(null)).toBeNull();
describe('parseTransactionParam', () => {
it('should return null if the input is null', () => {
expect(parseTransactionParam(null)).toBe(null);
});

it('should return null if the input is an empty string', () => {
expect(parseTransactionParam('')).toBe(null);
});

it('should return null if the input is not valid JSON', () => {
expect(parseTransactionParam('This is not a JSON string')).toBe(null);
});

it('should return the same object if it is of type Record<number, TransactionWithID>', () => {
const mockTransaction = { id: '123', amount: 456 };
const input: Record<number, TransactionWithID> = { 0: mockTransaction as any };
it('should return a Transaction object if the input is a valid JSON string representing a Transaction', () => {
const transactionJson = '{"id": "123", "amount": 456}';
const expectedTransaction = { id: '123', amount: 456 };

expect(parseTransactionsBulkMap(input)).toEqual(input);
expect(parseTransactionParam(transactionJson)).toEqual(expectedTransaction);
});

it('should return the object even if it does not strictly match Record<number, TransactionWithID>', () => {
const input = { key: 'value' }; // This doesn't match the type but our function should still return it
it('should return null if the input is a JSON string representing a non-object value', () => {
const notAnObject = '"This is a JSON string, but not an object"';

expect(parseTransactionParam(notAnObject)).toBe(null);
});

it('should return the same object if the input is already a Transaction object', () => {
const mockTransaction: Transaction = {
Account: '123',
TransactionType: 'Payment',
Amount: '456',
Destination: '789'
};

expect(parseTransactionsBulkMap(input as any)).toEqual(input);
expect(parseTransactionParam(mockTransaction)).toEqual(mockTransaction);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NFTokenCreateOfferFlagsInterface,
NFTokenMintFlags,
NFTokenMintFlagsInterface,
Transaction,
xrpToDrops
} from 'xrpl';
import { Amount, IssuedCurrencyAmount } from 'xrpl/dist/npm/models/common';
Expand All @@ -15,7 +16,6 @@ import {
PaymentFlags,
SetAccountFlags,
Signer,
TransactionWithID,
TrustSetFlags
} from '@gemwallet/constants';

Expand Down Expand Up @@ -362,16 +362,25 @@ export const createNFTOfferFlagsToNumber = (flags: NFTokenCreateOfferFlagsInterf
return result;
};

export const parseTransactionsBulkMap = (
json: object | null
): Record<number, TransactionWithID> | null => {
if (!json) {
export const parseTransactionParam = (input: Transaction | string | null): Transaction | null => {
if (!input) {
return null;
}

if (typeof input === 'object') {
return input;
}

// For API version < 3.6
try {
return json as Record<number, TransactionWithID>;
const parsedTransaction = JSON.parse(input);

if (typeof parsedTransaction === 'object' && parsedTransaction !== null) {
return parsedTransaction as Transaction;
}
} catch (error) {
return null;
}

return null;
};

0 comments on commit dc7e38a

Please sign in to comment.