Skip to content

Commit 38d29a2

Browse files
committed
wip
1 parent 181c32d commit 38d29a2

File tree

5 files changed

+59
-286
lines changed

5 files changed

+59
-286
lines changed

crates/cartridge/src/vrf/bootstrap.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
//! This module uses the starknet crate's account abstraction for transaction handling.
99
10-
use std::net::SocketAddr;
1110
use std::sync::Arc;
1211
use std::time::{Duration, Instant};
1312

@@ -123,28 +122,31 @@ pub fn derive_vrf_accounts(
123122
})
124123
}
125124

126-
pub async fn bootstrap_vrf(rpc_addr: SocketAddr, chain: &ChainSpec) -> Result<VrfBootstrapResult> {
127-
let rpc_url = Url::parse(&format!("http://{rpc_addr}"))?;
125+
pub async fn bootstrap_vrf(
126+
rpc_url: Url,
127+
bootstrapper_account_address: ContractAddress,
128+
bootstrapper_account_private_key: Felt,
129+
) -> Result<VrfBootstrapResult> {
128130
let provider = Arc::new(JsonRpcClient::new(HttpTransport::new(rpc_url.clone())));
129131

130-
let bootstrapper_account = prefunded_account(chain, 0)?;
131-
132132
// Get chain ID from the node
133133
let chain_id_felt = provider.chain_id().await.context("failed to get chain ID from node")?;
134134
let chain_id = ChainId::Id(chain_id_felt);
135135

136-
let derived = derive_vrf_accounts(bootstrapper_account.0, bootstrapper_account.1)?;
136+
let derived =
137+
derive_vrf_accounts(bootstrapper_account_address, bootstrapper_account_private_key)?;
137138
let vrf_account_address = derived.vrf_account_address;
138139
let account_public_key =
139-
SigningKey::from_secret_scalar(bootstrapper_account.1).verifying_key().scalar();
140+
SigningKey::from_secret_scalar(bootstrapper_account_private_key).verifying_key().scalar();
140141
let vrf_account_class_hash = vrf_account_class_hash()?;
141142

142143
// Create the source account for transactions
143-
let signer = LocalWallet::from(SigningKey::from_secret_scalar(bootstrapper_account.1));
144+
let signer =
145+
LocalWallet::from(SigningKey::from_secret_scalar(bootstrapper_account_private_key));
144146
let account = SingleOwnerAccount::new(
145147
provider.clone(),
146148
signer,
147-
bootstrapper_account.0.into(),
149+
bootstrapper_account_address.into(),
148150
chain_id_felt,
149151
ExecutionEncoding::New,
150152
);
@@ -180,7 +182,8 @@ pub async fn bootstrap_vrf(rpc_addr: SocketAddr, chain: &ChainSpec) -> Result<Vr
180182

181183
// Set VRF public key on the deployed account
182184
// Create account for the VRF account to call set_vrf_public_key on itself
183-
let vrf_signer = LocalWallet::from(SigningKey::from_secret_scalar(bootstrapper_account.1));
185+
let vrf_signer =
186+
LocalWallet::from(SigningKey::from_secret_scalar(bootstrapper_account_private_key));
184187
let mut vrf_account = SingleOwnerAccount::new(
185188
provider.clone(),
186189
vrf_signer,
@@ -232,7 +235,7 @@ pub async fn bootstrap_vrf(rpc_addr: SocketAddr, chain: &ChainSpec) -> Result<Vr
232235
chain_id,
233236
vrf_account_address,
234237
// right now, we take the bootstrapper account private key as the vrf account's private key
235-
vrf_account_private_key: bootstrapper_account.1,
238+
vrf_account_private_key: bootstrapper_account_private_key,
236239
})
237240
}
238241

@@ -309,21 +312,6 @@ fn felt_from_field<T: std::fmt::Display>(value: T) -> Result<Felt> {
309312
Felt::from_dec_str(&decimal).map_err(|err| anyhow!("invalid field value: {err}"))
310313
}
311314

