forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddress.cpp
More file actions
69 lines (55 loc) · 2.15 KB
/
Copy pathAddress.cpp
File metadata and controls
69 lines (55 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Address.h"
#include "BinaryCoding.h"
#include "Forging.h"
#include "../Base58.h"
#include "../BinaryCoding.h"
#include "../Hash.h"
#include "../HexCoding.h"
#include <TrezorCrypto/ecdsa.h>
using namespace TW;
using namespace TW::Tezos;
/// Address prefixes.
const std::array<byte, 3> tz1Prefix{6, 161, 159};
const std::array<byte, 3> tz2Prefix{6, 161, 161};
const std::array<byte, 3> tz3Prefix{6, 161, 164};
bool Address::isValid(const std::string& string) {
const auto decoded = Base58::bitcoin.decodeCheck(string);
if (decoded.size() != Address::size) {
return false;
}
// verify prefix
if (std::equal(tz1Prefix.begin(), tz1Prefix.end(), decoded.begin()) ||
std::equal(tz2Prefix.begin(), tz2Prefix.end(), decoded.begin()) ||
std::equal(tz3Prefix.begin(), tz3Prefix.end(), decoded.begin())) {
return true;
}
return false;
}
Address::Address(const PublicKey& publicKey) {
auto encoded = Data(publicKey.bytes.begin(), publicKey.bytes.end());
auto hash = Hash::blake2b(encoded, 20);
auto addressData = Data({6, 161, 159});
append(addressData, hash);
if (addressData.size() != Address::size)
throw std::invalid_argument("Invalid address key data");
std::copy(addressData.data(), addressData.data() + Address::size, bytes.begin());
}
std::string Address::deriveOriginatedAddress(const std::string& operationHash, int operationIndex) {
// Decode and remove 2 byte prefix.
auto decoded = Base58::bitcoin.decodeCheck(operationHash);
decoded.erase(decoded.begin(), decoded.begin() + 2);
TW::encode32BE(operationIndex, decoded);
auto hash = Hash::blake2b(decoded, 20);
auto prefix = Data({2, 90, 121});
prefix.insert(prefix.end(), hash.begin(), hash.end());
return Base58::bitcoin.encodeCheck(prefix);
}
Data Address::forge() const {
std::string s = string();
return forgePublicKeyHash(s);
}