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 support for rpcUrl with basic auth when retrieving chainId on net… #9815

Merged
merged 7 commits into from
Nov 9, 2020
20 changes: 16 additions & 4 deletions ui/app/helpers/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,30 @@ export function constructTxParams({
* or throws an error in case of failure.
*/
export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) {
let fetchUrl = rpcUrl
const headers = {
'Content-Type': 'application/json',
}
// Convert basic auth URL component to Authorization header
const authMatches = rpcUrl.match('(http[s]?)://(.+):(.+)@(.+)')
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
// Confirm that we have matches, and a username and password
if (authMatches && authMatches[2] && authMatches[3]) {
// eslint-disable-next-line no-unused-vars
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
const [_, protocol, username, password, remainderUrl] = authMatches
const encodedAuth = btoa(`${username}:${password}`)
headers.Authorization = `Basic ${encodedAuth}`
fetchUrl = `${protocol}://${remainderUrl}`
}
const jsonRpcResponse = await window
.fetch(rpcUrl, {
.fetch(fetchUrl, {
method: 'POST',
body: JSON.stringify({
id: Date.now().toString(),
jsonrpc: '2.0',
method: rpcMethod,
params: rpcParams,
}),
headers: {
'Content-Type': 'application/json',
},
headers,
cache: 'default',
})
.then((httpResponse) => httpResponse.json())
Expand Down