Reorganize CC assets code
This commit is contained in:
133
src/cc/CCutils.cpp
Normal file
133
src/cc/CCutils.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/******************************************************************************
|
||||
* Copyright © 2014-2018 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 "CCinclude.h"
|
||||
|
||||
/*
|
||||
CCutils has low level functions that are universally useful for all contracts.
|
||||
*/
|
||||
|
||||
CC* GetCryptoCondition(CScript const& scriptSig)
|
||||
{
|
||||
auto pc = scriptSig.begin();
|
||||
opcodetype opcode;
|
||||
std::vector<unsigned char> ffbin;
|
||||
if (scriptSig.GetOp(pc, opcode, ffbin))
|
||||
return cc_readFulfillmentBinary((uint8_t*)ffbin.data(), ffbin.size()-1);
|
||||
}
|
||||
|
||||
bool IsCCInput(CScript const& scriptSig)
|
||||
{
|
||||
CC *cond;
|
||||
if ( (cond= GetCryptoCondition(scriptSig)) == 0 )
|
||||
return false;
|
||||
cc_free(cond);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsEvalInput(uint8_t evalcode,CScript const& scriptSig)
|
||||
{
|
||||
CC *cond;
|
||||
if (!(cond = GetCryptoCondition(scriptSig)))
|
||||
return false;
|
||||
// Recurse the CC tree to find asset condition
|
||||
auto findEval = [&] (CC *cond, struct CCVisitor _) {
|
||||
bool r = cc_typeId(cond) == CC_Eval && cond->codeLength == 1 && cond->code[0] == evalcode;
|
||||
// false for a match, true for continue
|
||||
return r ? 0 : 1;
|
||||
};
|
||||
CCVisitor visitor = {findEval, (uint8_t*)"", 0, NULL};
|
||||
bool out =! cc_visit(cond, visitor);
|
||||
cc_free(cond);
|
||||
return out;
|
||||
}
|
||||
|
||||
uint256 revuint256(uint256 txid)
|
||||
{
|
||||
uint256 revtxid; int32_t i;
|
||||
for (i=31; i>=0; i--)
|
||||
((uint8_t *)&revtxid)[31-i] = ((uint8_t *)&txid)[i];
|
||||
return(revtxid);
|
||||
}
|
||||
|
||||
char *uint256_str(char *dest,uint256 txid)
|
||||
{
|
||||
int32_t i,j=0;
|
||||
for (i=31; i>=0; i--)
|
||||
sprintf(&dest[j++ * 2],"%02x",((uint8_t *)&txid)[i]);
|
||||
return(dest);
|
||||
}
|
||||
|
||||
uint256 Parseuint256(char *hexstr)
|
||||
{
|
||||
uint256 txid; int32_t i; std::vector<unsigned char> txidbytes(ParseHex(hexstr));
|
||||
for (i=31; i>=0; i--)
|
||||
((uint8_t *)&txid)[31-i] = ((uint8_t *)txidbytes.data())[i];
|
||||
return(txid);
|
||||
}
|
||||
|
||||
bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey)
|
||||
{
|
||||
CTxDestination address; txnouttype whichType;
|
||||
if ( ExtractDestination(scriptPubKey,address) != 0 )
|
||||
{
|
||||
strcpy(destaddr,(char *)CBitcoinAddress(address).ToString().c_str());
|
||||
return(true);
|
||||
}
|
||||
fprintf(stderr,"ExtractDestination failed\n");
|
||||
return(false);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
bool Myprivkey(uint8_t myprivkey[])
|
||||
{
|
||||
char coinaddr[64]; std::string strAddress; char *dest; int32_t i,n; CBitcoinAddress address; CKeyID keyID; CKey vchSecret;
|
||||
if ( Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG) != 0 )
|
||||
{
|
||||
n = (int32_t)strlen(coinaddr);
|
||||
strAddress.resize(n+1);
|
||||
dest = (char *)strAddress.data();
|
||||
for (i=0; i<n; i++)
|
||||
dest[i] = coinaddr[i];
|
||||
dest[i] = 0;
|
||||
if ( address.SetString(strAddress) != 0 && address.GetKeyID(keyID) != 0 )
|
||||
{
|
||||
#ifdef ENABLE_WALLET
|
||||
if ( pwalletMain->GetKey(keyID,vchSecret) != 0 )
|
||||
{
|
||||
memcpy(myprivkey,vchSecret.begin(),32);
|
||||
//for (i=0; i<32; i++)
|
||||
// fprintf(stderr,"%02x",myprivkey[i]);
|
||||
//fprintf(stderr," found privkey!\n");
|
||||
return(true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
fprintf(stderr,"privkey for the -pubkey= address is not in the wallet, importprivkey!\n");
|
||||
return(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user