forked from chips-blockchain/chips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc.cpp
More file actions
141 lines (109 loc) · 3.25 KB
/
cc.cpp
File metadata and controls
141 lines (109 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cryptoconditions.h>
#include <script/cc.h>
uint32_t ASSETCHAINS_CC = 0;
bool IsCryptoConditionsEnabled()
{
return 0 != ASSETCHAINS_CC;
}
bool IsSupportedCryptoCondition(const CC *cond)
{
int mask = cc_typeMask(cond);
if (mask & ~CCEnabledTypes) return false;
// Also require that the condition have at least one signable node
if (!(mask & CCSigningNodes)) return false;
return true;
}
bool IsSignedCryptoCondition(const CC *cond)
{
if (!cc_isFulfilled(cond)) return false;
if (1 << cc_typeId(cond) & CCSigningNodes) return true;
if (cc_typeId(cond) == CC_Threshold)
for (int i=0; i<cond->size; i++)
if (IsSignedCryptoCondition(cond->subconditions[i])) return true;
return false;
}
static unsigned char* CopyPubKey(CPubKey pkIn)
{
unsigned char* pk = (unsigned char*) malloc(33);
memcpy(pk, pkIn.begin(), 33); // TODO: compressed?
return pk;
}
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)
{
unsigned char buf[1000];
size_t len = cc_conditionBinary(cond, buf);
return CScript() << std::vector<unsigned char>(buf, buf+len) << OP_CHECKCRYPTOCONDITION;
}
CScript CCSig(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 CScript() << ffill;
}
std::string CCShowStructure(CC *cond)
{
std::string out;
if (cc_isAnon(cond)) {
out = "A" + std::to_string(cc_typeId(cond));
}
else if (cc_typeId(cond) == CC_Threshold) {
out += "(" + std::to_string(cond->threshold) + " of ";
for (int i=0; i<cond->size; i++) {
out += CCShowStructure(cond->subconditions[i]);
if (i < cond->size - 1) out += ",";
}
out += ")";
}
else {
out = std::to_string(cc_typeId(cond));
}
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;
auto pc = sig.begin();
if (sig.GetOp(pc, opcode, data)) return opcode > OP_0 && opcode <= OP_PUSHDATA4;
return false;
}
bool GetOpReturnData(const CScript &sig, std::vector<unsigned char> &data)
{
auto pc = sig.begin();
opcodetype opcode;
if (sig.GetOp2(pc, opcode, NULL))
if (opcode == OP_RETURN)
if (sig.GetOp(pc, opcode, data))
return opcode > OP_0 && opcode <= OP_PUSHDATA4;
return false;
}