rust-hdwallet 0.1.0-beta

Hierarchical deterministic wallet library for deriving cryptocurrency keys, addresses, and mnemonics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetworkName {
    Mainnet,
    Testnet,
    Regtest,
}

impl NetworkName {
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "mainnet" => Some(Self::Mainnet),
            "testnet" => Some(Self::Testnet),
            "regtest" => Some(Self::Regtest),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CryptocurrencyKind {
    Bitcoin,
    Ethereum,
    Litecoin,
    Dogecoin,
    Adcoin,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WitnessVersions(pub &'static [u8]);

impl WitnessVersions {
    /// Mirrors Python `WitnessVersions.get_witness_version(address)`.
    pub fn get_witness_version(&self, address: &str) -> Option<u8> {
        let key = address.to_ascii_uppercase().replace('-', "_");
        match key.as_str() {
            "P2TR" if self.0.first() == Some(&0x01) && self.0.len() >= 3 => Some(0x01),
            "P2WPKH" if self.0.first() == Some(&0x01) && self.0.len() >= 3 => Some(self.0[1]),
            "P2WSH" if self.0.first() == Some(&0x01) && self.0.len() >= 3 => Some(self.0[2]),
            "P2WPKH" if self.0.len() >= 2 => Some(self.0[0]),
            "P2WSH" if self.0.len() >= 2 => Some(self.0[1]),
            "P2WPKH" | "P2WSH" => self.0.first().copied(),
            _ => None,
        }
    }
}

pub type XPrivateKeyVersions = &'static [(&'static str, u32)];

pub type XPublicKeyVersions = &'static [(&'static str, u32)];

pub type AddressTypes = &'static [(&'static str, &'static str)];

pub type AddressPrefixes = &'static [&'static str];

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Params {
    pub checksum_length: Option<u8>,
    pub payment_id_length: Option<u8>,
    pub suffix: Option<u8>,
    pub address_prefix: Option<&'static str>,
    pub chain_prefixes_by_address_type: Option<&'static [(&'static str, &'static str)]>,
    pub alphabet: Option<&'static str>,
    pub field_size_hex: Option<&'static str>,
    pub tap_tweak_sha256: Option<&'static str>,
    pub cardano_wallet_types: Option<AddressTypes>,
    pub cardano_payment_prefix: Option<u8>,
    pub cardano_reward_prefix: Option<u8>,
    pub cardano_public_key_address: Option<u8>,
    pub cardano_redemption_address: Option<u8>,
    pub address_type_codes: Option<&'static [(&'static str, u8)]>,
    pub address_prefix_byte: Option<u8>,
    pub address_version_byte: Option<u8>,
    pub key_type: Option<u8>,
    pub address_type_prefix_bytes: Option<&'static [(&'static str, &'static [u8])]>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InfoValue<'a> {
    Str(&'a str),
    List(&'a [&'a str]),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InfoItem<'a> {
    pub key: &'static str,
    pub value: InfoValue<'a>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Info<'a>(pub &'a [InfoItem<'a>]);


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NetworkDescriptor {
    pub name: &'static str,
    pub public_key_address_prefix: Option<u32>,
    pub script_address_prefix: Option<u32>,
    pub hrp: Option<&'static str>,
    pub witness_versions: Option<WitnessVersions>,
    pub xprivate_key_versions: Option<XPrivateKeyVersions>,
    pub xpublic_key_versions: Option<XPublicKeyVersions>,
    pub message_prefix: Option<&'static [u8]>,
    pub wif_prefix: Option<u8>,
    pub legacy_public_key_address_prefix: Option<u8>,
    pub std_public_key_address_prefix: Option<u8>,
    pub legacy_script_address_prefix: Option<u8>,
    pub std_script_address_prefix: Option<u8>,
    pub standard: Option<u32>,
    pub integrated: Option<u32>,
    pub sub_address: Option<u32>,
    pub network_type: Option<u32>,
    pub payment_address_hrp: Option<&'static str>,
    pub reward_address_hrp: Option<&'static str>,
}

