forked from taikoxyz/raiko
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone_shot.rs
135 lines (112 loc) · 3.66 KB
/
one_shot.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
fs::{self, File, OpenOptions},
io::prelude::*,
path::Path,
str::FromStr,
};
use anyhow::{anyhow, bail, Error, Result};
use zeth_lib::{
consts::{ETH_MAINNET_CHAIN_SPEC, TAIKO_MAINNET_CHAIN_SPEC},
host::Init,
input::Input,
taiko::{
block_builder::{TaikoBlockBuilder, TaikoStrategyBundle},
host::TaikoExtra,
},
EthereumTxEssence,
};
use zeth_primitives::{
taiko::{string_to_bytes32, EvidenceType},
Address, B256,
};
use crate::{
app_args::{GlobalOpts, OneShotArgs},
signature::*,
};
pub const PRIV_KEY_FILENAME: &str = "priv.key";
pub async fn one_shot(global_opts: GlobalOpts, args: OneShotArgs) -> Result<()> {
println!(
"Global options: {:?}, OneShot options: {:?}",
global_opts, args
);
let path_str = args.blocks_data_file.to_string_lossy().to_string();
let block_no = u64::from_str(&String::from(
args.blocks_data_file
.file_prefix()
.unwrap()
.to_str()
.unwrap(),
))?;
println!("Reading input file {} (block no: {})", path_str, block_no);
let privkey_path = global_opts.secrets_dir.join(PRIV_KEY_FILENAME);
let prev_privkey = load_private_key(&privkey_path)?;
println!("Private key: {}", prev_privkey.display_secret());
// let (new_privkey, new_pubkey) = generate_new_keypair()?;
let new_pubkey = public_key(&prev_privkey);
let new_instance = public_key_to_address(&new_pubkey);
// fs::write(privkey_path, new_privkey.to_bytes())?;
let pi_hash = generate_proof(
path_str,
args.l1_blocks_data_file.to_string_lossy().to_string(),
args.prover,
args.graffiti,
block_no,
new_instance,
)
.await?;
println!("Data to be signed: {}", pi_hash);
let sig = sign_message(&prev_privkey, pi_hash)?;
const SGX_PROOF_LEN: usize = 89;
let mut proof = Vec::with_capacity(SGX_PROOF_LEN);
proof.extend(new_instance);
proof.extend(sig.to_bytes());
let proof = hex::encode(proof);
println!("Proof: 0x{}", proof);
println!("Public key: {}", new_pubkey);
print_powdr_info()
}
async fn generate_proof(
path_str: String,
l1_blocks_path: String,
prover: Address,
graffiti: B256,
block_no: u64,
new_pubkey: Address,
) -> Result<B256> {
// TODO: run Powdr here, Init<EthereumTxEssence> should be the same
let (init, extra) = parse_to_init(path_str, l1_blocks_path, prover, block_no, graffiti).await?;
let input: Input<zeth_lib::EthereumTxEssence> = init.clone().into();
let output = TaikoBlockBuilder::build_from(&TAIKO_MAINNET_CHAIN_SPEC, input)
.expect("Failed to build the resulting block");
let pi = zeth_lib::taiko::protocol_instance::assemble_protocol_instance(&extra, &output)?;
let pi_hash = pi.hash(EvidenceType::Sgx { new_pubkey });
Ok(pi_hash)
}
async fn parse_to_init(
blocks_path: String,
l1_blocks_path: String,
prover: Address,
block_no: u64,
graffiti: B256,
) -> Result<(Init<zeth_lib::EthereumTxEssence>, TaikoExtra), Error> {
let (init, extra) = tokio::task::spawn_blocking(move || {
zeth_lib::taiko::host::get_taiko_initial_data::<TaikoStrategyBundle>(
Some(l1_blocks_path),
ETH_MAINNET_CHAIN_SPEC.clone(),
None,
prover,
Some(blocks_path),
TAIKO_MAINNET_CHAIN_SPEC.clone(),
None,
block_no,
graffiti,
)
.expect("Could not init")
})
.await?;
Ok::<(Init<EthereumTxEssence>, TaikoExtra), _>((init, extra))
}
fn print_powdr_info() -> Result<()> {
// TODO change to powdr info
Ok(())
}