Skip to content

Commit

Permalink
add new authConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 30, 2024
1 parent 0bd23af commit 9a86ab1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/actions/getAtomicMatchBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
RENEGADE_API_KEY_HEADER,
REQUEST_EXTERNAL_MATCH_ROUTE,
} from '../constants.js'
import type { Config } from '../createConfig.js'
import type { AuthConfig } from '../createAuthConfig.js'
import { BaseError, type BaseErrorType } from '../errors/base.js'
import type { ExternalMatchResponse } from '../types/externalOrder.js'
import { postWithSymmetricKey } from '../utils/http.js'
Expand All @@ -22,7 +22,7 @@ export type GetAtomicMatchBundleReturnType = ExternalMatchResponse
export type GetAtomicMatchBundleErrorType = BaseErrorType

export async function getAtomicMatchBundle(
config: Config,
config: AuthConfig,
parameters: GetAtomicMatchBundleParameters,
): Promise<GetAtomicMatchBundleReturnType> {
const { base, quote, side, amount, minFillSize = BigInt(0) } = parameters
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/createAuthConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import invariant from 'tiny-invariant'
import type { BaseConfig } from './createConfig.js'
import type * as rustUtils from './utils.d.ts'

export type CreateAuthConfigParameters = {
apiKey: string
apiSecret: string
authServerUrl: string
utils?: typeof rustUtils
}

export function createAuthConfig(
parameters: CreateAuthConfigParameters,
): AuthConfig {
const { apiKey, apiSecret, authServerUrl } = parameters
invariant(
parameters.utils,
'Utils must be provided by the package if not supplied by the user.',
)
return {
utils: parameters.utils,
apiKey,
apiSecret,
getAuthServerUrl: (route = '') => {
const formattedRoute = route.startsWith('/') ? route : `/${route}`
return `${authServerUrl}${formattedRoute}`
},
}
}

export type AuthConfig = BaseConfig & {
apiSecret: string
apiKey: string
getAuthServerUrl: (route?: string) => string
}
23 changes: 5 additions & 18 deletions packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export type CreateConfigParameters = {
websocketPort?: number
viemClient?: PublicClient
adminKey?: string
apiSecret?: string
apiKey?: string
authServerUrl?: string
}

export function createConfig(parameters: CreateConfigParameters): Config {
Expand All @@ -51,9 +48,6 @@ export function createConfig(parameters: CreateConfigParameters): Config {
}),
websocketPort = 4000,
adminKey,
apiKey,
apiSecret,
authServerUrl,
} = parameters

invariant(
Expand Down Expand Up @@ -162,21 +156,18 @@ export function createConfig(parameters: CreateConfigParameters): Config {
},
viemClient,
adminKey,
apiKey,
apiSecret,
getAuthServerUrl: (route = '') => {
invariant(authServerUrl, 'Auth server URL not specified in config')
const formattedRoute = route.startsWith('/') ? route : `/${route}`
return `${authServerUrl}${formattedRoute}`
},
_internal: {
store,
ssr: Boolean(ssr),
},
}
}

export type Config = {
export type BaseConfig = {
utils: typeof rustUtils
}

export type Config = BaseConfig & {
readonly storage: Storage | null
darkPoolAddress: Address
getPriceReporterBaseUrl: () => string
Expand All @@ -198,12 +189,8 @@ export type Config = {
}
| undefined,
): () => void
utils: typeof rustUtils
viemClient: PublicClient
adminKey?: string
apiSecret?: string
apiKey?: string
getAuthServerUrl: (route?: string) => string
/**
* Not part of versioned API, proceed with caution.
* @internal
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,19 @@ export * from './constants.js'
////////////////////////////////////////////////////////////////////////////////

export {
type BaseConfig,
type Config,
type CreateConfigParameters,
type State,
createConfig,
} from '../createConfig.js'

export {
type AuthConfig,
type CreateAuthConfigParameters,
createAuthConfig,
} from '../createAuthConfig.js'

////////////////////////////////////////////////////////////////////////////////
// createStorage
////////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RENEGADE_SIG_EXPIRATION_HEADER_NAME,
SIG_EXPIRATION_BUFFER_MS,
} from '../constants.js'
import type { Config } from '../createConfig.js'
import type { BaseConfig, Config } from '../createConfig.js'
import { BaseError } from '../errors/base.js'
import { parseBigJSON } from './bigJSON.js'

Expand Down Expand Up @@ -189,7 +189,7 @@ export async function getRelayerWithAdmin(config: Config, url: string) {
}

export async function postWithSymmetricKey(
config: Config,
config: BaseConfig,
{
body,
headers = {},
Expand Down Expand Up @@ -226,7 +226,7 @@ function getPathFromUrl(url: string): string {

/// Add an auth expiration and signature to a set of headers
export function addExpiringAuthToHeaders(
config: Config,
config: BaseConfig,
path: string,
headers: Record<string, string>,
body: string,
Expand Down
14 changes: 12 additions & 2 deletions packages/node/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from '@renegade-fi/core'
export * from '@renegade-fi/core/actions'
export * from '@renegade-fi/core/constants'

import { createConfig as core_createConfig } from '@renegade-fi/core'
import { createConfig as core_createConfig, createAuthConfig as core_createAuthConfig } from '@renegade-fi/core'

import * as RustUtils from '../../renegade-utils/index.js'

Expand All @@ -16,7 +16,17 @@ function createConfig(
return config
}

export { createConfig }
function createAuthConfig(
...args: Parameters<typeof core_createAuthConfig>
): ReturnType<typeof core_createAuthConfig> {
const config = core_createAuthConfig({
...args[0],
utils: RustUtils,
})
return config
}

export { createAuthConfig, createConfig }

////////////////////////////////////////////////////////////////////////////////
// Actions
Expand Down
15 changes: 13 additions & 2 deletions packages/react/src/exports/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////////
// createConfig
////////////////////////////////////////////////////////////////////////////////
import { createConfig as core_createConfig } from '@renegade-fi/core'
import { createConfig as core_createConfig, createAuthConfig as core_createAuthConfig } from '@renegade-fi/core'

import * as RustUtils from '../../renegade-utils/index.js'

Expand All @@ -15,7 +15,18 @@ function createConfig(
return config
}

export { createConfig }

function createAuthConfig(
...args: Parameters<typeof core_createAuthConfig>
): ReturnType<typeof core_createAuthConfig> {
const config = core_createAuthConfig({
...args[0],
utils: RustUtils,
})
return config
}

export { createAuthConfig, createConfig }

////////////////////////////////////////////////////////////////////////////////
// Context
Expand Down

0 comments on commit 9a86ab1

Please sign in to comment.