Repository navigation
Expand file tree
/
Copy pathpreimage.c
More file actions
74 lines (51 loc) · 1.98 KB
/
Copy pathpreimage.c
File metadata and controls
74 lines (51 loc) · 1.98 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
#include "asn/Condition.h"
#include "asn/Fulfillment.h"
#include "asn/OCTET_STRING.h"
#include "include/cJSON.h"
#include "include/sha256.h"
#include "cryptoconditions.h"
struct CCType CC_PreimageType;
static CC *preimageFromJSON(const cJSON *params, char *err) {
CC *cond = cc_new(CC_Preimage);
if (!jsonGetBase64(params, "preimage", err, &cond->preimage, &cond->preimageLength)) {
free(cond);
return NULL;
}
return cond;
}
static void preimageToJSON(const CC *cond, cJSON *params) {
jsonAddBase64(params, "preimage", cond->preimage, cond->preimageLength);
}
static unsigned long preimageCost(const CC *cond) {
return (unsigned long) cond->preimageLength;
}
static unsigned char *preimageFingerprint(const CC *cond) {
unsigned char *hash = calloc(1, 32);
sha256(cond->preimage, cond->preimageLength, hash);
return hash;
}
static CC *preimageFromFulfillment(const Fulfillment_t *ffill) {
CC *cond = cc_new(CC_Preimage);
PreimageFulfillment_t p = ffill->choice.preimageSha256;
cond->preimage = calloc(1, p.preimage.size);
memcpy(cond->preimage, p.preimage.buf, p.preimage.size);
cond->preimageLength = p.preimage.size;
return cond;
}
static Fulfillment_t *preimageToFulfillment(const CC *cond) {
Fulfillment_t *ffill = calloc(1, sizeof(Fulfillment_t));
ffill->present = Fulfillment_PR_preimageSha256;
PreimageFulfillment_t *pf = &ffill->choice.preimageSha256;
OCTET_STRING_fromBuf(&pf->preimage, cond->preimage, cond->preimageLength);
return ffill;
}
int preimageIsFulfilled(const CC *cond) {
return 1;
}
static void preimageFree(CC *cond) {
free(cond->preimage);
}
static uint32_t preimageSubtypes(const CC *cond) {
return 0;
}
struct CCType CC_PreimageType = { 0, "preimage-sha-256", Condition_PR_preimageSha256, 0, &preimageFingerprint, &preimageCost, &preimageSubtypes, &preimageFromJSON, &preimageToJSON, &preimageFromFulfillment, &preimageToFulfillment, &preimageIsFulfilled, &preimageFree };