1use crate::{
2 cmd::{
3 erc20::build_provider_with_signer,
4 send::{cast_send, cast_send_with_access_key},
5 },
6 tempo,
7 tx::{SendTxOpts, TxParams},
8};
9use alloy_primitives::{Address, B256, keccak256};
10use alloy_signer::Signer;
11use eyre::Result;
12use foundry_cli::utils::{LoadConfig, get_chain};
13use foundry_common::provider::ProviderBuilder;
14use rand::{RngCore, SeedableRng, rngs::StdRng};
15use std::time::{Duration, Instant};
16use tempo_alloy::{
17 TempoNetwork,
18 contracts::precompiles::{ADDRESS_REGISTRY_ADDRESS, IAddressRegistry},
19};
20use tempo_primitives::{MasterId, TempoAddressExt, UserTag};
21
22const POW_BYTES: usize = 4;
23
24pub(crate) struct Output {
25 pub(crate) salt: B256,
26 pub(crate) registration_hash: B256,
27 pub(crate) master_id: MasterId,
28 pub(crate) zero_tag_virtual_address: Address,
29}
30
31pub(super) fn run(
32 master: Address,
33 salt: Option<B256>,
34 threads: Option<usize>,
35 seed: Option<B256>,
36 no_random: bool,
37) -> Result<Output> {
38 if !master.is_valid_master() {
39 eyre::bail!(
40 "invalid master address {master}; see https://docs.tempo.xyz/protocol/tips/tip-1022"
41 );
42 }
43
44 if let Some(salt) = salt {
45 let output = derive(master, salt);
46 if !has_pow(&output.registration_hash, POW_BYTES) {
47 eyre::bail!(
48 "provided salt does not satisfy TIP-1022 proof of work: {}",
49 output.registration_hash
50 );
51 }
52 print_output(&output, None)?;
53 return Ok(output);
54 }
55
56 let mut n_threads = threads.unwrap_or(0);
57 if n_threads == 0 {
58 n_threads = std::thread::available_parallelism().map_or(1, |n| n.get());
59 }
60
61 let mut salt = B256::ZERO;
62 if !no_random {
63 let mut rng = match seed {
64 Some(seed) => StdRng::from_seed(seed.0),
65 None => StdRng::from_os_rng(),
66 };
67 rng.fill_bytes(&mut salt[..]);
68 }
69
70 sh_status!("Mining TIP-1022 salt for {master} with {n_threads} threads...")?;
71
72 let timer = Instant::now();
73 let output = mine(master, salt, n_threads, POW_BYTES)?;
74 print_output(&output, Some(timer.elapsed()))?;
75 Ok(output)
76}
77
78pub(super) async fn register(
79 master: Address,
80 salt: B256,
81 send_tx: SendTxOpts,
82 mut tx_opts: TxParams,
83) -> Result<()> {
84 let config = send_tx.eth.load_config()?;
85 let timeout = send_tx.timeout.unwrap_or(config.transaction_timeout);
86 let provider = ProviderBuilder::<TempoNetwork>::from_config(&config)?.build()?;
87 let chain = get_chain(config.chain, &provider).await?;
88 tempo::ensure_session_not_browser(&tx_opts.tempo, send_tx.browser.browser)?;
89 let (signer, tempo_access_key) =
90 tempo::resolve_session_or_wallet_signer(&tx_opts.tempo, &send_tx.eth.wallet, chain.id())
91 .await?;
92 let signer = signer.ok_or_else(|| {
93 eyre::eyre!(
94 "--register requires a signer or Tempo keychain identity (for example --private-key or --from)"
95 )
96 })?;
97
98 let sender =
99 tempo_access_key.as_ref().map(|ak| ak.wallet_address).unwrap_or_else(|| signer.address());
100
101 if sender != master {
102 eyre::bail!(
103 "registration sender mismatch: mined salt is for {master}, but the configured signer would register as {sender}"
104 );
105 }
106
107 let mut tx = IAddressRegistry::new(ADDRESS_REGISTRY_ADDRESS, &provider)
108 .registerVirtualMaster(salt)
109 .into_transaction_request();
110 let expires_at = tx_opts.tempo.resolve_expires();
111 tempo::print_expires(expires_at)?;
112 tx_opts.apply::<TempoNetwork>(&mut tx, chain.is_legacy());
113
114 sh_status!("Submitting registerVirtualMaster({salt}) on Tempo...")?;
115
116 if let Some(ref access_key) = tempo_access_key {
117 tempo::fill_access_key_transaction(&provider, &mut tx, access_key, chain).await?;
118 cast_send_with_access_key(
119 &provider,
120 tx,
121 &signer,
122 access_key,
123 Some(chain),
124 send_tx.cast_async,
125 send_tx.confirmations,
126 timeout,
127 )
128 .await?;
129 } else {
130 let provider = build_provider_with_signer::<TempoNetwork>(&send_tx, signer)?;
131 cast_send(
132 provider,
133 tx,
134 Some(chain),
135 send_tx.cast_async,
136 send_tx.sync,
137 send_tx.confirmations,
138 timeout,
139 )
140 .await?;
141 }
142
143 Ok(())
144}
145
146pub(crate) fn mine(
147 master: Address,
148 salt: B256,
149 n_threads: usize,
150 pow_bytes: usize,
151) -> Result<Output> {
152 let mut packed = [0u8; 52];
153 packed[..20].copy_from_slice(master.as_slice());
154
155 crate::cmd::miner::mine_salt(salt, n_threads, move |salt| {
156 packed[20..].copy_from_slice(salt.as_slice());
157 let registration_hash = keccak256(packed);
158
159 has_pow(®istration_hash, pow_bytes).then(|| {
160 let master_id = MasterId::from_slice(®istration_hash[4..8]);
161 let zero_tag_virtual_address = Address::new_virtual(master_id, UserTag::ZERO);
162 Output { salt, registration_hash, master_id, zero_tag_virtual_address }
163 })
164 })
165 .ok_or_else(|| eyre::eyre!("virtual master mining failed: all threads panicked"))
166}
167
168pub(crate) fn derive(master: Address, salt: B256) -> Output {
169 let registration_hash = registration_hash(master, salt);
170 let master_id = MasterId::from_slice(®istration_hash[4..8]);
171 let zero_tag_virtual_address = Address::new_virtual(master_id, UserTag::ZERO);
172
173 Output { salt, registration_hash, master_id, zero_tag_virtual_address }
174}
175
176pub(crate) fn registration_hash(master: Address, salt: B256) -> B256 {
177 let mut packed = [0u8; 52];
178 packed[..20].copy_from_slice(master.as_slice());
179 packed[20..].copy_from_slice(salt.as_slice());
180 keccak256(packed)
181}
182
183pub(crate) fn has_pow(registration_hash: &B256, pow_bytes: usize) -> bool {
184 registration_hash[..pow_bytes].iter().all(|byte| *byte == 0)
185}
186
187fn print_output(output: &Output, elapsed: Option<Duration>) -> Result<()> {
188 let header = if let Some(elapsed) = elapsed {
189 format!("Found salt in {elapsed:?}\n")
190 } else {
191 String::new()
192 };
193
194 sh_println!(
195 r#"{header}Salt: {}
196Registration hash: {}
197Master ID: {}
198Zero-tag address: {}"#,
199 output.salt,
200 output.registration_hash,
201 output.master_id,
202 output.zero_tag_virtual_address,
203 )?;
204 Ok(())
205}
206
207#[cfg(test)]
208mod tests {
209 use super::*;
210 use alloy_primitives::{address, b256};
211
212 #[test]
213 fn derives_master_id_and_zero_tag_address() {
214 let master = address!("0x1234567890123456789012345678901234567890");
215 let salt = b256!("0x0000000000000000000000000000000000000000000000000000000000000001");
216 let output = derive(master, salt);
217
218 assert_eq!(
219 output.registration_hash,
220 b256!("0x661db5481211842e0330ea3e4cf0b4e7e5abd2314161ce16e9a99e7460480f21"),
221 );
222 assert_eq!(output.master_id, MasterId::from([0x12, 0x11, 0x84, 0x2e]));
223 assert_eq!(
224 output.zero_tag_virtual_address,
225 address!("0x1211842efdfdfdfdfdfdfdfdfdfd000000000000"),
226 );
227 assert_eq!(output.master_id, MasterId::from_slice(&output.registration_hash[4..8]));
228 assert_eq!(
229 output.zero_tag_virtual_address,
230 Address::new_virtual(output.master_id, UserTag::ZERO),
231 );
232 }
233
234 #[test]
235 fn mines_pow_with_reduced_difficulty() -> Result<()> {
236 let master = address!("0x1234567890123456789012345678901234567890");
237 let output = mine(master, B256::ZERO, 1, 1)?;
238
239 assert_eq!(
240 output.salt,
241 b256!("0x000000000000000000000000000000000000000000000000f301000000000000"),
242 );
243 assert_eq!(output.registration_hash[0], 0);
244 assert_eq!(output.master_id, MasterId::from_slice(&output.registration_hash[4..8]));
245 assert_eq!(
246 output.zero_tag_virtual_address,
247 Address::new_virtual(output.master_id, UserTag::ZERO),
248 );
249 Ok(())
250 }
251
252 #[test]
253 fn has_pow_checks_leading_zero_bytes() {
254 let mut hash = B256::ZERO;
255 assert!(has_pow(&hash, 4));
256 assert!(has_pow(&hash, 0));
257
258 hash[3] = 1;
259 assert!(!has_pow(&hash, 4));
260 assert!(has_pow(&hash, 3));
261 assert!(has_pow(&hash, 0));
262 }
263
264 #[test]
265 fn rejects_invalid_master_addresses() {
266 assert!(!Address::ZERO.is_valid_master());
267 assert!(!address!("0x00000000fdfdfdfdfdfdfdfdfdfd000000000001").is_valid_master());
268 assert!(!address!("0x20c0000000000000000000000000000000000001").is_valid_master());
269 }
270}