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

JsonRpcProvider: Basic access authentication support #97

Merged
merged 2 commits into from
Mar 10, 2024
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
} from "./config.js";

// Instantiate a provider for querying information on the chain!
//
// MAINNET_RPC_URL and an element of DISPATCHERS is a string representing an
// endpoint URL to the Pocket Network which may or may not contains basic
// authentication credentials e.g. "https://scott:[email protected]".
export const provider = new JsonRpcProvider({
rpcUrl: MAINNET_RPC_URL,
// If you'll Instantiate a relayer, you need to add dispatchers as well
Expand Down
1 change: 1 addition & 0 deletions packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@pokt-foundation/pocketjs-abstract-provider": "workspace:*",
"abort-controller": "^3.0.0",
"buffer": "^6.0.3",
"debug": "^4.3.3",
"undici": "^5.26.2"
}
Expand Down
20 changes: 16 additions & 4 deletions packages/provider/src/json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AbortController from 'abort-controller'
import debug from 'debug'
import { Buffer } from 'buffer'
import { fetch, Response } from 'undici'
import {
Account,
Expand Down Expand Up @@ -93,14 +94,25 @@ export class JsonRpcProvider implements AbstractProvider {
]
: this.rpcUrl

const routedRpcUrl = `${finalRpcUrl}${route}`
const headers = {
'Content-Type': 'application/json',
};

const url = new URL(route, finalRpcUrl);
if (url.username || url.password) {
const credential =
Buffer.from(`${url.username}:${url.password}`).toString('base64');
msmania marked this conversation as resolved.
Show resolved Hide resolved
headers['Authorization'] = `Basic ${credential}`;
url.username = '';
url.password = '';
}

const routedRpcUrl = url.toString();

const rpcResponse = await fetch(routedRpcUrl, {
method: 'POST',
signal: controller.signal as AbortSignal,
headers: {
'Content-Type': 'application/json',
},
headers: headers,
body: JSON.stringify(body),
}).catch((error) => {
debug(`${routedRpcUrl} attempt ${retriesPerformed + 1} failure`)
Expand Down
Loading
Loading