From 1e153ea07b1f0cb0d4249c62d317ea240fad53d0 Mon Sep 17 00:00:00 2001 From: Andrei Covaciu Date: Tue, 3 Dec 2024 11:29:54 +0200 Subject: [PATCH] LIVE-14970: prevent undefined error on swap mobile (#8459) --- .changeset/shy-penguins-itch.md | 5 +++++ .../platform/transfer/completeExchange.ts | 9 +++++--- .../src/exchange/swap/completeExchange.ts | 17 +++++++++----- .../src/exchange/swap/initSwap.ts | 22 +++++++++++++------ 4 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 .changeset/shy-penguins-itch.md diff --git a/.changeset/shy-penguins-itch.md b/.changeset/shy-penguins-itch.md new file mode 100644 index 000000000000..73f4486da7a6 --- /dev/null +++ b/.changeset/shy-penguins-itch.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +Prevent undefined error on mobile swap diff --git a/libs/ledger-live-common/src/exchange/platform/transfer/completeExchange.ts b/libs/ledger-live-common/src/exchange/platform/transfer/completeExchange.ts index 055442183e6e..5494a2728c6b 100644 --- a/libs/ledger-live-common/src/exchange/platform/transfer/completeExchange.ts +++ b/libs/ledger-live-common/src/exchange/platform/transfer/completeExchange.ts @@ -4,9 +4,9 @@ import { TransportStatusError, WrongDeviceForAccount } from "@ledgerhq/errors"; import { delay } from "../../../promise"; import { - isExchangeTypeNg, - ExchangeTypes, createExchange, + ExchangeTypes, + isExchangeTypeNg, PayloadSignatureComputedFormat, } from "@ledgerhq/hw-app-exchange"; import perFamily from "../../../generated/exchange"; @@ -106,8 +106,11 @@ const completeExchange = ( const payoutAddressParameters = await perFamily[ mainPayoutCurrency.family - ].getSerializedAddressParameters(mainAccount.freshAddressPath, mainAccount.derivationMode); + ]?.getSerializedAddressParameters(mainAccount.freshAddressPath, mainAccount.derivationMode); if (unsubscribed) return; + if (!payoutAddressParameters) { + throw new Error(`Family not supported: ${mainPayoutCurrency.family}`); + } const { config: payoutAddressConfig, signature: payoutAddressConfigSignature } = await getCurrencyExchangeConfig(payoutCurrency); diff --git a/libs/ledger-live-common/src/exchange/swap/completeExchange.ts b/libs/ledger-live-common/src/exchange/swap/completeExchange.ts index 09e7f6e4eb80..3c76f7fff7a9 100644 --- a/libs/ledger-live-common/src/exchange/swap/completeExchange.ts +++ b/libs/ledger-live-common/src/exchange/swap/completeExchange.ts @@ -1,7 +1,7 @@ import { TransportStatusError, - WrongDeviceForAccountRefund, WrongDeviceForAccountPayout, + WrongDeviceForAccountRefund, } from "@ledgerhq/errors"; import { log } from "@ledgerhq/logs"; import { firstValueFrom, from, Observable } from "rxjs"; @@ -14,14 +14,13 @@ import perFamily from "../../generated/exchange"; import { withDevice } from "../../hw/deviceAccess"; import { delay } from "../../promise"; import { - ExchangeTypes, createExchange, + ExchangeTypes, getExchangeErrorMessage, PayloadSignatureComputedFormat, } from "@ledgerhq/hw-app-exchange"; import type { CompleteExchangeInputSwap, CompleteExchangeRequestEvent } from "../platform/types"; -import { getSwapProvider } from "../providers"; -import { convertToAppExchangePartnerKey } from "../providers"; +import { convertToAppExchangePartnerKey, getSwapProvider } from "../providers"; import { CompleteExchangeStep, convertTransportError } from "../error"; import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName"; import BigNumber from "bignumber.js"; @@ -130,12 +129,15 @@ const completeExchange = ( const payoutAddressParameters = await perFamily[ mainPayoutCurrency.family - ].getSerializedAddressParameters( + ]?.getSerializedAddressParameters( payoutAccount.freshAddressPath, payoutAccount.derivationMode, mainPayoutCurrency.id, ); if (unsubscribed) return; + if (!payoutAddressParameters) { + throw new Error(`Family not supported: ${mainPayoutCurrency.family}`); + } //-- Special case of SPLToken //- NOT READY YET @@ -184,12 +186,15 @@ const completeExchange = ( if (unsubscribed) return; const refundAddressParameters = await perFamily[ mainRefundCurrency.family - ].getSerializedAddressParameters( + ]?.getSerializedAddressParameters( refundAccount.freshAddressPath, refundAccount.derivationMode, mainRefundCurrency.id, ); if (unsubscribed) return; + if (!refundAddressParameters) { + throw new Error(`Family not supported: ${mainRefundCurrency.family}`); + } const { config: refundAddressConfig, signature: refundAddressConfigSignature } = await getCurrencyExchangeConfig(refundCurrency); diff --git a/libs/ledger-live-common/src/exchange/swap/initSwap.ts b/libs/ledger-live-common/src/exchange/swap/initSwap.ts index 0d9675994348..a3a36bf50858 100644 --- a/libs/ledger-live-common/src/exchange/swap/initSwap.ts +++ b/libs/ledger-live-common/src/exchange/swap/initSwap.ts @@ -4,12 +4,16 @@ import { WrongDeviceForAccountPayout, WrongDeviceForAccountRefund, } from "@ledgerhq/errors"; -import Exchange, { ExchangeTypes, RateTypes } from "@ledgerhq/hw-app-exchange"; +import Exchange, { + decodePayloadProtobuf, + ExchangeTypes, + RateTypes, +} from "@ledgerhq/hw-app-exchange"; import network from "@ledgerhq/live-network/network"; import { log } from "@ledgerhq/logs"; import { BigNumber } from "bignumber.js"; import invariant from "invariant"; -import { Observable, firstValueFrom, from } from "rxjs"; +import { firstValueFrom, from, Observable } from "rxjs"; import secp256k1 from "secp256k1"; import { getCurrencyExchangeConfig } from "../"; import { getAccountCurrency, getMainAccount } from "../../account"; @@ -26,9 +30,7 @@ import { delay } from "../../promise"; import { getSwapAPIBaseURL, getSwapUserIP } from "./"; import { mockInitSwap } from "./mock"; import type { InitSwapInput, SwapRequestEvent } from "./types"; -import { decodePayloadProtobuf } from "@ledgerhq/hw-app-exchange"; -import { getSwapProvider } from "../providers"; -import { convertToAppExchangePartnerKey } from "../providers"; +import { convertToAppExchangePartnerKey, getSwapProvider } from "../providers"; import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName"; import { CEXProviderConfig } from "../providers/swap"; @@ -196,12 +198,15 @@ const initSwap = (input: InitSwapInput): Observable => { } const payoutAddressParameters = await perFamily[ mainPayoutCurrency.family - ].getSerializedAddressParameters( + ]?.getSerializedAddressParameters( payoutAccount.freshAddressPath, payoutAccount.derivationMode, mainPayoutCurrency.id, ); if (unsubscribed) return; + if (!payoutAddressParameters) { + throw new Error(`Family not supported: ${mainPayoutCurrency.family}`); + } const { config: payoutAddressConfig, signature: payoutAddressConfigSignature } = await getCurrencyExchangeConfig(payoutCurrency); @@ -230,12 +235,15 @@ const initSwap = (input: InitSwapInput): Observable => { } const refundAddressParameters = await perFamily[ mainRefundCurrency.family - ].getSerializedAddressParameters( + ]?.getSerializedAddressParameters( refundAccount.freshAddressPath, refundAccount.derivationMode, mainRefundCurrency.id, ); if (unsubscribed) return; + if (!refundAddressParameters) { + throw new Error(`Family not supported: ${mainRefundCurrency.family}`); + } const { config: refundAddressConfig, signature: refundAddressConfigSignature } = await getCurrencyExchangeConfig(refundCurrency); if (unsubscribed) return;