mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Add support for rpcUrl with basic auth when retrieving chainId on net… (#9815)
This commit is contained in:
parent
248f171b65
commit
e72f943951
@ -464,8 +464,22 @@ export function constructTxParams({
|
|||||||
* or throws an error in case of failure.
|
* or throws an error in case of failure.
|
||||||
*/
|
*/
|
||||||
export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) {
|
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 { origin, pathname, username, password, search } = new URL(rpcUrl)
|
||||||
|
// URLs containing username and password needs special processing
|
||||||
|
if (username && password) {
|
||||||
|
const encodedAuth = Buffer.from(`${username}:${password}`).toString(
|
||||||
|
'base64',
|
||||||
|
)
|
||||||
|
headers.Authorization = `Basic ${encodedAuth}`
|
||||||
|
fetchUrl = `${origin}${pathname}${search}`
|
||||||
|
}
|
||||||
const jsonRpcResponse = await window
|
const jsonRpcResponse = await window
|
||||||
.fetch(rpcUrl, {
|
.fetch(fetchUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id: Date.now().toString(),
|
id: Date.now().toString(),
|
||||||
@ -473,9 +487,7 @@ export async function jsonRpcRequest(rpcUrl, rpcMethod, rpcParams = []) {
|
|||||||
method: rpcMethod,
|
method: rpcMethod,
|
||||||
params: rpcParams,
|
params: rpcParams,
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers,
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
cache: 'default',
|
cache: 'default',
|
||||||
})
|
})
|
||||||
.then((httpResponse) => httpResponse.json())
|
.then((httpResponse) => httpResponse.json())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user