initial commit to use CCopt for custom CC
This commit is contained in:
@@ -226,8 +226,8 @@ bool priv2addr(char *coinaddr,uint8_t buf33[33],uint8_t priv32[32]);
|
|||||||
CPubKey buf2pk(uint8_t *buf33);
|
CPubKey buf2pk(uint8_t *buf33);
|
||||||
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len);
|
void endiancpy(uint8_t *dest,uint8_t *src,int32_t len);
|
||||||
uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv,int32_t entropyvout,int32_t usevout);
|
uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv,int32_t entropyvout,int32_t usevout);
|
||||||
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk);
|
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk, const std::vector<std::vector<unsigned char>>* vData = NULL);
|
||||||
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2);
|
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2, const std::vector<std::vector<unsigned char>>* vData = NULL);
|
||||||
CC *MakeCCcond1(uint8_t evalcode,CPubKey pk);
|
CC *MakeCCcond1(uint8_t evalcode,CPubKey pk);
|
||||||
CC *MakeCCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2);
|
CC *MakeCCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2);
|
||||||
CC* GetCryptoCondition(CScript const& scriptSig);
|
CC* GetCryptoCondition(CScript const& scriptSig);
|
||||||
|
|||||||
@@ -58,20 +58,37 @@ CC *MakeCCcond1(uint8_t evalcode,CPubKey pk)
|
|||||||
return CCNewThreshold(2, {condCC, Sig});
|
return CCNewThreshold(2, {condCC, Sig});
|
||||||
}
|
}
|
||||||
|
|
||||||
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk)
|
CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue, CPubKey pk, const std::vector<std::vector<unsigned char>>* vData)
|
||||||
{
|
{
|
||||||
CTxOut vout;
|
CTxOut vout;
|
||||||
CC *payoutCond = MakeCCcond1(evalcode,pk);
|
CC *payoutCond = MakeCCcond1(evalcode,pk);
|
||||||
vout = CTxOut(nValue,CCPubKey(payoutCond));
|
vout = CTxOut(nValue,CCPubKey(payoutCond));
|
||||||
|
if ( vData )
|
||||||
|
{
|
||||||
|
std::vector<std::vector<unsigned char>> vtmpData = std::vector<std::vector<unsigned char>>(vData->begin(), vData->end());
|
||||||
|
std::vector<CPubKey> vPubKeys = std::vector<CPubKey>();
|
||||||
|
vPubKeys.push_back(pk);
|
||||||
|
COptCCParams ccp = COptCCParams(COptCCParams::VERSION, evalcode, 1, 1, vPubKeys, vtmpData);
|
||||||
|
vout.scriptPubKey << ccp.AsVector() << OP_DROP;
|
||||||
|
}
|
||||||
cc_free(payoutCond);
|
cc_free(payoutCond);
|
||||||
return(vout);
|
return(vout);
|
||||||
}
|
}
|
||||||
|
|
||||||
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk1,CPubKey pk2)
|
CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk1,CPubKey pk2, const std::vector<std::vector<unsigned char>>* vData)
|
||||||
{
|
{
|
||||||
CTxOut vout;
|
CTxOut vout;
|
||||||
CC *payoutCond = MakeCCcond1of2(evalcode,pk1,pk2);
|
CC *payoutCond = MakeCCcond1of2(evalcode,pk1,pk2);
|
||||||
vout = CTxOut(nValue,CCPubKey(payoutCond));
|
vout = CTxOut(nValue,CCPubKey(payoutCond));
|
||||||
|
if ( vData )
|
||||||
|
{
|
||||||
|
std::vector<std::vector<unsigned char>> vtmpData = std::vector<std::vector<unsigned char>>(vData->begin(), vData->end());
|
||||||
|
std::vector<CPubKey> vPubKeys = std::vector<CPubKey>();
|
||||||
|
vPubKeys.push_back(pk1);
|
||||||
|
vPubKeys.push_back(pk2);
|
||||||
|
COptCCParams ccp = COptCCParams(COptCCParams::VERSION, evalcode, 1, 2, vPubKeys, vtmpData);
|
||||||
|
vout.scriptPubKey << ccp.AsVector() << OP_DROP;
|
||||||
|
}
|
||||||
cc_free(payoutCond);
|
cc_free(payoutCond);
|
||||||
return(vout);
|
return(vout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
CScript custom_opret(uint8_t funcid,CPubKey pk)
|
CScript custom_opret(uint8_t funcid,CPubKey pk)
|
||||||
{
|
{
|
||||||
CScript opret; uint8_t evalcode = EVAL_CUSTOM;
|
CScript opret; uint8_t evalcode = EVAL_CUSTOM;
|
||||||
opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << pk);
|
opret << OP_RETURN << E_MARSHAL(ss << evalcode << funcid << pk);
|
||||||
return(opret);
|
return(opret);
|
||||||
@@ -64,25 +64,54 @@ UniValue custom_func1(uint64_t txfee,struct CCcontract_info *cp,cJSON *params)
|
|||||||
mypk = pubkey2pk(Mypubkey());
|
mypk = pubkey2pk(Mypubkey());
|
||||||
if ( AddNormalinputs(mtx,mypk,COIN+txfee,64) >= COIN+txfee ) // add utxo to mtx
|
if ( AddNormalinputs(mtx,mypk,COIN+txfee,64) >= COIN+txfee ) // add utxo to mtx
|
||||||
{
|
{
|
||||||
mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,mypk)); // make vout0
|
// make op_return payload as normal.
|
||||||
// add opreturn, change is automatically added and tx is properly signed
|
CScript opret = custom_opret('1',mypk);
|
||||||
rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,custom_opret('1',mypk));
|
std::vector<std::vector<unsigned char>> vData = std::vector<std::vector<unsigned char>>();
|
||||||
|
vData.push_back(std::vector<unsigned char>(opret.begin(), opret.end()));
|
||||||
|
// make vout0 with op_return included as payload.
|
||||||
|
mtx.vout.push_back(MakeCC1vout(cp->evalcode,amount,mypk,&vData));
|
||||||
|
fprintf(stderr, "vout size2.%li\n", mtx.vout.size());
|
||||||
|
rawtx = FinalizeCCTx(0,cp,mtx,mypk,txfee,CScript());
|
||||||
return(custom_rawtxresult(result,rawtx,broadcastflag));
|
return(custom_rawtxresult(result,rawtx,broadcastflag));
|
||||||
}
|
}
|
||||||
return(result);
|
return(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool has_opret(const CTransaction &tx, uint8_t evalcode)
|
||||||
|
{
|
||||||
|
for ( auto vout : tx.vout )
|
||||||
|
{
|
||||||
|
if ( vout.scriptPubKey[1] == evalcode )
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool custom_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx)
|
bool custom_validate(struct CCcontract_info *cp,int32_t height,Eval *eval,const CTransaction tx)
|
||||||
{
|
{
|
||||||
char expectedaddress[64]; CPubKey pk;
|
char expectedaddress[64]; CPubKey pk;
|
||||||
if ( tx.vout.size() != 2 ) // make sure the tx only has 2 outputs
|
CScript opret; int32_t numvout;
|
||||||
|
if ( !has_opret(tx, EVAL_CUSTOM) )
|
||||||
|
{
|
||||||
|
std::vector<std::vector<unsigned char>> vParams = std::vector<std::vector<unsigned char>>();
|
||||||
|
CScript dummy;
|
||||||
|
if ( tx.vout[0].scriptPubKey.IsPayToCryptoCondition(&dummy, vParams) && vParams.size() == 1 )
|
||||||
|
{
|
||||||
|
opret << E_MARSHAL(ss << vParams[0]);
|
||||||
|
}
|
||||||
|
numvout = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
opret = tx.vout[1].scriptPubKey;
|
||||||
|
numvout = 2;
|
||||||
|
}
|
||||||
|
if ( tx.vout.size() != numvout ) // make sure the tx only has appropriate outputs
|
||||||
return eval->Invalid("invalid number of vouts");
|
return eval->Invalid("invalid number of vouts");
|
||||||
else if ( custom_opretdecode(pk,tx.vout[1].scriptPubKey) != '1' ) // verify has opreturn
|
else if ( custom_opretdecode(pk,opret) != '1' ) // verify opreturn payload
|
||||||
return eval->Invalid("invalid opreturn");
|
return eval->Invalid("invalid opreturn");
|
||||||
GetCCaddress(cp,expectedaddress,pk);
|
GetCCaddress(cp,expectedaddress,pk);
|
||||||
if ( IsCClibvout(cp,tx,0,expectedaddress) == COIN ) // make sure amount and destination matches
|
if ( IsCClibvout(cp,tx,0,expectedaddress) == COIN ) // make sure amount and destination matches
|
||||||
return(true);
|
return(true);
|
||||||
else return eval->Invalid("invalid vout0 amount");
|
else return eval->Invalid("invalid vout0 amount");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -796,7 +796,7 @@ bool IsStandardTx(const CTransaction& tx, string& reason, const int nHeight)
|
|||||||
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
|
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
|
||||||
reason = "bare-multisig";
|
reason = "bare-multisig";
|
||||||
return false;
|
return false;
|
||||||
} else if (txout.scriptPubKey.IsPayToCryptoCondition() == 0 && txout.IsDust(::minRelayTxFee)) {
|
} else if (whichType != TX_CRYPTOCONDITION && txout.IsDust(::minRelayTxFee)) {
|
||||||
reason = "dust";
|
reason = "dust";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user