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 "Address.h"
8

9
#include "../Base58.h"
10
#include <TrezorCrypto/ecdsa.h>
11

12
#include <cassert>
13

14
using namespace TW::Groestlcoin;
15

16 1
bool Address::isValid(const std::string& string) {
17 1
    const auto decoded = Base58::bitcoin.decodeCheck(string, Hash::groestl512d);
18 1
    if (decoded.size() != Address::size) {
19 1
        return false;
20
    }
21 1
    return true;
22
    // return isValid(string, std::vector<byte>{36, 5});
23
}
24

25 1
bool Address::isValid(const std::string& string, const std::vector<byte>& validPrefixes) {
26 1
    const auto decoded = Base58::bitcoin.decodeCheck(string, Hash::groestl512d);
27 1
    if (decoded.size() != Address::size) {
28 1
        return false;
29
    }
30 1
    if (std::find(validPrefixes.begin(), validPrefixes.end(), decoded[0]) == validPrefixes.end()) {
31 1
        return false;
32
    }
33 1
    return true;
34
}
35

36 1
Address::Address(const std::string& string) {
37 1
    const auto decoded = Base58::bitcoin.decodeCheck(string, Hash::groestl512d);
38 1
    if (decoded.size() != Address::size) {
39 0
        throw std::invalid_argument("Invalid address string");
40
    }
41

42 1
    std::copy(decoded.begin(), decoded.end(), bytes.begin());
43
}
44

45 0
Address::Address(const std::vector<uint8_t>& data) {
46 0
    if (!isValid(data)) {
47 0
        throw std::invalid_argument("Invalid address key data");
48
    }
49 0
    std::copy(data.begin(), data.end(), bytes.begin());
50
}
51

52 1
Address::Address(const PublicKey& publicKey, uint8_t prefix) {
53 1
    if (publicKey.type != TWPublicKeyTypeSECP256k1) {
54 0
        throw std::invalid_argument("Groestlcoin::Address needs a compressed SECP256k1 public key.");
55
    }
56 1
    bytes[0] = prefix;
57 1
    ecdsa_get_pubkeyhash(publicKey.bytes.data(), HASHER_SHA2_RIPEMD, bytes.data() + 1);
58
}
59

60 1
std::string Address::string() const {
61 1
    return Base58::bitcoin.encodeCheck(bytes, Hash::groestl512d);
62
}

Read our documentation on viewing source code .

Loading