vanity_core/chains/
decred.rs1use crate::chain::{ChainGrinder, GrindAttempt, KeyExport, KeypairResult};
7use crate::pattern::Pattern;
8use blake_hash::{Blake256, Digest as _};
9use ripemd::Ripemd160;
10use secp256k1::{Secp256k1, SecretKey};
11use sha2::Digest;
12
13use super::base58_odds::Base58Layout;
14use super::util::{
15 build_base58_pattern, grind_secp256k1, matches_pattern, secret_from_attempt, BASE58_ALPHABET,
16};
17
18const DS_PREFIX: [u8; 2] = [0x07, 0x3f];
19const WIF_PREFIX: [u8; 2] = [0x22, 0xde];
21
22const LAYOUT: Base58Layout = Base58Layout {
23 version: &DS_PREFIX,
24 random_len: 24,
25 alphabet: BASE58_ALPHABET,
26};
27
28#[derive(Clone, Default)]
29pub struct DecredGrinder;
30
31impl DecredGrinder {
32 fn hash160_blake(pubkey: &[u8]) -> [u8; 20] {
33 let blake = Blake256::digest(pubkey);
34 Ripemd160::digest(blake).into()
35 }
36
37 fn derive(secret: &SecretKey) -> String {
38 let secp = Secp256k1::new();
39 Self::address_from_hash(&Self::hash160_blake(&secret.public_key(&secp).serialize()))
40 }
41
42 fn address_from_hash(hash: &[u8; 20]) -> String {
43 let mut payload = Vec::with_capacity(26);
44 payload.extend_from_slice(&DS_PREFIX);
45 payload.extend_from_slice(hash);
46 let c1 = Blake256::digest(&payload);
47 let c2 = Blake256::digest(&c1);
48 payload.extend_from_slice(&c2[..4]);
49 bs58::encode(payload).into_string()
50 }
51
52 fn wif_encode(secret: [u8; 32]) -> String {
53 let mut payload = Vec::with_capacity(39);
54 payload.extend_from_slice(&WIF_PREFIX);
55 payload.extend_from_slice(&secret);
56 payload.push(0x01);
57 let c1 = Blake256::digest(&payload);
58 let c2 = Blake256::digest(&c1);
59 payload.extend_from_slice(&c2[..4]);
60 bs58::encode(payload).into_string()
61 }
62}
63
64impl ChainGrinder for DecredGrinder {
65 fn id(&self) -> &'static str {
66 "dcr"
67 }
68
69 fn display_name(&self) -> &'static str {
70 "Decred (Ds… P2PKH)"
71 }
72
73 fn grind_attempt(&self) -> (String, GrindAttempt) {
74 grind_secp256k1(Self::derive)
75 }
76
77 fn finalize(&self, attempt: GrindAttempt) -> KeypairResult {
78 let secret_bytes = secret_from_attempt(attempt);
79 let secret_key = SecretKey::from_slice(&secret_bytes).expect("valid secp256k1 secret");
80 let address = Self::derive(&secret_key);
81
82 KeypairResult {
83 address,
84 exports: vec![
85 KeyExport {
86 label: "Private Key (hex)".into(),
87 value: hex::encode(secret_bytes),
88 hint: Some("Decrediton / dcrwallet".into()),
89 },
90 KeyExport {
91 label: "Private Key (WIF)".into(),
92 value: Self::wif_encode(secret_bytes),
93 hint: Some("Decred compressed WIF (Pm…)".into()),
94 },
95 ],
96 }
97 }
98
99 fn build_pattern(
100 &self,
101 prefix: Option<&str>,
102 suffix: Option<&str>,
103 contains: Option<&str>,
104 exact: bool,
105 ) -> Result<Pattern, String> {
106 let mut pattern =
107 build_base58_pattern(prefix, suffix, contains, exact, BASE58_ALPHABET, 35)?;
108 LAYOUT.apply_lead(self.display_name(), &mut pattern, "Ds")?;
109 Ok(pattern)
110 }
111
112 fn expected_attempts(&self, pattern: &Pattern) -> f64 {
113 LAYOUT.expected_attempts(pattern)
114 }
115
116 fn matches(&self, address: &str, pattern: &Pattern) -> bool {
117 matches_pattern(address, pattern, false)
118 }
119
120 fn supports_exact_case(&self) -> bool {
121 true
122 }
123
124 fn pattern_hint(&self) -> &'static str {
125 "Base58. Mainnet P2PKH always starts with Ds (added for you)."
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::DecredGrinder;
132 use crate::chain::ChainGrinder;
133
134 #[test]
135 fn addresses_start_with_ds() {
136 let g = DecredGrinder;
137 for _ in 0..16 {
138 let (addr, attempt) = g.grind_attempt();
139 assert!(addr.starts_with("Ds"), "got {addr}");
140 assert_eq!(g.finalize(attempt).address, addr);
141 }
142 }
143
144 #[test]
145 fn blake256_known_vectors() {
146 use blake_hash::{Blake256, Digest as _};
147 assert_eq!(
149 hex::encode(Blake256::digest(&[0u8])),
150 "0ce8d4ef4dd7cd8d62dfded9d4edb0a774ae6a41929a74da23109e8f11139c87"
151 );
152 assert_eq!(
153 hex::encode(Blake256::digest(&[0u8; 72])),
154 "d419bad32d504fb7d44d460c42c5593fe544fa4c135dec31e21bd9abdcc22d41"
155 );
156 }
157
158 #[test]
159 fn known_vector_ds_address() {
160 let hash: [u8; 20] = hex::decode("2789d58cfa0957d206f025c2af056fc8a77cebb0")
162 .unwrap()
163 .try_into()
164 .unwrap();
165 assert_eq!(
166 DecredGrinder::address_from_hash(&hash),
167 "DsUZxxoHJSty8DCfwfartwTYbuhmVct7tJu"
168 );
169 }
170
171 #[test]
172 fn wif_starts_with_pm() {
173 let g = DecredGrinder;
174 let (_addr, attempt) = g.grind_attempt();
175 let result = g.finalize(attempt);
176 let wif = &result.exports[1].value;
177 assert!(wif.starts_with("Pm"), "got {wif}");
178 }
179}