From e6b3676b3758a60b1d5428ef9c689c9ee45ecc5f Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Mon, 8 Apr 2019 15:27:03 -0400 Subject: [PATCH] Wallet: GetIssuanceAssets to just get CAssets for issuances --- src/wallet/wallet.cpp | 75 +++++++++++++++++++++++-------------------- src/wallet/wallet.h | 2 ++ 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 697c726a9e..5e85c671cc 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -5196,32 +5196,52 @@ CPubKey CWalletTx::GetOutputBlindingPubKey(unsigned int output_index) const { return ret; } +void CWalletTx::GetIssuanceAssets(unsigned int vinIndex, CAsset* out_asset, CAsset* out_reissuance_token) const { + assert(vinIndex < tx->vin.size()); + const CAssetIssuance& issuance = tx->vin[vinIndex].assetIssuance; + + if (out_asset && issuance.nAmount.IsNull()) { + out_asset->SetNull(); + out_asset = nullptr; + } + if (out_reissuance_token && issuance.nInflationKeys.IsNull()) { + out_reissuance_token->SetNull(); + out_reissuance_token = nullptr; + } + if (!(out_asset || out_reissuance_token)) return; + + if (issuance.assetBlindingNonce.IsNull()) { + uint256 entropy; + GenerateAssetEntropy(entropy, tx->vin[vinIndex].prevout, issuance.assetEntropy); + if (out_reissuance_token) { + CalculateReissuanceToken(*out_reissuance_token, entropy, issuance.nAmount.IsCommitment()); + } + if (out_asset) { + CalculateAsset(*out_asset, entropy); + } + } + else { + if (out_reissuance_token) { + // Re-issuances don't emit issuance tokens + out_reissuance_token->SetNull(); + } + if (out_asset) { + CalculateAsset(*out_asset, issuance.assetEntropy); + } + } +} + uint256 CWalletTx::GetIssuanceBlindingFactor(unsigned int input_index, bool reissuance_token) const { assert(input_index < tx->vin.size()); CAsset asset; const CAssetIssuance& issuance = tx->vin[input_index].assetIssuance; const CTxWitness& wit = tx->witness; - if ((issuance.nAmount.IsNull() && !reissuance_token) || (issuance.nInflationKeys.IsNull() && reissuance_token)) { + GetIssuanceAssets(input_index, reissuance_token ? nullptr : &asset, reissuance_token ? &asset : nullptr); + if (asset.IsNull()) { return uint256(); } const std::vector& rangeproof = wit.vtxinwit.size() <= input_index ? std::vector() : (reissuance_token ? wit.vtxinwit[input_index].vchInflationKeysRangeproof : wit.vtxinwit[input_index].vchIssuanceAmountRangeproof); unsigned int mapValueInd = GetPseudoInputOffset(input_index, reissuance_token)+tx->vout.size(); - if (issuance.assetBlindingNonce.IsNull()) { - uint256 entropy; - GenerateAssetEntropy(entropy, tx->vin[input_index].prevout, issuance.assetEntropy); - if (reissuance_token) { - CalculateReissuanceToken(asset, entropy, issuance.nInflationKeys.IsCommitment()); - } else { - CalculateAsset(asset, entropy); - } - } - else { - if (reissuance_token) { - // Re-issuances don't emit issuance tokens - return uint256(); - } - CalculateAsset(asset, issuance.assetEntropy); - } uint256 ret; CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.begin(), tx->vin[input_index].prevout.hash.end()) << tx->vin[input_index].prevout.n); @@ -5232,31 +5252,18 @@ uint256 CWalletTx::GetIssuanceBlindingFactor(unsigned int input_index, bool reis CAmount CWalletTx::GetIssuanceAmount(unsigned int input_index, bool reissuance_token) const { assert(input_index < tx->vin.size()); CAsset asset; - CAsset token; const CAssetIssuance& issuance = tx->vin[input_index].assetIssuance; const CTxWitness& wit = tx->witness; - if ((issuance.nAmount.IsNull() && !reissuance_token) || (issuance.nInflationKeys.IsNull() && reissuance_token)) { - return 0; + GetIssuanceAssets(input_index, reissuance_token ? nullptr : &asset, reissuance_token ? &asset : nullptr); + if (asset.IsNull()) { + return -1; } unsigned int mapValueInd = GetPseudoInputOffset(input_index, reissuance_token)+tx->vout.size(); const std::vector& rangeproof = wit.vtxinwit.size() <= input_index ? std::vector() : (reissuance_token ? wit.vtxinwit[input_index].vchInflationKeysRangeproof : wit.vtxinwit[input_index].vchIssuanceAmountRangeproof); - if (issuance.assetBlindingNonce.IsNull()) { - uint256 entropy; - GenerateAssetEntropy(entropy, tx->vin[input_index].prevout, issuance.assetEntropy); - CalculateReissuanceToken(token, entropy, issuance.nAmount.IsCommitment()); - CalculateAsset(asset, entropy); - } - else { - if (reissuance_token) { - // Re-issuances don't emit issuance tokens - return -1; - } - CalculateAsset(asset, issuance.assetEntropy); - } CAmount ret; CScript blindingScript(CScript() << OP_RETURN << std::vector(tx->vin[input_index].prevout.hash.begin(), tx->vin[input_index].prevout.hash.end()) << tx->vin[input_index].prevout.n); - GetBlindingData(mapValueInd, rangeproof, reissuance_token ? issuance.nInflationKeys : issuance.nAmount, CConfidentialAsset((reissuance_token ? token : asset)), CConfidentialNonce(), blindingScript, nullptr, &ret, nullptr, nullptr, nullptr); + GetBlindingData(mapValueInd, rangeproof, reissuance_token ? issuance.nInflationKeys : issuance.nAmount, CConfidentialAsset(asset), CConfidentialNonce(), blindingScript, nullptr, &ret, nullptr, nullptr, nullptr); return ret; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 71524f3dde..86a726dfb4 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -533,6 +533,8 @@ public: uint256 GetOutputAssetBlindingFactor(unsigned int output_index) const; //! Returns the underlying asset type, or 0 if unknown CAsset GetOutputAsset(unsigned int output_index) const; + //! Get the issuance CAssets for both the asset itself and the issuing tokens + void GetIssuanceAssets(unsigned int vinIndex, CAsset* out_asset, CAsset* out_reissuance_token) const; // ! Returns receiver's blinding pubkey CPubKey GetOutputBlindingPubKey(unsigned int output_index) const; //! Get the issuance blinder for either the asset itself or the issuing tokens