forked from risc0/risc0-foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.rs
117 lines (98 loc) · 4.15 KB
/
publisher.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This application demonstrates how to send an off-chain proof request
// to the Bonsai proving service and publish the received proofs directly
// to your deployed app contract.
use alloy::{
network::EthereumWallet, providers::ProviderBuilder, signers::local::PrivateKeySigner,
sol_types::SolValue,
};
use alloy_primitives::{Address, U256};
use anyhow::{Context, Result};
use clap::Parser;
use methods::AB_ROTATION_ELF;
use risc0_ethereum_contracts::encode_seal;
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, VerifierContext};
use url::Url;
// // `IEvenNumber` interface automatically generated via the alloy `sol!` macro.
// alloy::sol!(
// #[sol(rpc, all_derives)]
// "../contracts/IABRotation.sol"
// );
/// Arguments of the publisher CLI.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Ethereum chain ID
#[clap(long)]
chain_id: u64,
/// Ethereum Node endpoint.
#[clap(long, env)]
eth_wallet_private_key: PrivateKeySigner,
/// Ethereum Node endpoint.
#[clap(long)]
rpc_url: Url,
/// Application's contract address on Ethereum
#[clap(long)]
contract: Address,
/// The input to provide to the guest binary
#[clap(short, long)]
input: U256,
}
fn main() -> Result<()> {
env_logger::init();
// Parse CLI Arguments: The application starts by parsing command-line arguments provided by the user.
let args = Args::parse();
// Create an alloy provider for that private key and URL.
let wallet = EthereumWallet::from(args.eth_wallet_private_key);
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_http(args.rpc_url);
// ABI encode input: Before sending the proof request to the Bonsai proving service,
// the input number is ABI-encoded to match the format expected by the guest code running in the zkVM.
let input = args.input.abi_encode();
let env = ExecutorEnv::builder().write_slice(&input).build()?;
let receipt = default_prover()
.prove_with_ctx(
env,
&VerifierContext::default(),
AB_ROTATION_ELF,
&ProverOpts::groth16(),
)?
.receipt;
// Encode the seal with the selector.
let seal = encode_seal(&receipt)?;
// Extract the journal from the receipt.
let journal = receipt.journal.bytes.clone();
// TODO: this
// Decode Journal: Upon receiving the proof, the application decodes the journal to extract
// the verified number. This ensures that the number being submitted to the blockchain matches
// the number that was verified off-chain.
// let x = U256::abi_decode(&journal, true).context("decoding journal data")?;
// Construct function call: Using the IEvenNumber interface, the application constructs
// the ABI-encoded function call for the set function of the EvenNumber contract.
// This call includes the verified number, the post-state digest, and the seal (proof).
// let contract = IEvenNumber::new(args.contract, provider);
// let call_builder = contract.set(x, seal.into());
//
// // Initialize the async runtime environment to handle the transaction sending.
// let runtime = tokio::runtime::Runtime::new()?;
//
// // Send transaction: Finally, send the transaction to the Ethereum blockchain,
// // effectively calling the set function of the EvenNumber contract with the verified number and proof.
// let pending_tx = runtime.block_on(call_builder.send())?;
// runtime.block_on(pending_tx.get_receipt())?;
Ok(())
}