Skip to content

Commit

Permalink
[fortuna] bind provider and contract address (#1218)
Browse files Browse the repository at this point in the history
* bind provider and contract address

* pre commit
  • Loading branch information
Dev Kalra authored Jan 10, 2024
1 parent 43bc28d commit 0eee513
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fortuna/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fortuna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fortuna"
version = "3.1.0"
version = "3.2.0"
edition = "2021"

[dependencies]
Expand Down
29 changes: 19 additions & 10 deletions fortuna/src/command/register_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ use {
state::PebbleHashChain,
},
anyhow::Result,
ethers::{
signers::{
LocalWallet,
Signer,
},
types::Address,
},
std::sync::Arc,
};

Expand All @@ -20,22 +27,24 @@ pub struct CommitmentMetadata {
/// Register as a randomness provider. This method will generate and commit to a new random
/// hash chain from the configured secret & a newly generated random value.
pub async fn register_provider(opts: &RegisterProviderOptions) -> Result<()> {
// Initialize a Provider to interface with the EVM contract.
let contract = Arc::new(
SignablePythContract::from_config(
&Config::load(&opts.config.config)?.get_chain_config(&opts.chain_id)?,
&opts.private_key,
)
.await?,
);
let chain_config = Config::load(&opts.config.config)?.get_chain_config(&opts.chain_id)?;

// Initialize a Provider to interface with the EVM contract.
let contract =
Arc::new(SignablePythContract::from_config(&chain_config, &opts.private_key).await?);
// Create a new random hash chain.
let random = rand::random::<[u8; 32]>();
let secret = opts.randomness.load_secret()?;

let commitment_length = opts.randomness.chain_length;
let mut chain =
PebbleHashChain::from_config(&secret, &opts.chain_id, &random, commitment_length)?;
let mut chain = PebbleHashChain::from_config(
&secret,
&opts.chain_id,
&opts.private_key.clone().parse::<LocalWallet>()?.address(),
&chain_config.contract_addr,
&random,
commitment_length,
)?;

// Arguments to the contract to register our new provider.
let fee_in_wei = opts.fee;
Expand Down
2 changes: 2 additions & 0 deletions fortuna/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
let hash_chain = PebbleHashChain::from_config(
&secret,
&chain_id,
&opts.provider,
&chain_config.contract_addr,
&metadata.seed,
metadata.chain_length,
)?;
Expand Down
5 changes: 5 additions & 0 deletions fortuna/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
ensure,
Result,
},
ethers::types::Address,
sha3::{
Digest,
Keccak256,
Expand Down Expand Up @@ -35,12 +36,16 @@ impl PebbleHashChain {
pub fn from_config(
secret: &str,
chain_id: &ChainId,
provider_address: &Address,
contract_address: &Address,
random: &[u8; 32],
chain_length: u64,
) -> Result<Self> {
let mut input: Vec<u8> = vec![];
input.extend_from_slice(&hex::decode(secret)?);
input.extend_from_slice(&chain_id.as_bytes());
input.extend_from_slice(&provider_address.as_bytes());
input.extend_from_slice(&contract_address.as_bytes());
input.extend_from_slice(random);

let secret: [u8; 32] = Keccak256::digest(input).into();
Expand Down

0 comments on commit 0eee513

Please sign in to comment.