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

feat: Added API Keys #190

Closed
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### Added
- RPC API Keys used to interact with services such as Pagoda Console.

## [0.5.0]
### Added
- Error handling with opaque `workspaces::error::Error` type: https://github.com/near/workspaces-rs/pull/149
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,4 @@ For a full example, take a look at [workspaces/tests/deploy_project.rs](https://
These environment variables will be useful if there was ever a snag hit:
- `NEAR_RPC_TIMEOUT_SECS`: The default is 10 seconds, but this is the amount of time beforing timing out waiting for a RPC service when talking to the sandbox or any other network such as testnet.
- `NEAR_SANDBOX_BIN_PATH`: Set this to our own prebuilt `neard-sandbox` bin path if we want to use a non-default version of the sandbox or configure nearcore with our own custom features that we want to test in workspaces.
- `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics.
2 changes: 1 addition & 1 deletion workspaces/src/network/betanet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Betanet {

impl Betanet {
pub(crate) async fn new() -> crate::result::Result<Self> {
let client = Client::new(RPC_URL);
let client = Client::new(RPC_URL)?;
client.wait_for_rpc().await?;

Ok(Self {
Expand Down
4 changes: 2 additions & 2 deletions workspaces/src/network/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Mainnet {

impl Mainnet {
pub(crate) async fn new() -> Result<Self> {
let client = Client::new(RPC_URL);
let client = Client::new(RPC_URL)?;
client.wait_for_rpc().await?;

Ok(Self {
Expand All @@ -38,7 +38,7 @@ impl Mainnet {
}

pub(crate) async fn archival() -> Result<Self> {
let client = Client::new(ARCHIVAL_URL);
let client = Client::new(ARCHIVAL_URL)?;
client.wait_for_rpc().await?;

Ok(Self {
Expand Down
2 changes: 1 addition & 1 deletion workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Sandbox {
pub(crate) async fn new() -> Result<Self> {
let mut server = SandboxServer::default();
server.start()?;
let client = Client::new(&server.rpc_addr());
let client = Client::new(&server.rpc_addr())?;
client.wait_for_rpc().await?;

let info = Info {
Expand Down
4 changes: 2 additions & 2 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Testnet {

impl Testnet {
pub(crate) async fn new() -> Result<Self> {
let client = Client::new(RPC_URL);
let client = Client::new(RPC_URL)?;
client.wait_for_rpc().await?;

Ok(Self {
Expand All @@ -46,7 +46,7 @@ impl Testnet {
}

pub(crate) async fn archival() -> Result<Self> {
let client = Client::new(ARCHIVAL_URL);
let client = Client::new(ARCHIVAL_URL)?;
client.wait_for_rpc().await?;

Ok(Self {
Expand Down
13 changes: 9 additions & 4 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ pub struct Client {
}

impl Client {
pub(crate) fn new(rpc_addr: &str) -> Self {
pub(crate) fn new(rpc_addr: &str) -> Result<Self> {
let connector = JsonRpcClient::new_client();
let rpc_client = connector.connect(rpc_addr);
let mut rpc_client = connector.connect(rpc_addr);
if let Ok(api_key) = std::env::var("NEAR_RPC_API_KEY") {
miraclx marked this conversation as resolved.
Show resolved Hide resolved
let api_key = near_jsonrpc_client::auth::ApiKey::new(api_key)
.map_err(|e| ErrorKind::DataConversion.custom(e))?;
Comment on lines +49 to +54
Copy link
Contributor

@austinabell austinabell Sep 9, 2022

Choose a reason for hiding this comment

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

Since we don't have the ability to override the RPC URL, this feels like a half-baked solution that won't be usable by anyone. I wonder if we should have a separate endpoint, like workspaces::mainnet, except that it points to RPCaaS URL and requires an API key so this can't be misused. API keys are specific to that, so exposing the setting an API key for mainnet in general seems odd

Copy link
Member Author

Choose a reason for hiding this comment

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

right, forgot that the Console will have it's own dedicated RPC URL instead of the defaults ones on *.near.org, so thought this would be low hanging fruit to add like this. This goes into the territory of custom networks with all of its quirks, but it does seem easy to just add something like workspaces::custom to point to a custom endpoint

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, or maybe providing reasoning for #103 so that this functionality could exist in a separate crate or any other community need. I feel like custom might not solve this issue since API keys are only applicable to the new RPCaaS endpoints so would be janky to include with that API.

Either way, if we do introduce this ability, it should be an unstable API as the API keys and endpoints might change in the future

rpc_client = rpc_client.header(api_key);
}

Self {
Ok(Self {
rpc_client,
rpc_addr: rpc_addr.into(),
access_key_nonces: RwLock::new(HashMap::new()),
}
})
}

pub(crate) async fn query_broadcast_tx(
Expand Down