Skip to content

Commit

Permalink
Merge pull request #355 from EdgeApp/matthew/sideshift-private-key
Browse files Browse the repository at this point in the history
Add secret header to SideShift
  • Loading branch information
peachbits authored Dec 2, 2024
2 parents d55768f + 5951e68 commit e1fa770
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/swap/central/sideshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -153,7 +153,8 @@ interface CreateSideshiftApiResponse {

const createSideshiftApi = (
baseUrl: string,
fetchCors: EdgeFetchFunction
fetchCors: EdgeFetchFunction,
privateKey?: string
): CreateSideshiftApiResponse => {
async function request<R>(
method: 'GET' | 'POST',
Expand All @@ -162,13 +163,18 @@ const createSideshiftApi = (
): Promise<R> {
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)
}))

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e1fa770

Please sign in to comment.