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
55 lines (47 loc) · 1.81 KB
/
Copy pathAddress.cpp
File metadata and controls
55 lines (47 loc) · 1.81 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
// 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 "../Base58.h"
#include "../Base58Address.h"
using namespace TW;
using namespace TW::Solana;
bool Address::isValid(const std::string& string) {
const auto data = Base58::bitcoin.decode(string);
return Address::isValid(data);
}
Address::Address(const std::string& string) {
const auto data = Base58::bitcoin.decode(string);
if (!isValid(data)) {
throw std::invalid_argument("Invalid address string");
}
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const PublicKey& publicKey) {
if (publicKey.type != TWPublicKeyTypeED25519) {
throw std::invalid_argument("Invalid public key type");
}
static_assert(PublicKey::ed25519Size == size);
std::copy(publicKey.bytes.begin(), publicKey.bytes.end(), bytes.data());
}
std::string Address::string() const {
return Base58::bitcoin.encode(bytes);
}
Data Address::vector() const {
Data vec(std::begin(bytes), std::end(bytes));
return vec;
}
Address addressFromValidatorSeed(Address& fromAddress, Address& validatorAddress,
Address& programId) {
Data extended = fromAddress.vector();
std::string seed = validatorAddress.string();
Data vecSeed(seed.begin(), seed.end());
vecSeed.resize(32);
Data additional = programId.vector();
extended.insert(extended.end(), vecSeed.begin(), vecSeed.end());
extended.insert(extended.end(), additional.begin(), additional.end());
Data hash = TW::Hash::sha256(extended);
return Address(PublicKey(hash, TWPublicKeyTypeED25519));
}