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 "BinaryCoding.h"
9
#include "Forging.h"
10

11
#include "../Base58.h"
12
#include "../BinaryCoding.h"
13
#include "../Hash.h"
14
#include "../HexCoding.h"
15

16
#include <TrezorCrypto/ecdsa.h>
17

18
using namespace TW;
19
using namespace TW::Tezos;
20

21
/// Address prefixes.
22
const std::array<byte, 3> tz1Prefix{6, 161, 159};
23
const std::array<byte, 3> tz2Prefix{6, 161, 161};
24
const std::array<byte, 3> tz3Prefix{6, 161, 164};
25

26 1
bool Address::isValid(const std::string& string) {
27 1
    const auto decoded = Base58::bitcoin.decodeCheck(string);
28 1
    if (decoded.size() != Address::size) {
29 1
        return false;
30
    }
31

32
    // verify prefix
33 1
    if (std::equal(tz1Prefix.begin(), tz1Prefix.end(), decoded.begin()) ||
34 1
        std::equal(tz2Prefix.begin(), tz2Prefix.end(), decoded.begin()) ||
35 1
        std::equal(tz3Prefix.begin(), tz3Prefix.end(), decoded.begin())) {
36 1
        return true;
37
    }
38

39 1
    return false;
40
}
41

42 1
Address::Address(const PublicKey& publicKey) {
43 1
    auto encoded = Data(publicKey.bytes.begin(), publicKey.bytes.end());
44 1
    auto hash = Hash::blake2b(encoded, 20);
45 1
    auto addressData = Data({6, 161, 159});
46 1
    append(addressData, hash);
47 1
    if (addressData.size() != Address::size)
48 0
        throw std::invalid_argument("Invalid address key data");
49 1
    std::copy(addressData.data(), addressData.data() + Address::size, bytes.begin());
50
}
51

52 1
std::string Address::deriveOriginatedAddress(const std::string& operationHash, int operationIndex) {
53
    // Decode and remove 2 byte prefix.
54 1
    auto decoded = Base58::bitcoin.decodeCheck(operationHash);
55 1
    decoded.erase(decoded.begin(), decoded.begin() + 2);
56 1
    TW::encode32BE(operationIndex, decoded);
57

58 1
    auto hash = Hash::blake2b(decoded, 20);
59

60 1
    auto prefix = Data({2, 90, 121});
61 1
    prefix.insert(prefix.end(), hash.begin(), hash.end());
62

63 1
    return Base58::bitcoin.encodeCheck(prefix);
64
}
65

66 1
Data Address::forge() const {
67 1
    std::string s = string();
68 1
    return forgePublicKeyHash(s);
69
}

Read our documentation on viewing source code .

Loading