seed-utils 0.1.0

Extend and truncate seeds, XOR them, derive child seeds and xpubs/xprvs at account or root level.
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
410
411
412
413
414
415
416
417
418
419
420
421
use std::str::FromStr;

use bip85::bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey};
use clap::{App, Arg, ArgMatches};
use seed_utils::WordCount;
use xyzpub::Version;

const CHILD_SUB: &str = "child";
const EXTEND_SUB: &str = "extend";
const TRUNCATE_SUB: &str = "truncate";
const XOR_SUB: &str = "xor";
const XPRV_SUB: &str = "xprv";
const XPUB_SUB: &str = "xpub";

const SEED_ARG: &str = "seed";
const INDEX_ARG: &str = "index";
const NUMBER_ARG: &str = "number";
const WORDS_ARG: &str = "words";
const ROOT_ARG: &str = "root";
const TYPE_ARG: &str = "type";

fn main() -> Result<(), String> {
    let matches = App::new("seed-utils")
        .version("0.1.0")
        .about("CLI seed utilities")
        .subcommand(
            App::new(CHILD_SUB)
                .about("Derives a child seed from a seed")
                .arg(
                    Arg::with_name(SEED_ARG)
                        .help("Seed to derive")
                        .required(true),
                )
                .arg(
                    Arg::with_name(INDEX_ARG)
                        .help("Index to derive at")
                        .short("i")
                        .long(INDEX_ARG)
                        .takes_value(true)
                        .default_value("0"),
                )
                .arg(
                    Arg::with_name(NUMBER_ARG)
                        .help("Number of seeds to derive, starting from index")
                        .short("n")
                        .long(NUMBER_ARG)
                        .takes_value(true)
                        .default_value("1"),
                )
                .arg(
                    Arg::with_name(WORDS_ARG)
                        .help("Number of words of the derived seed")
                        .short("w")
                        .long(WORDS_ARG)
                        .takes_value(true)
                        .possible_values(&["12", "18", "24"])
                        .default_value("24"),
                ),
        )
        .subcommand(
            App::new(EXTEND_SUB)
                .about("Creates a new seed by extending the entropy of a 12 or 18 word seed")
                .arg(
                    Arg::with_name(SEED_ARG)
                        .help("Seed to extend")
                        .index(1)
                        .required(true),
                )
                .arg(
                    Arg::with_name(WORDS_ARG)
                        .help("Number of words of the extended seed")
                        .short("w")
                        .long(WORDS_ARG)
                        .takes_value(true)
                        .possible_values(&["18", "24"])
                        .default_value("24"),
                ),
        )
        .subcommand(
            App::new(TRUNCATE_SUB)
                .about("Creates new seeds by shortening the entropy of another. 
                The new seed begins with the same words as the longer one, only the last word is different to satisfy its checksum")
                .arg(
                    Arg::with_name(SEED_ARG)
                        .help("Seed to truncate")
                        .index(1)
                        .required(true),
                )
                .arg(
                    Arg::with_name(WORDS_ARG)
                        .help("Number of words of the truncated seed")
                        .short("w")
                        .long(WORDS_ARG)
                        .takes_value(true)
                        .possible_values(&["12", "18"])
                        .default_value("12"),
                ),
        )
        .subcommand(
            App::new(XOR_SUB)
            .about("Does a XOR of multiple seeds")
            .arg(
                Arg::with_name(SEED_ARG)
                    .help("Seeds to xor")
                    .multiple(true)
                    .min_values(2)
                    .required(true),
            ),
        )
        .subcommand(
            App::new(XPUB_SUB)
                .about("Derives account or root xpubs from a seed")
                .arg(
                    Arg::with_name(SEED_ARG)
                        .help("Seed to derive xpubs from")
                        .index(1)
                        .required(true),
                )
                .arg(
                    Arg::with_name(ROOT_ARG)
                        .help("Derives xpub at bip32 root instead of account level")
                        .long(ROOT_ARG)
                        .short("r")
                        .takes_value(false)
                        .conflicts_with_all(&[INDEX_ARG, NUMBER_ARG]),
                )
                .arg(
                    Arg::with_name(INDEX_ARG)
                        .help("Index to derive xpub at")
                        .short("i")
                        .long(INDEX_ARG)
                        .takes_value(true)
                        .default_value("0"),
                )
                .arg(
                    Arg::with_name(NUMBER_ARG)
                        .help("Number of xpubs to derive, starting from index")
                        .short("n")
                        .long(NUMBER_ARG)
                        .takes_value(true)
                        .default_value("1"),
                )
                .arg(
                    Arg::with_name(TYPE_ARG)
                        .help("Type of xpub to return")
                        .short("t")
                        .long(TYPE_ARG)
                        .takes_value(true)
                        .possible_values(&["xpub", "ypub", "zpub"])
                        .default_value("zpub"),
                ),
        )
        .subcommand(
            App::new(XPRV_SUB)
                .about("Derives account or root xprvs from a seed")
                .arg(
                    Arg::with_name(SEED_ARG)
                        .help("Seed to derive xprvs from")
                        .index(1)
                        .required(true),
                )
                .arg(
                    Arg::with_name(ROOT_ARG)
                        .help("Derives xprv at bip32 root instead of account level")
                        .long(ROOT_ARG)
                        .short("r")
                        .takes_value(false)
                        .conflicts_with_all(&[INDEX_ARG, NUMBER_ARG]),
                )
                .arg(
                    Arg::with_name(INDEX_ARG)
                        .help("Index to derive xprv at")
                        .short("i")
                        .long(INDEX_ARG)
                        .takes_value(true)
                        .default_value("0"),
                )
                .arg(
                    Arg::with_name(NUMBER_ARG)
                        .help("Number of xprvs to derive, starting from index")
                        .short("n")
                        .long(NUMBER_ARG)
                        .takes_value(true)
                        .default_value("1"),
                )
                .arg(
                    Arg::with_name(TYPE_ARG)
                        .help("Type of xprv to return")
                        .short("t")
                        .long(TYPE_ARG)
                        .takes_value(true)
                        .possible_values(&["xprv", "yprv", "zprv"])
                        .default_value("zprv"),
                ),
        )
        .get_matches();
    process_matches(&matches)
}

/// Processes command line arguments.
fn process_matches(matches: &ArgMatches) -> Result<(), String> {
    match matches.subcommand_name() {
        Some(CHILD_SUB) => process_child_matches(matches.subcommand_matches(CHILD_SUB))?,
        Some(EXTEND_SUB) => process_extend_matches(matches.subcommand_matches(EXTEND_SUB))?,
        Some(TRUNCATE_SUB) => process_truncate_matches(matches.subcommand_matches(TRUNCATE_SUB))?,
        Some(XOR_SUB) => process_xor_matches(matches.subcommand_matches(XOR_SUB))?,
        Some(XPUB_SUB) => process_xpub_matches(matches.subcommand_matches(XPUB_SUB))?,
        Some(XPRV_SUB) => process_xprv_matches(matches.subcommand_matches(XPRV_SUB))?,
        Some(unknown) => return Err(format!("Subcommand [{}] does not exist", unknown)),
        None => return Err("No subcommand was used. Try using --help for guidance.".to_string()),
    }

    Ok(())
}

/// Returns the `index` flag's value.
fn index_value(matches: Option<&ArgMatches>) -> Result<u32, String> {
    matches
        .unwrap()
        .value_of(INDEX_ARG)
        .ok_or_else(|| "index not set".to_string())?
        .parse::<u32>()
        .map_err(|_| "index can't be higher than 2^32".to_string())
}

/// Returns the `number` flag's value.
fn number_value(matches: Option<&ArgMatches>) -> Result<u8, String> {
    matches
        .unwrap()
        .value_of(NUMBER_ARG)
        .ok_or_else(|| "number not set".to_string())?
        .parse::<u8>()
        .map_err(|_| "number can't be higher than 255".to_string())
}

/// Returns the `seed` flag's value.
fn seed_value<'a>(matches: Option<&'a ArgMatches>) -> Result<&'a str, String> {
    matches
        .unwrap()
        .value_of(SEED_ARG)
        .ok_or_else(|| "seed not set".to_string())
}

