Skip to content

Commit

Permalink
Only use whitelisted plugins for centralized swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
peachbits committed Feb 9, 2024
1 parent a466162 commit 56a65ad
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/swap/central/changehero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
CurrencyCodeTranscriptionMap,
getCodesWithTranscription,
getMaxSwappable,
Expand Down Expand Up @@ -194,15 +195,6 @@ export function makeChangeHeroPlugin(
getAddress(request.toWallet)
])

// Supported chains must be whitelisted
if (
MAINNET_CODE_TRANSCRIPTION[request.fromWallet.currencyInfo.pluginId] ==
null ||
MAINNET_CODE_TRANSCRIPTION[request.toWallet.currencyInfo.pluginId] == null
) {
throw new SwapCurrencyError(swapInfo, request)
}

const {
fromCurrencyCode,
toCurrencyCode,
Expand Down Expand Up @@ -376,6 +368,11 @@ export function makeChangeHeroPlugin(
async fetchSwapQuote(req: EdgeSwapRequest): Promise<EdgeSwapQuote> {
const request = convertRequest(req)
checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(
MAINNET_CODE_TRANSCRIPTION,
request,
swapInfo
)

const newRequest = await getMaxSwappable(getFixedQuote, request)
const swapOrder = await getFixedQuote(newRequest)
Expand Down
6 changes: 6 additions & 0 deletions src/swap/central/changenow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
ensureInFuture,
getCodesWithTranscription,
getMaxSwappable,
Expand Down Expand Up @@ -397,6 +398,11 @@ export function makeChangeNowPlugin(
const request = convertRequest(req)

checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(
MAINNET_CODE_TRANSCRIPTION,
request,
swapInfo
)

const newRequest = await getMaxSwappable(
fetchSwapQuoteInner,
Expand Down
6 changes: 6 additions & 0 deletions src/swap/central/exolix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
getCodesWithTranscription,
getMaxSwappable,
InvalidCurrencyCodes,
Expand Down Expand Up @@ -345,6 +346,11 @@ export function makeExolixPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const request = convertRequest(req)

checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(
MAINNET_CODE_TRANSCRIPTION,
request,
swapInfo
)

const newRequest = await getMaxSwappable(
getFixedQuote,
Expand Down
6 changes: 6 additions & 0 deletions src/swap/central/godex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
getCodesWithTranscription,
getMaxSwappable,
InvalidCurrencyCodes,
Expand Down Expand Up @@ -363,6 +364,11 @@ export function makeGodexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
): Promise<EdgeSwapQuote> {
const request = convertRequest(req)
checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(
MAINNET_CODE_TRANSCRIPTION,
request,
swapInfo
)

const newRequest = await getMaxSwappable(
fetchSwapQuoteInner,
Expand Down
6 changes: 6 additions & 0 deletions src/swap/central/letsexchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
CurrencyCodeTranscriptionMap,
getCodesWithTranscription,
getMaxSwappable,
Expand Down Expand Up @@ -370,6 +371,11 @@ export function makeLetsExchangePlugin(
): Promise<EdgeSwapQuote> {
const request = convertRequest(req)
checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(
MAINNET_CODE_TRANSCRIPTION,
request,
swapInfo
)

const newRequest = await getMaxSwappable(
fetchSwapQuoteInner,
Expand Down
2 changes: 2 additions & 0 deletions src/swap/central/sideshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {

import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
ensureInFuture,
getCodesWithTranscription,
getMaxSwappable,
Expand Down Expand Up @@ -322,6 +323,7 @@ const createFetchSwapQuote = (api: SideshiftApi, affiliateId: string) =>
async function fetchSwapQuote(req: EdgeSwapRequest): Promise<EdgeSwapQuote> {
const request = convertRequest(req)
checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(MAINNET_CODE_TRANSCRIPTION, request, swapInfo)

const newRequest = await getMaxSwappable(
fetchSwapQuoteInner,
Expand Down
2 changes: 2 additions & 0 deletions src/swap/central/swapuz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { div18 } from '../../util/biggystringplus'
import {
checkInvalidCodes,
checkWhitelistedMainnetCodes,
ensureInFuture,
getCodesWithTranscription,
getMaxSwappable,
Expand Down Expand Up @@ -138,6 +139,7 @@ export function makeSwapuzPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const { fromWallet, toWallet } = request

checkInvalidCodes(INVALID_CURRENCY_CODES, request, swapInfo)
checkWhitelistedMainnetCodes(MAINNET_CODE_TRANSCRIPTION, request, swapInfo)

// Grab addresses:
const [fromAddress, toAddress] = await Promise.all([
Expand Down
15 changes: 14 additions & 1 deletion src/util/swapHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SwapCurrencyError
} from 'edge-core-js/types'

import { EdgeSwapRequestPlugin, MakeTxParams } from '../swap/types'
import { EdgeSwapRequestPlugin, MakeTxParams, StringMap } from '../swap/types'

const likeKindAssets = [
['BTC', 'WBTC', 'SBTC', 'RBTC'],
Expand Down Expand Up @@ -375,6 +375,19 @@ export function checkInvalidCodes(
throw new SwapCurrencyError(swapInfo, request)
}

export const checkWhitelistedMainnetCodes = (
whitelist: StringMap,
request: EdgeSwapRequest,
swapInfo: EdgeSwapInfo
): void => {
if (
whitelist[request.fromWallet.currencyInfo.pluginId] == null ||
whitelist[request.toWallet.currencyInfo.pluginId] == null
) {
throw new SwapCurrencyError(swapInfo, request)
}
}

export interface CurrencyCodeTranscriptions {
[code: string]: {
[code: string]: string
Expand Down

0 comments on commit 56a65ad

Please sign in to comment.