From 5951e68f239cd6113c3d974b421b9c366aa022d8 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 2 Dec 2024 15:37:17 -0800 Subject: [PATCH] Add secret header to SideShift --- src/swap/central/sideshift.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/swap/central/sideshift.ts b/src/swap/central/sideshift.ts index b38fa448..65665857 100644 --- a/src/swap/central/sideshift.ts +++ b/src/swap/central/sideshift.ts @@ -36,7 +36,7 @@ import { SwapOrder } from '../../util/swapHelpers' import { convertRequest, getAddress, memoType } from '../../util/utils' -import { EdgeSwapRequestPlugin } from '../types' +import { EdgeSwapRequestPlugin, StringMap } from '../types' // See https://help.sideshift.ai/en/articles/4559664-which-coins-and-tokens-are-listed for list of supported currencies export const MAINNET_CODE_TRANSCRIPTION: CurrencyPluginIdSwapChainCodeMap = { @@ -153,7 +153,8 @@ interface CreateSideshiftApiResponse { const createSideshiftApi = ( baseUrl: string, - fetchCors: EdgeFetchFunction + fetchCors: EdgeFetchFunction, + privateKey?: string ): CreateSideshiftApiResponse => { async function request( method: 'GET' | 'POST', @@ -162,13 +163,18 @@ const createSideshiftApi = ( ): Promise { const url = `${baseUrl}${path}` + const headers: StringMap = { + 'Content-Type': 'application/json' + } + if (privateKey != null) { + headers['x-sideshift-secret'] = privateKey + } + const reply = await (method === 'GET' - ? fetchCors(url) + ? fetchCors(url, { headers }) : fetchCors(url, { method, - headers: { - 'Content-Type': 'application/json' - }, + headers, body: JSON.stringify(body) })) @@ -423,7 +429,11 @@ export function makeSideshiftPlugin( opts: EdgeCorePluginOptions ): EdgeSwapPlugin { const { io, initOptions } = opts - const api = createSideshiftApi(SIDESHIFT_BASE_URL, io.fetchCors ?? io.fetch) + const api = createSideshiftApi( + SIDESHIFT_BASE_URL, + io.fetchCors ?? io.fetch, + initOptions.privateKey + ) const fetchSwapQuote = createFetchSwapQuote( api, initOptions.affiliateId,