312-
fn prefunded_account(chain_spec: &ChainSpec, index: u16) -> Result<(ContractAddress, Felt)> {
313-
let (address, allocation) = chain_spec
314-
.genesis()
315-
.accounts()
316-
.nth(index as usize)
317-
.ok_or_else(|| anyhow!("prefunded account index {} out of range", index))?;
318-
319-
let private_key = match allocation {
320-
GenesisAccountAlloc::DevAccount(account) => account.private_key,
321-
_ => return Err(anyhow!("prefunded account {} has no private key", address)),
322-
};
323-
324-
Ok((*address, private_key))
325-
}
326-
327315
#[cfg(test)]
328316
mod tests {
329317
use katana_primitives::Felt;

crates/cli/src/args.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ pub struct SequencerNodeArgs {
139139
#[command(flatten)]
140140
pub paymaster: PaymasterOptions,
141141

142-
#[cfg(feature = "vrf")]
143-
#[command(flatten)]
144-
pub vrf: VrfOptions,
145-
146142
#[cfg(feature = "tee")]
147143
#[command(flatten)]
148144
pub tee: TeeOptions,
@@ -217,26 +213,21 @@ impl SequencerNodeArgs {
217213
};
218214

219215
#[cfg(feature = "vrf")]
220-
let mut vrf = if self.vrf.is_external() {
216+
let mut vrf = if self.paymaster.vrf.is_external() {
221217
None
222218
} else {
223-
use cartridge::{VrfService, VrfServiceConfig};
219+
use crate::sidecar::bootstrap_vrf;
224220

225-
let result = cartridge::bootstrap_vrf(
221+
let vrf = bootstrap_vrf(
222+
&self.paymaster.vrf,
226223
handle.rpc().addr().clone(),
227224
&handle.node().config().chain,
228225
)
229-
.await?;
230-
231-
let vrf_process = VrfService::new(VrfServiceConfig {
232-
secret_key: result.secret_key,
233-
vrf_account_address: result.vrf_account_address,
234-
vrf_private_key: result.vrf_account_private_key,
235-
})
226+
.await?
236227
.start()
237228
.await?;
238229

239-
Some(vrf_process)
230+
Some(vrf)
240231
};
241232

242233
// Wait until an OS signal (ie SIGINT, SIGTERM) is received or the node is shutdown.
@@ -279,14 +270,6 @@ impl SequencerNodeArgs {
279270
#[cfg(feature = "paymaster")]
280271
let paymaster = self.paymaster_config(&chain)?;
281272

282-
#[cfg(feature = "vrf")]
283-
let vrf = self.vrf_config()?;
284-
285-
#[cfg(all(feature = "vrf", feature = "cartridge"))]
286-
if vrf.is_some() && paymaster.is_none() {
287-
return Err(anyhow::anyhow!("--vrf requires paymaster; enable --paymaster"));
288-
}
289-
290273
// the `katana init` will automatically generate a messaging config. so if katana is run
291274
// with `--chain` then the `--messaging` flag is not required. this is temporary and
292275
// the messagign config will eventually be removed slowly.
@@ -351,7 +334,7 @@ impl SequencerNodeArgs {
351334
// We put it here so that even when the individual api are explicitly specified
352335
// (ie `--rpc.api`) we guarantee that the cartridge rpc is enabled.
353336
#[cfg(feature = "cartridge")]
354-
if self.paymaster.enabled || self.vrf.enabled {
337+
if self.paymaster.enabled || self.paymaster.vrf.enabled {
355338
modules.add(RpcModuleKind::Cartridge);
356339
}
357340

@@ -431,7 +414,7 @@ impl SequencerNodeArgs {
431414
}
432415

433416
#[cfg(feature = "vrf")]
434-
if self.vrf.enabled {
417+
if self.paymaster.vrf.enabled {
435418
katana_slot_controller::add_vrf_account_class(&mut chain_spec.genesis);
436419
katana_slot_controller::add_vrf_consumer_class(&mut chain_spec.genesis);
437420
}
@@ -607,7 +590,7 @@ impl SequencerNodeArgs {
607590

608591
#[cfg(feature = "vrf")]
609592
fn vrf_config(&self) -> Result<Option<VrfConfig>> {
610-
build_vrf_config(&self.vrf)
593+
build_vrf_config(&self.paymaster.vrf)
611594
}
612595

613596
#[cfg(feature = "tee")]
@@ -686,11 +669,6 @@ impl SequencerNodeArgs {
686669
self.paymaster.merge(config.paymaster.as_ref());
687670
}
688671

689-
#[cfg(feature = "vrf")]
690-
{
691-
self.vrf.merge(config.vrf.as_ref());
692-
}
693-
694672
#[cfg(feature = "explorer")]
695673
{
696674
if !self.explorer.explorer {

crates/cli/src/file.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ pub struct NodeArgsConfig {
3030
pub cartridge: Option<CartridgeOptions>,
3131
#[cfg(feature = "paymaster")]
3232
pub paymaster: Option<PaymasterOptions>,
33-
#[cfg(feature = "vrf")]
34-
pub vrf: Option<VrfOptions>,
3533
#[cfg(feature = "explorer")]
3634
pub explorer: Option<ExplorerOptions>,
3735
}
@@ -98,11 +96,6 @@ impl TryFrom<SequencerNodeArgs> for NodeArgsConfig {
9896
};
9997
}
10098

101-
#[cfg(feature = "vrf")]
102-
{
103-
node_config.vrf = if args.vrf == VrfOptions::default() { None } else { Some(args.vrf) };
104-
}
105-
10699
#[cfg(feature = "explorer")]
107100
{
108101
node_config.explorer = if args.explorer == ExplorerOptions::default() {

crates/cli/src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,10 @@ pub struct PaymasterOptions {
608608
#[arg(requires_all = ["paymaster_enabled", "cartridge_paymaster"])]
609609
#[serde(default = "default_api_url")]
610610
pub cartridge_api: Url,
611+
612+
#[cfg(feature = "vrf")]
613+
#[command(flatten)]
614+
pub vrf: VrfOptions,
611615
}
612616

613617
#[cfg(feature = "paymaster")]
@@ -638,6 +642,10 @@ impl PaymasterOptions {
638642
if self.bin.is_none() {
639643
self.bin = other.bin.clone();
640644
}
645+
646+
if self.vrf == VrfOptions::default() {
647+
self.vrf = other.vrf.clone();
648+
}
641649
}
642650
}
643651
}
@@ -650,7 +658,10 @@ pub struct VrfOptions {
650658
///
651659
/// By default, the VRF runs as a sidecar process. If `--vrf.url` is provided,
652660
/// it will connect to an external VRF service instead.
661+
///
662+
/// Requires the Cartridge paymaster to be enabled i.e., `--paymaster.cartridge`.
653663
#[arg(long = "vrf", id = "vrf_enabled")]
664+
#[arg(requires = "cartridge_paymaster")]
654665
#[serde(default)]
655666
pub enabled: bool,
656667

@@ -730,6 +741,8 @@ impl Default for PaymasterOptions {
730741
price_api_key: None,
731742
cartridge_paymaster: false,
732743
cartridge_api: default_api_url(),
744+
#[cfg(feature = "vrf")]
745+
vrf: VrfOptions::default(),
733746
}
734747
}
735748
}

0 commit comments

Comments
 (0)