betprotocol functions

This commit is contained in:
Scott Sadler
2018-04-03 04:48:24 -03:00
parent 660b32c300
commit 375927407b
18 changed files with 371 additions and 55 deletions

View File

@@ -17,7 +17,7 @@ make
## Status
JSON interface not particularly safe. The rest is getting there.
JSON interface may not be particularly safe. The rest is pretty good now.
## Embedding

View File

@@ -16,7 +16,7 @@ struct CCType;
enum CCTypeId {
CC_Condition = -1,
CC_Anon = -1,
CC_Preimage = 0,
CC_Prefix = 1,
CC_Threshold = 2,

View File

@@ -17,8 +17,7 @@ static CC *mkAnon(const Condition_t *asnCond) {
printf("Unknown ASN type: %i", asnCond->present);
return 0;
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_AnonType;
CC *cond = cc_new(CC_Anon);
cond->conditionType = realType;
const CompoundSha256Condition_t *deets = &asnCond->choice.thresholdSha256;
memcpy(cond->fingerprint, deets->fingerprint.buf, 32);

View File

@@ -255,7 +255,7 @@ CC *cc_readConditionBinary(const unsigned char *cond_bin, size_t length) {
int cc_isAnon(const CC *cond) {
return cond->type->typeId == CC_Condition;
return cond->type->typeId == CC_Anon;
}
@@ -281,9 +281,10 @@ char *cc_typeName(const CC *cond) {
return cc_isAnon(cond) ? cond->conditionType->name : cond->type->name;
}
CC *cc_new(int typeId) {
CC *cond = malloc(sizeof(CC*));
cond->type = CCTypeRegistry[type];
CC *cond = calloc(1, sizeof(CC));
cond->type = CCTypeRegistry[typeId];
return cond;
}

View File

@@ -104,8 +104,7 @@ static CC *ed25519FromJSON(const cJSON *params, unsigned char *err) {
}
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_Ed25519Type;
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = pk;
cond->signature = sig;
return cond;
@@ -125,8 +124,7 @@ static void ed25519ToJSON(const CC *cond, cJSON *params) {
static CC *ed25519FromFulfillment(const Fulfillment_t *ffill) {
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_Ed25519Type;
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = malloc(32);
memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32);
cond->signature = malloc(64);

View File

@@ -42,11 +42,10 @@ static CC *evalFromJSON(const cJSON *params, unsigned char *err) {
return NULL;
}
CC *cond = calloc(1, sizeof(CC));
CC *cond = cc_new(CC_Eval);
strcpy(cond->method, method_item->valuestring);
cond->paramsBin = paramsBin;
cond->paramsBinLength = paramsBinLength;
cond->type = &CC_EvalType;
return cond;
}
@@ -64,8 +63,7 @@ static void evalToJSON(const CC *cond, cJSON *params) {
static CC *evalFromFulfillment(const Fulfillment_t *ffill) {
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_EvalType;
CC *cond = cc_new(CC_Eval);
EvalFulfillment_t *eval = &ffill->choice.evalSha256;

View File

@@ -42,8 +42,7 @@ static CC *prefixFromFulfillment(const Fulfillment_t *ffill) {
PrefixFulfillment_t *p = ffill->choice.prefixSha256;
CC *sub = fulfillmentToCC(p->subfulfillment);
if (!sub) return 0;
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_PrefixType;
CC *cond = cc_new(CC_Prefix);
cond->maxMessageLength = p->maxMessageLength;
cond->prefix = calloc(1, p->prefix.size);
memcpy(cond->prefix, p->prefix.buf, p->prefix.size);
@@ -88,8 +87,7 @@ static CC *prefixFromJSON(const cJSON *params, unsigned char *err) {
return NULL;
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_PrefixType;
CC *cond = cc_new(CC_Prefix);
cond->maxMessageLength = (unsigned long) mml_item->valuedouble;
cond->subcondition = sub;

View File

@@ -18,8 +18,7 @@ static CC *preimageFromJSON(const cJSON *params, unsigned char *err) {
}
unsigned char *preimage_b64 = preimage_item->valuestring;
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_PreimageType;
CC *cond = cc_new(CC_Preimage);
cond->preimage = base64_decode(preimage_b64, &cond->preimageLength);
return cond;
}
@@ -45,8 +44,7 @@ static unsigned char *preimageFingerprint(const CC *cond) {
static CC *preimageFromFulfillment(const Fulfillment_t *ffill) {
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_PreimageType;
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);

View File

@@ -202,8 +202,7 @@ static CC *cc_secp256k1Condition(const unsigned char *publicKey, const unsigned
memcpy(sig, signature, SECP256K1_SIG_SIZE);
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_Secp256k1Type;
CC *cond = cc_new(CC_Secp256k1);
cond->publicKey = pk;
cond->signature = sig;
return cond;

View File

@@ -119,8 +119,7 @@ static CC *thresholdFromFulfillment(const Fulfillment_t *ffill) {
}
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_ThresholdType;
CC *cond = cc_new(CC_Threshold);
cond->threshold = threshold;
cond->size = size;
cond->subconditions = subconditions;
@@ -173,8 +172,7 @@ static CC *thresholdFromJSON(const cJSON *params, unsigned char *err) {
return NULL;
}
CC *cond = calloc(1, sizeof(CC));
cond->type = &CC_ThresholdType;
CC *cond = cc_new(CC_Threshold);
cond->threshold = (long) threshold_item->valuedouble;
cond->size = cJSON_GetArraySize(subfulfillments_item);
cond->subconditions = calloc(cond->size, sizeof(CC*));