Skip to content

Commit

Permalink
Enforce BONSAI_API_KEY usage (#71)
Browse files Browse the repository at this point in the history
enforce BONSAI_API_KEY usage
  • Loading branch information
capossele authored Jan 9, 2024
1 parent 1289e0d commit b48a796
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ It also provides an SDK in Rust that can be used to interact with it. You can ch
1. Send a callback request directly to the Relay by running:

```bash
cargo run --example offchain_request "$APP_ADDRESS" 10
BONSAI_API_KEY="YOUR_API_KEY_OR_EMPTY_IF_DEV_MODE" cargo run --example offchain_request "$APP_ADDRESS" 10
```

2. Check the relayed result:
Expand Down Expand Up @@ -156,7 +156,7 @@ You now have a deployment on a testnet that you can interact with sending either
1. Send a callback request directly to the Relay by running:

```bash
cargo run --example offchain_request "$APP_ADDRESS" 10
BONSAI_API_KEY="YOUR_API_KEY_OR_EMPTY_IF_DEV_MODE" cargo run --example offchain_request "$APP_ADDRESS" 10
```

2. Check the relayed result:
Expand Down
21 changes: 16 additions & 5 deletions relay/examples/offchain_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::env;

use alloy_primitives::U256;
use alloy_sol_types::SolValue;
use anyhow::Context;
Expand All @@ -38,18 +40,27 @@ struct Args {
bonsai_relay_api_url: String,

/// Bonsai API key. Used by the relay to send requests to the Bonsai proving
/// service. Defaults to empty, providing no authentication.
#[arg(long, env, default_value = "")]
bonsai_api_key: String,
/// service. Can be set to an empty string when DEV_MODE is enabled.
#[arg(long, env)]
bonsai_api_key: Option<String>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();
// check for bonsai_api_key
if args.bonsai_api_key.is_none() && env::var("BONSAI_API_KEY").is_err() {
eprintln!(
"Error: the following required arguments were not provided: \
\n'BONSAI_API_KEY' must be set either as an argument or as an environment variable. \
\nIf `DEV_MODE` is enabled, you can use an empty string."
);
std::process::exit(1);
}
// initialize a relay client
let relay_client = Client::from_parts(
args.bonsai_relay_api_url.clone(), // Set BONSAI_API_URL or replace this line.
args.bonsai_api_key.clone(), // Set BONSAI_API_KEY or replace this line.
args.bonsai_relay_api_url.clone(),
args.bonsai_api_key.unwrap(),
)
.context("Failed to initialize the relay client")?;

Expand Down
10 changes: 2 additions & 8 deletions tests/BonsaiStarter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ contract BonsaiStarterTest is BonsaiTest {
function testOffChainMock() public {
bytes32 imageId = queryImageId("FIBONACCI");
// Deploy a new starter instance
BonsaiStarter starter = new BonsaiStarter(
IBonsaiRelay(bonsaiRelay),
imageId
);
BonsaiStarter starter = new BonsaiStarter(IBonsaiRelay(bonsaiRelay), imageId);

// Anticipate a callback invocation on the starter contract
vm.expectCall(address(starter), abi.encodeWithSelector(BonsaiStarter.storeResult.selector));
Expand All @@ -48,10 +45,7 @@ contract BonsaiStarterTest is BonsaiTest {
// Test the BonsaiStarter contract by mocking an on-chain callback request
function testOnChainMock() public {
// Deploy a new starter instance
BonsaiStarter starter = new BonsaiStarter(
IBonsaiRelay(bonsaiRelay),
queryImageId("FIBONACCI")
);
BonsaiStarter starter = new BonsaiStarter(IBonsaiRelay(bonsaiRelay), queryImageId("FIBONACCI"));

// Anticipate an on-chain callback request to the relay
vm.expectCall(address(bonsaiRelay), abi.encodeWithSelector(IBonsaiRelay.requestCallback.selector));
Expand Down
4 changes: 1 addition & 3 deletions tests/BonsaiStarterLowLevel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ contract BonsaiStarterLowLevelTest is BonsaiTest {

function testMockLowLevelCall() public {
// Deploy a new starter instance
BonsaiStarterLowLevel starter = new BonsaiStarterLowLevel(
IBonsaiRelay(bonsaiRelay),
queryImageId('FIBONACCI'));
BonsaiStarterLowLevel starter = new BonsaiStarterLowLevel(IBonsaiRelay(bonsaiRelay), queryImageId("FIBONACCI"));

// Anticipate a callback request to the relay
vm.expectCall(address(bonsaiRelay), abi.encodeWithSelector(IBonsaiRelay.requestCallback.selector));
Expand Down

0 comments on commit b48a796

Please sign in to comment.