From b985f312b471dab9365c626a46ef6df276861111 Mon Sep 17 00:00:00 2001 From: yoshihide shimada Date: Thu, 8 Nov 2018 10:56:31 +0900 Subject: [PATCH] Add Asset parameter to MutateTxAddOutData --- src/bitcoin-tx.cpp | 52 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index cc8cee7d65..6a8ed317df 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -395,27 +395,47 @@ static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& s static void MutateTxAddOutData(CMutableTransaction& tx, const std::string& strInput) { CAmount value = 0; + + // separate [VALUE:]DATA[:ASSET] in string + std::vector vStrInputParts; + boost::split(vStrInputParts, strInput, boost::is_any_of(":")); - // separate [VALUE:]DATA in string - size_t pos = strInput.find(':'); - - if (pos==0) + // Check that there are enough parameters + if (vStrInputParts[0].empty()) throw std::runtime_error("TX output value not specified"); - if (pos != std::string::npos) { - // Extract and validate VALUE - value = ExtractAndValidateValue(strInput.substr(0, pos)); - } - - // extract and validate DATA - std::string strData = strInput.substr(pos + 1, std::string::npos); - - if (!IsHex(strData)) - throw std::runtime_error("invalid TX output data"); + if (vStrInputParts.size()>3) + throw std::runtime_error("too many separators"); + + std::vector data; + CAsset asset(Params().GetConsensus().pegged_asset); + + if (vStrInputParts.size()==1) { + std::string strData = vStrInputParts[0]; + if (!IsHex(strData)) + throw std::runtime_error("invalid TX output data"); + data = ParseHex(strData); - std::vector data = ParseHex(strData); + } else { + value = ExtractAndValidateValue(vStrInputParts[0]); + std::string strData = vStrInputParts[1]; + if (!IsHex(strData)) + throw std::runtime_error("invalid TX output data"); + data = ParseHex(strData); + + if (vStrInputParts.size()==3) { + std::string strAsset = vStrInputParts[2]; + if (!IsHex(strAsset)) + throw std::runtime_error("invalid TX output asset type"); + + asset.SetHex(strAsset); + if (asset.IsNull()) { + throw std::runtime_error("invalid TX output asset type"); + } + } + } - CTxOut txout(Params().GetConsensus().pegged_asset, value, CScript() << OP_RETURN << data); + CTxOut txout(asset, value, CScript() << OP_RETURN << data); tx.vout.push_back(txout); }