// 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 "utilstrencodings.h" #include #include 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 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& assetsToInit) { for (std::string strToSplit : assetsToInit) { std::vector 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("bitcoin")); } 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::vector CAssetsDir::GetKnownAssets() const { std::vector knownAssets; for (auto it = mapAssets.begin(); it != mapAssets.end(); it++) { knownAssets.push_back(it->second); } return knownAssets; }