Skip to main content

paralock_core/protocol/
privacy.rs

1use rand_core::CryptoRngCore;
2use serde::{Deserialize, Serialize};
3
4use crate::evm::EvmAddress;
5use crate::keys::{PrivateKey, Secret};
6
7/// Level-1 privacy options. None of them hides the link between the two legs of one swap (adaptor mode does that);
8/// they keep a swap from linking to the rest of your activity.
9#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct PrivacyOptions {
12    /// Pay the stablecoins to a new address created for this swap.
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub fresh_recipient: Option<bool>,
15    /// Let a relayer submit the claim, so the fresh address needs no gas. The relayer can't redirect the funds.
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub relay_claim: Option<bool>,
18    /// Refuse amounts other than the standard denominations, so amounts don't fingerprint a swap.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub standard_amounts_only: Option<bool>,
21    /// Wait a random 0..jitterMs before claiming, so claim timing doesn't line up exactly with the lock.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub jitter_ms: Option<u64>,
24}
25
26/// 0.1, 0.5, 1 and 5 ZEC, in zatoshi.
27pub const STANDARD_AMOUNTS_ZAT: [u64; 4] = [10_000_000, 50_000_000, 100_000_000, 500_000_000];
28
29/// True for one of [`STANDARD_AMOUNTS_ZAT`].
30pub fn is_standard_amount(zat: u64) -> bool {
31    STANDARD_AMOUNTS_ZAT.contains(&zat)
32}
33
34/// A fresh 32-byte swap secret and its sha256 hashlock (the same hash locks both chains) (`newSecret`).
35pub fn new_secret<R: CryptoRngCore + ?Sized>(rng: &mut R) -> (Secret, [u8; 32]) {
36    let secret = Secret::random(rng);
37    let hashlock = secret.hashlock();
38    (secret, hashlock)
39}
40
41/// A new EVM account for receiving one swap's stablecoins (`freshEvmAccount`). Keep the private key: it controls the
42/// funds.
43pub fn fresh_evm_account<R: CryptoRngCore + ?Sized>(rng: &mut R) -> (EvmAddress, PrivateKey) {
44    let key = PrivateKey::random(rng);
45    (EvmAddress::from_public_key(&key.public_key()), key)
46}