Files
hush3/src/cc/CCutils.cpp
2024-11-11 08:25:38 -05:00

257 lines
8.4 KiB
C++

// Copyright (c) 2016-2024 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
// CCutils has low level functions that are universally useful for all contracts.
#include "CCinclude.h"
#include "hush_structs.h"
#include "key_io.h"
#ifdef TESTMODE
#define MIN_NON_NOTARIZED_CONFIRMS 2
#else
#define MIN_NON_NOTARIZED_CONFIRMS 101
#endif // TESTMODE
int32_t hush_dpowconfs(int32_t height,int32_t numconfs);
struct hush_state *hush_stateptr(char *symbol,char *dest);
extern uint32_t HUSH_DPOWCONFS;
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len)
{
int32_t i,j=0;
#if defined(WORDS_BIGENDIAN)
for (i=31; i>=0; i--)
dest[j++] = src[i];
#else
memcpy(dest,src,len);
#endif
}
int32_t has_opret(const CTransaction &tx, uint8_t evalcode)
{
int i = 0;
for ( auto vout : tx.vout )
{
//fprintf(stderr, "[txid.%s] 1.%i 2.%i 3.%i 4.%i\n",tx.GetHash().GetHex().c_str(), vout.scriptPubKey[0], vout.scriptPubKey[1], vout.scriptPubKey[2], vout.scriptPubKey[3]);
if ( vout.scriptPubKey.size() > 3 && vout.scriptPubKey[0] == OP_RETURN && vout.scriptPubKey[2] == evalcode )
return i;
i++;
}
return 0;
}
bool makeCCopret(CScript &opret, std::vector<std::vector<unsigned char>> &vData)
{
return false;
}
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue, CPubKey pk, std::vector<std::vector<unsigned char>>* vData)
{
CTxOut vout;
return(vout);
}
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk1,CPubKey pk2, std::vector<std::vector<unsigned char>>* vData)
{
CTxOut vout;
return(vout);
}
bool IsCCInput(CScript const& scriptSig)
{
return false;
}
bool CheckTxFee(const CTransaction &tx, uint64_t txfee, uint32_t height, uint64_t blocktime, int64_t &actualtxfee)
{
LOCK(mempool.cs);
CCoinsView dummy;
CCoinsViewCache view(&dummy);
int64_t interest; uint64_t valuein;
CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
view.SetBackend(viewMemPool);
valuein = view.GetValueIn(height,&interest,tx,blocktime);
actualtxfee = valuein-tx.GetValueOut();
if ( actualtxfee > txfee )
{
//fprintf(stderr, "actualtxfee.%li vs txfee.%li\n", actualtxfee, txfee);
return false;
}
return true;
}
uint32_t GetLatestTimestamp(int32_t height)
{
if ( HUSH_NSPV_SUPERLITE ) return ((uint32_t)NSPV_blocktime(height));
return(hush_heightstamp(height));
} // :P
bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey)
{
CTxDestination address; txnouttype whichType;
destaddr[0] = 0;
if ( scriptPubKey.begin() != 0 )
{
if ( ExtractDestination(scriptPubKey,address) != 0 )
{
strcpy(destaddr,(char *)CBitcoinAddress(address).ToString().c_str());
return(true);
}
}
//fprintf(stderr,"ExtractDestination failed\n");
return(false);
}
bool pubkey2addr(char *destaddr,uint8_t *pubkey33)
{
std::vector<uint8_t>pk; int32_t i;
for (i=0; i<33; i++)
pk.push_back(pubkey33[i]);
return(Getscriptaddress(destaddr,CScript() << pk << OP_CHECKSIG));
}
bool priv2addr(char *coinaddr,uint8_t *buf33,uint8_t priv32[32])
{
CKey priv; CPubKey pk; int32_t i; uint8_t *src;
priv.SetKey32(priv32);
pk = priv.GetPubKey();
if ( buf33 != 0 )
{
src = (uint8_t *)pk.begin();
for (i=0; i<33; i++)
buf33[i] = src[i];
}
return(Getscriptaddress(coinaddr, CScript() << ParseHex(HexStr(pk)) << OP_CHECKSIG));
}
std::vector<uint8_t> Mypubkey()
{
extern uint8_t NOTARY_PUBKEY33[33];
std::vector<uint8_t> pubkey; int32_t i; uint8_t *dest,*pubkey33;
pubkey33 = NOTARY_PUBKEY33;
pubkey.resize(33);
dest = pubkey.data();
for (i=0; i<33; i++)
dest[i] = pubkey33[i];
return(pubkey);
}
extern char NSPV_wifstr[],NSPV_pubkeystr[];
extern uint32_t NSPV_logintime;
#define NSPV_AUTOLOGOUT 777
CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv)
{
if ( unspendablepriv != 0 )
memcpy(unspendablepriv,cp->CCpriv,32);
return(pubkey2pk(ParseHex(cp->CChexstr)));
}
int32_t NSPV_coinaddr_inmempool(char const *logcategory,char *coinaddr,uint8_t CCflag);
extern struct NSPV_mempoolresp NSPV_mempoolresult;
extern bool NSPV_evalcode_inmempool(uint8_t evalcode,uint8_t funcid);
/* Get the block merkle root for a proof
* IN: proofData
* OUT: merkle root
* OUT: transaction IDS
*/
uint256 BitcoinGetProofMerkleRoot(const std::vector<uint8_t> &proofData, std::vector<uint256> &txids)
{
CMerkleBlock merkleBlock;
if (!E_UNMARSHAL(proofData, ss >> merkleBlock))
return uint256();
return merkleBlock.txn.ExtractMatches(txids);
}
extern struct NSPV_inforesp NSPV_inforesult;
int32_t hush_get_current_height()
{
if ( HUSH_NSPV_SUPERLITE )
{
return (NSPV_inforesult.height);
}
else return chainActive.LastTip()->GetHeight();
}
bool hush_txnotarizedconfirmed(uint256 txid)
{
char str[65];
int32_t confirms,notarized=0,txheight=0,currentheight=0;;
CTransaction tx;
uint256 hashBlock;
CBlockIndex *pindex;
char symbol[HUSH_SMART_CHAIN_MAXLEN],dest[HUSH_SMART_CHAIN_MAXLEN]; struct hush_state *sp;
if ( HUSH_NSPV_SUPERLITE )
{
if ( NSPV_myGetTransaction(txid,tx,hashBlock,txheight,currentheight) == 0 )
{
fprintf(stderr,"hush_txnotarizedconfirmed cant find txid %s\n",txid.ToString().c_str());
return(0);
}
else if (txheight<=0)
{
fprintf(stderr,"hush_txnotarizedconfirmed no txheight.%d for txid %s\n",txheight,txid.ToString().c_str());
return(0);
}
else if (txheight>currentheight)
{
fprintf(stderr,"hush_txnotarizedconfirmed backwards heights for txid %s hts.(%d %d)\n",txid.ToString().c_str(),txheight,currentheight);
return(0);
}
confirms=1 + currentheight - txheight;
}
else
{
if ( myGetTransaction(txid,tx,hashBlock) == 0 )
{
fprintf(stderr,"hush_txnotarizedconfirmed cant find txid %s\n",txid.ToString().c_str());
return(0);
}
else if ( hashBlock == zeroid )
{
fprintf(stderr,"hush_txnotarizedconfirmed no hashBlock for txid %s\n",txid.ToString().c_str());
return(0);
}
else if ( (pindex= hush_blockindex(hashBlock)) == 0 || (txheight= pindex->GetHeight()) <= 0 )
{
fprintf(stderr,"hush_txnotarizedconfirmed no txheight.%d %p for txid %s\n",txheight,pindex,txid.ToString().c_str());
return(0);
}
else if ( (pindex= chainActive.LastTip()) == 0 || pindex->GetHeight() < txheight )
{
fprintf(stderr,"hush_txnotarizedconfirmed backwards heights for txid %s hts.(%d %d)\n",txid.ToString().c_str(),txheight,(int32_t)pindex->GetHeight());
return(0);
}
confirms=1 + pindex->GetHeight() - txheight;
}
if ((sp= hush_stateptr(symbol,dest)) != 0 && (notarized=sp->NOTARIZED_HEIGHT) > 0 && txheight > sp->NOTARIZED_HEIGHT) notarized=0;
#ifdef TESTMODE
notarized=0;
#endif //TESTMODE
if (notarized>0 && confirms > 1)
return (true);
else if (notarized==0 && confirms >= MIN_NON_NOTARIZED_CONFIRMS)
return (true);
return (false);
}
extern std::string MYCCLIBNAME;