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

8
#include "Address.h"
9
#include <TrezorCrypto/nano.h>
10

11
#include <string>
12

13
using namespace TW::Nano;
14

15 1
const std::string kPrefixNano{"nano_"};
16 1
const std::string kPrefixXrb{"xrb_"};
17

18 1
bool Address::isValid(const std::string& address) {
19 1
    bool valid = false;
20

21 1
    valid = nano_validate_address(
22 1
        kPrefixNano.c_str(), kPrefixNano.length(),
23 1
        address.c_str(), address.length(),
24
        NULL);
25 1
    if (!valid) {
26 1
        valid = nano_validate_address(
27 1
            kPrefixXrb.c_str(), kPrefixXrb.length(),
28 1
            address.c_str(), address.length(),
29
            NULL);
30
    }
31

32 1
    return valid;
33
}
34

35 1
Address::Address(const std::string& address) {
36 1
    bool valid = false;
37

38 1
    valid = nano_validate_address(
39 1
        kPrefixNano.c_str(), kPrefixNano.length(),
40 1
        address.c_str(), address.length(),
41 1
        bytes.data());
42

43 1
    if (!valid) {
44 1
        valid = nano_validate_address(
45 1
            kPrefixXrb.c_str(), kPrefixXrb.length(),
46 1
            address.c_str(), address.length(),
47 1
            bytes.data());
48
    }
49

50
    // Ensure address is valid
51 1
    if (!valid) {
52 1
        throw std::invalid_argument("Invalid address data");
53
    }
54
}
55

56 1
Address::Address(const PublicKey& publicKey) {
57 1
    if (publicKey.type != TWPublicKeyTypeED25519Blake2b) {
58 1
        throw std::invalid_argument("Invalid public key type");
59
    }
60

61 1
    auto keyBytes = publicKey.bytes;
62 1
    std::copy(keyBytes.begin(), keyBytes.begin() + 32, bytes.data());
63
}
64

65 1
std::string Address::string() const {
66 1
    std::array<char, 5 + 60 + 1 + 1> out = {0};
67

68 1
    size_t count = nano_get_address(
69 1
            bytes.data(),
70 1
            kPrefixNano.c_str(), kPrefixNano.length(),
71 1
            out.data(), out.size());
72
    // closing \0
73 1
    assert(count < out.size());
74 1
    out[count] = 0;
75 1
    return std::string(out.data());
76
}

Read our documentation on viewing source code .

Loading