Browse Source

heir funcs changed to one set

metaverse
dimxy 6 years ago
parent
commit
d63d43c8ef
  1. 17
      src/cc/CCHeir.h
  2. 403
      src/cc/heir.cpp
  3. 2
      src/cc/heir_validate.h
  4. 4
      src/rpc/server.cpp
  5. 3
      src/rpc/server.h
  6. 299
      src/wallet/rpcwallet.cpp

17
src/cc/CCHeir.h

@ -27,19 +27,10 @@ bool HeirValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,
class CoinHelper;
class TokenHelper;
// CCcustom
// this would not link
//template <class Helper> std::string HeirFund(uint64_t txfee,int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 assetid);
//template <class Helper> UniValue HeirAdd(uint256 fundingtxid, uint64_t txfee, int64_t amount);
//template <class Helper> UniValue HeirClaim(uint256 fundingtxid, uint64_t txfee, int64_t nValue);
UniValue HeirFundCoinCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 assetid);
UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 assetid);
UniValue HeirClaimCoinCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirClaimTokenCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirAddCoinCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirAddTokenCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirFundCoinCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid);
UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid);
UniValue HeirClaimCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount);
UniValue HeirInfo(uint256 fundingtxid);
UniValue HeirList();

403
src/cc/heir.cpp

