All latest updates, smart contracts, including oracle from KMD/jl777

This commit is contained in:
Michael Toutonghi
2018-09-13 13:00:13 -07:00
156 changed files with 13699 additions and 1126 deletions

View File

@@ -55,6 +55,7 @@ static void anonToJSON(const CC *cond, cJSON *params) {
static unsigned char *anonFingerprint(const CC *cond) {
unsigned char *out = calloc(1, 32);
fprintf(stderr,"anon fingerprint %p %p\n",out,cond->fingerprint);
memcpy(out, cond->fingerprint, 32);
return out;
}

View File

@@ -8,7 +8,7 @@
#define _CompoundSha256Condition_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Condition_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include "SimpleSha256Condition.h"

View File

@@ -8,7 +8,7 @@
#define _ConditionTypes_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <BIT_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Ed25519FingerprintContents_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Ed25519Sha512Fulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _EvalFulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Fulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include "PreimageFulfillment.h"

View File

@@ -5,7 +5,7 @@
#ifndef _INTEGER_H_
#define _INTEGER_H_
#include <asn_application.h>
#include "asn_application.h"
#include <asn_codecs_prim.h>
#ifdef __cplusplus

View File

@@ -12,7 +12,7 @@
#ifndef _NativeInteger_H_
#define _NativeInteger_H_
#include <asn_application.h>
#include "asn_application.h"
#include <INTEGER.h>
#ifdef __cplusplus

View File

@@ -5,7 +5,7 @@
#ifndef _OCTET_STRING_H_
#define _OCTET_STRING_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -8,7 +8,7 @@
#define _PrefixFingerprintContents_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _PrefixFulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _PreimageFulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _RsaFingerprintContents_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _RsaSha256Fulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Secp256k1FingerprintContents_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _Secp256k1Fulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _SimpleSha256Condition_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <OCTET_STRING.h>

View File

@@ -8,7 +8,7 @@
#define _ThresholdFingerprintContents_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <NativeInteger.h>

View File

@@ -8,7 +8,7 @@
#define _ThresholdFulfillment_H_
#include <asn_application.h>
#include "asn_application.h"
/* Including external dependencies */
#include <asn_SET_OF.h>

View File

@@ -5,7 +5,7 @@
#ifndef ASN_CODECS_PRIM_H
#define ASN_CODECS_PRIM_H
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -5,7 +5,7 @@
#ifndef _BER_DECODER_H_
#define _BER_DECODER_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -6,7 +6,7 @@
#ifndef _CONSTR_CHOICE_H_
#define _CONSTR_CHOICE_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -5,7 +5,7 @@
#ifndef _CONSTR_SEQUENCE_H_
#define _CONSTR_SEQUENCE_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -5,7 +5,7 @@
#ifndef _CONSTR_SET_OF_H_
#define _CONSTR_SET_OF_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -12,8 +12,8 @@
#ifndef _CONSTR_TYPE_H_
#define _CONSTR_TYPE_H_
#include <ber_tlv_length.h>
#include <ber_tlv_tag.h>
#include "ber_tlv_length.h"
#include "ber_tlv_tag.h"
#ifdef __cplusplus
extern "C" {
@@ -35,13 +35,13 @@ typedef struct asn_struct_ctx_s {
ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
} asn_struct_ctx_t;
#include <ber_decoder.h> /* Basic Encoding Rules decoder */
#include <der_encoder.h> /* Distinguished Encoding Rules encoder */
#include <xer_decoder.h> /* Decoder of XER (XML, text) */
#include <xer_encoder.h> /* Encoder into XER (XML, text) */
#include <per_decoder.h> /* Packet Encoding Rules decoder */
#include <per_encoder.h> /* Packet Encoding Rules encoder */
#include <constraints.h> /* Subtype constraints support */
#include "ber_decoder.h" /* Basic Encoding Rules decoder */
#include "der_encoder.h" /* Distinguished Encoding Rules encoder */
#include "xer_decoder.h" /* Decoder of XER (XML, text) */
#include "xer_encoder.h" /* Encoder into XER (XML, text) */
#include "per_decoder.h" /* Packet Encoding Rules decoder */
#include "per_encoder.h" /* Packet Encoding Rules encoder */
#include "constraints.h" /* Subtype constraints support */
/*
* Free the structure according to its specification.

View File

@@ -5,7 +5,7 @@
#ifndef _DER_ENCODER_H_
#define _DER_ENCODER_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -1,4 +1,4 @@
#include <asn_application.h>
#include "asn_application.h"
#include <asn_internal.h>
#include <per_decoder.h>

View File

@@ -5,8 +5,8 @@
#ifndef _PER_DECODER_H_
#define _PER_DECODER_H_
#include <asn_application.h>
#include <per_support.h>
#include "asn_application.h"
#include "per_support.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -1,4 +1,4 @@
#include <asn_application.h>
#include "asn_application.h"
#include <asn_internal.h>
#include <per_encoder.h>

View File

@@ -5,8 +5,8 @@
#ifndef _PER_ENCODER_H_
#define _PER_ENCODER_H_
#include <asn_application.h>
#include <per_support.h>
#include "asn_application.h"
#include "per_support.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -3,7 +3,7 @@
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <per_support.h>
#include "per_support.h"
#include <constr_TYPE.h>
#include <per_opentype.h>

View File

@@ -5,7 +5,7 @@
*/
#include <asn_system.h>
#include <asn_internal.h>
#include <per_support.h>
#include "per_support.h"
char *
per_data_string(asn_per_data_t *pd) {

View File

@@ -2,7 +2,7 @@
* Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_application.h>
#include "asn_application.h"
#include <asn_internal.h>
#include <xer_support.h> /* XER/XML parsing support */

View File

@@ -5,7 +5,7 @@
#ifndef _XER_DECODER_H_
#define _XER_DECODER_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -5,7 +5,7 @@
#ifndef _XER_ENCODER_H_
#define _XER_ENCODER_H_
#include <asn_application.h>
#include "asn_application.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -29,11 +29,7 @@
#include "src/json_rpc.c"
#include <cJSON.h>
#ifdef __LP64__
#include <stdlib.h>
#else
#include <malloc.h> // Index into CTransaction.vjoinsplit
#endif
struct CCType *CCTypeRegistry[] = {
@@ -75,8 +71,7 @@ char *cc_conditionUri(const CC *cond) {
unsigned char *encoded = base64_encode(fp, 32);
unsigned char *out = calloc(1, 1000);
sprintf(out, "ni:///sha-256;%s?fpt=%s&cost=%lu",
encoded, cc_typeName(cond), cc_getCost(cond));
sprintf(out, "ni:///sha-256;%s?fpt=%s&cost=%lu",encoded, cc_typeName(cond), cc_getCost(cond));
if (cond->type->getSubtypes) {
appendUriSubtypes(cond->type->getSubtypes(cond), out);
@@ -151,7 +146,6 @@ void asnCondition(const CC *cond, Condition_t *asn) {
// This may look a little weird - we dont have a reference here to the correct
// union choice for the condition type, so we just assign everything to the threshold
// type. This works out nicely since the union choices have the same binary interface.
CompoundSha256Condition_t *choice = &asn->choice.thresholdSha256;
choice->cost = cc_getCost(cond);
choice->fingerprint.buf = cond->type->fingerprint(cond);
@@ -199,7 +193,7 @@ CC *fulfillmentToCC(Fulfillment_t *ffill) {
CC *cc_readFulfillmentBinary(const unsigned char *ffill_bin, size_t ffill_bin_len) {
CC *cond = 0;
unsigned char *buf = malloc(ffill_bin_len);
unsigned char *buf = calloc(1,ffill_bin_len);
Fulfillment_t *ffill = 0;
asn_dec_rval_t rval = ber_decode(0, &asn_DEF_Fulfillment, (void **)&ffill, ffill_bin, ffill_bin_len);
if (rval.code != RC_OK) {
@@ -236,12 +230,14 @@ int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
const unsigned char *condBin, size_t condBinLength,
VerifyEval verifyEval, void *evalContext) {
unsigned char targetBinary[1000];
//fprintf(stderr,"in cc_verify cond.%p msg.%p[%d] dohash.%d condbin.%p[%d]\n",cond,msg,(int32_t)msgLength,doHashMsg,condBin,(int32_t)condBinLength);
const size_t binLength = cc_conditionBinary(cond, targetBinary);
if (0 != memcmp(condBin, targetBinary, binLength)) {
fprintf(stderr,"cc_verify error A\n");
return 0;
}
if (!cc_ed25519VerifyTree(cond, msg, msgLength)) {
fprintf(stderr,"cc_verify error B\n");
return 0;
}
@@ -250,10 +246,12 @@ int cc_verify(const struct CC *cond, const unsigned char *msg, size_t msgLength,
else memcpy(msgHash, msg, 32);
if (!cc_secp256k1VerifyTreeMsg32(cond, msgHash)) {
fprintf(stderr,"cc_verify error C\n");
return 0;
}
if (!cc_verifyEval(cond, verifyEval, evalContext)) {
//fprintf(stderr,"cc_verify error D\n");
return 0;
}
return 1;

View File

@@ -27,6 +27,7 @@ struct CCType CC_Ed25519Type;
static unsigned char *ed25519Fingerprint(const CC *cond) {
Ed25519FingerprintContents_t *fp = calloc(1, sizeof(Ed25519FingerprintContents_t));
//fprintf(stderr,"ed25519 fingerprint %p %p\n",fp,cond->publicKey);
OCTET_STRING_fromBuf(&fp->publicKey, cond->publicKey, 32);
return hashFingerprintContents(&asn_DEF_Ed25519FingerprintContents, fp);
}
@@ -62,7 +63,7 @@ static int ed25519Sign(CC *cond, CCVisitor visitor) {
if (cond->type->typeId != CC_Ed25519Type.typeId) return 1;
CCEd25519SigningData *signing = (CCEd25519SigningData*) visitor.context;
if (0 != memcmp(cond->publicKey, signing->pk, 32)) return 1;
if (!cond->signature) cond->signature = malloc(64);
if (!cond->signature) cond->signature = calloc(1,64);
ed25519_sign(cond->signature, visitor.msg, visitor.msgLength,
signing->pk, signing->skpk);
signing->nSigned++;
@@ -141,9 +142,9 @@ static void ed25519ToJSON(const CC *cond, cJSON *params) {
static CC *ed25519FromFulfillment(const Fulfillment_t *ffill) {
CC *cond = cc_new(CC_Ed25519);
cond->publicKey = malloc(32);
cond->publicKey = calloc(1,32);
memcpy(cond->publicKey, ffill->choice.ed25519Sha256.publicKey.buf, 32);
cond->signature = malloc(64);
cond->signature = calloc(1,64);
memcpy(cond->signature, ffill->choice.ed25519Sha256.signature.buf, 64);
return cond;
}

View File

@@ -27,6 +27,7 @@ struct CCType CC_EvalType;
static unsigned char *evalFingerprint(const CC *cond) {
unsigned char *hash = calloc(1, 32);
//fprintf(stderr,"evalfingerprint %p %p\n",hash,cond->code);
sha256(cond->code, cond->codeLength, hash);
return hash;
}
@@ -68,7 +69,7 @@ static CC *evalFromFulfillment(const Fulfillment_t *ffill) {
OCTET_STRING_t octets = eval->code;
cond->codeLength = octets.size;
cond->code = malloc(octets.size);
cond->code = calloc(1,octets.size);
memcpy(cond->code, octets.buf, octets.size);
return cond;

View File

@@ -28,7 +28,7 @@ extern "C" {
#endif
#define BUF_SIZE 1024 * 1024
#define BUF_SIZE 4096
typedef char bool;
@@ -56,8 +56,8 @@ typedef struct CCType {
/*
* Globals
*/
struct CCType *CCTypeRegistry[32];
int CCTypeRegistryLength;
extern struct CCType *CCTypeRegistry[];
extern int CCTypeRegistryLength;
/*

View File

@@ -17,11 +17,8 @@
#include "internal.h"
#include <cJSON.h>
#ifdef __LP64__
#include <stdlib.h>
#else
#include <malloc.h> // Index into CTransaction.vjoinsplit
#endif
static cJSON *jsonCondition(CC *cond) {
cJSON *root = cJSON_CreateObject();

View File

@@ -26,7 +26,7 @@ struct CCType CC_PrefixType;
static int prefixVisitChildren(CC *cond, CCVisitor visitor) {
size_t prefixedLength = cond->prefixLength + visitor.msgLength;
unsigned char *prefixed = malloc(prefixedLength);
unsigned char *prefixed = calloc(1,prefixedLength);
memcpy(prefixed, cond->prefix, cond->prefixLength);
memcpy(prefixed + cond->prefixLength, visitor.msg, visitor.msgLength);
visitor.msg = prefixed;
@@ -39,6 +39,7 @@ static int prefixVisitChildren(CC *cond, CCVisitor visitor) {
static unsigned char *prefixFingerprint(const CC *cond) {
PrefixFingerprintContents_t *fp = calloc(1, sizeof(PrefixFingerprintContents_t));
//fprintf(stderr,"prefixfinger %p %p\n",fp,cond->prefix);
asnCondition(cond->subcondition, &fp->subcondition); // TODO: check asnCondition for safety
fp->maxMessageLength = cond->maxMessageLength;
OCTET_STRING_fromBuf(&fp->prefix, cond->prefix, cond->prefixLength);

View File

@@ -46,6 +46,7 @@ static unsigned long preimageCost(const CC *cond) {
static unsigned char *preimageFingerprint(const CC *cond) {
unsigned char *hash = calloc(1, 32);
//fprintf(stderr,"preimage %p %p\n",hash,cond->preimage);
sha256(cond->preimage, cond->preimageLength, hash);
return hash;
}

View File

@@ -45,9 +45,11 @@ void lockSign() {
int read = (int) fread(&ent, 1, 32, fp);
fclose(fp);
#endif
if (read != 32) {
fprintf(stderr, "Could not read 32 bytes entropy from system\n");
exit(1);
if (read != 32)
{
int32_t i;
for (i=0; i<32; i++)
((uint8_t *)ent)[i] = rand();
}
if (!secp256k1_context_randomize(ec_ctx_sign, ent)) {
fprintf(stderr, "Could not randomize secp256k1 context\n");
@@ -73,6 +75,7 @@ void initVerify() {
static unsigned char *secp256k1Fingerprint(const CC *cond) {
Secp256k1FingerprintContents_t *fp = calloc(1, sizeof(Secp256k1FingerprintContents_t));
//fprintf(stderr,"secpfinger %p %p size %d vs %d\n",fp,cond->publicKey,(int32_t)sizeof(Secp256k1FingerprintContents_t),(int32_t)SECP256K1_PK_SIZE);
OCTET_STRING_fromBuf(&fp->publicKey, cond->publicKey, SECP256K1_PK_SIZE);
return hashFingerprintContents(&asn_DEF_Secp256k1FingerprintContents, fp);
}

View File

@@ -86,6 +86,7 @@ static int cmpConditionBin(const void *a, const void *b) {
static unsigned char *thresholdFingerprint(const CC *cond) {
/* Create fingerprint */
ThresholdFingerprintContents_t *fp = calloc(1, sizeof(ThresholdFingerprintContents_t));
//fprintf(stderr,"thresholdfinger %p\n",fp);
fp->threshold = cond->threshold;
for (int i=0; i<cond->size; i++) {
Condition_t *asnCond = asnConditionNew(cond->subconditions[i]);

View File

@@ -39,7 +39,7 @@ static int mod_table[] = {0, 2, 1};
void build_decoding_table() {
decoding_table = malloc(256);
decoding_table = calloc(1,256);
for (int i = 0; i < 64; i++)
decoding_table[(unsigned char) encoding_table[i]] = i;
}
@@ -49,7 +49,7 @@ unsigned char *base64_encode(const unsigned char *data, size_t input_length) {
size_t output_length = 4 * ((input_length + 2) / 3);
unsigned char *encoded_data = malloc(output_length + 1);
unsigned char *encoded_data = calloc(1,output_length + 1);
if (encoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
@@ -90,7 +90,7 @@ unsigned char *base64_decode(const unsigned char *data_,
size_t input_length = strlen(data_);
int rem = input_length % 4;
unsigned char *data = malloc(input_length + (4-rem));
unsigned char *data = calloc(1,input_length + (4-rem));
strcpy(data, data_);
// for unpadded b64
@@ -111,7 +111,7 @@ unsigned char *base64_decode(const unsigned char *data_,
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = malloc(*output_length);
unsigned char *decoded_data = calloc(1,*output_length);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
@@ -137,6 +137,7 @@ unsigned char *base64_decode(const unsigned char *data_,
void base64_cleanup() {
free(decoding_table);
decoding_table = 0;
}
@@ -217,7 +218,7 @@ unsigned char *hashFingerprintContents(asn_TYPE_descriptor_t *asnType, void *fp)
fprintf(stderr, "Encoding fingerprint failed\n");
return 0;
}
unsigned char *hash = malloc(32);
unsigned char *hash = calloc(1,32);
sha256(buf, rc.encoded, hash);
return hash;
}
@@ -225,7 +226,7 @@ unsigned char *hashFingerprintContents(asn_TYPE_descriptor_t *asnType, void *fp)
char* cc_hex_encode(const uint8_t *bin, size_t len)
{
char* hex = malloc(len*2+1);
char* hex = calloc(1,len*2+1);
if (bin == NULL) return hex;
char map[16] = "0123456789ABCDEF";
for (int i=0; i<len; i++) {