forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 7
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
alex v
committed
Nov 10, 2024
1 parent
847e55c
commit dce6216
Showing
44 changed files
with
1,471 additions
and
220 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2024 The Navio developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef NAVIO_BLSCT_TOKENS_INFO_H | ||
#define NAVIO_BLSCT_TOKENS_INFO_H | ||
|
||
#include <blsct/public_key.h> | ||
#include <consensus/amount.h> | ||
#include <tinyformat.h> | ||
#include <util/moneystr.h> | ||
|
||
namespace blsct { | ||
enum TokenType : unsigned char { | ||
TOKEN = 0, | ||
NFT = 1 | ||
}; | ||
std::string TokenTypeToString(const TokenType& type) | ||
{ | ||
switch (type) { | ||
case TOKEN: { | ||
return "token"; | ||
} | ||
case NFT: { | ||
return "nft"; | ||
} | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
class TokenInfo | ||
{ | ||
public: | ||
TokenType type; | ||
blsct::PublicKey publicKey; | ||
std::map<std::string, std::string> mapMetadata; | ||
CAmount nTotalSupply; | ||
|
||
TokenInfo(const TokenType& type, const blsct::PublicKey& publicKey, const std::map<std::string, std::string>& mapMetadata, | ||
const CAmount& nTotalSupply) : type(type), publicKey(publicKey), mapMetadata(mapMetadata), nTotalSupply(nTotalSupply){}; | ||
TokenInfo(){}; | ||
|
||
SERIALIZE_METHODS(TokenInfo, obj) { READWRITE(static_cast<unsigned char>(obj.type), obj.publicKey, obj.mapMetadata, obj.nTotalSupply); }; | ||
|
||
std::string ToString() const | ||
{ | ||
std::string ret = strprintf("type=%s publicKey=%s", TokenTypeToString(type), publicKey.ToString()); | ||
for (auto& it : mapMetadata) { | ||
ret += strprintf(" %s=%s", it.first, it.second); | ||
} | ||
ret += strprintf(" nTotalSupply=%s", FormatMoney(nTotalSupply)); | ||
return ret; | ||
} | ||
}; | ||
|
||
class TokenEntry | ||
{ | ||
public: | ||
TokenInfo info; | ||
CAmount nSupply; | ||
std::map<CAmount, std::map<std::string, std::string>> mapMintedNft; | ||
|
||
TokenEntry(){}; | ||
TokenEntry(const TokenInfo& info, | ||
const CAmount& nSupply = 0) : info(info), nSupply(nSupply){}; | ||
TokenEntry(const TokenInfo& info, | ||
const std::map<CAmount, std::map<std::string, std::string>>& mapMintedNft) : info(info), mapMintedNft(mapMintedNft){}; | ||
|
||
bool Mint(const CAmount& amount) | ||
{ | ||
if (amount + nSupply > info.nTotalSupply) | ||
return false; | ||
nSupply += nSupply; | ||
return true; | ||
}; | ||
|
||
SERIALIZE_METHODS(TokenEntry, obj) | ||
{ | ||
READWRITE(obj.info); | ||
if (obj.info.type == TOKEN) | ||
READWRITE(obj.nSupply); | ||
else if (obj.info.type == NFT) | ||
READWRITE(obj.mapMintedNft); | ||
}; | ||
}; | ||
} // namespace blsct | ||
|
||
#endif // NAVIO_BLSCT_TOKENS_INFO_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2024 The Navio developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef NAVIO_BLSCT_TOKENS_PREDICATE_H | ||
#define NAVIO_BLSCT_TOKENS_PREDICATE_H | ||
|
||
namespace blsct { | ||
typedef std::vector<std::byte> VectorPredicate; | ||
} | ||
|
||
#endif // NAVIO_BLSCT_TOKENS_PREDICATE_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2024 The Navio developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <blsct/tokens/predicate_parser.h> | ||
|
||
namespace blsct { | ||
ParsedPredicate ParsePredicate(const VectorPredicate& vch) | ||
{ | ||
DataStream ss{vch}; | ||
PredicateOperation op; | ||
ss >> Using<CustomUintFormatter<1>>(op); | ||
|
||
if (op == CREATE_TOKEN) { | ||
CreateTokenPredicate p; | ||
ss >> p; | ||
return p; | ||
} else if (op == MINT) { | ||
MintTokenPredicate p; | ||
ss >> p; | ||
return p; | ||
} else if (op == NFT_MINT) { | ||
MintNftPredicate p; | ||
ss >> p; | ||
return p; | ||
} else if (op == PAY_FEE) { | ||
PayFeePredicate p; | ||
ss >> p; | ||
return p; | ||
} else { | ||
throw std::ios_base::failure("unknown predicate operation"); | ||
} | ||
} | ||
|
||
bool ExecutePredicate(const ParsedPredicate& predicate, CCoinsViewCache& view, const bool& fDisconnect) | ||
{ | ||
if (predicate.IsCreateTokenPredicate()) { | ||
auto hash = predicate.GetPublicKey().GetHash(); | ||
|
||
if (!fDisconnect && view.HaveToken(hash)) return false; | ||
|
||
if (fDisconnect) | ||
view.EraseToken(hash); | ||
else | ||
view.AddToken(hash, std::move(predicate.GetTokenInfo())); | ||
|
||
return true; | ||
} else if (predicate.IsMintTokenPredicate()) { | ||
auto hash = predicate.GetPublicKey().GetHash(); | ||
|
||
if (!view.HaveToken(hash)) | ||
return false; | ||
|
||
blsct::TokenEntry token; | ||
|
||
if (!view.GetToken(hash, token)) | ||
return false; | ||
|
||
if (!token.Mint(predicate.GetAmount() * (1 - 2 * fDisconnect))) | ||
return false; | ||
|
||
view.AddToken(hash, std::move(token)); | ||
|
||
return true; | ||
} else if (predicate.IsMintNftPredicate()) { | ||
auto hash = predicate.GetPublicKey().GetHash(); | ||
|
||
if (!view.HaveToken(hash)) | ||
return false; | ||
|
||
blsct::TokenEntry token; | ||
|
||
if (!view.GetToken(hash, token)) | ||
return false; | ||
|
||
if (token.mapMintedNft.contains(predicate.GetNftId()) == !fDisconnect) | ||
return false; | ||
|
||
token.mapMintedNft[predicate.GetNftId()] = predicate.GetNftMetaData(); | ||
|
||
view.AddToken(hash, std::move(token)); | ||
|
||
return true; | ||
} else if (predicate.IsPayFeePredicate()) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool ExecutePredicate(const VectorPredicate& vch, CCoinsViewCache& view, const bool& fDisconnect) | ||
{ | ||
return ExecutePredicate(ParsePredicate(vch), view, fDisconnect); | ||
} | ||
} // namespace blsct |
Oops, something went wrong.