-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0930e6d
commit 517a398
Showing
40 changed files
with
589 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 <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::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)); | ||
} | ||
if (asset.IsNull()) { | ||
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Unknown label and invalid asset hex: %s", strasset.c_str())); | ||
} | ||
return asset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
#ifndef BITCOIN_ASSETSDIR_H | ||
#define BITCOIN_ASSETSDIR_H | ||
|
||
#include <asset.h> | ||
|
||
#include <map> | ||
|
||
class AssetMetadata | ||
{ | ||
std::string label; | ||
public: | ||
AssetMetadata() : label("") {}; | ||
AssetMetadata(std::string _label) : label(_label) {}; | ||
|
||
const std::string& GetLabel() const | ||
{ | ||
return label; | ||
} | ||
}; | ||
|
||
class CAssetsDir | ||
{ | ||
std::map<CAsset, AssetMetadata> mapAssetMetadata; | ||
std::map<std::string, CAsset> mapAssets; | ||
|
||
void Set(const CAsset& asset, const AssetMetadata& metadata); | ||
void SetHex(const std::string& assetHex, const std::string& label); | ||
public: | ||
void InitFromStrings(const std::vector<std::string>& assetsToInit, const std::string& pegged_asset_name); | ||
|
||
/** | ||
* @param label A label string | ||
* @return asset id corresponding to the asset label | ||
*/ | ||
CAsset GetAsset(const std::string& label) const; | ||
|
||
AssetMetadata GetMetadata(const CAsset& asset) const; | ||
|
||
/** @return the label associated to the asset id */ | ||
std::string GetLabel(const CAsset& asset) const; | ||
|
||
std::vector<CAsset> GetKnownAssets() const; | ||
}; | ||
|
||
/** | ||
* Returns asset id corresponding to the given asset expression, which is either an asset label or a hex value. | ||
* @param strasset A label string or a hex value corresponding to an asset | ||
* @return The asset ID for the given expression | ||
*/ | ||
CAsset GetAssetFromString(const std::string& strasset); | ||
|
||
#endif // BITCOIN_ASSETSDIR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.