/// Returns the `seed` flag's values as a list of seeds.
fn seed_values<'a>(matches: Option<&'a ArgMatches>) -> Result<Vec<&'a str>, String> {
    Ok(matches
        .unwrap()
        .values_of(SEED_ARG)
        .ok_or_else(|| "seeds not set".to_string())?
        .into_iter()
        .collect())
}

/// Returns the `words` flag's value.
fn word_count_value(matches: Option<&ArgMatches>) -> Result<WordCount, String> {
    let count = matches
        .unwrap()
        .value_of(WORDS_ARG)
        .ok_or("word count not set")?;

    WordCount::from_str(count).map_err(|e| e.to_string())
}

/// Returns the `type` flag's value.
fn type_value(matches: Option<&ArgMatches>) -> Result<Version, String> {
    let version = matches
        .unwrap()
        .value_of(TYPE_ARG)
        .ok_or_else(|| "type not set".to_string())?;
    Version::from_str(version).map_err(|_| format!("Version prefix [{}] is not supported", version))
}

/// Returns the `root` flag.
fn is_root(matches: Option<&ArgMatches>) -> bool {
    matches.unwrap().is_present(ROOT_ARG)
}

/// Processes the `child` subcommand.
fn process_child_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    // Return early because every field is either required or has a default value
    let seed_str = seed_value(matches)?;
    let index = index_value(matches)?;
    let number = number_value(matches)?;
    let word_count = word_count_value(matches)?;

    let derived =
        seed_utils::derive_child_seeds(seed_str, (index, index + number as u32), &word_count)
            .map_err(|e| e.to_string())?;

    for (i, mnemonic) in derived {
        println!("Index {}: {}", i, mnemonic);
    }

    Ok(())
}

