Skip to content

Commit

Permalink
Merge pull request ElementsProject#44 from kallewoof/elements-tx-asse…
Browse files Browse the repository at this point in the history
…t-args

elements-tx asset support in outaddr and outscript
  • Loading branch information
instagibbs committed Feb 27, 2017
2 parents e5861bb + 2310fcc commit 0a5ce0f
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,30 +226,35 @@ static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)

static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
{
// separate VALUE:ADDRESS in string
size_t pos = strInput.find(':');
if ((pos == string::npos) ||
(pos == 0) ||
(pos == (strInput.size() - 1)))
// separate VALUE:ADDRESS:ASSET in string
std::vector<std::string> vStrOutAddrParts;
boost::split(vStrOutAddrParts, strInput, boost::is_any_of(":"));
if (vStrOutAddrParts.size()<3)
throw runtime_error("TX output missing separator");

// extract and validate VALUE
string strValue = strInput.substr(0, pos);
string strValue = vStrOutAddrParts[0];
CAmount value;
if (!ParseMoney(strValue, value))
throw runtime_error("invalid TX output value");

// extract and validate ADDRESS
string strAddr = strInput.substr(pos + 1, string::npos);
string strAddr = vStrOutAddrParts[1];
CBitcoinAddress addr(strAddr);
if (!addr.IsValid())
throw runtime_error("invalid TX output address");

// extract and validate ASSET
string strAsset = vStrOutAddrParts[2];
CAssetID asset = uint256S(strAsset);
if (asset == CAssetID())
throw runtime_error("invalid TX output asset type");

// build standard output script via GetScriptForDestination()
CScript scriptPubKey = GetScriptForDestination(addr.Get());

// construct TxOut, append to transaction output list
CTxOut txout(BITCOINID, value, scriptPubKey);
CTxOut txout(asset, value, scriptPubKey);
if (addr.IsBlinded()) {
CPubKey pubkey = addr.GetBlindingKey();
txout.nValue.vchNonceCommitment = std::vector<unsigned char>(pubkey.begin(), pubkey.end());
Expand Down Expand Up @@ -353,24 +358,28 @@ static void MutateTxBlind(CMutableTransaction& tx, const string& strInput)

static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
{
// separate VALUE:SCRIPT in string
size_t pos = strInput.find(':');
if ((pos == string::npos) ||
(pos == 0))
throw runtime_error("TX output missing separator");
// separate VALUE:SCRIPT:ASSET in string
std::vector<std::string> vStrOutScriptParts;
boost::split(vStrOutScriptParts, strInput, boost::is_any_of(":"));
if (vStrOutScriptParts.size()<3)
throw runtime_error("TX out script missing separator");

// extract and validate VALUE
string strValue = strInput.substr(0, pos);
string strValue = vStrOutScriptParts[0];
CAmount value;
if (!ParseMoney(strValue, value))
throw runtime_error("invalid TX output value");

// extract and validate script
string strScript = strInput.substr(pos + 1, string::npos);
string strScript = vStrOutScriptParts[1];
CScript scriptPubKey = ParseScript(strScript); // throws on err

// extract and validate asset
string strAsset = vStrOutScriptParts[2];
CAssetID asset = uint256S(strAsset);

// construct TxOut, append to transaction output list
CTxOut txout(BITCOINID, value, scriptPubKey);
CTxOut txout(asset, value, scriptPubKey);
tx.vout.push_back(txout);
}

Expand Down

0 comments on commit 0a5ce0f

Please sign in to comment.