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
#include "../Base58.h"
9
#include "../Base58Address.h"
10

11
using namespace TW;
12
using namespace TW::Solana;
13

14 1
bool Address::isValid(const std::string& string) {
15 1
    const auto data = Base58::bitcoin.decode(string);
16 1
    return Address::isValid(data);
17
}
18

19 1
Address::Address(const std::string& string) {
20 1
    const auto data = Base58::bitcoin.decode(string);
21 1
    if (!isValid(data)) {
22 0
        throw std::invalid_argument("Invalid address string");
23
    }
24 1
    std::copy(data.begin(), data.end(), bytes.begin());
25
}
26

27 1
Address::Address(const PublicKey& publicKey) {
28 1
    if (publicKey.type != TWPublicKeyTypeED25519) {
29 0
        throw std::invalid_argument("Invalid public key type");
30
    }
31
    static_assert(PublicKey::ed25519Size == size);
32 1
    std::copy(publicKey.bytes.begin(), publicKey.bytes.end(), bytes.data());
33
}
34

35 1
std::string Address::string() const {
36 1
    return Base58::bitcoin.encode(bytes);
37
}
38

39 1
Data Address::vector() const {
40 1
    Data vec(std::begin(bytes), std::end(bytes));
41 1
    return vec;
42
}
43

44 1
Address addressFromValidatorSeed(const Address& fromAddress, const Address& validatorAddress,
45
                                 const Address& programId) {
46 1
    Data extended = fromAddress.vector();
47 1
    std::string seed = validatorAddress.string();
48 1
    Data vecSeed(seed.begin(), seed.end());
49 1
    vecSeed.resize(32);
50 1
    Data additional = programId.vector();
51 1
    extended.insert(extended.end(), vecSeed.begin(), vecSeed.end());
52 1
    extended.insert(extended.end(), additional.begin(), additional.end());
53 1
    Data hash = TW::Hash::sha256(extended);
54 1
    return Address(PublicKey(hash, TWPublicKeyTypeED25519));
55
}

Read our documentation on viewing source code .

Loading