/// Processes the `extend` subcommand.
fn process_extend_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    // Return early because every field is either required or has a default value
    let seed_str = seed_value(matches)?;
    let word_count = word_count_value(matches)?;

    let extended_seed =
        seed_utils::extend_seed(seed_str, &word_count).map_err(|e| e.to_string())?;
    println!("Extended seed: {}", extended_seed);

    Ok(())
}

/// Processes the `truncate` subcommand.
fn process_truncate_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    // Return early because seed is required and word count has a default
    let seed_str = seed_value(matches)?;
    let word_count = word_count_value(matches)?;

    let truncated_seed =
        seed_utils::truncate_seed(&seed_str, &word_count).map_err(|e| e.to_string())?;
    println!("Truncated seed: {}", truncated_seed);

    Ok(())
}

/// Processes the `xor` subcommand.
fn process_xor_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    let seeds = seed_values(matches)?;

    if let Some(xor) = seed_utils::xor_seeds(&seeds).map_err(|e| e.to_string())? {
        println!("XORed seed: {}", xor);
    } else {
        println!("No seeds to XOR");
    }

    Ok(())
}

/// Processes the `xpub` subcommand.
fn process_xpub_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    // Return early because every field is either required or has a default value
    let seed_str = seed_value(matches)?;
    let version = type_value(matches)?;

    // Print root key if flag is present
    if is_root(matches) {
        let master = seed_utils::derive_root_xpub(seed_str)
            .map_err(|e| e.to_string())?
            .versioned_string(&version)?;
        println!("Root xpub: {}", master);

        return Ok(());
    }

    // Derive extended public keys
    let index = index_value(matches)?;
    let number = number_value(matches)?;
    let derived =
        seed_utils::derive_xpubs_from_seed(seed_str, (index, index + number as u32), &version)
            .map_err(|e| e.to_string())?;
    for (i, xpub) in derived {
        println!(
            "Derived xpub at {}: {}",
            i,
            xpub.versioned_string(&version)?
        );
    }

    Ok(())
}

/// Processes the `xprv` subcommand.
fn process_xprv_matches(matches: Option<&ArgMatches>) -> Result<(), String> {
    // Return early because every field is either required or has a default value
    let seed_str = seed_value(matches)?;
    let version = type_value(matches)?;

    // Print root key if flag is present
    if is_root(matches) {
        let master = seed_utils::derive_root_xprv(seed_str)
            .map_err(|e| e.to_string())?
            .versioned_string(&version)?;
        println!("Root xprv: {}", master);

        return Ok(());
    }

    // Derive extended private keys
    let index = index_value(matches)?;
    let number = number_value(matches)?;
    let derived =
        seed_utils::derive_xprvs_from_seed(seed_str, (index, index + number as u32), &version)
            .map_err(|e| e.to_string())?;
    for (i, xpub) in derived {
        println!(
            "Derived xprv at {}: {}",
            i,
            xpub.versioned_string(&version)?
        );
    }

    Ok(())
}

/// Trait for returning a versioned string of a Bitcoin address.
trait VersionedString {
    /// Returns a versioned string or `Err` if conversion fails.
    /// Failure may occur when `self`'s string value is not a valid base58 check encoded Bitcoin address.
    fn versioned_string(&self, version: &Version) -> Result<String, String>;
}

impl VersionedString for ExtendedPubKey {
    fn versioned_string(&self, version: &Version) -> Result<String, String> {
        xyzpub::convert_version(self.to_string(), version)
            .map_err(|_| "Failed to convert extended public key to requested version".to_string())
    }
}

impl VersionedString for ExtendedPrivKey {
    fn versioned_string(&self, version: &Version) -> Result<String, String> {
        xyzpub::convert_version(self.to_string(), version)
            .map_err(|_| "Failed to convert extended private key to requested version".to_string())
    }
}