Skip to content

Commit 4c8ab91

Browse files
committed
Make API tokens required to connect to hypersync, and don't force a login for envio start
1 parent d8171c4 commit 4c8ab91

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed

codegenerator/cli/npm/envio/src/sources/HyperSyncSource.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ type options = {
151151
allEventSignatures: array<string>,
152152
shouldUseHypersyncClientDecoder: bool,
153153
eventRouter: EventRouter.t<Internal.evmEventConfig>,
154-
apiToken: option<string>,
154+
apiToken: option<string>, // this should be required (not an option)
155155
clientMaxRetries: int,
156156
clientTimeoutMillis: int,
157157
lowercaseAddresses: bool,
@@ -181,7 +181,7 @@ let make = (
181181
~url=endpointUrl,
182182
~apiToken,
183183
~maxNumRetries=clientMaxRetries,
184-
~httpReqTimeoutMillis=clientTimeoutMillis,
184+
~httpReqTimeoutMillis=clientTimeoutMillis,
185185
~enableChecksumAddresses=!lowercaseAddresses,
186186
)
187187

codegenerator/cli/src/commands.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub mod login {
364364
use tokio::time::sleep;
365365

366366
/// Default UI/API base URL. Change this constant to point to your deployment.
367-
pub const AUTH_BASE_URL: &str = "http://localhost:3000";
367+
pub const AUTH_BASE_URL: &str = "https://envio.dev";
368368

369369
fn get_api_base_url() -> String {
370370
// Allow override via ENVIO_API_URL, otherwise use the constant above.
@@ -382,28 +382,9 @@ pub mod login {
382382
expires_in: i32,
383383
}
384384

385-
#[derive(Debug, Deserialize)]
386-
struct User {
387-
#[allow(dead_code)]
388-
id: Option<String>,
389-
#[allow(dead_code)]
390-
name: Option<String>,
391-
#[allow(dead_code)]
392-
email: Option<String>,
393-
#[allow(dead_code)]
394-
#[serde(rename = "githubId")]
395-
github_id: Option<String>,
396-
#[allow(dead_code)]
397-
#[serde(rename = "githubLogin")]
398-
github_login: Option<String>,
399-
}
400-
401385
#[derive(Debug, Deserialize)]
402386
struct CliAuthStatus {
403387
completed: bool,
404-
#[allow(dead_code)]
405-
user: Option<User>,
406-
#[allow(dead_code)]
407388
error: Option<String>,
408389
token: Option<String>,
409390
}
@@ -607,6 +588,30 @@ pub mod hypersync {
607588
TokenManager::new(SERVICE_NAME, HYPERSYNC_ACCOUNT).store_token(token)
608589
}
609590

591+
pub async fn get_hypersync_token() -> Result<Option<String>> {
592+
// 1) If we already have a token in keyring, use it
593+
if let Some(existing) = TokenManager::new(SERVICE_NAME, HYPERSYNC_ACCOUNT).get_token()? {
594+
return Ok(Some(existing));
595+
}
596+
597+
// 2) If we have a JWT, try to list existing tokens without creating a new one
598+
let jwt = match get_stored_jwt()? {
599+
Some(t) => t,
600+
None => return Ok(None), // Not logged in; do not login or provision
601+
};
602+
603+
let base = get_hypersync_tokens_base_url();
604+
let client = reqwest::Client::new();
605+
let tokens = list_api_tokens(&client, &base, &jwt)
606+
.await
607+
.unwrap_or_default();
608+
if let Some(token) = tokens.get(0) {
609+
store_api_token(token)?;
610+
return Ok(Some(token.clone()));
611+
}
612+
Ok(None)
613+
}
614+
610615
pub async fn provision_and_get_token() -> Result<String> {
611616
let base = get_hypersync_tokens_base_url();
612617
let jwt = match get_stored_jwt()? {

codegenerator/cli/src/executor/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub async fn execute(command_line_args: CommandLineArgs) -> Result<()> {
7676

7777
commands::db_migrate::run_db_setup(&config, &persisted_state).await?;
7878
}
79-
// Populate HyperSync API token from vault unless .env overrides
79+
// During `start`, attempt to retrieve an existing HyperSync token without logging in or creating one.
8080
{
8181
use crate::config_parsing::system_config::{DataSource, MainEvmDataSource};
8282
let uses_hypersync = config.get_networks().iter().any(|n| match &n.sync_source {
@@ -86,13 +86,7 @@ pub async fn execute(command_line_args: CommandLineArgs) -> Result<()> {
8686
DataSource::Fuel { .. } => true,
8787
});
8888
if uses_hypersync {
89-
// Do not prompt login for start; best-effort provision if possible
90-
if let Err(e) = commands::hypersync::provision_and_get_token().await {
91-
eprintln!(
92-
"Warning: could not provision HyperSync token automatically: {}",
93-
e
94-
);
95-
}
89+
let _ = commands::hypersync::get_hypersync_token().await;
9690
}
9791
}
9892

codegenerator/cli/templates/static/codegen/src/eventFetching/NetworkSources.res

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,36 @@ let evm = (
2121
->EventRouter.fromEvmEventModsOrThrow(~chain)
2222

2323
let sources = switch hyperSync {
24-
| Some(endpointUrl) => [
25-
HyperSyncSource.make({
26-
chain,
27-
contracts,
28-
endpointUrl,
29-
allEventSignatures,
30-
eventRouter,
31-
shouldUseHypersyncClientDecoder: Env.Configurable.shouldUseHypersyncClientDecoder->Option.getWithDefault(
32-
shouldUseHypersyncClientDecoder,
33-
),
34-
apiToken: Env.envioApiToken,
35-
clientMaxRetries: Env.hyperSyncClientMaxRetries,
36-
clientTimeoutMillis: Env.hyperSyncClientTimeoutMillis,
37-
lowercaseAddresses,
38-
}),
39-
]
24+
| Some(endpointUrl) => {
25+
switch Env.envioApiToken {
26+
| Some(_) => ()
27+
| None => {
28+
Js.Console.error(
29+
"HyperSync is configured as a datasource but ENVIO_API_TOKEN is not set.",
30+
)
31+
Js.Console.error(
32+
"Please run 'envio login` to log in, or alternatively add your ENVIO_API_TOKEN to your project .env file.",
33+
)
34+
NodeJs.process->NodeJs.exitWithCode(Failure)
35+
}
36+
}
37+
[
38+
HyperSyncSource.make({
39+
chain,
40+
contracts,
41+
endpointUrl,
42+
allEventSignatures,
43+
eventRouter,
44+
shouldUseHypersyncClientDecoder: Env.Configurable.shouldUseHypersyncClientDecoder->Option.getWithDefault(
45+
shouldUseHypersyncClientDecoder,
46+
),
47+
apiToken: Env.envioApiToken,
48+
clientMaxRetries: Env.hyperSyncClientMaxRetries,
49+
clientTimeoutMillis: Env.hyperSyncClientTimeoutMillis,
50+
lowercaseAddresses,
51+
}),
52+
]
53+
}
4054
| _ => []
4155
}
4256
rpcs->Js.Array2.forEach(({?syncConfig, url, sourceFor}) => {

0 commit comments

Comments
 (0)