forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccount.cpp
More file actions
56 lines (48 loc) · 1.95 KB
/
Copy pathAccount.cpp
File metadata and controls
56 lines (48 loc) · 1.95 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
// 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 "Account.h"
#include "../Base64.h"
#include "../Coin.h"
#include "../HexCoding.h"
using namespace TW;
using namespace TW::Keystore;
namespace CodingKeys {
static const auto address = "address";
static const auto derivationPath = "derivationPath";
static const auto extendedPublicKey = "extendedPublicKey";
static const auto indices = "indices";
static const auto value = "value";
static const auto hardened = "hardened";
} // namespace CodingKeys
Account::Account(const nlohmann::json& json) {
if (json[CodingKeys::derivationPath].is_object()) {
const auto indices = json[CodingKeys::derivationPath][CodingKeys::indices];
for (auto& indexJSON : indices) {
derivationPath.indices.emplace_back(indexJSON[CodingKeys::value].get<uint32_t>(),
indexJSON[CodingKeys::hardened].get<bool>());
}
} else if (json[CodingKeys::derivationPath].is_string()) {
derivationPath = DerivationPath(json[CodingKeys::derivationPath].get<std::string>());
}
if (json.count(CodingKeys::address) != 0 && json[CodingKeys::address].is_string()) {
address = json[CodingKeys::address].get<std::string>();
} else {
address = "";
}
if (json.count(CodingKeys::extendedPublicKey) > 0 &&
json[CodingKeys::extendedPublicKey].is_string()) {
extendedPublicKey = json[CodingKeys::extendedPublicKey].get<std::string>();
}
}
nlohmann::json Account::json() const {
nlohmann::json j;
j[CodingKeys::address] = address;
j[CodingKeys::derivationPath] = derivationPath.string();
if (!extendedPublicKey.empty()) {
j[CodingKeys::extendedPublicKey] = extendedPublicKey;
}
return j;
}