-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathassetsdir.cpp
More file actions
113 lines (95 loc) · 3.45 KB
/
assetsdir.cpp
File metadata and controls
113 lines (95 loc) · 3.45 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2017-2017 The Elements Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <assetsdir.h>
#include <chainparams.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
void CAssetsDir::Set(const CAsset& asset, const AssetMetadata& metadata)
{
// No asset or label repetition
if (GetLabel(asset) != "")
throw std::runtime_error(strprintf("duplicated asset '%s'", asset.GetHex()));
if (GetAsset(metadata.GetLabel()) != CAsset())
throw std::runtime_error(strprintf("duplicated label '%s'", metadata.GetLabel()));
mapAssetMetadata[asset] = metadata;
mapAssets[metadata.GetLabel()] = asset;
}
void CAssetsDir::SetHex(const std::string& assetHex, const std::string& label)
{
if (!IsHex(assetHex) || assetHex.size() != 64)
throw std::runtime_error("The asset must be hex string of length 64");
const std::vector<std::string> protectedLabels = {"", "*", "bitcoin", "Bitcoin", "btc"};
for (std::string proLabel : protectedLabels) {
if (label == proLabel) {
throw std::runtime_error(strprintf("'%s' label is protected", proLabel));
}
}
Set(CAsset(uint256S(assetHex)), AssetMetadata(label));
}
void CAssetsDir::InitFromStrings(const std::vector<std::string>& assetsToInit, const std::string& pegged_asset_name)
{
for (std::string strToSplit : assetsToInit) {
std::vector<std::string> vAssets;
boost::split(vAssets, strToSplit, boost::is_any_of(":"));
if (vAssets.size() != 2) {
throw std::runtime_error("-assetdir parameters malformed, expecting asset:label");
}
SetHex(vAssets[0], vAssets[1]);
}
// Set "bitcoin" to the pegged asset for tests
Set(Params().GetConsensus().pegged_asset, AssetMetadata(pegged_asset_name));
}
CAsset CAssetsDir::GetAsset(const std::string& label) const
{
auto it = mapAssets.find(label);
if (it != mapAssets.end())
return it->second;
return CAsset();
}
AssetMetadata CAssetsDir::GetMetadata(const CAsset& asset) const
{
auto it = mapAssetMetadata.find(asset);
if (it != mapAssetMetadata.end())
return it->second;
return AssetMetadata("");
}
std::string CAssetsDir::GetLabel(const CAsset& asset) const
{
return GetMetadata(asset).GetLabel();
}
std::string CAssetsDir::GetIdentifier(const CAsset& asset) const
{
const std::string label = GetMetadata(asset).GetLabel();
if (!label.empty()) return label;
return asset.GetHex();
}
std::vector<CAsset> CAssetsDir::GetKnownAssets() const
{
std::vector<CAsset> knownAssets;
for (auto it = mapAssets.begin(); it != mapAssets.end(); it++) {
knownAssets.push_back(it->second);
}
return knownAssets;
}
CAsset GetAssetFromString(const std::string& strasset) {
CAsset asset = gAssetsDir.GetAsset(strasset);
if (asset.IsNull() && strasset.size() == 64 && IsHex(strasset)) {
asset = CAsset(uint256S(strasset));
}
return asset;
}
// GLOBAL:
CAssetsDir _gAssetsDir;
const CAssetsDir& gAssetsDir = _gAssetsDir;
void InitGlobalAssetDir(const std::vector<std::string>& assetsToInit, const std::string& pegged_asset_name)
{
_gAssetsDir.InitFromStrings(assetsToInit, pegged_asset_name);
}
// Used in testing
void ClearGlobalAssetDir()
{
_gAssetsDir = CAssetsDir();
}