|
| 1 | +// Copyright (c) 2017-2017 The Elements Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <assetsdir.h> |
| 6 | +#include <chainparams.h> |
| 7 | + |
| 8 | +#include <tinyformat.h> |
| 9 | +#include <utilstrencodings.h> |
| 10 | + |
| 11 | +#include <boost/algorithm/string/classification.hpp> |
| 12 | +#include <boost/algorithm/string/split.hpp> |
| 13 | + |
| 14 | +void CAssetsDir::Set(const CAsset& asset, const AssetMetadata& metadata) |
| 15 | +{ |
| 16 | + // No asset or label repetition |
| 17 | + if (GetLabel(asset) != "") |
| 18 | + throw std::runtime_error(strprintf("duplicated asset '%s'", asset.GetHex())); |
| 19 | + if (GetAsset(metadata.GetLabel()) != CAsset()) |
| 20 | + throw std::runtime_error(strprintf("duplicated label '%s'", metadata.GetLabel())); |
| 21 | + |
| 22 | + mapAssetMetadata[asset] = metadata; |
| 23 | + mapAssets[metadata.GetLabel()] = asset; |
| 24 | +} |
| 25 | + |
| 26 | +void CAssetsDir::SetHex(const std::string& assetHex, const std::string& label) |
| 27 | +{ |
| 28 | + if (!IsHex(assetHex) || assetHex.size() != 64) |
| 29 | + throw std::runtime_error("The asset must be hex string of length 64"); |
| 30 | + |
| 31 | + const std::vector<std::string> protectedLabels = {"", "*", "bitcoin", "Bitcoin", "btc"}; |
| 32 | + for (std::string proLabel : protectedLabels) { |
| 33 | + if (label == proLabel) { |
| 34 | + throw std::runtime_error(strprintf("'%s' label is protected", proLabel)); |
| 35 | + } |
| 36 | + } |
| 37 | + Set(CAsset(uint256S(assetHex)), AssetMetadata(label)); |
| 38 | +} |
| 39 | + |
| 40 | +void CAssetsDir::InitFromStrings(const std::vector<std::string>& assetsToInit, const std::string& pegged_asset_name) |
| 41 | +{ |
| 42 | + for (std::string strToSplit : assetsToInit) { |
| 43 | + std::vector<std::string> vAssets; |
| 44 | + boost::split(vAssets, strToSplit, boost::is_any_of(":")); |
| 45 | + if (vAssets.size() != 2) { |
| 46 | + throw std::runtime_error("-assetdir parameters malformed, expecting asset:label"); |
| 47 | + } |
| 48 | + SetHex(vAssets[0], vAssets[1]); |
| 49 | + } |
| 50 | + // Set "bitcoin" to the pegged asset for tests |
| 51 | + Set(Params().GetConsensus().pegged_asset, AssetMetadata(pegged_asset_name)); |
| 52 | +} |
| 53 | + |
| 54 | +CAsset CAssetsDir::GetAsset(const std::string& label) const |
| 55 | +{ |
| 56 | + auto it = mapAssets.find(label); |
| 57 | + if (it != mapAssets.end()) |
| 58 | + return it->second; |
| 59 | + return CAsset(); |
| 60 | +} |
| 61 | + |
| 62 | +AssetMetadata CAssetsDir::GetMetadata(const CAsset& asset) const |
| 63 | +{ |
| 64 | + auto it = mapAssetMetadata.find(asset); |
| 65 | + if (it != mapAssetMetadata.end()) |
| 66 | + return it->second; |
| 67 | + return AssetMetadata(""); |
| 68 | +} |
| 69 | + |
| 70 | +std::string CAssetsDir::GetLabel(const CAsset& asset) const |
| 71 | +{ |
| 72 | + return GetMetadata(asset).GetLabel(); |
| 73 | +} |
| 74 | + |
| 75 | +std::vector<CAsset> CAssetsDir::GetKnownAssets() const |
| 76 | +{ |
| 77 | + std::vector<CAsset> knownAssets; |
| 78 | + for (auto it = mapAssets.begin(); it != mapAssets.end(); it++) { |
| 79 | + knownAssets.push_back(it->second); |
| 80 | + } |
| 81 | + return knownAssets; |
| 82 | +} |
| 83 | + |
| 84 | +CAsset GetAssetFromString(const std::string& strasset) { |
| 85 | + CAsset asset = gAssetsDir.GetAsset(strasset); |
| 86 | + if (asset.IsNull() && strasset.size() == 64 && IsHex(strasset)) { |
| 87 | + asset = CAsset(uint256S(strasset)); |
| 88 | + } |
| 89 | + return asset; |
| 90 | +} |
| 91 | + |
| 92 | +// GLOBAL: |
| 93 | +CAssetsDir _gAssetsDir; |
| 94 | +const CAssetsDir& gAssetsDir = _gAssetsDir; |
| 95 | + |
| 96 | +void InitGlobalAssetDir(const std::vector<std::string>& assetsToInit, const std::string& pegged_asset_name) |
| 97 | +{ |
| 98 | + _gAssetsDir.InitFromStrings(assetsToInit, pegged_asset_name); |
| 99 | +} |
0 commit comments