-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlib.rs
More file actions
149 lines (126 loc) · 4.66 KB
/
lib.rs
File metadata and controls
149 lines (126 loc) · 4.66 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use aligned_sdk::sdk::{submit, verify_proof_onchain};
use aligned_sdk::types::{AlignedVerificationData, Chain, ProvingSystemId, VerificationData};
use dialoguer::Confirm;
use ethers::core::k256::ecdsa::SigningKey;
use ethers::prelude::*;
use ethers::providers::{Http, Provider};
use ethers::signers::{LocalWallet, Wallet};
use ethers::types::Address;
pub mod risc0;
pub mod sp1;
pub mod jolt;
pub mod utils;
const BATCHER_URL: &str = "wss://batcher.alignedlayer.com";
const BATCHER_PAYMENTS_ADDRESS: &str = "0x815aeCA64a974297942D2Bbf034ABEe22a38A003";
pub async fn submit_proof_and_wait_for_verification(
verification_data: VerificationData,
wallet: Wallet<SigningKey>,
rpc_url: String,
) -> anyhow::Result<AlignedVerificationData> {
let res = submit(BATCHER_URL, &verification_data, wallet)
.await
.map_err(|e| anyhow::anyhow!("Failed to submit proof for verification: {:?}", e))?;
match res {
Some(aligned_verification_data) => {
println!(
"Proof submitted successfully on batch {}, waiting for verification...",
hex::encode(aligned_verification_data.batch_merkle_root)
);
for _ in 0..10 {
if verify_proof_onchain(
aligned_verification_data.clone(),
Chain::Holesky,
rpc_url.as_str(),
)
.await
.is_ok_and(|r| r)
{
return Ok(aligned_verification_data);
}
println!("Proof not verified yet. Waiting 10 seconds before checking again...");
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
}
anyhow::bail!("Proof verification failed");
}
None => {
anyhow::bail!("Proof submission failed, no verification data");
}
}
}
pub async fn pay_batcher(
from: Address,
signer: Arc<SignerMiddleware<Provider<Http>, LocalWallet>>,
) -> anyhow::Result<()> {
if !Confirm::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt("We are going to pay 0.004eth for the proof submission to aligned. Do you want to continue?")
.interact()
.expect("Failed to read user input")
{
anyhow::bail!("Payment cancelled")
}
let addr = Address::from_str(BATCHER_PAYMENTS_ADDRESS).map_err(|e| anyhow::anyhow!(e))?;
let tx = TransactionRequest::new()
.from(from)
.to(addr)
.value(4000000000000000u128);
match signer
.send_transaction(tx, None)
.await
.map_err(|e| anyhow::anyhow!("Failed to send tx {}", e))?
.await
.map_err(|e| anyhow::anyhow!("Failed to submit tx {}", e))?
{
Some(receipt) => {
println!(
"Payment sent. Transaction hash: {:x}",
receipt.transaction_hash
);
Ok(())
}
None => {
anyhow::bail!("Payment failed");
}
}
}
pub fn submit_proof_to_aligned(
keystore_path: PathBuf,
proof_path: &str,
elf_path: &str,
proof_system_id: ProvingSystemId,
) -> anyhow::Result<()> {
let keystore_password = rpassword::prompt_password("Enter keystore password: ")
.expect("Failed to read keystore password");
let wallet = LocalWallet::decrypt_keystore(keystore_path, keystore_password)
.expect("Failed to decrypt keystore")
.with_chain_id(17000u64);
let proof = fs::read(proof_path).expect("failed to serialize proof");
let elf_data = fs::read(elf_path).expect("failed to serialize elf");
let rpc_url = "https://ethereum-holesky-rpc.publicnode.com";
let provider = Provider::<Http>::try_from(rpc_url).expect("Failed to connect to provider");
let signer = Arc::new(SignerMiddleware::new(provider.clone(), wallet.clone()));
let runtime = tokio::runtime::Runtime::new().expect("Failed to create runtime");
runtime
.block_on(pay_batcher(wallet.address(), signer.clone()))
.expect("Failed to pay for proof submission");
let verification_data = VerificationData {
proving_system: proof_system_id,
proof,
proof_generator_addr: wallet.address(),
vm_program_code: Some(elf_data),
verification_key: None,
pub_input: None,
};
println!("Submitting proof to aligned for verification");
runtime
.block_on(submit_proof_and_wait_for_verification(
verification_data,
wallet,
rpc_url.to_string(),
))
.expect("failed to submit proof");
Ok(())
}