]>
Commit | Line | Data |
---|---|---|
563581af SS |
1 | #include "cryptoconditions/include/cryptoconditions.h" |
2 | #include "komodo_cc.h" | |
3 | ||
4 | ||
5 | bool IsCryptoConditionsEnabled() | |
6 | { | |
7 | return 0 != ASSETCHAINS_CC; | |
8 | } | |
9 | ||
563581af SS |
10 | |
11 | bool IsSupportedCryptoCondition(const CC *cond) | |
12 | { | |
13 | int mask = cc_typeMask(cond); | |
14 | ||
15 | if (mask & ~CCEnabledTypes) return false; | |
16 | ||
17 | // Also require that the condition have at least one signable node | |
18 | if (!(mask & CCSigningNodes)) return false; | |
19 | ||
20 | return true; | |
21 | } | |
22 | ||
23 | ||
24 | bool IsSignedCryptoCondition(const CC *cond) | |
25 | { | |
26 | if (!cc_isFulfilled(cond)) return false; | |
27 | if (1 << cc_typeId(cond) & CCSigningNodes) return true; | |
28 | if (cc_typeId(cond) == CC_Threshold) | |
29 | for (int i=0; i<cond->size; i++) | |
30 | if (IsSignedCryptoCondition(cond->subconditions[i])) return true; | |
31 | return false; | |
32 | } | |
561f3e18 SS |
33 | |
34 | ||
35 | ||
36 | CScript CCPubKey(const CC *cond) | |
37 | { | |
38 | unsigned char buf[1000]; | |
39 | size_t len = cc_conditionBinary(cond, buf); | |
40 | return CScript() << std::vector<unsigned char>(buf, buf+len) << OP_CHECKCRYPTOCONDITION; | |
41 | } | |
42 | ||
43 | ||
44 | CScript CCSig(const CC *cond) | |
45 | { | |
46 | unsigned char buf[1000]; | |
47 | size_t len = cc_fulfillmentBinary(cond, buf, 1000); | |
48 | auto ffill = std::vector<unsigned char>(buf, buf+len); | |
49 | ffill.push_back(1); // SIGHASH_ALL | |
50 | return CScript() << ffill; | |
51 | } | |
52 | ||
53 | ||
54 | std::string CCShowStructure(CC *cond) | |
55 | { | |
56 | std::string out; | |
57 | if (cc_isAnon(cond)) { | |
58 | out = "A" + std::to_string(cc_typeId(cond)); | |
59 | } | |
60 | else if (cc_typeId(cond) == CC_Threshold) { | |
61 | out += "(" + std::to_string(cond->threshold) + " of "; | |
62 | for (int i=0; i<cond->size; i++) { | |
63 | out += CCShowStructure(cond->subconditions[i]); | |
64 | if (i < cond->size - 1) out += ","; | |
65 | } | |
66 | out += ")"; | |
67 | } | |
68 | else { | |
69 | out = std::to_string(cc_typeId(cond)); | |
70 | } | |
71 | return out; | |
72 | } | |
73 | ||
74 | ||
75 | CC* CCPrune(CC *cond) | |
76 | { | |
77 | std::vector<unsigned char> ffillBin; | |
78 | GetPushData(CCSig(cond), ffillBin); | |
79 | return cc_readFulfillmentBinary(ffillBin.data(), ffillBin.size()-1); | |
80 | } | |
81 | ||
82 | ||
83 | bool GetPushData(const CScript &sig, std::vector<unsigned char> &data) | |
84 | { | |
85 | opcodetype opcode; | |
86 | auto pc = sig.begin(); | |
87 | if (sig.GetOp(pc, opcode, data)) return opcode > OP_0 && opcode <= OP_PUSHDATA4; | |
88 | return false; | |
89 | } | |
90 | ||
91 | ||
92 | bool GetOpReturnData(const CScript &sig, std::vector<unsigned char> &data) | |
93 | { | |
94 | auto pc = sig.begin(); | |
95 | opcodetype opcode; | |
96 | if (sig.GetOp2(pc, opcode, NULL)) | |
97 | if (opcode == OP_RETURN) | |
98 | if (sig.GetOp(pc, opcode, data)) | |
99 | return opcode > OP_0 && opcode <= OP_PUSHDATA4; | |
100 | return false; | |
101 | } |