More work on CoinbaseGuard and validation

This commit is contained in:
miketout
2018-10-03 16:26:41 -07:00
parent aa3ce4242f
commit 905fe35e50
8 changed files with 224 additions and 53 deletions

View File

@@ -20,6 +20,7 @@
#define OPRETTYPE_TIMELOCK 1
#define OPRETTYPE_STAKEPARAMS 2
#define OPRETTYPE_STAKECHEAT 3
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes

View File

@@ -19,6 +19,94 @@ typedef vector<unsigned char> valtype;
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
COptCCParams::COptCCParams(std::vector<unsigned char> &vch)
{
CScript inScr = CScript(vch.begin(), vch.end());
if (inScr.size() > 1)
{
CScript::const_iterator pc = inScr.begin();
opcodetype opcode;
std::vector<std::vector<unsigned char>> data;
std::vector<unsigned char> param;
bool valid = true;
while (pc < inScr.end())
{
param.clear();
if (inScr.GetOp(pc, opcode, param))
{
if (opcode > 0 && opcode <= OP_PUSHDATA4 && param.size() > 0)
{
data.push_back(param);
}
else
{
valid = false;
break;
}
}
}
if (valid && pc == inScr.end() && data.size() > 0)
{
version = 0;
param = data[0];
if (param.size() == 4)
{
version = param[0];
evalCode = param[1];
n = param[2];
m = param[3];
if (version != VERSION || m != 1 || (n != 1 && n != 2) || data.size() <= n)
{
// we only support one version, and 1 of 1 or 1 of 2 now, so set invalid
version = 0;
}
else
{
// load keys and data
vKeys.clear();
vData.clear();
int i;
for (i = 1; i <= n; i++)
{
vKeys.push_back(CPubKey(data[i]));
if (!vKeys[vKeys.size() - 1].IsValid())
{
version = 0;
break;
}
}
if (version != 0)
{
// get the rest of the data
for ( ; i < data.size(); i++)
{
vData.push_back(data[i]);
}
}
}
}
}
}
}
std::vector<unsigned char> COptCCParams::AsVector()
{
CScript cData = CScript();
cData << std::vector<unsigned char>({version, evalCode, n, m});
for (auto k : vKeys)
{
cData << std::vector<unsigned char>(k.begin(), k.end());
}
for (auto d : vData)
{
cData << std::vector<unsigned char>(d);
}
return std::vector<unsigned char>(cData.begin(), cData.end());
}
CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
const char* GetTxnOutputType(txnouttype t)

View File

@@ -91,34 +91,19 @@ class COptCCParams
uint8_t version;
uint8_t evalCode;
uint8_t m, n; // for m of n sigs required, n pub keys for sigs will follow
std::vector<CPubKey> vKeys; // n public keys to aid in signing
std::vector<std::vector<unsigned char>> vData; // extra parameters
COptCCParams() : version(0), evalCode(0), n(0), m(0) {}
COptCCParams(uint8_t ver, uint8_t code, uint8_t _n, uint8_t _m) : version(ver), evalCode(code), n(_n), m(_m) {}
COptCCParams(uint8_t ver, uint8_t code, uint8_t _n, uint8_t _m, std::vector<CPubKey> &vkeys, std::vector<std::vector<unsigned char>> &vdata) :
version(ver), evalCode(code), n(_n), m(_m), vKeys(vkeys), vData(vdata) {}
COptCCParams(std::vector<unsigned char> &vch)
{
version = 0;
if (vch.size() == 4)
{
version = vch[0];
evalCode = vch[1];
n = vch[2];
m = vch[3];
if (version != VERSION && m == 1 && (n == 1 || n == 2))
{
// we only support one version, and 1 of 1 or 1 of 2 now, so set invalid
version = 0;
}
}
}
COptCCParams(std::vector<unsigned char> &vch);
bool IsValid() { return version != 0; }
std::vector<unsigned char> AsVector()
{
std::vector<unsigned char> vch = std::vector<unsigned char>({version, evalCode, n, m});
}
std::vector<unsigned char> AsVector();
};
/** Check whether a CTxDestination is a CNoDestination. */