@ -14,8 +14,6 @@
******************************************************************************/
#include "CCHeir.h"
#include "CCtokens.h"
#include "heir_validate.h"
class CoinHelper;
@ -122,27 +120,27 @@ bool HeirValidate(struct CCcontract_info* cpHeir, Eval* eval, const CTransaction
//if (chainActive.Height() < 741)
// return true;
uint256 fundingTxidInOpret = zeroid, latestTxid = zeroid, dummyTokenid, tokenid = zeroid;
uint256 fundingTxidInOpret = zeroid, latestTxid = zeroid, dummyTokenid, tokenidThis, tokenid = zeroid;
CScript fundingTxOpRetScript;
uint8_t hasHeirSpendingBegun = 0, hasHeirSpendingBegunDummy;
CScript opret = (tx.vout.size() > 0) ? tx.vout[tx.vout.size() - 1].scriptPubKey : CScript(); // check boundary
uint8_t funcId = DecodeHeirEitherOpRet(opret, tokenid, fundingTxidInOpret, hasHeirSpendingBegunDummy, true);
uint8_t funcId = DecodeHeirEitherOpRet(opret, tokenidThis, fundingTxidInOpret, hasHeirSpendingBegunDummy, true);
if (funcId == 0)
return eval->Invalid("invalid opreturn format");
if (funcId != 'F') {
if (fundingTxidInOpret == zeroid) {
return eval->Invalid("invalid tx opreturn format: no fundingtxid present");
return eval->Invalid("incorrect tx opreturn: no fundingtxid present");
}
if (tokenid == zeroid)
latestTxid = FindLatestFundingTx<CoinHelper>(fundingTxidInOpret, dummyTokenid, fundingTxOpRetScript, hasHeirSpendingBegun);
else
latestTxid = FindLatestFundingTx<TokenHelper>(fundingTxidInOpret, tokenid, fundingTxOpRetScript, hasHeirSpendingBegun);
latestTxid = FindLatestFundingTx(fundingTxidInOpret, tokenid, fundingTxOpRetScript, hasHeirSpendingBegun);
if( tokenid != zeroid && tokenid != tokenidThis )
return eval->Invalid("incorrect tx tokenid");
if (latestTxid == zeroid) {
return eval->Invalid("invalid heir transaction: no funding tx found");
return eval->Invalid("no fundingtx found");
}
}
else {
@ -406,7 +404,7 @@ uint8_t DecodeHeirEitherOpRet(CScript scriptPubKey, uint256 &tokenid, uint256 &f
* find the latest funding tx: it may be the first F tx or one of A or C tx's
* Note: this function is also called from validation code (use non-locking calls)
*/
template <class Helper> uint256 _FindLatestFundingTx(uint256 fundingtxid, uint8_t& funcId, uint256 &tokenid, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, CScript& fundingOpretScript, uint8_t &hasHeirSpendingBegun)
uint256 _FindLatestFundingTx(uint256 fundingtxid, uint8_t& funcId, uint256 &tokenid, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, CScript& fundingOpretScript, uint8_t &hasHeirSpendingBegun)
{
CTransaction fundingtx;
uint256 hashBlock;
@ -489,7 +487,7 @@ template <class Helper> uint256 _FindLatestFundingTx(uint256 fundingtxid, uint8_
}
// overload for validation code
template <class Helper> uint256 FindLatestFundingTx(uint256 fundingtxid, uint256 &tokenid, CScript& opRetScript, uint8_t &hasHeirSpendingBegun)
uint256 FindLatestFundingTx(uint256 fundingtxid, uint256 &tokenid, CScript& opRetScript, uint8_t &hasHeirSpendingBegun)
{
uint8_t funcId;
CPubKey ownerPubkey;
@ -497,15 +495,15 @@ template <class Helper> uint256 FindLatestFundingTx(uint256 fundingtxid, uint256
int64_t inactivityTime;
std::string heirName;
return _FindLatestFundingTx<Helper>(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTime, heirName, opRetScript, hasHeirSpendingBegun);
return _FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTime, heirName, opRetScript, hasHeirSpendingBegun);
}
// overload for transaction creation code
template <class Helper> uint256 FindLatestFundingTx(uint256 fundingtxid, uint8_t& funcId, uint256 &tokenid, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, uint8_t &hasHeirSpendingBegun)
uint256 FindLatestFundingTx(uint256 fundingtxid, uint8_t& funcId, uint256 &tokenid, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, uint8_t &hasHeirSpendingBegun)
{
CScript opRetScript;
return _FindLatestFundingTx<Helper>(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTime, heirName, opRetScript, hasHeirSpendingBegun);
return _FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTime, heirName, opRetScript, hasHeirSpendingBegun);
}
// add inputs of 1 of 2 cc address
@ -704,111 +702,115 @@ UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName
* creates tx to add more funds to cryptocondition address for spending by either funds' owner or heir
* @return result object with raw tx or error text
*/
template <class Helper> UniValue HeirAdd(uint256 fundingtxid, uint64_t txfee, int64_t amount)
template <class Helper> UniValue _HeirAdd(uint256 fundingtxid, uint64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun)
{
UniValue result(UniValue::VOBJ); //, a(UniValue::VARR);
UniValue result(UniValue::VOBJ);
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
CPubKey ownerPubkey, heirPubkey;
int64_t inputs, CCchange = 0;
int64_t inactivityTimeSec;
struct CCcontract_info *cp, C;
std::string rawhex;
uint256 lasttxid, tokenid;
std::string heirName;
uint8_t funcId;
uint8_t hasHeirSpendingBegun = 0;
cp = CCinit(&C, Helper::getMyEval()); // for tokens shoud be EVAL_TOKENS to sign it correctly!
//cp = CCinit(&C, EVAL_HEIR); // for tokens shoud be EVAL_TOKENS to sign it correctly!
if (txfee == 0)
txfee = 10000;
if ((lasttxid = FindLatestFundingTx<Helper>(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) {
int32_t numblocks;
CPubKey myPubkey = pubkey2pk(Mypubkey());
CPubKey myPubkey = pubkey2pk(Mypubkey());
// check if it is the owner
if (myPubkey != ownerPubkey) {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "adding funds is only allowed for the owner of this contract"));
return result;
}
// check if it is the owner
if (myPubkey != ownerPubkey) {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "adding funds is only allowed for the owner of this contract"));
return result;
}
if (AddNormalinputs(mtx, myPubkey, txfee, 3) > 0) { // txfee for miners
if (AddNormalinputs(mtx, myPubkey, txfee, 3) > 0) { // txfee for miners
int64_t inputs, change;
int64_t inputs, change;
if ((inputs = Helper::addOwnerInputs(tokenid, mtx, myPubkey, amount, 64)) > 0) { // TODO: why 64 max inputs?
if ((inputs = Helper::addOwnerInputs(tokenid, mtx, myPubkey, amount, 64)) > 0) { // TODO: why 64 max inputs?
// we do not use markers anymore - storing data in opreturn is better
// add marker vout:
/* char markeraddr[64];
CPubKey markerpubkey = CCtxidaddr(markeraddr, fundingtxid);
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); // txfee 1, txfee 2 - for miners
std::cerr << "HeirAdd() adding markeraddr=" << markeraddr << '\n'; */
// we do not use markers anymore - storing data in opreturn is better
// add marker vout:
/* char markeraddr[64];
CPubKey markerpubkey = CCtxidaddr(markeraddr, fundingtxid);
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); // txfee 1, txfee 2 - for miners
std::cerr << "HeirAdd() adding markeraddr=" << markeraddr << '\n'; */
// add cryptocondition to spend this funded amount for either pk
mtx.vout.push_back(Helper::make1of2Vout(amount, ownerPubkey, heirPubkey));
// add cryptocondition to spend this funded amount for either pk
mtx.vout.push_back(Helper::make1of2Vout(amount, ownerPubkey, heirPubkey));
if (inputs > amount)
change = (inputs - amount); // -txfee <-- txfee pays user
if (inputs > amount)
change = (inputs - amount); // -txfee <-- txfee pays user
//std::cerr << "HeirAdd() inputs=" << inputs << " amount=" << amount << " txfee=" << txfee << " change=" << change << '\n';
//std::cerr << "HeirAdd() inputs=" << inputs << " amount=" << amount << " txfee=" << txfee << " change=" << change << '\n';
if (change != 0) { // vout[1]
mtx.vout.push_back(Helper::makeUserVout(change, myPubkey));
}
if (change != 0) { // vout[1]
mtx.vout.push_back(Helper::makeUserVout(change, myPubkey));
}
// add 1of2 vout validation pubkeys - needed only for tokens:
std::vector<CPubKey> voutTokenPubkeys;
voutTokenPubkeys.push_back(ownerPubkey);
voutTokenPubkeys.push_back(heirPubkey);
// add 1of2 vout validation pubkeys - needed only for tokens:
std::vector<CPubKey> voutTokenPubkeys;
voutTokenPubkeys.push_back(ownerPubkey);
voutTokenPubkeys.push_back(heirPubkey);
// add opreturn 'A' and sign tx: // this txfee ignored
std::string rawhextx = (FinalizeCCTx(0, cp, mtx, myPubkey, txfee,
Helper::makeAddOpRet(tokenid, voutTokenPubkeys, fundingtxid, hasHeirSpendingBegun)));
// add opreturn 'A' and sign tx: // this txfee ignored
std::string rawhextx = (FinalizeCCTx(0, cp, mtx, myPubkey, txfee,
Helper::makeAddOpRet(tokenid, voutTokenPubkeys, fundingtxid, hasHeirSpendingBegun)));
if (!rawhextx.empty()) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hextx", rawhextx));
}
else {
std::cerr << "HeirAdd error in FinalizeCCtx" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "sign error"));
}
if (!rawhextx.empty()) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hextx", rawhextx));
}
else {
std::cerr << "HeirAdd cannot find owner cc inputs" << std::endl;
else {
std::cerr << "HeirAdd error in FinalizeCCtx" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find owner cc inputs"));
result.push_back(Pair("error", "sign error"));
}
}
else {
std::cerr << "HeirAdd cannot find normal inputs for tx fee" << std::endl;
std::cerr << "HeirAdd cannot find owner cc inputs" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find normal inputs for tx fee"));
result.push_back(Pair("error", "can't find owner cc inputs"));
}
}
else {
std::cerr << "HeirAdd cannot find normal inputs for tx fee" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find normal inputs for tx fee"));
}
} else {
fprintf(stderr, "HeirAdd can't find any heir CC funding tx's\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find any heir CC funding transactions"));
}
return result;
}
UniValue HeirAddCoinCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
return HeirAdd<CoinHelper>(fundingtxid, txfee, amount);
}
UniValue HeirAddTokenCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
return HeirAdd<TokenHelper>(fundingtxid, txfee, amount);
UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
CPubKey ownerPubkey, heirPubkey;
int64_t inactivityTimeSec;
uint256 latesttxid, tokenid = zeroid;
uint8_t funcId;
std::string heirName;
uint8_t hasHeirSpendingBegun = 0;
if ((latesttxid = FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) {
if (tokenid == zeroid)
return _HeirAdd<CoinHelper>(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
else
return _HeirAdd<TokenHelper>(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
}
else {
UniValue result(UniValue::VOBJ);
fprintf(stderr, "HeirAdd() can't find any heir CC funding tx's\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find any heir CC funding transactions"));
return result;
}
}
@ -817,146 +819,155 @@ UniValue HeirAddTokenCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount)
* creates tx to spend funds from cryptocondition address by either funds' owner or heir
* @return result object with raw tx or error text
*/
template <typename Helper>UniValue HeirClaim(uint256 fundingtxid, uint64_t txfee, int64_t amount)
template <typename Helper>UniValue _HeirClaim(uint256 fundingtxid, uint64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun)
{
UniValue result(UniValue::VOBJ); //, a(UniValue::VARR);
UniValue result(UniValue::VOBJ);
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());
CPubKey myPubkey, ownerPubkey, heirPubkey;
CPubKey myPubkey;
int64_t inputs, change = 0;
int64_t inactivityTimeSec;
struct CCcontract_info *cp, C;
uint256 latesttxid, tokenid;
uint8_t funcId;
std::string heirName;
uint8_t hasHeirSpendingBegun = 0;
//cp = CCinit(&C, Helper::getMyEval());
cp = CCinit(&C, EVAL_HEIR);
if (txfee == 0)
txfee = 10000;
if ((latesttxid = FindLatestFundingTx<Helper>(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) {
int32_t numblocks;
uint64_t durationSec = 0;
int32_t numblocks;
uint64_t durationSec = 0;
// we do not need to find duration if spending already has begun
if (!hasHeirSpendingBegun) {
durationSec = CCduration(numblocks, latesttxid);
std::cerr << "HeirClaim() duration=" << durationSec << " inactivityTime=" << inactivityTimeSec << " numblocks=" << numblocks << std::endl;
}
// we do not need to find duration if spending already has begun
if (!hasHeirSpendingBegun) {
durationSec = CCduration(numblocks, latesttxid);
std::cerr << "HeirClaim() duration=" << durationSec << " inactivityTime=" << inactivityTimeSec << " numblocks=" << numblocks << std::endl;
}
// spending is allowed if there is already spending tx or inactivity time
//bool isAllowedToHeir = (funcId == 'C' || durationSec > inactivityTimeSec) ? true : false;
bool isAllowedToHeir = (hasHeirSpendingBegun || durationSec > inactivityTimeSec) ? true : false;
myPubkey = pubkey2pk(Mypubkey());
// spending is allowed if there is already spending tx or inactivity time
//bool isAllowedToHeir = (funcId == 'C' || durationSec > inactivityTimeSec) ? true : false;
bool isAllowedToHeir = (hasHeirSpendingBegun || durationSec > inactivityTimeSec) ? true : false;
myPubkey = pubkey2pk(Mypubkey());
// if it is the heir, check if spending not allowed to heir yet
if (myPubkey == heirPubkey && !isAllowedToHeir) {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "spending is not allowed yet for the heir"));
return result;
}
// if it is the heir, check if spending not allowed to heir yet
if (myPubkey == heirPubkey && !isAllowedToHeir) {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "spending is not allowed yet for the heir"));
return result;
}
// we do not use markers any more:
// we allow owner to spend funds at any time:
// if it is the owner, check if spending already allowed to heir
/* if (myPubkey == ownerPubkey && isAllowedToHeir) {
result.push_back(Pair("result", "spending is not already allowed for the owner"));
return result;
} */
// add spending txfee from the calling user
if (AddNormalinputs(mtx, myPubkey, txfee, 3) > 0) {
// add spending from cc 1of2 address
if ((inputs = Add1of2AddressInputs<Helper>(cp, fundingtxid, mtx, ownerPubkey, heirPubkey, amount, 60)) >= amount) // TODO: why only 60 inputs?
{
/*if (inputs < amount) {
std::cerr << "HeirClaim() cant find enough HeirCC 1of2 inputs, found=" << inputs << " required=" << amount << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find heir CC funding"));
// we do not use markers any more:
// we allow owner to spend funds at any time:
// if it is the owner, check if spending already allowed to heir
/* if (myPubkey == ownerPubkey && isAllowedToHeir) {
result.push_back(Pair("result", "spending is not already allowed for the owner"));
return result;
} */
}*/
// add spending txfee from the calling user
if (AddNormalinputs(mtx, myPubkey, txfee, 3) > 0) {
// add vout with amount to claiming address
mtx.vout.push_back(Helper::makeUserVout(amount, myPubkey)); // vout[0]
// add spending from cc 1of2 address
if ((inputs = Add1of2AddressInputs<Helper>(cp, fundingtxid, mtx, ownerPubkey, heirPubkey, amount, 60)) >= amount) // TODO: why only 60 inputs?
{
/*if (inputs < amount) {
std::cerr << "HeirClaim() cant find enough HeirCC 1of2 inputs, found=" << inputs << " required=" << amount << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find heir CC funding"));
// calc and add change vout:
if (inputs > amount)
change = (inputs - amount); // -txfee <-- txfee pays user
return result;
}*/
//std::cerr << "HeirClaim() inputs=" << inputs << " amount=" << amount << " txfee=" << txfee << " change=" << change << '\n';
// add vout with amount to claiming address
mtx.vout.push_back(Helper::makeUserVout(amount, myPubkey)); // vout[0]
// change to 1of2 funding addr:
if (change != 0) { // vout[1]
mtx.vout.push_back(Helper::make1of2Vout(change, ownerPubkey, heirPubkey)); // using always pubkeys from OP_RETURN in order to not mixing them up!
}
// calc and add change vout:
if (inputs > amount)
change = (inputs - amount); // -txfee <-- txfee pays user
// add marker vout:
/*char markeraddr[64];
CPubKey markerpubkey = CCtxidaddr(markeraddr, fundingtxid);
// NOTE: amount = 0 is not working: causes error code: -26, error message : 64 : dust
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); // txfee 1, txfee 2 - for miners
std::cerr << "HeirClaim() adding markeraddr=" << markeraddr << '\n'; */
//std::cerr << "HeirClaim() inputs=" << inputs << " amount=" << amount << " txfee=" << txfee << " change=" << change << '\n';
// get address of 1of2 cond
char coinaddr[64];
Helper::GetCoinsOrTokensCCaddress1of2(coinaddr, ownerPubkey, heirPubkey);
// change to 1of2 funding addr:
if (change != 0) { // vout[1]
mtx.vout.push_back(Helper::make1of2Vout(change, ownerPubkey, heirPubkey)); // using always pubkeys from OP_RETURN in order to not mixing them up!
}
// retrieve priv key addresses for FinalizeCCtx:
uint8_t myprivkey[32];
Myprivkey(myprivkey);
// set pubkeys for finding 1of2 cc in FinalizeCCtx to sign it:
Helper::CCaddrCoinsOrTokens1of2set(cp, ownerPubkey, heirPubkey, coinaddr);
// add 1of2 vout validation pubkeys (this is for tokens):
std::vector<CPubKey> voutTokenPubkeys;
voutTokenPubkeys.push_back(ownerPubkey);
voutTokenPubkeys.push_back(heirPubkey);
// add opreturn 'C' and sign tx: // this txfee will be ignored
std::string rawhextx = FinalizeCCTx(0, cp, mtx, myPubkey, txfee,
Helper::makeClaimOpRet(tokenid, voutTokenPubkeys, fundingtxid, (myPubkey == heirPubkey) ? 1 : hasHeirSpendingBegun)); // forward isHeirSpending to the next latest tx
if (!rawhextx.empty()) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hextx", rawhextx));
}
else {
std::cerr << "HeirAdd error in FinalizeCCtx" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "sign error"));
}
// add marker vout:
/*char markeraddr[64];
CPubKey markerpubkey = CCtxidaddr(markeraddr, fundingtxid);
// NOTE: amount = 0 is not working: causes error code: -26, error message : 64 : dust
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(markerpubkey)) << OP_CHECKSIG)); // txfee 1, txfee 2 - for miners
std::cerr << "HeirClaim() adding markeraddr=" << markeraddr << '\n'; */
// get address of 1of2 cond
char coinaddr[64];
Helper::GetCoinsOrTokensCCaddress1of2(coinaddr, ownerPubkey, heirPubkey);
// retrieve priv key addresses for FinalizeCCtx:
uint8_t myprivkey[32];
Myprivkey(myprivkey);
// set pubkeys for finding 1of2 cc in FinalizeCCtx to sign it:
Helper::CCaddrCoinsOrTokens1of2set(cp, ownerPubkey, heirPubkey, coinaddr);
// add 1of2 vout validation pubkeys (this is for tokens):
std::vector<CPubKey> voutTokenPubkeys;
voutTokenPubkeys.push_back(ownerPubkey);
voutTokenPubkeys.push_back(heirPubkey);
// add opreturn 'C' and sign tx: // this txfee will be ignored
std::string rawhextx = FinalizeCCTx(0, cp, mtx, myPubkey, txfee,
Helper::makeClaimOpRet(tokenid, voutTokenPubkeys, fundingtxid, (myPubkey == heirPubkey) ? 1 : hasHeirSpendingBegun)); // forward isHeirSpending to the next latest tx
if (!rawhextx.empty()) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hextx", rawhextx));
}
else {
std::cerr << "HeirAdd error in FinalizeCCtx" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "sign error"));
}
} else {
fprintf(stderr, "HeirClaim() cant find Heir CC inputs\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find heir CC funding"));
}
} else {
fprintf(stderr, "HeirClaim() cant find sufficient user inputs for tx fee\n");
fprintf(stderr, "HeirClaim() cant find Heir CC inputs\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find sufficient user inputs to pay transaction fee"));
result.push_back(Pair("error", "can't find heir CC funding"));
}
} else {
fprintf(stderr, "HeirClaim() can't find any heir CC funding tx's\n");
fprintf(stderr, "HeirClaim() cant find sufficient user inputs for tx fee\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find any heir CC funding transactions"));
result.push_back(Pair("error", "can't find sufficient user inputs to pay transaction fee"));
}
return result;
}
UniValue HeirClaimCoinCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
return HeirClaim<CoinHelper>(fundingtxid, txfee, amount);
}
UniValue HeirClaimTokenCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
return HeirClaim<TokenHelper>(fundingtxid, txfee, amount);
UniValue HeirClaimCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) {
CPubKey ownerPubkey, heirPubkey;
int64_t inactivityTimeSec;
uint256 latesttxid, tokenid = zeroid;
uint8_t funcId;
std::string heirName;
uint8_t hasHeirSpendingBegun = 0;
if ((latesttxid = FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) {
if( tokenid == zeroid )
return _HeirClaim<CoinHelper>(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
else
return _HeirClaim<TokenHelper>(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
}
else {
UniValue result(UniValue::VOBJ);
fprintf(stderr, "HeirClaim() can't find any heir CC funding tx's\n");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "can't find any heir CC funding transactions"));
return result;
}
}
/**
* heirinfo rpc call implementation
* returns some information about heir CC contract plan by a handle of initial fundingtxid:
@ -980,32 +991,28 @@ UniValue HeirInfo(uint256 fundingtxid)
if (myGetTransaction(fundingtxid, fundingtx, hashBlock) && fundingtx.vout.size()) {
CPubKey ownerPubkey, heirPubkey;
uint256 latestFundingTxid;
uint256 dummyTokenid, tokenid = zeroid; // important to clear tokenid
std::string heirName;
int64_t inactivityTimeSec;
const bool noLogging = false;
uint8_t funcId;
CScript opret = fundingtx.vout.size() > 0 ? fundingtx.vout[fundingtx.vout.size() - 1].scriptPubKey : CScript();
/*CScript opret = fundingtx.vout.size() > 0 ? fundingtx.vout[fundingtx.vout.size() - 1].scriptPubKey : CScript();
uint8_t funcId = DecodeHeirEitherOpRet(opret, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, true);
if (funcId == 0) {
std::cerr << "HeirInfo() this fundingtx is incorrect" << std::endl;
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "initial tx F not found"));
return result;
}
}*/
struct CCcontract_info *cp, C;
cp = CCinit(&C, EVAL_HEIR);
uint8_t hasHeirSpendingBegun = 0;
if (tokenid == zeroid) // coins
latestFundingTxid = FindLatestFundingTx<CoinHelper>(fundingtxid, funcId, dummyTokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
else // tokens
latestFundingTxid = FindLatestFundingTx<TokenHelper>(fundingtxid, funcId, tokenid /*<-verify for tokens*/, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
uint256 latestFundingTxid = FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun);
if (latestFundingTxid != zeroid) {
int32_t numblocks;
uint64_t durationSec = 0;

2
src/cc/heir_validate.h

@ -10,7 +10,7 @@
CScript EncodeHeirCreateOpRet(uint8_t funcid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName);
CScript EncodeHeirOpRet(uint8_t funcid, uint256 fundingtxid, uint8_t isHeirSpendingBegan);
template <class Helper> uint256 FindLatestFundingTx(uint256 fundingtxid, uint256 &tokenid, CScript& opRetScript, uint8_t &isHeirSpendingBegan);
uint256 FindLatestFundingTx(uint256 fundingtxid, uint256 &tokenid, CScript& opRetScript, uint8_t &isHeirSpendingBegan);
//uint8_t DecodeHeirOpRet(CScript scriptPubKey, uint256& fundingtxid, uint8_t &isHeirSpendingBegan, bool noLogging = false);
//uint8_t DecodeHeirOpRet(CScript scriptPubKey, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, bool noLogging = false);
uint8_t DecodeHeirEitherOpRet(CScript scriptPubKey, uint256 &tokenid, CPubKey& ownerPubkey, CPubKey& heirPubkey, int64_t& inactivityTime, std::string& heirName, bool noLogging = false);

4
src/rpc/server.cpp

@ -411,9 +411,9 @@ static const CRPCCommand vRPCCommands[] =
{ "heir", "heirfund", &heirfund, true },
{ "heir", "heiradd", &heiradd, true },
{ "heir", "heirclaim", &heirclaim, true },
{ "heir", "heirfundtokens", &heirfundtokens, true },
/* { "heir", "heirfundtokens", &heirfundtokens, true },
{ "heir", "heiraddtokens", &heiraddtokens, true },
{ "heir", "heirclaimtokens", &heirclaimtokens, true },
{ "heir", "heirclaimtokens", &heirclaimtokens, true },*/
{ "heir", "heirinfo", &heirinfo, true },
{ "heir", "heirlist", &heirlist, true },

3
src/rpc/server.h

@ -257,9 +257,6 @@ extern UniValue heiraddress(const UniValue& params, bool fHelp);
extern UniValue heirfund(const UniValue& params, bool fHelp);
extern UniValue heiradd(const UniValue& params, bool fHelp);
extern UniValue heirclaim(const UniValue& params, bool fHelp);
extern UniValue heirfundtokens(const UniValue& params, bool fHelp);
extern UniValue heiraddtokens(const UniValue& params, bool fHelp);
extern UniValue heirclaimtokens(const UniValue& params, bool fHelp);
extern UniValue heirinfo(const UniValue& params, bool fHelp);
extern UniValue heirlist(const UniValue& params, bool fHelp);
extern UniValue channelsaddress(const UniValue& params, bool fHelp);

299
src/wallet/rpcwallet.cpp

@ -5417,7 +5417,6 @@ UniValue gatewaysaddress(const UniValue& params, bool fHelp)
UniValue heiraddress(const UniValue& params, bool fHelp)
{
/*
struct CCcontract_info *cp,C; std::vector<unsigned char> pubkey;
cp = CCinit(&C,EVAL_HEIR);
if ( fHelp || params.size() > 1 )
@ -5427,131 +5426,6 @@ UniValue heiraddress(const UniValue& params, bool fHelp)
if ( params.size() == 1 )
pubkey = ParseHex(params[0].get_str().c_str());
return(CCaddress(cp,(char *)"Heir",pubkey));
*/
// make fake token tx:
struct CCcontract_info *cp, C;
if (fHelp || (params.size() < 1))
throw runtime_error("heiraddress A|G|H|T|R assetid|destpubkey amountcoins [heirpubkey] [fundingtxid]\n");
if (ensure_CCrequirements() < 0)
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
char badKind = ((char *)params[0].get_str().c_str())[0];
if (badKind == 'A') {
std::vector<unsigned char> destPubkey;
if (params.size() == 2) {
cp = CCinit(&C, EVAL_HEIR);
destPubkey = ParseHex(params[1].get_str().c_str());
return(CCaddress(cp, (char *)"Heir", destPubkey));
}
else
return std::string("bad params for A");
}
CPubKey myPubkey = pubkey2pk(Mypubkey());
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());;
if (badKind != 'R') {
if (badKind == 'G' && params.size() != 3)
return std::string("incorrect params for G");
if (badKind == 'H' && params.size() != 5)
return std::string("incorrect params for H, = 5");
if (badKind == 'T' && params.size() != 3)
return std::string("incorrect params for T, = 3");
uint256 assetid = Parseuint256((char *)params[1].get_str().c_str());
int64_t amount = atof(params[2].get_str().c_str()) * COIN;
uint256 fundingtxid;
CPubKey heirPubkey;
if (badKind == 'H') {
std::vector<unsigned char> heirPubkeyStr = ParseHex(params[3].get_str().c_str());
heirPubkey = pubkey2pk(heirPubkeyStr);
fundingtxid = Parseuint256((char *)params[4].get_str().c_str());
}
int64_t txfee = 10000;
uint8_t evalCodeInOpret = (badKind == 'H') ? EVAL_HEIR : EVAL_GATEWAYS;
cp = CCinit(&C, EVAL_ASSETS);
int64_t normalInputs = AddNormalinputs(mtx, myPubkey, txfee + amount, 60);
// int64_t ccInputs = 0;
if (badKind == 'T') {
// just empty fake token
mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS, amount, myPubkey)); //note you need destPubkey for sending to gateways
}
else if (badKind == 'H') {
// heir add funding tx
mtx.vout.push_back(MakeCC1of2vout(EVAL_ASSETS, amount, myPubkey, heirPubkey)); // add cryptocondition to spend amount for either pk
}
else { // if (badKind == 'G')
CPubKey gatewayContractPubKey = GetUnspendable(cp, 0);
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(gatewayContractPubKey)) << OP_CHECKSIG));
}
int64_t change = (normalInputs - amount);
if (change != 0) {
mtx.vout.push_back(CTxOut(change, CScript() << ParseHex(HexStr(myPubkey)) << OP_CHECKSIG));
}
std::cerr << "make fake token for contract=" << badKind << " added normalInputs=" << normalInputs << " change=" << change << std::endl;
// note: it sets eval=EVAL_ASSETS in opreturn both for tokens and gateways cc addr
//script = EncodeAssetOpRet('t', assetid, zeroid, 0, (badKind == 'A' ? Mypubkey() : destPubkey)); // dimxy: are we sure about destPubkey here? it may be just pubkey of the author...
CScript opret;
assetid = revuint256(assetid);
fundingtxid = revuint256(fundingtxid);
if (badKind == 'T')
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid );
else if (badKind == 'H')
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid << (uint8_t)'A' << myPubkey << heirPubkey << (int64_t)120 << fundingtxid);
else
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid); // EVAL_GATEWAYS
return(FinalizeCCTx(0, cp, mtx, myPubkey, txfee, opret)); // normal change added here
}
else
{
// move vout from user cc addr to gateways unspendable
CTransaction srctx;
uint256 hashBlock;
const bool allowSlow = false;
uint256 srctxid = Parseuint256((char *)params[1].get_str().c_str());
struct CCcontract_info *cp, C;
cp = CCinit(&C, EVAL_GATEWAYS);
uint64_t txfee = 10000;
CPubKey gatewayspk = GetUnspendable(cp, 0);
if (myGetTransaction(srctxid, srctx, hashBlock) && srctx.vout.size() > 0) {
int64_t normalInputs = AddNormalinputs(mtx, myPubkey, 2 * txfee, 4);
mtx.vin.push_back(CTxIn(srctxid, 2, CScript())); // repaired src tx
mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS, srctx.vout[2].nValue, gatewayspk));
CScript script = srctx.vout[srctx.vout.size() - 1].scriptPubKey;
//mtx.fOverwintered = true;
return(FinalizeCCTx(0, cp, mtx, myPubkey, txfee, script)); // normal change added here
}
}
return std::string("there has been some error");
}
@ -7432,129 +7306,12 @@ UniValue getbalance64(const UniValue& params, bool fHelp)
return ret;
}
// heir contract functions for coins
UniValue heirfund(const UniValue& params, bool fHelp)
{
UniValue result(UniValue::VOBJ);
//uint256 txid;
int64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
//TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 5)
throw runtime_error("heirfund fee funds heirname heirpubkey inactivitytime\n");
if (ensure_CCrequirements() < 0)
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
const CKeyStore& keystore = *pwalletMain;
LOCK2(cs_main, pwalletMain->cs_wallet);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
name = params[2].get_str();
pubkey = ParseHex(params[3].get_str().c_str());
inactivitytime = atof((char*)params[4].get_str().c_str());
result = HeirFundCoinCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, zeroid);
/* if (hex.size() > 0) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
}
else
ERR_RESULT("couldn't create heir fund");*/
return result;
}
UniValue heiradd(const UniValue& params, bool fHelp)
{
UniValue result; // UniValue result(UniValue::VOBJ);
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heiradd fee funds fundingtxid\n");
if (ensure_CCrequirements() < 0)
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
const CKeyStore& keystore = *pwalletMain;
LOCK2(cs_main, pwalletMain->cs_wallet);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirAddCoinCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
UniValue heirclaim(const UniValue& params, bool fHelp)
{
UniValue result; // result(UniValue::VOBJ);
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heirclaim fee funds fundingtxid\n");
if (ensure_CCrequirements() < 0)
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
const CKeyStore& keystore = *pwalletMain;
LOCK2(cs_main, pwalletMain->cs_wallet);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirClaimCoinCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
// same heir contract functions for tokens
UniValue heirfundtokens(const UniValue& params, bool fHelp)
// heir contract functions for coins and tokens
UniValue heirfund(const UniValue& params, bool fHelp)
{
UniValue result(UniValue::VOBJ);
uint256 assetid;
uint256 tokenid = zeroid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
@ -7562,12 +7319,11 @@ UniValue heirfundtokens(const UniValue& params, bool fHelp)
std::vector<unsigned char> pubkey;
std::string name;
//TODO: do we need this (dimxy)?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 6)
throw runtime_error("heirfundtokens fee funds heirname heirpubkey inactivitytime assetid\n");
if (fHelp || params.size() != 5 && params.size() != 6)
throw runtime_error("heirfundtokens fee funds heirname heirpubkey inactivitytime [tokenid]\n");
if (ensure_CCrequirements() < 0)
throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n");
@ -7578,26 +7334,27 @@ UniValue heirfundtokens(const UniValue& params, bool fHelp)
amount = atoll((char*)params[1].get_str().c_str());
name = params[2].get_str();
pubkey = ParseHex(params[3].get_str().c_str());
inactivitytime = atof((char*)params[4].get_str().c_str());
assetid = Parseuint256((char*)params[5].get_str().c_str());
if( !pubkey2pk(pubkey).IsValid() )
throw runtime_error("incorrect pubkey\n");
result = HeirFundTokenCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, assetid);
/* if (hex.size() > 0) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
inactivitytime = atof((char*)params[4].get_str().c_str());
if (params.size() == 6) {
tokenid = Parseuint256((char*)params[5].get_str().c_str());
if(tokenid == zeroid)
throw runtime_error("incorrect tokenid\n");
}
else
ERR_RESULT("couldn't create heir fund");*/
if( tokenid == zeroid )
result = HeirFundCoinCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, zeroid);
else
result = HeirFundTokenCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, tokenid);
return result;
}
UniValue heiraddtokens(const UniValue& params, bool fHelp)
UniValue heiradd(const UniValue& params, bool fHelp)
{
UniValue result; // UniValue result(UniValue::VOBJ);
UniValue result;
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
@ -7606,7 +7363,6 @@ UniValue heiraddtokens(const UniValue& params, bool fHelp)
std::vector<unsigned char> pubkey;
std::string name;
// TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
@ -7622,18 +7378,11 @@ UniValue heiraddtokens(const UniValue& params, bool fHelp)
amount = atoll((char*)params[1].get_str().c_str());
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirAddTokenCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
result = HeirAddCaller(fundingtxid, txfee, amount);
return result;
}
UniValue heirclaimtokens(const UniValue& params, bool fHelp)
UniValue heirclaim(const UniValue& params, bool fHelp)
{
UniValue result; // result(UniValue::VOBJ);
uint256 fundingtxid;
@ -7660,13 +7409,7 @@ UniValue heirclaimtokens(const UniValue& params, bool fHelp)
amount = atoll((char*)params[1].get_str().c_str());
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirClaimTokenCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
result = HeirClaimCaller(fundingtxid, txfee, amount);
return result;
}

Loading…
Cancel
Save