2015-09-24 07:15:28 +02:00
|
|
|
#include "nameclaim.h"
|
2015-12-23 01:11:50 +01:00
|
|
|
#include "hash.h"
|
2015-02-03 02:57:53 +01:00
|
|
|
#include "util.h"
|
2015-01-15 01:17:19 +01:00
|
|
|
|
2015-10-01 08:37:47 +02:00
|
|
|
std::vector<unsigned char> uint32_t_to_vch(uint32_t n)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> vchN;
|
|
|
|
vchN.resize(4);
|
|
|
|
vchN[0] = n >> 24;
|
|
|
|
vchN[1] = n >> 16;
|
|
|
|
vchN[2] = n >> 8;
|
|
|
|
vchN[3] = n;
|
|
|
|
return vchN;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t vch_to_uint32_t(std::vector<unsigned char>& vchN)
|
|
|
|
{
|
|
|
|
uint32_t n;
|
|
|
|
if (vchN.size() != 4)
|
|
|
|
{
|
|
|
|
LogPrintf("%s() : a vector<unsigned char> with size other than 4 has been given", __func__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
n = vchN[0] << 24 | vchN[1] << 16 | vchN[2] << 8 | vchN[3];
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:15:28 +02:00
|
|
|
bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
CScript::const_iterator pc = scriptIn.begin();
|
2015-09-24 07:15:28 +02:00
|
|
|
return DecodeClaimScript(scriptIn, op, vvchParams, pc);
|
2015-01-15 01:17:19 +01:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:15:28 +02:00
|
|
|
bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams, CScript::const_iterator& pc)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
opcodetype opcode;
|
|
|
|
if (!scriptIn.GetOp(pc, opcode))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-25 23:09:21 +02:00
|
|
|
|
2015-12-23 01:11:50 +01:00
|
|
|
if (opcode != OP_CLAIM_NAME && opcode != OP_SUPPORT_CLAIM && opcode != OP_UPDATE_CLAIM)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
op = opcode;
|
|
|
|
|
2015-09-25 23:09:21 +02:00
|
|
|
std::vector<unsigned char> vchParam1;
|
|
|
|
std::vector<unsigned char> vchParam2;
|
|
|
|
std::vector<unsigned char> vchParam3;
|
|
|
|
// Valid formats:
|
|
|
|
// OP_CLAIM_NAME vchName vchValue OP_2DROP OP_DROP pubkeyscript
|
2015-12-23 01:11:50 +01:00
|
|
|
// OP_UPDATE_CLAIM vchName vchClaimId vchValue OP_2DROP OP_2DROP pubkeyscript
|
|
|
|
// OP_SUPPORT_CLAIM vchName vchClaimId OP_2DROP OP_DROP pubkeyscript
|
2015-01-15 01:17:19 +01:00
|
|
|
// All others are invalid.
|
|
|
|
|
2015-09-25 23:09:21 +02:00
|
|
|
if (!scriptIn.GetOp(pc, opcode, vchParam1) || opcode < 0 || opcode > OP_PUSHDATA4)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-25 23:09:21 +02:00
|
|
|
if (!scriptIn.GetOp(pc, opcode, vchParam2) || opcode < 0 || opcode > OP_PUSHDATA4)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-23 01:11:50 +01:00
|
|
|
if (op == OP_UPDATE_CLAIM || op == OP_SUPPORT_CLAIM)
|
2015-09-25 23:09:21 +02:00
|
|
|
{
|
2015-12-23 01:11:50 +01:00
|
|
|
if (vchParam2.size() != 160/8)
|
2015-10-01 08:37:47 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-23 01:11:50 +01:00
|
|
|
}
|
|
|
|
if (op == OP_UPDATE_CLAIM)
|
|
|
|
{
|
|
|
|
if (!scriptIn.GetOp(pc, opcode, vchParam3) || opcode < 0 || opcode > OP_PUSHDATA4)
|
2015-09-25 23:09:21 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 01:17:19 +01:00
|
|
|
if (!scriptIn.GetOp(pc, opcode) || opcode != OP_2DROP)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-23 01:11:50 +01:00
|
|
|
if (!scriptIn.GetOp(pc, opcode))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((op == OP_CLAIM_NAME || op == OP_SUPPORT_CLAIM) && opcode != OP_DROP)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if ((op == OP_UPDATE_CLAIM) && opcode != OP_2DROP)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:09:21 +02:00
|
|
|
vvchParams.push_back(vchParam1);
|
|
|
|
vvchParams.push_back(vchParam2);
|
2015-12-23 01:11:50 +01:00
|
|
|
if (op == OP_UPDATE_CLAIM)
|
2015-09-25 23:09:21 +02:00
|
|
|
{
|
|
|
|
vvchParams.push_back(vchParam3);
|
|
|
|
}
|
2015-01-15 01:17:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-23 01:11:50 +01:00
|
|
|
uint160 ClaimIdHash(const uint256& txhash, uint32_t nOut)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> claimToHash(txhash.begin(), txhash.end());
|
|
|
|
std::vector<unsigned char> vchnOut = uint32_t_to_vch(nOut);
|
|
|
|
claimToHash.insert(claimToHash.end(), vchnOut.begin(), vchnOut.end());
|
|
|
|
return Hash160(claimToHash);
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:15:28 +02:00
|
|
|
CScript StripClaimScriptPrefix(const CScript& scriptIn)
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
int op;
|
2015-10-01 08:37:47 +02:00
|
|
|
return StripClaimScriptPrefix(scriptIn, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
CScript StripClaimScriptPrefix(const CScript& scriptIn, int& op)
|
|
|
|
{
|
2015-01-15 01:17:19 +01:00
|
|
|
std::vector<std::vector<unsigned char> > vvchParams;
|
|
|
|
CScript::const_iterator pc = scriptIn.begin();
|
|
|
|
|
2015-09-24 07:15:28 +02:00
|
|
|
if (!DecodeClaimScript(scriptIn, op, vvchParams, pc))
|
2015-01-15 01:17:19 +01:00
|
|
|
{
|
|
|
|
return scriptIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CScript(pc, scriptIn.end());
|
|
|
|
}
|
2015-01-30 16:10:36 +01:00
|
|
|
|
2016-04-12 02:48:16 +02:00
|
|
|
size_t ClaimScriptSize(const CScript& scriptIn)
|
|
|
|
{
|
|
|
|
CScript strippedScript = StripClaimScriptPrefix(scriptIn);
|
|
|
|
return scriptIn.size() - strippedScript.size();
|
|
|
|
}
|
|
|
|
|