Delete more CCs #381
This commit is contained in:
@@ -44,70 +44,11 @@ static unsigned char* CopyPubKey(CPubKey pkIn)
|
||||
}
|
||||
|
||||
|
||||
CC* CCNewThreshold(int t, std::vector<CC*> v)
|
||||
{
|
||||
CC *cond = cc_new(CC_Threshold);
|
||||
cond->threshold = t;
|
||||
cond->size = v.size();
|
||||
cond->subconditions = (CC**) calloc(v.size(), sizeof(CC*));
|
||||
memcpy(cond->subconditions, v.data(), v.size() * sizeof(CC*));
|
||||
return cond;
|
||||
}
|
||||
|
||||
|
||||
CC* CCNewSecp256k1(CPubKey k)
|
||||
{
|
||||
CC *cond = cc_new(CC_Secp256k1);
|
||||
cond->publicKey = CopyPubKey(k);
|
||||
return cond;
|
||||
}
|
||||
|
||||
|
||||
CC* CCNewEval(std::vector<unsigned char> code)
|
||||
{
|
||||
CC *cond = cc_new(CC_Eval);
|
||||
cond->code = (unsigned char*) malloc(code.size());
|
||||
memcpy(cond->code, code.data(), code.size());
|
||||
cond->codeLength = code.size();
|
||||
return cond;
|
||||
}
|
||||
|
||||
|
||||
CScript CCPubKey(const CC *cond)
|
||||
{
|
||||
return CScript();
|
||||
}
|
||||
|
||||
|
||||
CScript CCSig(const CC *cond)
|
||||
{
|
||||
return CScript();
|
||||
}
|
||||
|
||||
std::vector<unsigned char> CCSigVec(const CC *cond)
|
||||
{
|
||||
unsigned char buf[10000];
|
||||
size_t len = cc_fulfillmentBinary(cond, buf, 10000);
|
||||
auto ffill = std::vector<unsigned char>(buf, buf+len);
|
||||
ffill.push_back(1); // SIGHASH_ALL
|
||||
return ffill;
|
||||
}
|
||||
|
||||
std::string CCShowStructure(CC *cond)
|
||||
{
|
||||
std::string out;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
CC* CCPrune(CC *cond)
|
||||
{
|
||||
std::vector<unsigned char> ffillBin;
|
||||
GetPushData(CCSig(cond), ffillBin);
|
||||
return cc_readFulfillmentBinary(ffillBin.data(), ffillBin.size()-1);
|
||||
}
|
||||
|
||||
|
||||
bool GetPushData(const CScript &sig, std::vector<unsigned char> &data)
|
||||
{
|
||||
opcodetype opcode;
|
||||
|
||||
@@ -958,6 +958,7 @@ bool EvalScript(
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
case OP_CHECKCRYPTOCONDITION:
|
||||
case OP_CHECKCRYPTOCONDITIONVERIFY:
|
||||
{
|
||||
@@ -987,6 +988,7 @@ bool EvalScript(
|
||||
}
|
||||
}
|
||||
break;
|
||||
*/
|
||||
|
||||
INTERPRETER_DEFAULT:
|
||||
default:
|
||||
@@ -1361,43 +1363,7 @@ int TransactionSignatureChecker::CheckCryptoCondition(
|
||||
const CScript& scriptCode,
|
||||
uint32_t consensusBranchId) const
|
||||
{
|
||||
// Hash type is one byte tacked on to the end of the fulfillment
|
||||
if (ffillBin.empty())
|
||||
return false;
|
||||
|
||||
CC *cond;
|
||||
int error = cc_readFulfillmentBinaryExt((unsigned char*)ffillBin.data(), ffillBin.size()-1, &cond);
|
||||
if (error || !cond) return -1;
|
||||
|
||||
if (!IsSupportedCryptoCondition(cond)) return 0;
|
||||
if (!IsSignedCryptoCondition(cond)) return 0;
|
||||
|
||||
uint256 sighash;
|
||||
int nHashType = ffillBin.back();
|
||||
try {
|
||||
sighash = SignatureHash(CCPubKey(cond), *txTo, nIn, nHashType, amount, consensusBranchId, this->txdata);
|
||||
} catch (logic_error ex) {
|
||||
return 0;
|
||||
}
|
||||
/*int32_t z; uint8_t *ptr;
|
||||
ptr = (uint8_t *)scriptCode.data();
|
||||
for (z=0; z<scriptCode.size(); z++)
|
||||
fprintf(stderr,"%02x",ptr[z]);
|
||||
fprintf(stderr," <- CScript\n");
|
||||
for (z=0; z<32; z++)
|
||||
fprintf(stderr,"%02x",((uint8_t *)&sighash)[z]);
|
||||
fprintf(stderr," sighash nIn.%d nHashType.%d %.8f id.%d\n",(int32_t)nIn,(int32_t)nHashType,(double)amount/COIN,(int32_t)consensusBranchId);
|
||||
*/
|
||||
VerifyEval eval = [] (CC *cond, void *checker) {
|
||||
//fprintf(stderr,"checker.%p\n",(TransactionSignatureChecker*)checker);
|
||||
return ((TransactionSignatureChecker*)checker)->CheckEvalCondition(cond);
|
||||
};
|
||||
//fprintf(stderr,"non-checker path\n");
|
||||
int out = cc_verify(cond, (const unsigned char*)&sighash, 32, 0,
|
||||
condBin.data(), condBin.size(), eval, (void*)this);
|
||||
//fprintf(stderr,"out.%d from cc_verify\n",(int32_t)out);
|
||||
cc_free(cond);
|
||||
return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -72,19 +72,8 @@ bool TransactionSignatureCreator::CreateSig(std::vector<unsigned char>& vchSig,
|
||||
}
|
||||
//fprintf(stderr,"privkey (%s) for %s\n",NSPV_wifstr,EncodeDestination(key.GetPubKey().GetID()).c_str());
|
||||
|
||||
if (scriptCode.IsPayToCryptoCondition())
|
||||
if (0) // scriptCode.IsPayToCryptoCondition())
|
||||
{
|
||||
CC *cc = (CC *)extraData;
|
||||
// assume either 1of1 or 1of2. if the condition created by the
|
||||
if (!cc || cc_signTreeSecp256k1Msg32(cc, key.begin(), hash.begin()) == 0)
|
||||
{
|
||||
fprintf(stderr,"CC tree error\n");
|
||||
return false;
|
||||
}
|
||||
vchSig = CCSigVec(cc);
|
||||
if ( HUSH_NSPV_SUPERLITE )
|
||||
memset((uint8_t *)key.begin(),0,32);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -139,25 +128,6 @@ static bool SignN(const vector<valtype>& multisigdata, const BaseSignatureCreato
|
||||
return nSigned==nRequired;
|
||||
}
|
||||
|
||||
CC *CCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2)
|
||||
{
|
||||
std::vector<CC*> pks;
|
||||
pks.push_back(CCNewSecp256k1(pk1));
|
||||
pks.push_back(CCNewSecp256k1(pk2));
|
||||
CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
|
||||
CC *Sig = CCNewThreshold(1, pks);
|
||||
return CCNewThreshold(2, {condCC, Sig});
|
||||
}
|
||||
|
||||
CC *CCcond1(uint8_t evalcode,CPubKey pk)
|
||||
{
|
||||
std::vector<CC*> pks;
|
||||
pks.push_back(CCNewSecp256k1(pk));
|
||||
CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
|
||||
CC *Sig = CCNewThreshold(1, pks);
|
||||
return CCNewThreshold(2, {condCC, Sig});
|
||||
}
|
||||
|
||||
std::vector<CCcontract_info> &GetCryptoConditions()
|
||||
{
|
||||
static bool initialized = false;
|
||||
@@ -173,36 +143,12 @@ std::vector<CCcontract_info> &GetCryptoConditions()
|
||||
|
||||
bool GetCCByUnspendableAddress(struct CCcontract_info *cp, char *addrstr)
|
||||
{
|
||||
std::vector<CCcontract_info> &vCC = GetCryptoConditions();
|
||||
bool found = false;
|
||||
|
||||
for (int i = 0; i < vCC.size(); i++)
|
||||
{
|
||||
if (strcmp(addrstr, vCC[i].unspendableCCaddr) == 0)
|
||||
{
|
||||
found = true;
|
||||
*cp = vCC[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CCinitLite(struct CCcontract_info *cp, uint8_t evalcode)
|
||||
{
|
||||
std::vector<CCcontract_info> &vCC = GetCryptoConditions();
|
||||
bool found = false;
|
||||
|
||||
for (int i = 0; i < vCC.size(); i++)
|
||||
{
|
||||
if (vCC[i].evalcode == evalcode)
|
||||
{
|
||||
found = true;
|
||||
*cp = vCC[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _Getscriptaddress(char *destaddr, const CScript &scriptPubKey)
|
||||
@@ -222,118 +168,12 @@ bool _Getscriptaddress(char *destaddr, const CScript &scriptPubKey)
|
||||
|
||||
CScript _CCPubKey(const CC *cond)
|
||||
{
|
||||
unsigned char buf[1000];
|
||||
size_t len = cc_conditionBinary(cond, buf);
|
||||
return CScript() << std::vector<unsigned char>(buf, buf+len) << OP_CHECKCRYPTOCONDITION;
|
||||
return CScript();
|
||||
}
|
||||
|
||||
static bool SignStepCC(const BaseSignatureCreator& creator, const CScript& scriptPubKey, vector<valtype> &vSolutions,
|
||||
vector<valtype>& ret, uint32_t consensusBranchId)
|
||||
{
|
||||
CScript subScript;
|
||||
vector<CPubKey> vPK;
|
||||
vector<valtype> vParams = vector<valtype>();
|
||||
COptCCParams p;
|
||||
|
||||
// get information to sign with
|
||||
CCcontract_info C;
|
||||
|
||||
scriptPubKey.IsPayToCryptoCondition(&subScript, vParams);
|
||||
if (vParams.empty())
|
||||
{
|
||||
// get the keyID address of the cc and if it is an unspendable cc address, use its pubkey
|
||||
// we have nothing else
|
||||
char addr[64];
|
||||
if (_Getscriptaddress(addr, subScript) && GetCCByUnspendableAddress(&C, addr))
|
||||
{
|
||||
vPK.push_back(CPubKey(ParseHex(C.CChexstr)));
|
||||
p = COptCCParams(p.VERSION, C.evalcode, 1, 1, vPK, vParams);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p = COptCCParams(vParams[0]);
|
||||
}
|
||||
|
||||
if (p.IsValid() && p.vKeys.size() >= p.n)
|
||||
{
|
||||
bool is1of2 = (p.m == 1 && p.n == 2);
|
||||
CKey privKey;
|
||||
|
||||
// must be a valid cc eval code
|
||||
if (CCinitLite(&C, p.evalCode))
|
||||
{
|
||||
// pay to cc address is a valid tx
|
||||
if (!is1of2)
|
||||
{
|
||||
bool havePriv = creator.KeyStore().GetKey(p.vKeys[0].GetID(), privKey);
|
||||
|
||||
// if we don't have the private key, it must be the unspendable address
|
||||
if (!havePriv && (p.vKeys[0] == CPubKey(ParseHex(C.CChexstr))))
|
||||
{
|
||||
privKey = CKey();
|
||||
std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
|
||||
privKey.Set(vch.begin(), vch.end(), false);
|
||||
}
|
||||
|
||||
CC *cc = CCcond1(p.evalCode, p.vKeys[0]);
|
||||
|
||||
if (cc)
|
||||
{
|
||||
vector<unsigned char> vch;
|
||||
if (creator.CreateSig(vch, p.vKeys[0].GetID(), _CCPubKey(cc), consensusBranchId, &privKey, (void *)cc))
|
||||
{
|
||||
ret.push_back(vch);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr,"vin has 1of1 CC signing error with address.(%s)\n", p.vKeys[0].GetID().ToString().c_str());
|
||||
}
|
||||
|
||||
cc_free(cc);
|
||||
return ret.size() != 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// first of priv key in our key store or contract address is what we sign with
|
||||
for (auto pk : p.vKeys)
|
||||
{
|
||||
if (creator.IsKeystoreValid() && creator.KeyStore().GetKey(pk.GetID(), privKey) && privKey.IsValid())
|
||||
break;
|
||||
|
||||
if (pk == CPubKey(ParseHex(C.CChexstr)))
|
||||
{
|
||||
privKey = CKey();
|
||||
std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
|
||||
privKey.Set(vch.begin(), vch.end(), false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!privKey.IsValid())
|
||||
return false;
|
||||
|
||||
CC *cc = CCcond1of2(p.evalCode, p.vKeys[0], p.vKeys[1]);
|
||||
|
||||
if (cc)
|
||||
{
|
||||
vector<unsigned char> vch;
|
||||
if (creator.CreateSig(vch, p.vKeys[0].GetID(), _CCPubKey(cc), consensusBranchId, &privKey, (void *)cc))
|
||||
{
|
||||
ret.push_back(vch);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr,"vin has 1of2 CC signing error with addresses.(%s)\n(%s)\n", p.vKeys[0].GetID().ToString().c_str(), p.vKeys[1].GetID().ToString().c_str());
|
||||
}
|
||||
|
||||
cc_free(cc);
|
||||
return ret.size() != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user