1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "DerivationPath.h"
8

9
#include <cstdio>
10
#include <stdexcept>
11

12
using namespace TW;
13

14 1
DerivationPath::DerivationPath(const std::string& string) {
15 1
    auto it = string.data();
16 1
    const auto end = string.data() + string.size();
17

18 1
    if (it != end && *it == 'm') {
19 1
        ++it;
20
    }
21 1
    if (it != end && *it == '/') {
22 1
        ++it;
23
    }
24

25 1
    while (it != end) {
26
        uint32_t value;
27 1
        if (std::sscanf(it, "%ud", &value) != 1) {
28 1
            throw std::invalid_argument("Invalid component");
29
        }
30 1
        while (it != end && isdigit(*it)) {
31 1
            ++it;
32
        }
33

34 1
        auto hardened = (it != end && *it == '\'');
35 1
        if (hardened) {
36 1
            ++it;
37
        }
38 1
        indices.emplace_back(value, hardened);
39

40 1
        if (it == end) {
41 1
            break;
42
        }
43 1
        if (*it != '/') {
44 1
            throw std::invalid_argument("Components should be separated by '/'");
45
        }
46 1
        ++it;
47
    }
48
}
49

50 1
std::string DerivationPath::string() const noexcept {
51 1
    std::string result = "m/";
52 1
    for (auto& index : indices) {
53 1
        result += index.string();
54 1
        result += "/";
55
    }
56 1
    if (result.back() == '/') {
57 1
        result.erase(result.end() - 1);
58
    }
59 1
    return result;
60
}

Read our documentation on viewing source code .

Loading