// 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.            *
 *                                                                            *
 ******************************************************************************/
#include <assert.h>
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "script/cc.h"
#include "cc/eval.h"
#include "cc/utils.h"
#include "cc/CCinclude.h"
#include "main.h"
#include "chain.h"
#include "core_io.h"
#define FAUCET2SIZE COIN
#define EVAL_FAUCET2 EVAL_FIRSTUSER

#ifdef BUILD_CUSTOMCC
#include "customcc.h"
#endif

#ifndef BUILD_GAMESCC
void hush_netevent(std::vector<uint8_t> payload) {}
#endif

extern std::string MYCCLIBNAME;

char *CClib_name() { return((char *)MYCCLIBNAME.c_str()); }

struct CClib_rpcinfo
{
    char *CCname,*method,*help;
    int32_t numrequiredargs,maxargs;
    uint8_t funcid,evalcode;
}

CClib_methods[] =
{
    { (char *)"faucet2", (char *)"fund", (char *)"amount", 1, 1, 'F', EVAL_FAUCET2 },
    { (char *)"faucet2", (char *)"get", (char *)"<no args>", 0, 0, 'G', EVAL_FAUCET2 },
#ifdef BUILD_CUSTOMCC
    RPC_FUNCS
#endif
};

std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *params);

cJSON *cclib_reparse(int32_t *nump,char *jsonstr) // assumes origparams will be freed by caller
{
    cJSON *params; char *newstr; int32_t i,j;
    *nump = 0;
    if ( jsonstr != 0 )
    {
        if ( jsonstr[0] == '"' && jsonstr[strlen(jsonstr)-1] == '"' )
        {
            jsonstr[strlen(jsonstr)-1] = 0;
            jsonstr++;
        }
        newstr = (char *)malloc(strlen(jsonstr)+1);
        for (i=j=0; jsonstr[i]!=0; i++)
        {
            if ( jsonstr[i] == '%' && jsonstr[i+1] == '2' && jsonstr[i+2] == '2' )
            {
                newstr[j++] = '"';
                i += 2;
            }
            else if ( jsonstr[i] == '\'' )
                newstr[j++] = '"';
            else newstr[j++] = jsonstr[i];
        }
        newstr[j] = 0;
        params = cJSON_Parse(newstr);
        if ( 0 && params != 0 )
            printf("new.(%s) -> %s\n",newstr,jprint(params,0));
        free(newstr);
        *nump = cJSON_GetArraySize(params);
        //free(origparams);
    } else params = 0;
    return(params);
}

UniValue CClib_method(struct CCcontract_info *cp,char *method,char *jsonstr)
{
    UniValue result(UniValue::VOBJ);
    return(result);
}

UniValue CClib_info(struct CCcontract_info *cp)
{
    UniValue result(UniValue::VOBJ);
    return(result);
}

UniValue CClib(struct CCcontract_info *cp,char *method,char *jsonstr)
{
    UniValue result(UniValue::VOBJ);
    return(result);
}

int64_t IsCClibvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v,char *cmpaddr)
{
    return(0);
}

bool CClibExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
{
    return false;
}

bool CClib_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx,unsigned int nIn)
{
    return false;
}

int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs,char *cmpaddr,int32_t CCflag)
{
    return 0;
}

int64_t AddCClibtxfee(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk)
{
    return(0);
}

std::string Faucet2Fund(struct CCcontract_info *cp,uint64_t txfee,int64_t funds)
{
    return("");
}

std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *params)
{
    return("");
}

UniValue cclib_error(UniValue &result,const char *errorstr)
{
    return(result);
}

uint256 juint256(cJSON *obj)
{
    uint256 tmp; bits256 t = jbits256(obj,0);
    memcpy(&tmp,&t,sizeof(tmp));
    return(revuint256(tmp));
}

int32_t cclib_parsehash(uint8_t *hash32,cJSON *item,int32_t len)
{
    return(0);
}


#if BUILD_CUSTOMCC
#include "customcc.cpp"
#endif