pub trait INetwork {
    fn name(&self) -> &'static str;

    fn public_key_address_prefix(&self) -> Option<u32> {
        None
    }
    fn script_address_prefix(&self) -> Option<u32> {
        None
    }
    fn hrp(&self) -> Option<&'static str> {
        None
    }
    fn witness_versions(&self) -> Option<WitnessVersions> {
        None
    }
    fn xprivate_key_versions(&self) -> Option<XPrivateKeyVersions> {
        None
    }
    fn xpublic_key_versions(&self) -> Option<XPublicKeyVersions> {
        None
    }
    fn message_prefix(&self) -> Option<&'static [u8]> {
        None
    }
    fn wif_prefix(&self) -> Option<u8> {
        None
    }
    fn legacy_public_key_address_prefix(&self) -> Option<u8> {
        None
    }
    fn std_public_key_address_prefix(&self) -> Option<u8> {
        None
    }
    fn legacy_script_address_prefix(&self) -> Option<u8> {
        None
    }
    fn std_script_address_prefix(&self) -> Option<u8> {
        None
    }
    fn standard(&self) -> Option<u32> {
        None
    }
    fn integrated(&self) -> Option<u32> {
        None
    }
    fn sub_address(&self) -> Option<u32> {
        None
    }
    fn network_type(&self) -> Option<u32> {
        None
    }
    fn payment_address_hrp(&self) -> Option<&'static str> {
        None
    }
    fn reward_address_hrp(&self) -> Option<&'static str> {
        None
    }
}

impl INetwork for NetworkDescriptor {
    fn name(&self) -> &'static str {
        self.name
    }

    fn public_key_address_prefix(&self) -> Option<u32> {
        self.public_key_address_prefix
    }

    fn script_address_prefix(&self) -> Option<u32> {
        self.script_address_prefix
    }

    fn hrp(&self) -> Option<&'static str> {
        self.hrp
    }

    fn witness_versions(&self) -> Option<WitnessVersions> {
        self.witness_versions
    }

    fn xprivate_key_versions(&self) -> Option<XPrivateKeyVersions> {
        self.xprivate_key_versions
    }

    fn xpublic_key_versions(&self) -> Option<XPublicKeyVersions> {
        self.xpublic_key_versions
    }

    fn message_prefix(&self) -> Option<&'static [u8]> {
        self.message_prefix
    }

    fn wif_prefix(&self) -> Option<u8> {
        self.wif_prefix
    }

    fn legacy_public_key_address_prefix(&self) -> Option<u8> {
        self.legacy_public_key_address_prefix
    }

    fn std_public_key_address_prefix(&self) -> Option<u8> {
        self.std_public_key_address_prefix
    }

    fn legacy_script_address_prefix(&self) -> Option<u8> {
        self.legacy_script_address_prefix
    }

    fn std_script_address_prefix(&self) -> Option<u8> {
        self.std_script_address_prefix
    }

    fn standard(&self) -> Option<u32> {
        self.standard
    }

    fn integrated(&self) -> Option<u32> {
        self.integrated
    }

    fn sub_address(&self) -> Option<u32> {
        self.sub_address
    }

    fn network_type(&self) -> Option<u32> {
        self.network_type
    }

    fn payment_address_hrp(&self) -> Option<&'static str> {
        self.payment_address_hrp
    }

    fn reward_address_hrp(&self) -> Option<&'static str> {
        self.reward_address_hrp
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CryptocurrencyDescriptor {
    pub name: &'static str,
    pub symbol: &'static str,
    pub info: Info<'static>,
    pub ecc: &'static str,
    pub coin_type: u32,
    pub support_bip38: bool,
    pub networks: &'static [(&'static str, &'static NetworkDescriptor)],
    pub default_network: &'static NetworkDescriptor,
    pub entropies: &'static [&'static str],
    pub mnemonics: &'static [&'static str],
    pub seeds: &'static [&'static str],
    pub hds: &'static [&'static str],
    pub default_hd: &'static str,
    pub default_path: &'static str,
    pub addresses: &'static [&'static str],
    pub default_address: &'static str,
    pub address_types: Option<AddressTypes>,
    pub default_address_type: Option<&'static str>,
    pub address_prefixes: Option<AddressPrefixes>,
    pub default_address_prefix: Option<&'static str>,
    pub semantics: &'static [&'static str],
    pub default_semantic: Option<&'static str>,
    pub params: Option<Params>,
}

