diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index 84ac41bb52..b44538064d 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -271,7 +271,7 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const std::string& strIn // extract and validate ASSET std::string strAsset = vStrOutAddrParts[2]; - CAssetID asset = uint256S(strAsset); + CAssetID asset(uint256S(strAsset)); if (asset == CAssetID()) throw std::runtime_error("invalid TX output asset type"); @@ -441,7 +441,7 @@ static void MutateTxBlind(CMutableTransaction& tx, const std::string& strInput) std::vector output_pubkeys; std::vector input_amounts; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; for (size_t nIn = 0; nIn < tx.vin.size(); nIn++) { std::vector entry; boost::split(entry, input_blinding[nIn], boost::is_any_of(",")); @@ -452,7 +452,7 @@ static void MutateTxBlind(CMutableTransaction& tx, const std::string& strInput) uint256 assetblind; assetblind.SetHex(entry[2]); input_asset_blinds.push_back(assetblind); - uint256 id; + CAssetID id; id.SetHex(entry[3]); input_asset_ids.push_back(id); CAmount value; @@ -510,7 +510,7 @@ static void MutateTxAddOutScript(CMutableTransaction& tx, const std::string& str // extract and validate asset std::string strAsset = vStrOutScriptParts[2]; - CAssetID asset = uint256S(strAsset); + CAssetID asset(uint256S(strAsset)); // construct TxOut, append to transaction output list CTxOut txout(asset, value, scriptPubKey); diff --git a/src/blind.cpp b/src/blind.cpp index 8df6b86def..3cc651b10b 100644 --- a/src/blind.cpp +++ b/src/blind.cpp @@ -34,7 +34,7 @@ public: static Blind_ECC_Init ecc_init_on_load; -bool UnblindOutput(const CKey &key, const CTxOut& txout, CAmount& amount_out, uint256& blinding_factor_out, uint256& asset_id_out, uint256& asset_blinding_factor_out) +bool UnblindOutput(const CKey &key, const CTxOut& txout, CAmount& amount_out, uint256& blinding_factor_out, CAssetID& asset_id_out, uint256& asset_blinding_factor_out) { if (!key.IsValid()) { return false; @@ -63,18 +63,18 @@ bool UnblindOutput(const CKey &key, const CTxOut& txout, CAmount& amount_out, ui if (!res || amount > (uint64_t)MAX_MONEY || !MoneyRange((CAmount)amount) || msg_size != 64 || secp256k1_generator_generate_blinded(secp256k1_blind_context, &recoveredGen, msg+32, msg+64) != 1 || !memcmp(&gen, &recoveredGen, 33)) { amount_out = 0; blinding_factor_out = uint256(); - asset_id_out = uint256(); + asset_id_out = CAssetID(); asset_blinding_factor_out = uint256(); return false; } else { amount_out = (CAmount)amount; - asset_id_out = uint256(std::vector(msg, msg+32)); + asset_id_out = CAssetID(std::vector(msg, msg+32)); asset_blinding_factor_out = uint256(std::vector(msg+32, msg+64)); return true; } } -int BlindOutputs(std::vector& input_blinding_factors, const std::vector& input_asset_blinding_factors, const std::vector& input_asset_ids, const std::vector& input_amounts, std::vector& output_blinding_factors, std::vector& output_asset_blinding_factors, const std::vector& output_pubkeys, CMutableTransaction& tx) +int BlindOutputs(std::vector& input_blinding_factors, const std::vector& input_asset_blinding_factors, const std::vector& input_asset_ids, const std::vector& input_amounts, std::vector& output_blinding_factors, std::vector& output_asset_blinding_factors, const std::vector& output_pubkeys, CMutableTransaction& tx) { assert(tx.vout.size() == output_blinding_factors.size()); assert(tx.vout.size() == output_pubkeys.size()); @@ -153,7 +153,7 @@ int BlindOutputs(std::vector& input_blinding_factors, const std::vecto unsigned char asset_blind[tx.vout.size()][32]; secp256k1_pedersen_commitment commit; secp256k1_generator gen; - uint256 assetID; + CAssetID assetID; for (size_t nOut = 0; nOut < tx.vout.size(); nOut++) { CTxOut& out = tx.vout[nOut]; diff --git a/src/blind.h b/src/blind.h index 32e13cd8e0..8ec8ba7125 100644 --- a/src/blind.h +++ b/src/blind.h @@ -5,7 +5,7 @@ #include "pubkey.h" #include "primitives/transaction.h" -bool UnblindOutput(const CKey& blinding_key, const CTxOut& txout, CAmount& amount_out, uint256& blinding_factor_out, uint256& asset_id_out, uint256& asset_blinding_factor_out); +bool UnblindOutput(const CKey& blinding_key, const CTxOut& txout, CAmount& amount_out, uint256& blinding_factor_out, CAssetID& asset_id_out, uint256& asset_blinding_factor_out); /* Returns the number of ouputs that were successfully blinded. * In many cases a `0` can be fixed by adding an additional output. @@ -18,6 +18,6 @@ bool UnblindOutput(const CKey& blinding_key, const CTxOut& txout, CAmount& amoun * @param[in] output_pubkeys - If non-null, these pubkeys will be used in conjunction with the non-null passed in output blinding factors. * @param[in/out] tx - The transaction to be modified. */ -int BlindOutputs(std::vector& input_blinding_factors, const std::vector& input_asset_blinding_factors, const std::vector& input_asset_ids, const std::vector& input_amounts, std::vector& output_blinding_factors, std::vector& output_asset_blinding_factors, const std::vector& output_pubkeys, CMutableTransaction& tx); +int BlindOutputs(std::vector& input_blinding_factors, const std::vector& input_asset_blinding_factors, const std::vector& input_asset_ids, const std::vector& input_amounts, std::vector& output_blinding_factors, std::vector& output_asset_blinding_factors, const std::vector& output_pubkeys, CMutableTransaction& tx); #endif diff --git a/src/chainparams.cpp b/src/chainparams.cpp index d645f2bb41..1e694c74e1 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -29,7 +29,7 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de return returnScript; } -static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, const CScript& scriptChallenge, int32_t nVersion, const CAmount& genesisReward, const uint32_t rewardShards, const uint256& assetID) +static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, const CScript& scriptChallenge, int32_t nVersion, const CAmount& genesisReward, const uint32_t rewardShards, const CAssetID& assetID) { // Shards must be evenly divisible assert(MAX_MONEY % rewardShards == 0); diff --git a/src/chainparams.h b/src/chainparams.h index df5c694401..98e590fcf1 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -102,7 +102,7 @@ protected: std::string strNetworkID; CBlock genesis; uint256 parentGenesisBlockHash; - uint256 bitcoinID; + CAssetID bitcoinID; std::vector vFixedSeeds; bool fMiningRequiresPeers; bool fDefaultConsistencyChecks; diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp index a73fa6174b..bb5dc0b5cb 100644 --- a/src/primitives/transaction.cpp +++ b/src/primitives/transaction.cpp @@ -16,7 +16,7 @@ void CTxOutAsset::SetNull() vchSurjectionproof.clear(); } -bool CTxOutAsset::GetAssetID(uint256& assetID) const +bool CTxOutAsset::GetAssetID(CAssetID& assetID) const { if (!IsAssetID()) return false; @@ -24,7 +24,7 @@ bool CTxOutAsset::GetAssetID(uint256& assetID) const return true; } -void CTxOutAsset::SetToAssetID(const uint256& assetID) +void CTxOutAsset::SetToAssetID(const CAssetID& assetID) { vchAssetTag.reserve(nAssetTagSize); vchAssetTag.push_back(1); @@ -86,7 +86,7 @@ CTxOut::CTxOut(const CTxOutAsset& nAssetIn, const CTxOutValue& nValueIn, CScript std::string CTxOut::ToString() const { - uint256 assetID; + CAssetID assetID; std::string strAsset; if (nAsset.IsAssetID() && nAsset.GetAssetID(assetID)) strAsset = strprintf("nAsset=%s, ", assetID.ToString()); @@ -175,7 +175,7 @@ bool CTransaction::HasValidFee() const fee = vout[i].nValue.GetAmount(); if (fee == 0 || !MoneyRange(fee)) return false; - uint256 assetid; + CAssetID assetid; vout[i].nAsset.GetAssetID(assetid); totalFee[assetid] += fee; } @@ -188,7 +188,7 @@ CAmountMap CTransaction::GetFee() const CAmountMap fee; for (unsigned int i = 0; i < vout.size(); i++) if (vout[i].IsFee()) { - uint256 assetid; + CAssetID assetid; vout[i].nAsset.GetAssetID(assetid); fee[assetid] += vout[i].nValue.GetAmount(); } diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index f912da8219..fd9219e59a 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -31,7 +31,7 @@ public: SetNull(); } - CTxOutAsset(const uint256& assetID) + CTxOutAsset(const CAssetID& assetID) { SetToAssetID(assetID); } @@ -56,7 +56,7 @@ public: { return vchAssetTag.size()==nAssetTagSize && vchAssetTag[0]==1; } - bool GetAssetID(uint256& assetID) const; + bool GetAssetID(CAssetID& assetID) const; bool IsAssetCommitment() const { @@ -75,7 +75,7 @@ public: } private: - void SetToAssetID(const uint256& assetID); + void SetToAssetID(const CAssetID& assetID); }; class CTxOutValue @@ -222,7 +222,7 @@ public: { if (!nValue.IsAmount()) return false; // FIXME - uint256 assetid; + CAssetID assetid; if (!nAsset.GetAssetID(assetid) || assetid != BITCOINID) return false; //Withdrawlocks are evaluated at a higher, static feerate @@ -234,7 +234,7 @@ public: bool IsFee() const { - uint256 assetid; + CAssetID assetid; if (scriptPubKey == CScript() && nValue.IsAmount() && nAsset.IsAssetID()) return true; return false; diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 36372d2bc2..84004d84b6 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -145,7 +145,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry) } const CTxOutAsset& asset = txout.nAsset; if (asset.IsAssetID()) { - uint256 assetID; + CAssetID assetID; asset.GetAssetID(assetID); out.push_back(Pair("assetid", assetID.GetHex())); } @@ -479,7 +479,7 @@ UniValue createrawtransaction(const JSONRPCRequest& request) assetids = request.params[3].get_obj(); } - uint256 bitcoinid(BITCOINID); + CAssetID bitcoinid(BITCOINID); CAmountMap inputValue; for (unsigned int idx = 0; idx < inputs.size(); idx++) { @@ -508,10 +508,10 @@ UniValue createrawtransaction(const JSONRPCRequest& request) CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence); - uint256 asset(bitcoinid); + CAssetID asset(bitcoinid); const UniValue& asset_val = find_value(o, "assetid"); if (asset_val.isStr()) { - asset = ParseHashO(o, "assetid"); + asset = CAssetID(ParseHashO(o, "assetid")); } UniValue vout_value = find_value(o, "amount"); @@ -530,11 +530,11 @@ UniValue createrawtransaction(const JSONRPCRequest& request) vector addrList = sendTo.getKeys(); BOOST_FOREACH(const string& name_, addrList) { // Defaults to bitcoin - uint256 asset(bitcoinid); + CAssetID asset(bitcoinid); if (!assetids.isNull()) { if (find_value(assetids, name_).isNull()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Given output_assetid address is not a valid given output address: ")+name_); - asset = ParseHashO(assetids, name_); + asset = CAssetID(ParseHashO(assetids, name_)); } if (name_ == "data") { @@ -584,12 +584,12 @@ UniValue createrawtransaction(const JSONRPCRequest& request) // Retrieve already-existing output blinds for a given transaction (if known to wallet) // or blank spots to be filled by BlindOutputs -void FillOutputBlinds(const CMutableTransaction& tx, bool fUseWallet, std::vector& output_value_blinds, std::vector& output_asset_blinds, std::vector& output_asset_ids, std::vector& output_pubkeys) { +void FillOutputBlinds(const CMutableTransaction& tx, bool fUseWallet, std::vector& output_value_blinds, std::vector& output_asset_blinds, std::vector& output_asset_ids, std::vector& output_pubkeys) { for (size_t nOut = 0; nOut < tx.vout.size(); nOut++) { if (!tx.vout[nOut].nValue.IsAmount()) { uint256 blinding_factor; uint256 asset_blinding_factor; - uint256 asset_id; + CAssetID asset_id; CAmount amount; #ifdef ENABLE_WALLET if (fUseWallet && UnblindOutput(pwalletMain->GetBlindingKey(&tx.vout[nOut].scriptPubKey), tx.vout[nOut], amount, blinding_factor, asset_id, asset_blinding_factor) != 0) { @@ -606,7 +606,7 @@ void FillOutputBlinds(const CMutableTransaction& tx, bool fUseWallet, std::vecto output_pubkeys.push_back(CPubKey()); output_value_blinds.push_back(uint256()); output_asset_blinds.push_back(uint256()); - output_asset_ids.push_back(uint256()); + output_asset_ids.push_back(CAssetID()); } else { CPubKey pubkey(tx.vout[nOut].nValue.vchNonceCommitment); if (!pubkey.IsValid()) { @@ -615,7 +615,7 @@ void FillOutputBlinds(const CMutableTransaction& tx, bool fUseWallet, std::vecto output_pubkeys.push_back(pubkey); output_value_blinds.push_back(uint256()); output_asset_blinds.push_back(uint256()); - output_asset_ids.push_back(uint256()); + output_asset_ids.push_back(CAssetID()); } } } @@ -680,10 +680,10 @@ UniValue rawblindrawtransaction(const JSONRPCRequest& request) std::vector input_amounts; std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector output_value_blinds; std::vector output_asset_blinds; - std::vector output_asset_ids; + std::vector output_asset_ids; std::vector output_pubkeys; for (size_t nIn = 0; nIn < tx.vin.size(); nIn++) { if (!inputBlinds[nIn].isStr()) @@ -707,7 +707,7 @@ UniValue rawblindrawtransaction(const JSONRPCRequest& request) input_blinds.push_back(uint256S(blind)); input_asset_blinds.push_back(uint256S(assetblind)); - input_asset_ids.push_back(uint256S(assetid)); + input_asset_ids.push_back(CAssetID(uint256S(assetid))); input_amounts.push_back(inputAmounts[nIn].get_int64()); } @@ -760,11 +760,11 @@ UniValue blindrawtransaction(const JSONRPCRequest& request) std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector input_amounts; std::vector output_blinds; std::vector output_asset_blinds; - std::vector output_asset_ids; + std::vector output_asset_ids; std::vector output_pubkeys; for (size_t nIn = 0; nIn < tx.vin.size(); nIn++) { std::map::iterator it = pwalletMain->mapWallet.find(tx.vin[nIn].prevout.hash); @@ -777,7 +777,7 @@ UniValue blindrawtransaction(const JSONRPCRequest& request) input_blinds.push_back(it->second.GetBlindingFactor(tx.vin[nIn].prevout.n)); input_asset_blinds.push_back(it->second.GetAssetBlindingFactor(tx.vin[nIn].prevout.n)); if (it->second.tx->vout[tx.vin[nIn].prevout.n].nAsset.IsAssetID()) { - uint256 assetID; + CAssetID assetID; it->second.tx->vout[tx.vin[nIn].prevout.n].nAsset.GetAssetID(assetID); input_asset_ids.push_back(assetID); } diff --git a/src/test/blind_tests.cpp b/src/test/blind_tests.cpp index 24da463c4a..43063ba49a 100644 --- a/src/test/blind_tests.cpp +++ b/src/test/blind_tests.cpp @@ -30,9 +30,9 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) CKey keyDummy; // Any asset id will do - uint256 bitcoinID(GetRandHash()); - uint256 otherID(GetRandHash()); - uint256 unblinded_id; + CAssetID bitcoinID(GetRandHash()); + CAssetID otherID(GetRandHash()); + CAssetID unblinded_id; uint256 asset_blind; unsigned char k1[32] = {1,2,3}; @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) // Try to blind with a single non-fee output, which fails as its blinding factor ends up being zero. std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector input_amounts; std::vector output_blinds; std::vector output_asset_blinds; @@ -119,7 +119,7 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) BOOST_CHECK(UnblindOutput(key1, tx3.vout[0], unblinded_amount, blind3, unblinded_id, asset_blind) == 1); BOOST_CHECK(unblinded_amount == 100); BOOST_CHECK(unblinded_id == bitcoinID); - uint256 temp_asset_id; + CAssetID temp_asset_id; uint256 temp_asset_blinder; BOOST_CHECK(UnblindOutput(keyDummy, tx3.vout[2], unblinded_amount, blindDummy, temp_asset_id, temp_asset_blinder) == 1); BOOST_CHECK(unblinded_amount == 0); @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector input_amounts; std::vector output_blinds; std::vector output_asset_blinds; @@ -191,7 +191,7 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector input_amounts; std::vector output_blinds; std::vector output_asset_blinds; @@ -230,13 +230,13 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) CWalletTx wtx(&wallet, MakeTransactionRef(tx4)); uint256 factor = wtx.GetBlindingFactor(0); uint256 asset_factor = wtx.GetAssetBlindingFactor(0); - uint256 asset_id = wtx.GetAssetID(0); + CAssetID asset_id = wtx.GetAssetID(0); CPubKey pubkey = wtx.GetBlindingPubKey(0); CAmount amount = wtx.GetValueOut(0); BOOST_CHECK(factor == uint256()); BOOST_CHECK(asset_factor == uint256()); - BOOST_CHECK(asset_id == uint256()); + BOOST_CHECK(asset_id == CAssetID()); BOOST_CHECK(pubkey == CPubKey()); BOOST_CHECK(amount == -1); @@ -279,7 +279,7 @@ BOOST_AUTO_TEST_CASE(naive_blinding_test) BOOST_CHECK(amount == 11); #endif CAmount unblinded_amount; - uint256 asset_id_out; + CAssetID asset_id_out; uint256 asset_blinder_out; BOOST_CHECK(UnblindOutput(key1, tx4.vout[0], unblinded_amount, blind4, asset_id_out, asset_blinder_out) == 0); BOOST_CHECK(UnblindOutput(key2, tx4.vout[0], unblinded_amount, blind4, asset_id_out, asset_blinder_out) == 1); diff --git a/src/validation.cpp b/src/validation.cpp index d198480ddf..5ec6b292b2 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -684,7 +684,7 @@ bool VerifyAmounts(const CCoinsViewCache& cache, const CTransaction& tx, std::ve return false; if (asset.IsAssetID()) { - uint256 fixedAsset; + CAssetID fixedAsset; asset.GetAssetID(fixedAsset); ret = secp256k1_generator_generate(secp256k1_ctx_verify_amounts, &gen, fixedAsset.begin()); assert(ret != 0); @@ -729,7 +729,7 @@ bool VerifyAmounts(const CCoinsViewCache& cache, const CTransaction& tx, std::ve return false; if (asset.IsAssetID()) { - uint256 fixedAsset; + CAssetID fixedAsset; asset.GetAssetID(fixedAsset); ret = secp256k1_generator_generate(secp256k1_ctx_verify_amounts, &gen, fixedAsset.begin()); assert(ret != 0); @@ -788,7 +788,7 @@ bool VerifyAmounts(const CCoinsViewCache& cache, const CTransaction& tx, std::ve { const CTxOutAsset& asset = cache.GetOutputFor(tx.vin[i]).nAsset; if (asset.IsAssetID()) { - uint256 fixedAsset; + CAssetID fixedAsset; asset.GetAssetID(fixedAsset); ret = secp256k1_generator_generate(secp256k1_ctx_verify_amounts, &ephemeral_input_tags[i], fixedAsset.begin()); assert(ret != 0); @@ -834,7 +834,7 @@ bool VerifyCoinbaseAmount(const CTransaction& tx, const CAmountMap& mapFees) for (unsigned int i = 0; i < tx.vout.size(); i++) { if (!tx.vout[i].nValue.IsAmount() || !tx.vout[i].nAsset.IsAssetID()) return false; - uint256 assetid; + CAssetID assetid; tx.vout[i].nAsset.GetAssetID(assetid); remaining[assetid] -= tx.vout[i].nValue.GetAmount(); } @@ -1902,7 +1902,7 @@ void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint entropy = ComputeFastMerkleRoot(leaves); } -void CalculateAssetID(uint256& assetID, const uint256& entropy) +void CalculateAssetID(CAssetID& assetID, const uint256& entropy) { static const uint256 kZero = uint256S("0x0000000000000000000000000000000000000000000000000000000000000000"); // H_a : asset tag @@ -1912,10 +1912,10 @@ void CalculateAssetID(uint256& assetID, const uint256& entropy) leaves.reserve(2); leaves.push_back(entropy); leaves.push_back(kZero); - assetID = ComputeFastMerkleRoot(leaves); + assetID = CAssetID(ComputeFastMerkleRoot(leaves)); } -void CalculateReissuanceToken(uint256& reissuanceTokenID, const uint256& entropy, bool fConfidential) +void CalculateReissuanceToken(CAssetID& reissuanceTokenID, const uint256& entropy, bool fConfidential) { static const uint256 kOne = uint256S("0x0000000000000000000000000000000000000000000000000000000000000001"); static const uint256 kTwo = uint256S("0x0000000000000000000000000000000000000000000000000000000000000002"); @@ -1929,7 +1929,7 @@ void CalculateReissuanceToken(uint256& reissuanceTokenID, const uint256& entropy leaves.reserve(2); leaves.push_back(entropy); leaves.push_back(fConfidential? kTwo: kOne); - reissuanceTokenID = ComputeFastMerkleRoot(leaves); + reissuanceTokenID = CAssetID(ComputeFastMerkleRoot(leaves)); } bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::set >& setWithdrawsSpent, std::vector *pvChecks) diff --git a/src/validation.h b/src/validation.h index c779410bf5..5b05147a28 100644 --- a/src/validation.h +++ b/src/validation.h @@ -415,7 +415,7 @@ void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint * commitment setup to derive the unblinded asset tag. * @param[in] entropy The asset entropy returned by GenerateAssetEntropy. */ -void CalculateAssetID(uint256& assetID, const uint256& entropy); +void CalculateAssetID(CAssetID& assetID, const uint256& entropy); /** * Derive the asset reissuance token assetID from the entropy and reissuance @@ -429,7 +429,7 @@ void CalculateAssetID(uint256& assetID, const uint256& entropy); * @param[in] fConfidential Set to true if the initial issuance was blinded, * false otherwise. */ -void CalculateReissuanceToken(uint256& reissuanceTokenID, const uint256& entropy, bool fConfidential); +void CalculateReissuanceToken(CAssetID& reissuanceTokenID, const uint256& entropy, bool fConfidential); /** * Verify the transaction's outputs spend exactly what its inputs provide, plus some excess amount. diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index f90238e9f9..80f09f8461 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -71,19 +71,19 @@ void EnsureWalletIsUnlocked() UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string& strasset) { UniValue obj(UniValue::VOBJ); - uint256 id = wallet->GetAssetIDFromLabel(strasset); - std::string label = wallet->GetAssetLabelFromID(uint256S(strasset)); - if (strasset != "*" && (id == uint256() && label == "")) { + CAssetID id = wallet->GetAssetIDFromLabel(strasset); + std::string label = wallet->GetAssetLabelFromID(CAssetID(uint256S(strasset))); + if (strasset != "*" && (id == CAssetID() && label == "")) { throw JSONRPCError(RPC_WALLET_ERROR, "Input does not match a known asset tag/label pair."); } - else if (id != uint256()) { + else if (id != CAssetID()) { strasset = id.GetHex(); } if (strasset == "*") { for(std::map::const_iterator it = balance.begin(); it != balance.end(); ++it) { // Unknown assets - if (it->first == uint256()) + if (it->first == CAssetID()) continue; UniValue pair(UniValue::VOBJ); if (wallet->mapAssetLabels.count(it->first)) { @@ -94,7 +94,7 @@ UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string& str } } else { - return ValueFromAmount(balance[uint256S(strasset)]); + return ValueFromAmount(balance[CAssetID(uint256S(strasset))]); } return obj; } @@ -498,9 +498,9 @@ UniValue sendtoaddress(const JSONRPCRequest& request) } CAssetID id(uint256S(asset)); - if (pwalletMain->GetAssetLabelFromID(uint256S(asset)) == "") + if (pwalletMain->GetAssetLabelFromID(CAssetID(uint256S(asset))) == "") id = pwalletMain->GetAssetIDFromLabel(asset); - if (id == uint256()) + if (id == CAssetID()) throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); EnsureWalletIsUnlocked(); @@ -1014,9 +1014,9 @@ UniValue sendmany(const JSONRPCRequest& request) } CAssetID asset(uint256S(strasset)); - if (pwalletMain->GetAssetLabelFromID(uint256S(strasset)) == "") + if (pwalletMain->GetAssetLabelFromID(CAssetID(uint256S(strasset))) == "") asset = pwalletMain->GetAssetIDFromLabel(strasset); - if (asset == uint256()) + if (asset == CAssetID()) throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); if (!address.IsValid()) @@ -1277,9 +1277,9 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts) asset = params[3].get_str(); } CAssetID id(uint256S(asset)); - if (asset != "*" && pwalletMain->GetAssetLabelFromID(uint256S(asset)) == "") + if (asset != "*" && pwalletMain->GetAssetLabelFromID(CAssetID(uint256S(asset))) == "") id = pwalletMain->GetAssetIDFromLabel(asset); - if (asset != "*" && id == uint256()) + if (asset != "*" && id == CAssetID()) throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); // Tally @@ -1510,7 +1510,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("category", "send")); entry.push_back(Pair("amount", ValueFromAmount(-s.amount))); entry.push_back(Pair("amountblinder", s.amountBlindingFactor.GetHex())); - entry.push_back(Pair("assetid", s.assetID.GetHex())); + entry.push_back(Pair("assetid", s.asset.GetHex())); entry.push_back(Pair("assetblinder", s.assetBlindingFactor.GetHex())); if (pwalletMain->mapAddressBook.count(s.destination)) entry.push_back(Pair("label", pwalletMain->mapAddressBook[s.destination].name)); @@ -1553,7 +1553,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe } entry.push_back(Pair("amount", ValueFromAmount(r.amount))); entry.push_back(Pair("amountblinder", r.amountBlindingFactor.GetHex())); - entry.push_back(Pair("assetid", r.assetID.GetHex())); + entry.push_back(Pair("assetid", r.asset.GetHex())); entry.push_back(Pair("assetblinder", r.assetBlindingFactor.GetHex())); if (pwalletMain->mapAddressBook.count(r.destination)) entry.push_back(Pair("label", account)); @@ -2651,7 +2651,7 @@ UniValue listunspent(const JSONRPCRequest& request) CAssetID id; if (asset != "*") { id = pwalletMain->GetAssetIDFromString(asset); - if (id == uint256()) + if (id == CAssetID()) throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); } @@ -2673,7 +2673,7 @@ UniValue listunspent(const JSONRPCRequest& request) CAmount nValue = out.tx->GetValueOut(out.i); CAssetID assetid = out.tx->GetAssetID(out.i); - if (nValue == -1 || assetid == uint256()) + if (nValue == -1 || assetid == CAssetID()) continue; if (asset != "*" && id != assetid) { @@ -3635,7 +3635,7 @@ UniValue addassetlabel(const JSONRPCRequest& request) throw JSONRPCError(RPC_TYPE_ERROR, "Please pick a label between 3 and 32 characters."); } - pwalletMain->SetAssetPair(label, uint256S(id)); + pwalletMain->SetAssetPair(label, CAssetID(uint256S(id))); return NullUniValue; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ddcda583a6..367aa04135 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1398,7 +1398,7 @@ bool CWallet::IsHDEnabled() return !hdChain.masterKeyID.IsNull(); } -bool CWallet::SetAssetPair(const std::string& label, const uint256& id) +bool CWallet::SetAssetPair(const std::string& label, const CAssetID& id) { LOCK(cs_wallet); if (!CWalletDB(strWalletFile).WriteAssetIDLabelPair(id, label) || @@ -1480,7 +1480,7 @@ void CWalletTx::GetAmounts(list& listReceived, { const CTxOut& txout = tx->vout[i]; CAmount nValueOut = GetValueOut(i); - uint256 assetID = GetAssetID(i); + CAssetID assetID = GetAssetID(i); if (nValueOut >= 0) { nValueUnaccounted[assetID] -= nValueOut; @@ -1747,7 +1747,7 @@ CAmountMap CWalletTx::GetCredit(unsigned int nTxOut, const isminefilter& filter) if (pwallet->IsMine(tx->vout[nTxOut]) & filter) amount[GetAssetID(nTxOut)] = GetValueOut(nTxOut); // Can be -1 if someone sent us a transaction using a wrong scanning key: - if (amount[uint256()] == -1) + if (amount[CAssetID()] == -1) return CAmountMap(); if (!MoneyRange(amount)) throw std::runtime_error("CWallet::GetCredit(): value out of range"); @@ -1940,7 +1940,7 @@ bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const return CTransaction(tx1) == CTransaction(tx2); } -void CWalletTx::SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pubkeyIn, uint256 blindingfactorIn, uint256 assetIDIn, uint256 assetBlindingFactorIn) const +void CWalletTx::SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pubkeyIn, uint256 blindingfactorIn, const CAssetID& assetIn, uint256 assetBlindingFactorIn) const { assert(nOut < tx->vout.size()); if (mapValue["blindingdata"].size() < (nOut + 1) * 138) { @@ -1953,7 +1953,7 @@ void CWalletTx::SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pub memcpy(&*(it + 1), &amountIn, 8); memcpy(&*(it + 9), blindingfactorIn.begin(), 32); memcpy(&*(it + 41), assetBlindingFactorIn.begin(), 32); - memcpy(&*(it + 73), assetIDIn.begin(), 32); + memcpy(&*(it + 73), assetIn.begin(), 32); if (pubkeyIn.IsValid() && pubkeyIn.size() == 33) { memcpy(&*(it + 105), pubkeyIn.begin(), 33); } else { @@ -1962,7 +1962,7 @@ void CWalletTx::SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pub } -void CWalletTx::GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* ppubkeyOut, uint256* pblindingfactorOut, uint256* pAssetIDOut, uint256* passetBlindingFactorOut) const +void CWalletTx::GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* ppubkeyOut, uint256* pblindingfactorOut, CAssetID* pAssetOut, uint256* passetBlindingFactorOut) const { // Blinding data is cached in a serialized record mapWallet["blindingdata"]. // It contains a concatenation byte vectors, 74 bytes per txout. @@ -1985,22 +1985,22 @@ void CWalletTx::GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* CAmount amount = -1; CPubKey pubkey; uint256 blindingfactor; - uint256 assetID; + CAssetID asset; uint256 assetBlindingFactor; if (*it == 1) { memcpy(&amount, &*(it + 1), 8); memcpy(blindingfactor.begin(), &*(it + 9), 32); memcpy(assetBlindingFactor.begin(), &*(it + 41), 32); - memcpy(assetID.begin(), &*(it + 73), 32); + memcpy(asset.begin(), &*(it + 73), 32); pubkey.Set(it + 105, it + 138); } else { - pwallet->ComputeBlindingData(tx->vout[nOut], amount, pubkey, blindingfactor, assetID, assetBlindingFactor); + pwallet->ComputeBlindingData(tx->vout[nOut], amount, pubkey, blindingfactor, asset, assetBlindingFactor); *it = 1; memcpy(&*(it + 1), &amount, 8); memcpy(&*(it + 9), blindingfactor.begin(), 32); memcpy(&*(it + 41), assetBlindingFactor.begin(), 32); - memcpy(&*(it + 73), assetID.begin(), 32); + memcpy(&*(it + 73), asset.begin(), 32); if (pubkey.IsValid() && pubkey.size() == 33) { memcpy(&*(it + 105), pubkey.begin(), 33); } else { @@ -2012,7 +2012,7 @@ void CWalletTx::GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* if (ppubkeyOut) *ppubkeyOut = pubkey; if (pblindingfactorOut) *pblindingfactorOut = blindingfactor; if (passetBlindingFactorOut) *passetBlindingFactorOut = assetBlindingFactor; - if (pAssetIDOut) *pAssetIDOut = assetID; + if (pAssetOut) *pAssetOut = asset; } CAmount CWalletTx::GetValueOut(unsigned int nOut) const { @@ -2033,8 +2033,8 @@ uint256 CWalletTx::GetAssetBlindingFactor(unsigned int nOut) const { return ret; } -uint256 CWalletTx::GetAssetID(unsigned int nOut) const { - uint256 ret; +CAssetID CWalletTx::GetAssetID(unsigned int nOut) const { + CAssetID ret; GetBlindingData(nOut, NULL, NULL, NULL, &ret, NULL); return ret; } @@ -2549,7 +2549,7 @@ bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool ov strFailReason = _("Pre-funded amounts must be non-blinded"); return false; } - uint256 assetID; + CAssetID assetID; txOut.nAsset.GetAssetID(assetID); CRecipient recipient = {txOut.scriptPubKey, txOut.nValue.GetAmount(), assetID, CPubKey(txOut.nValue.vchNonceCommitment), false}; vecSend.push_back(recipient); @@ -2620,7 +2620,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt unsigned int nSubtractFeeFromAmount = 0; for (const auto& recipient : vecSend) { - if (mapValue[recipient.asset] < 0 || recipient.nAmount < 0 || recipient.asset == uint256()) + if (mapValue[recipient.asset] < 0 || recipient.nAmount < 0 || recipient.asset == CAssetID()) { strFailReason = _("Transaction amounts must not be negative"); return false; @@ -2876,17 +2876,17 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt // Create blinded outputs std::vector input_blinds; std::vector input_asset_blinds; - std::vector input_asset_ids; + std::vector input_asset_ids; std::vector output_blinds; std::vector input_amounts; std::vector output_asset_blinds; - std::vector output_asset_ids; + std::vector output_asset_ids; BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins) { uint256 blind = coin.first->GetBlindingFactor(coin.second); input_blinds.push_back(blind); uint256 asset_blind = coin.first->GetAssetBlindingFactor(coin.second); input_asset_blinds.push_back(asset_blind); - uint256 asset_id = coin.first->GetAssetID(coin.second); + CAssetID asset_id = coin.first->GetAssetID(coin.second); input_asset_ids.push_back(asset_id); CAmount amount = coin.first->GetValueOut(coin.second); input_amounts.push_back(amount); @@ -2899,7 +2899,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt if (outAmounts) outAmounts->push_back(txNew.vout[nOut].nValue.GetAmount()); vAmounts.push_back(txNew.vout[nOut].nValue.GetAmount()); - uint256 asset; + CAssetID asset; txNew.vout[nOut].nAsset.GetAssetID(asset); output_asset_ids.push_back(asset); } @@ -2948,7 +2948,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt for (unsigned int i = 0; i< vAmounts.size(); i++) { assert((output_pubkeys[i] == CPubKey())==(output_blinds[i] == uint256())); assert((output_pubkeys[i] == CPubKey())==(output_asset_blinds[i] == uint256())); - assert(output_asset_ids[i] != uint256()); + assert(output_asset_ids[i] != CAssetID()); wtxNew.SetBlindingData(i, vAmounts[i], output_pubkeys[i], output_blinds[i], output_asset_ids[i], output_asset_blinds[i]); } @@ -4317,22 +4317,22 @@ bool CMerkleTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& return ::AcceptToMemoryPool(mempool, state, tx, true, NULL, NULL, false, nAbsurdFee); } -std::string CWallet::GetAssetLabelFromID(const uint256& id) const +std::string CWallet::GetAssetLabelFromID(const CAssetID& id) const { LOCK(cs_wallet); - std::map::const_iterator it = mapAssetLabels.find(id); + std::map::const_iterator it = mapAssetLabels.find(id); if (it != mapAssetLabels.end()) return it->second; return ""; } -uint256 CWallet::GetAssetIDFromLabel(const std::string& label) const +CAssetID CWallet::GetAssetIDFromLabel(const std::string& label) const { LOCK(cs_wallet); - std::map::const_iterator it = mapAssetIDs.find(label); + std::map::const_iterator it = mapAssetIDs.find(label); if (it != mapAssetIDs.end()) return it->second; - return uint256(); + return CAssetID(); } CAssetID CWallet::GetAssetIDFromString(const std::string& asset) const @@ -4396,21 +4396,21 @@ bool CWallet::AddSpecificBlindingKey(const CScriptID& scriptid, const uint256& k return CWalletDB(strWalletFile).WriteSpecificBlindingKey(scriptid, key); } -bool CWallet::LoadAssetLabelIDMapping(const std::string& label, const uint256& id) +bool CWallet::LoadAssetLabelIDMapping(const std::string& label, const CAssetID& id) { AssertLockHeld(cs_wallet); mapAssetIDs[label] = id; return true; } -bool CWallet::LoadAssetIDLabelMapping(const uint256& id, const std::string& label) +bool CWallet::LoadAssetIDLabelMapping(const CAssetID& id, const std::string& label) { AssertLockHeld(cs_wallet); mapAssetLabels[id] = label; return true; } -void CWallet::ComputeBlindingData(const CTxOut& output, CAmount& amount, CPubKey& pubkey, uint256& blindingfactor, uint256& assetID, uint256& assetBlindingFactor) const +void CWallet::ComputeBlindingData(const CTxOut& output, CAmount& amount, CPubKey& pubkey, uint256& blindingfactor, CAssetID& assetID, uint256& assetBlindingFactor) const { if (output.nValue.IsAmount() && output.nAsset.IsAssetID()) { amount = output.nValue.GetAmount(); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 621be8a470..b327f9a4ac 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -171,7 +171,7 @@ struct COutputEntry { CTxDestination destination; CAmount amount; - uint256 assetID; + CAssetID asset; int vout; CPubKey confidentiality_pubkey; uint256 amountBlindingFactor; @@ -447,10 +447,10 @@ public: std::set GetConflicts() const; // For use in wallet transaction creation to remember 3rd party values - void SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pubkeyIn, uint256 blindingfactorIn, uint256 assetIDIn, uint256 assetBlindingFactorIn) const; + void SetBlindingData(unsigned int nOut, CAmount amountIn, CPubKey pubkeyIn, uint256 blindingfactorIn, const CAssetID& assetIn, uint256 assetBlindingFactorIn) const; private: - void GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* ppubkeyOut, uint256* pblindingfactorOut, uint256* pAssetIDOut, uint256* passetBlindingFactorOut) const; + void GetBlindingData(unsigned int nOut, CAmount* pamountOut, CPubKey* ppubkeyOut, uint256* pblindingfactorOut, CAssetID* pAssetOut, uint256* passetBlindingFactorOut) const; void WipeUnknownBlindingData() const; public: @@ -460,7 +460,7 @@ public: //! Returns either the blinding factor (if it is to us) or 0 uint256 GetBlindingFactor(unsigned int nOut) const; uint256 GetAssetBlindingFactor(unsigned int nOut) const; - uint256 GetAssetID(unsigned int nOut) const; + CAssetID GetAssetID(unsigned int nOut) const; CPubKey GetBlindingPubKey(unsigned int nOut) const; }; @@ -920,9 +920,9 @@ public: bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const; void SetBestChain(const CBlockLocator& loc) override; - bool SetAssetPair(const std::string& label, const uint256& id); - bool LoadAssetLabelIDMapping(const std::string& label, const uint256& id); - bool LoadAssetIDLabelMapping(const uint256&, const std::string&); + bool SetAssetPair(const std::string& label, const CAssetID& id); + bool LoadAssetLabelIDMapping(const std::string& label, const CAssetID& id); + bool LoadAssetIDLabelMapping(const CAssetID&, const std::string&); DBErrors LoadWallet(bool& fFirstRunRet); DBErrors ZapWalletTx(std::vector& vWtx); @@ -1011,9 +1011,9 @@ public: bool AbandonTransaction(const uint256& hashTx); /* Returns the label of associated asset id */ - std::string GetAssetLabelFromID(const uint256& id) const; + std::string GetAssetLabelFromID(const CAssetID& id) const; /* Returns asset id corresponding to asset label */ - uint256 GetAssetIDFromLabel(const std::string& label) const; + CAssetID GetAssetIDFromLabel(const std::string& label) const; /** * Returns asset id corresponding to the given asset expression, which is either an asset label or a hex value. * @param asset A label string or a hex value corresponding to an asset @@ -1025,7 +1025,7 @@ public: CKey GetBlindingKey(const CScript* script) const; CPubKey GetBlindingPubKey(const CScript& script) const; - void ComputeBlindingData(const CTxOut& output, CAmount& amount, CPubKey& pubkey, uint256& blindingfactor, uint256& assetID, uint256& assetBlindingFactor) const; + void ComputeBlindingData(const CTxOut& output, CAmount& amount, CPubKey& pubkey, uint256& blindingfactor, CAssetID& asset, uint256& assetBlindingFactor) const; /** Mark a transaction as replaced by another transaction (e.g., BIP 125). */ bool MarkReplaced(const uint256& originalHash, const uint256& newHash); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index f97716c354..5f8f92f556 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -214,12 +214,12 @@ bool CWalletDB::WriteBlindingDerivationKey(const uint256& key) return Write(std::string("blindingderivationkey"), key); } -bool CWalletDB::WriteAssetIDLabelPair(const uint256& id, const std::string& label) +bool CWalletDB::WriteAssetIDLabelPair(const CAssetID& id, const std::string& label) { return Write(make_pair(std::string("idlabelmapping"), id), label); } -bool CWalletDB::WriteAssetLabelIDPair(const std::string& label, const uint256& id) +bool CWalletDB::WriteAssetLabelIDPair(const std::string& label, const CAssetID& id) { return Write(make_pair(std::string("labelidmapping"), label), id); } @@ -581,7 +581,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { string label; ssKey >> label; - uint256 id; + CAssetID id; ssValue >> id; if (!pwallet->LoadAssetLabelIDMapping(label, id)) { strErr = "Error reading wallet database: LoadAssetLabelIDMapping failed"; @@ -590,7 +590,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, } else if (strType == "idlabelmapping") { - uint256 id; + CAssetID id; ssKey >> id; string label; ssValue >> label; diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index a2d76dbc50..39a3238d6a 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -170,8 +170,8 @@ public: bool WriteSpecificBlindingKey(const CScriptID& scriptid, const uint256& key); bool WriteBlindingDerivationKey(const uint256& key); - bool WriteAssetIDLabelPair(const uint256& id, const std::string& label); - bool WriteAssetLabelIDPair(const std::string& label, const uint256& id); + bool WriteAssetIDLabelPair(const CAssetID& id, const std::string& label); + bool WriteAssetLabelIDPair(const std::string& label, const CAssetID& id); DBErrors LoadWallet(CWallet* pwallet); DBErrors FindWalletTx(CWallet* pwallet, std::vector& vTxHash, std::vector& vWtx);