1use crate::{
2 cmd::{
3 erc20::build_provider_with_signer,
4 send::{cast_send, cast_send_with_access_key},
5 tip20::mine,
6 },
7 tempo,
8 tx::{CastTxSender, SendTxOpts, TxParams},
9};
10use alloy_network::Network;
11use alloy_primitives::{Address, B256};
12use alloy_provider::Provider;
13use alloy_signer::Signer;
14use eyre::Result;
15use foundry_cli::{
16 json::print_json_success,
17 utils::{LoadConfig, get_chain},
18};
19use foundry_common::{
20 FoundryTransactionBuilder,
21 fmt::{UIfmt, UIfmtReceiptExt},
22 provider::ProviderBuilder,
23 shell,
24 tempo::print_resolved_fee_token_selection,
25};
26use rand::{RngCore, SeedableRng, rngs::StdRng};
27use serde_json::json;
28use std::time::Instant;
29use tempo_alloy::{
30 TempoNetwork,
31 contracts::precompiles::{ADDRESS_REGISTRY_ADDRESS, IAddressRegistry},
32};
33use tempo_primitives::{TempoAddressExt, UserTag};
34
35const POW_BYTES: usize = 4;
36
37#[allow(clippy::too_many_arguments)]
38pub(super) async fn run(
39 owner: Address,
40 salt: Option<B256>,
41 tag: u64,
42 count: u32,
43 threads: Option<usize>,
44 seed: Option<B256>,
45 no_random: bool,
46 no_register: bool,
47 send_tx: SendTxOpts,
48 tx_opts: TxParams,
49) -> Result<()> {
50 if count == 0 {
51 return Ok(());
53 }
54
55 if !owner.is_valid_master() {
56 eyre::bail!(
57 "invalid owner address {owner}; see https://docs.tempo.xyz/protocol/tips/tip-1022"
58 );
59 }
60
61 let output = if let Some(salt) = salt {
62 let output = mine::derive(owner, salt);
63 if !mine::has_pow(&output.registration_hash, POW_BYTES) {
64 eyre::bail!(
65 "provided salt does not satisfy TIP-1022 proof of work: {}",
66 output.registration_hash
67 );
68 }
69 output
70 } else {
71 let mut n_threads = threads.unwrap_or(0);
72 if n_threads == 0 {
73 n_threads = std::thread::available_parallelism().map_or(1, |n| n.get());
74 }
75
76 let mut start_salt = B256::ZERO;
77 if !no_random {
78 let mut rng = match seed {
79 Some(seed) => StdRng::from_seed(seed.0),
80 None => StdRng::from_os_rng(),
81 };
82 rng.fill_bytes(&mut start_salt[..]);
83 }
84
85 if !shell::is_json() {
86 sh_status!("Mining TIP-1022 salt for {owner} with {n_threads} threads...")?;
87 }
88 let timer = Instant::now();
89 let output = mine::mine(owner, start_salt, n_threads, POW_BYTES)?;
90 if !shell::is_json() {
91 sh_status!("Found salt in {:?}", timer.elapsed())?;
92 }
93 output
94 };
95
96 const MAX_USER_TAG: u64 = 0x0000_FFFF_FFFF_FFFF;
97 let mut virtual_addresses = Vec::with_capacity(count as usize);
98 for i in 0..count {
99 let tag_value = tag
100 .checked_add(i as u64)
101 .filter(|&t| t <= MAX_USER_TAG)
102 .ok_or_else(|| eyre::eyre!("tag overflow: tag + count exceeds the 6-byte user tag range (max {MAX_USER_TAG:#x})"))?;
103 let raw = tag_value.to_be_bytes();
104 let user_tag = UserTag::new(raw[2..].try_into().expect("slice is 6 bytes"));
105 let vaddr = Address::new_virtual(output.master_id, user_tag);
106 virtual_addresses.push((user_tag, vaddr));
107 }
108
109 let payload = json!({
110 "salt": format!("{}", output.salt),
111 "registration_hash": format!("{}", output.registration_hash),
112 "master_id": format!("{}", output.master_id),
113 "virtual_addresses": virtual_addresses.iter().map(|(tag, addr)| json!({
114 "tag": format!("{tag}"),
115 "address": format!("{addr}"),
116 })).collect::<Vec<_>>(),
117 });
118
119 if !shell::is_json() {
120 sh_println!(
121 "Salt: {}
122Registration hash: {}
123Master ID: {}",
124 output.salt,
125 output.registration_hash,
126 output.master_id,
127 )?;
128 sh_println!("\nVirtual addresses:")?;
129 for (tag, vaddr) in &virtual_addresses {
130 sh_println!(" tag={tag} {vaddr}")?;
131 }
132 }
133
134 if no_register {
135 if shell::is_json() {
136 print_json_success(payload)?;
137 }
138 return Ok(());
139 }
140
141 let tx_hash = register(owner, output.salt, send_tx, tx_opts).await?;
142
143 if shell::is_json() {
144 let mut payload = payload;
145 payload["registration_tx_hash"] = json!(format!("{tx_hash:#x}"));
146 print_json_success(payload)?;
147 }
148
149 Ok(())
150}
151
152async fn register(
153 owner: Address,
154 salt: B256,
155 send_tx: SendTxOpts,
156 mut tx_opts: TxParams,
157) -> Result<B256> {
158 let config = send_tx.eth.load_config()?;
159 let timeout = send_tx.timeout.unwrap_or(config.transaction_timeout);
160 let provider = ProviderBuilder::<TempoNetwork>::from_config(&config)?.build()?;
161 let chain = get_chain(config.chain, &provider).await?;
162 tempo::ensure_session_not_browser(&tx_opts.tempo, send_tx.browser.browser)?;
163 let (signer, tempo_access_key) =
164 tempo::resolve_session_or_wallet_signer(&tx_opts.tempo, &send_tx.eth.wallet, chain.id())
165 .await?;
166 let signer = signer.ok_or_else(|| {
167 eyre::eyre!("cast vaddr create requires a signer (for example --private-key or --from)")
168 })?;
169
170 let sender =
171 tempo_access_key.as_ref().map(|ak| ak.wallet_address).unwrap_or_else(|| signer.address());
172
173 if sender != owner {
174 eyre::bail!(
175 "signer mismatch: salt is for {owner}, but the configured signer would register as {sender}"
176 );
177 }
178
179 let mut tx = IAddressRegistry::new(ADDRESS_REGISTRY_ADDRESS, &provider)
180 .registerVirtualMaster(salt)
181 .into_transaction_request();
182 let expires_at = tx_opts.tempo.resolve_expires();
183 tempo::print_expires(expires_at)?;
184 tx_opts.apply::<TempoNetwork>(&mut tx, chain.is_legacy());
185
186 sh_status!("Submitting registerVirtualMaster({salt})...")?;
187
188 if let Some(ref access_key) = tempo_access_key {
189 tempo::fill_access_key_transaction(&provider, &mut tx, access_key, chain).await?;
190 if shell::is_json() {
191 print_resolved_fee_token_selection(Some(chain), tx.fee_token())?;
193 let raw_tx = tx
194 .sign_with_access_key(
195 &provider,
196 &signer,
197 access_key.wallet_address,
198 access_key.key_address,
199 access_key.key_authorization.as_ref(),
200 )
201 .await?;
202 let tx_hash = *provider.send_raw_transaction(&raw_tx).await?.tx_hash();
203 wait_for_receipt_if_needed(
204 &provider,
205 tx_hash,
206 send_tx.cast_async,
207 send_tx.confirmations,
208 timeout,
209 )
210 .await?;
211 Ok(tx_hash)
212 } else {
213 cast_send_with_access_key(
214 &provider,
215 tx,
216 &signer,
217 access_key,
218 Some(chain),
219 send_tx.cast_async,
220 send_tx.confirmations,
221 timeout,
222 )
223 .await
224 }
225 } else {
226 let provider = build_provider_with_signer::<TempoNetwork>(&send_tx, signer)?;
227 if shell::is_json() {
228 print_resolved_fee_token_selection(Some(chain), tx.fee_token())?;
230 let cast = CastTxSender::new(&provider);
231 if send_tx.sync {
232 cast.send_sync(tx).await.map(|(tx_hash, _)| tx_hash)
233 } else {
234 let pending_tx = cast.send(tx).await?;
235 let tx_hash = *pending_tx.inner().tx_hash();
236 wait_for_receipt_if_needed(
237 &provider,
238 tx_hash,
239 send_tx.cast_async,
240 send_tx.confirmations,
241 timeout,
242 )
243 .await?;
244 Ok(tx_hash)
245 }
246 } else {
247 cast_send(
248 provider,
249 tx,
250 Some(chain),
251 send_tx.cast_async,
252 send_tx.sync,
253 send_tx.confirmations,
254 timeout,
255 )
256 .await
257 }
258 }
259}
260
261async fn wait_for_receipt_if_needed<P: Provider<TempoNetwork>>(
262 provider: &P,
263 tx_hash: B256,
264 cast_async: bool,
265 confirmations: u64,
266 timeout: u64,
267) -> Result<()>
268where
269 <TempoNetwork as Network>::TransactionRequest: FoundryTransactionBuilder<TempoNetwork>,
270 <TempoNetwork as Network>::ReceiptResponse: UIfmt + UIfmtReceiptExt,
271{
272 if !cast_async {
273 CastTxSender::new(provider)
274 .receipt(format!("{tx_hash:#x}"), None, confirmations, Some(timeout), false)
275 .await?;
276 }
277 Ok(())
278}