pub trait ICryptocurrency {
    fn name(&self) -> &'static str;
    fn symbol(&self) -> &'static str;
    fn info(&self) -> Info<'static>;
    fn ecc_name(&self) -> &'static str;
    fn coin_type(&self) -> u32;
    fn support_bip38(&self) -> bool;
    fn networks(&self) -> &'static [(&'static str, &'static NetworkDescriptor)];
    fn default_network(&self) -> &'static NetworkDescriptor;
    fn entropies(&self) -> &'static [&'static str];
    fn mnemonics(&self) -> &'static [&'static str];
    fn seeds(&self) -> &'static [&'static str];
    fn hds(&self) -> &'static [&'static str];
    fn default_hd(&self) -> &'static str;
    fn default_path(&self) -> &'static str;
    fn addresses(&self) -> &'static [&'static str];
    fn default_address(&self) -> &'static str;

    fn address_types(&self) -> Option<AddressTypes> {
        None
    }
    fn default_address_type(&self) -> Option<&'static str> {
        None
    }
    fn address_prefixes(&self) -> Option<AddressPrefixes> {
        None
    }
    fn default_address_prefix(&self) -> Option<&'static str> {
        None
    }
    fn semantics(&self) -> &'static [&'static str];
    fn default_semantic(&self) -> Option<&'static str>;
    fn params(&self) -> Option<Params> {
        None
    }
}

impl ICryptocurrency for CryptocurrencyDescriptor {
    fn name(&self) -> &'static str {
        self.name
    }

    fn symbol(&self) -> &'static str {
        self.symbol
    }

    fn info(&self) -> Info<'static> {
        self.info
    }

    fn ecc_name(&self) -> &'static str {
        self.ecc
    }

    fn coin_type(&self) -> u32 {
        self.coin_type
    }

    fn support_bip38(&self) -> bool {
        self.support_bip38
    }

    fn networks(&self) -> &'static [(&'static str, &'static NetworkDescriptor)] {
        self.networks
    }

    fn default_network(&self) -> &'static NetworkDescriptor {
        self.default_network
    }

    fn entropies(&self) -> &'static [&'static str] {
        self.entropies
    }

    fn mnemonics(&self) -> &'static [&'static str] {
        self.mnemonics
    }

    fn seeds(&self) -> &'static [&'static str] {
        self.seeds
    }

    fn hds(&self) -> &'static [&'static str] {
        self.hds
    }

    fn default_hd(&self) -> &'static str {
        self.default_hd
    }

    fn default_path(&self) -> &'static str {
        self.default_path
    }

    fn addresses(&self) -> &'static [&'static str] {
        self.addresses
    }

    fn default_address(&self) -> &'static str {
        self.default_address
    }

    fn address_types(&self) -> Option<AddressTypes> {
        self.address_types
    }

    fn default_address_type(&self) -> Option<&'static str> {
        self.default_address_type
    }

    fn address_prefixes(&self) -> Option<AddressPrefixes> {
        self.address_prefixes
    }

    fn default_address_prefix(&self) -> Option<&'static str> {
        self.default_address_prefix
    }

    fn semantics(&self) -> &'static [&'static str] {
        self.semantics
    }

    fn default_semantic(&self) -> Option<&'static str> {
        self.default_semantic
    }

    fn params(&self) -> Option<Params> {
        self.params
    }
}