Skip to main content

cast/cmd/tip20/
mine.rs

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_println!("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 (signer, tempo_access_key) = send_tx.eth.wallet.maybe_signer().await?;
85    let signer = signer.ok_or_else(|| {
86        eyre::eyre!(
87            "--register requires a signer or Tempo keychain identity (for example --private-key or --from)"
88        )
89    })?;
90
91    let sender =
92        tempo_access_key.as_ref().map(|ak| ak.wallet_address).unwrap_or_else(|| signer.address());
93
94    if sender != master {
95        eyre::bail!(
96            "registration sender mismatch: mined salt is for {master}, but the configured signer would register as {sender}"
97        );
98    }
99
100    let config = send_tx.eth.load_config()?;
101    let timeout = send_tx.timeout.unwrap_or(config.transaction_timeout);
102    let provider = ProviderBuilder::<TempoNetwork>::from_config(&config)?.build()?;
103
104    let mut tx = IAddressRegistry::new(ADDRESS_REGISTRY_ADDRESS, &provider)
105        .registerVirtualMaster(salt)
106        .into_transaction_request();
107    let expires_at = tx_opts.tempo.resolve_expires();
108    tempo::print_expires(expires_at)?;
109    tx_opts.apply::<TempoNetwork>(&mut tx, get_chain(config.chain, &provider).await?.is_legacy());
110
111    sh_println!("Submitting registerVirtualMaster({salt}) on Tempo...")?;
112
113    if let Some(ref access_key) = tempo_access_key {
114        cast_send_with_access_key(
115            &provider,
116            tx,
117            &signer,
118            access_key,
119            send_tx.cast_async,
120            send_tx.confirmations,
121            timeout,
122        )
123        .await?;
124    } else {
125        let provider = build_provider_with_signer::<TempoNetwork>(&send_tx, signer)?;
126        cast_send(provider, tx, send_tx.cast_async, send_tx.sync, send_tx.confirmations, timeout)
127            .await?;
128    }
129
130    Ok(())
131}
132
133pub(crate) fn mine(
134    master: Address,
135    salt: B256,
136    n_threads: usize,
137    pow_bytes: usize,
138) -> Result<Output> {
139    let mut packed = [0u8; 52];
140    packed[..20].copy_from_slice(master.as_slice());
141
142    crate::cmd::miner::mine_salt(salt, n_threads, move |salt| {
143        packed[20..].copy_from_slice(salt.as_slice());
144        let registration_hash = keccak256(packed);
145
146        has_pow(&registration_hash, pow_bytes).then(|| {
147            let master_id = MasterId::from_slice(&registration_hash[4..8]);
148            let zero_tag_virtual_address = Address::new_virtual(master_id, UserTag::ZERO);
149            Output { salt, registration_hash, master_id, zero_tag_virtual_address }
150        })
151    })
152    .ok_or_else(|| eyre::eyre!("virtual master mining failed: all threads panicked"))
153}
154
155pub(crate) fn derive(master: Address, salt: B256) -> Output {
156    let registration_hash = registration_hash(master, salt);
157    let master_id = MasterId::from_slice(&registration_hash[4..8]);
158    let zero_tag_virtual_address = Address::new_virtual(master_id, UserTag::ZERO);
159
160    Output { salt, registration_hash, master_id, zero_tag_virtual_address }
161}
162
163pub(crate) fn registration_hash(master: Address, salt: B256) -> B256 {
164    let mut packed = [0u8; 52];
165    packed[..20].copy_from_slice(master.as_slice());
166    packed[20..].copy_from_slice(salt.as_slice());
167    keccak256(packed)
168}
169
170pub(crate) fn has_pow(registration_hash: &B256, pow_bytes: usize) -> bool {
171    registration_hash[..pow_bytes].iter().all(|byte| *byte == 0)
172}
173
174fn print_output(output: &Output, elapsed: Option<Duration>) -> Result<()> {
175    let header = if let Some(elapsed) = elapsed {
176        format!("Found salt in {elapsed:?}\n")
177    } else {
178        String::new()
179    };
180
181    sh_println!(
182        r#"{header}Salt:              {}
183Registration hash: {}
184Master ID:         {}
185Zero-tag address:  {}"#,
186        output.salt,
187        output.registration_hash,
188        output.master_id,
189        output.zero_tag_virtual_address,
190    )?;
191    Ok(())
192}
193
194#[cfg(test)]
195mod tests {
196    use super::*;
197    use alloy_primitives::{address, b256};
198
199    #[test]
200    fn derives_master_id_and_zero_tag_address() {
201        let master = address!("0x1234567890123456789012345678901234567890");
202        let salt = b256!("0x0000000000000000000000000000000000000000000000000000000000000001");
203        let output = derive(master, salt);
204
205        assert_eq!(
206            output.registration_hash,
207            b256!("0x661db5481211842e0330ea3e4cf0b4e7e5abd2314161ce16e9a99e7460480f21"),
208        );
209        assert_eq!(output.master_id, MasterId::from([0x12, 0x11, 0x84, 0x2e]));
210        assert_eq!(
211            output.zero_tag_virtual_address,
212            address!("0x1211842efdfdfdfdfdfdfdfdfdfd000000000000"),
213        );
214        assert_eq!(output.master_id, MasterId::from_slice(&output.registration_hash[4..8]));
215        assert_eq!(
216            output.zero_tag_virtual_address,
217            Address::new_virtual(output.master_id, UserTag::ZERO),
218        );
219    }
220
221    #[test]
222    fn mines_pow_with_reduced_difficulty() -> Result<()> {
223        let master = address!("0x1234567890123456789012345678901234567890");
224        let output = mine(master, B256::ZERO, 1, 1)?;
225
226        assert_eq!(
227            output.salt,
228            b256!("0x000000000000000000000000000000000000000000000000f301000000000000"),
229        );
230        assert_eq!(output.registration_hash[0], 0);
231        assert_eq!(output.master_id, MasterId::from_slice(&output.registration_hash[4..8]));
232        assert_eq!(
233            output.zero_tag_virtual_address,
234            Address::new_virtual(output.master_id, UserTag::ZERO),
235        );
236        Ok(())
237    }
238
239    #[test]
240    fn has_pow_checks_leading_zero_bytes() {
241        let mut hash = B256::ZERO;
242        assert!(has_pow(&hash, 4));
243        assert!(has_pow(&hash, 0));
244
245        hash[3] = 1;
246        assert!(!has_pow(&hash, 4));
247        assert!(has_pow(&hash, 3));
248        assert!(has_pow(&hash, 0));
249    }
250
251    #[test]
252    fn rejects_invalid_master_addresses() {
253        assert!(!Address::ZERO.is_valid_master());
254        assert!(!address!("0x00000000fdfdfdfdfdfdfdfdfdfd000000000001").is_valid_master());
255        assert!(!address!("0x20c0000000000000000000000000000000000001").is_valid_master());
256    }
257}