Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to replace host for local network #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/mixpanel.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This lib is only for server side
import mixpanel from 'mixpanel'

export default mixpanel.init(process.env.MIXPANEL_ID)
export default () => {}
4 changes: 2 additions & 2 deletions data/services.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@
"f_type": "Service",
"f_vsn": "1.0.0",
"type": "authn",
"method": "IFRAME/RPC",
"method": "HTTP/POST",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this supposed to be an additional service?

"uid": "devwallet#authn",
"endpoint": "http://localhost:8701/fcl/authn",
"endpoint": "http://localhost:8701/api/authn",
"provider": {
"address": "0xDevWallet",
"name": "Dev Wallet",
Expand Down
6 changes: 5 additions & 1 deletion helpers/servicePipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
filterUniqueServices,
filterSupportedStrategies,
overrideServicePorts,
overrideServiceHost,
} from './services'
import {
NETWORKS,
Expand All @@ -31,6 +32,7 @@ export const getServicePipes = ({
supportedStrategies,
network,
portOverride,
hostOverride,
}) => {
const platform = getPlatformFromUserAgent(userAgent)
const isLocal = network === NETWORKS.LOCAL
Expand Down Expand Up @@ -98,7 +100,9 @@ export const getServicePipes = ({
// Add services if supported
serviceOfTypeAuthn,
// Allow port override option if local
partial(overrideServicePorts, isLocal, portOverride)
partial(overrideServicePorts, isLocal && portOverride, portOverride),
// Allow host override option if local
partial(overrideServiceHost, isLocal && hostOverride, hostOverride)
),
},
]
Expand Down
14 changes: 13 additions & 1 deletion helpers/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getInstallLinkFromMetadata,
getProviderMetadataByAddress,
} from './metadata'
import { replacePort } from './urls'
import { replaceHost, replacePort } from './urls'

export const filterSupportedStrategies =
(supportedStrategies = []) =>
Expand Down Expand Up @@ -137,3 +137,15 @@ export const overrideServicePorts = (
return s
})
}

export const overrideServiceHost = (
shouldOverride,
hostOverride,
services = []
) => {
if (!shouldOverride) return services
return services.map(s => {
s.endpoint = replaceHost(s.endpoint, hostOverride)
return s
})
}
6 changes: 6 additions & 0 deletions helpers/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export function replacePort(currentUrl: string, portOverride: string): string {
url.port = portOverride
return url.toString()
}

export function replaceHost(currentUrl: string, hostOverride: string): string {
let url = new URL(currentUrl)
url.host = hostOverride
return url.toString()
}
8 changes: 5 additions & 3 deletions pages/api/[...slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function runMiddleware(req, res, fn) {
async function handler(req, res) {
await runMiddleware(req, res, cors)

const { slug, discoveryType, port: portQuery } = req.query
const { slug, discoveryType, port: portQuery, host: hostQuery } = req.query
const {
fclVersion,
include,
Expand All @@ -38,6 +38,7 @@ async function handler(req, res) {
clientServices,
supportedStrategies,
port: portBody,
host: hostBody,
} = req.body
const isValid = isValidPath(slug)
const network = getNetworkFromPath(slug).toLowerCase()
Expand All @@ -48,11 +49,11 @@ async function handler(req, res) {
return res.status(400).json({ message: 'Invalid Network' })
}

mixpanel.track('Wallet Discovery Request', {
/*mixpanel.track('Wallet Discovery Request', {
type: discoveryRequestType,
network,
fclVersion,
})
})*/

const servicePipes = getServicePipes({
fclVersion,
Expand All @@ -63,6 +64,7 @@ async function handler(req, res) {
supportedStrategies,
network,
portOverride: portQuery || portBody,
hostOverride: hostQuery || hostBody,
})
const versionPipe = findMatchingPipeVersion(fclVersion, servicePipes)
const discoveryServices = versionPipe(servicesJson[network])
Expand Down