Skip to content

Commit

Permalink
Merge pull request #151 from jl777/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
jl777 authored Nov 13, 2019
2 parents 089e489 + 089ef73 commit 6dcf98d
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bitcoin-send-tx
bitcoin-spv
bitcoin-txref
nspv
.vscode

Makefile
configure
Expand Down
16 changes: 10 additions & 6 deletions coins
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
"magic":"f60d75fd",
"nSPV": "27.102.107.29, 27.102.107.28, 27.102.107.27"
},
{
"coin": "BTCH",
"asset": "BTCH",
"fname": "Bitcoin Hush",
"rpcport": 8800,
"mm2": 1,
"p2p": 8799,
"magic":"f41c5eff",
"nSPV":"136.243.58.134"
},
{
"coin": "NSPV",
"asset": "NSPV",
Expand Down Expand Up @@ -2100,12 +2110,6 @@
"wiftype": 151,
"txfee": 100000
},
{
"coin": "BTCH",
"asset": "BTCH",
"fname": "Bitcoin Hush",
"rpcport": 8800
},
{
"coin": "ETOMIC",
"asset": "ETOMIC",
Expand Down
2 changes: 2 additions & 0 deletions include/btc/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ enum opcodetype {
OP_CHECKSIGVERIFY = 0xad,
OP_CHECKMULTISIG = 0xae,
OP_CHECKMULTISIGVERIFY = 0xaf,
OP_CHECKCRYPTOCONDITION = 0xcc,
OP_CHECKCRYPTOCONDITIONVERIFY = 0xcd,

// expansion
OP_NOP1 = 0xb0,
Expand Down
11 changes: 11 additions & 0 deletions include/nSPV_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ struct rpcrequest_info
#define NSPV_TXIDSRESP 0x0f
#define NSPV_MEMPOOL 0x10
#define NSPV_MEMPOOLRESP 0x11
#define NSPV_CCMODULEUTXOS 0x12
#define NSPV_CCMODULEUTXOSRESP 0x13
#define NSPV_MEMPOOL_ALL 0
#define NSPV_MEMPOOL_ADDRESS 1
#define NSPV_MEMPOOL_ISSPENT 2
#define NSPV_MEMPOOL_INMEMPOOL 3
#define NSPV_MEMPOOL_CCEVALCODE 4
#define NSPV_CC_TXIDS 16
#define NSPV_REMOTERPC 0x14
#define NSPV_REMOTERPCRESP 0x15

#define COIN SATOSHIDEN

Expand Down Expand Up @@ -221,6 +226,12 @@ struct NSPV_CCmtxinfo
struct NSPV_utxoresp used[NSPV_MAXVINS];
};

struct NSPV_remoterpcresp
{
char method[64];
char json[11000];
};

struct NSPV_header
{
int32_t height;
Expand Down
34 changes: 26 additions & 8 deletions src/tools/cryptoconditions/src/cryptoconditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,32 @@ size_t cc_fulfillmentBinary(const CC *cond, unsigned char *buf, size_t length) {
void asnCondition(const CC *cond, Condition_t *asn) {
asn->present = cc_isAnon(cond) ? cond->conditionType->asnType : cond->type->asnType;

// 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);
choice->fingerprint.size = 32;
choice->subtypes = asnSubtypes(cond->type->getSubtypes(cond));
// Fixed previous implementation as it was treating every asn as thresholdSha256 type and it was memory leaking
// because SimpleSha256Condition_t types do not have subtypes so it couldn't free it in the end.
int typeId=cond->type->typeId;
if (asn->present==Condition_PR_thresholdSha256 || asn->present==Condition_PR_prefixSha256)
{
CompoundSha256Condition_t *sequence=asn->present==Condition_PR_thresholdSha256?&asn->choice.thresholdSha256:&asn->choice.prefixSha256;
sequence->cost = cc_getCost(cond);
sequence->fingerprint.buf = cond->type->fingerprint(cond);
sequence->fingerprint.size = 32;
sequence->subtypes = asnSubtypes(cond->type->getSubtypes(cond));
}
else
{
SimpleSha256Condition_t *choice;
switch (asn->present)
{
case Condition_PR_preimageSha256: choice = &asn->choice.preimageSha256; break;
case Condition_PR_rsaSha256: choice = &asn->choice.rsaSha256; break;
case Condition_PR_ed25519Sha256: choice = &asn->choice.ed25519Sha256; break;
case Condition_PR_secp256k1Sha256: choice = &asn->choice.secp256k1Sha256; break;
case Condition_PR_evalSha256: choice = &asn->choice.evalSha256; break;
};
choice->cost = cc_getCost(cond);
choice->fingerprint.buf = cond->type->fingerprint(cond);
choice->fingerprint.size = 32;
}
}


Expand Down
1 change: 1 addition & 0 deletions src/tools/cryptoconditions/src/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ int cc_signTreeSecp256k1Msg32(CC *cond, const unsigned char *privateKey, const u
unsigned char publicKey[SECP256K1_PK_SIZE];
size_t ol = SECP256K1_PK_SIZE;
secp256k1_ec_pubkey_serialize(ec_ctx_verify, publicKey, &ol, &spk, SECP256K1_EC_COMPRESSED);
if ( 0 )
{
int32_t z;
for (z=0; z<33; z++)
Expand Down
6 changes: 5 additions & 1 deletion src/tools/cryptoconditions/src/threshold.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ static CC *thresholdFromJSON(const cJSON *params, char *err) {
for (int i=0; i<cond->size; i++) {
sub = cJSON_GetArrayItem(subfulfillments_item, i);
cond->subconditions[i] = cc_conditionFromJSON(sub, err);
if (err[0]) return NULL;
if (err[0] || cond->subconditions[i]==NULL)
{
if (cond) cc_free(cond);
return NULL;
}
}

return cond;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cryptoconditions/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ unsigned char *base64_decode(const unsigned char *data_,
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}

free(data);
return decoded_data;
}

Expand Down
201 changes: 201 additions & 0 deletions src/tools/nSPV_CCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#ifndef NSPV_CCUTILS_H
#define NSPV_CCUTILS_H

#include "tools/cryptoconditions/include/cryptoconditions.h"
#include "../include/btc/script.h"

#define CC_MAXVINS 1024

#define EVAL_FAUCET (0xe4)
const char *FaucetCCaddr = "R9zHrofhRbub7ER77B7NrVch3A63R39GuC";
const char *FaucetNormaladdr = "RKQV4oYs4rvxAWx1J43VnT73rSTVtUeckk";
char FaucetCChexstr[67] = { "03682b255c40d0cde8faee381a1a50bbb89980ff24539cb8518e294d3a63cefe12" };
uint8_t FaucetCCpriv[32] = { 0xd4, 0x4f, 0xf2, 0x31, 0x71, 0x7d, 0x28, 0x02, 0x4b, 0xc7, 0xdd, 0x71, 0xa0, 0x39, 0xc4, 0xbe, 0x1a, 0xfe, 0xeb, 0xc2, 0x46, 0xda, 0x76, 0xf8, 0x07, 0x53, 0x3d, 0x96, 0xb4, 0xca, 0xa0, 0xe9 };

typedef struct _CCSigData {
struct CC *cond; // pointer to cryptocondition
uint64_t voutValue;
cstring * voutScriptPubkey;
int32_t vini;
bool isCC;
} CCSigData;


void endiancpy(uint8_t *dest,uint8_t *src,int32_t len)
{
int32_t i,j=0;
#if defined(WORDS_BIGENDIAN)
for (i=31; i>=0; i--)
dest[j++] = src[i];
#else
memcpy(dest,src,len);
#endif
}

CC* CCNewEval(char *code,int32_t size)
{
CC *cond = cc_new(CC_Eval);
cond->code = (uint8_t*) malloc(size);
memcpy(cond->code, code, size);
cond->codeLength = size;
return cond;
}

CC* CCNewThreshold(int t, CC** v, int size)
{
CC *cond = cc_new(CC_Threshold);
cond->threshold = t;
cond->size = size;
cond->subconditions = (CC**) calloc(size, sizeof(CC*));
memcpy(cond->subconditions, v, size * sizeof(CC*));
return cond;
}

static unsigned char* CopyPubKey(uint8_t *pkIn)
{
unsigned char* pk = (unsigned char*) malloc(33);
memcpy(pk, pkIn, 33);
return pk;
}

CC* CCNewSecp256k1(uint8_t* k)
{
CC *cond = cc_new(CC_Secp256k1);
cond->publicKey = CopyPubKey(k);
return cond;
}

CC *MakeCCcond1(uint8_t evalcode,uint8_t *pk)
{
cstring *ss;
CC *c[1]={CCNewSecp256k1(pk)};
CC **pks=c;
ss=cstr_new_sz(1);
ser_varlen(ss,evalcode);
CC *condCC = CCNewEval(ss->str,ss->len);
CC *Sig = CCNewThreshold(1, pks, 1);
CC *v[2]= {condCC, Sig};
CC* cond=CCNewThreshold(2, v ,2);
cstr_free(ss, true);
cc_free(condCC);
cc_free(Sig);
cc_free(*pks);
return cond;
}

CC *MakeCCcond1of2(uint8_t evalcode,uint8_t *pk1, uint8_t *pk2)
{
cstring *ss;

CC *c[2]={CCNewSecp256k1(pk1),CCNewSecp256k1(pk2)};

CC **pks=c;
ss=cstr_new_sz(1);
ser_varlen(ss,evalcode);
CC *condCC = CCNewEval(ss->str,ss->len);
CC *Sig = CCNewThreshold(1, pks, 2);
CC *v[2]= {condCC, Sig};
CC* cond=CCNewThreshold(2, v ,2);
cstr_free(ss, true);
cc_free(condCC);
cc_free(Sig);
cc_free(*pks);
return cond;
}

void SerializeScript(cstring *script,unsigned char* buf, size_t len)
{
if (len < OP_PUSHDATA1) ser_varlen(script,len);
else if (len <= 0xFF)
{
ser_varlen(script,OP_PUSHDATA1);
ser_bytes(script,&len,1);
}
else if (len <= 0xFFFF)
{
ser_varlen(script,OP_PUSHDATA2);
ser_u16(script,len);
}
else
{
ser_varlen(script,OP_PUSHDATA4);
ser_u32(script,len);
}
ser_bytes(script,buf,len);
return;
}
cstring* CCPubKey(const CC *cond)
{
unsigned char buf[1000],ss[1024]; int32_t n=0;

size_t len = cc_conditionBinary(cond, buf);
cstring* ccpk = cstr_new_sz(len+24);
SerializeScript(ccpk,buf,len);
unsigned char c=OP_CHECKCRYPTOCONDITION;
ser_bytes(ccpk,&c,1);
return ccpk;
}

void CCSig(const CC *cond,cstring *script)
{
unsigned char buf[10001];
size_t len = cc_fulfillmentBinary(cond, buf, 10000);
buf[len++]=SIGHASH_ALL;
SerializeScript(script,buf,len);
return;
}

btc_tx_out *MakeCC1vout(uint8_t evalcode, uint64_t nValue,uint8_t *pk)
{
CC *payoutCond = MakeCCcond1(evalcode,pk);
btc_tx_out *vout = btc_tx_out_new();
vout->script_pubkey = CCPubKey(payoutCond);
vout->value = nValue;
cc_free(payoutCond);
return(vout);
}

btc_tx_out *MakeCC1of2vout(uint8_t evalcode,uint64_t nValue,uint8_t *pk1,uint8_t *pk2)
{
btc_tx_out *vout = btc_tx_out_new();
CC *payoutCond = MakeCCcond1of2(evalcode,pk1,pk2);
vout->script_pubkey = CCPubKey(payoutCond);
vout->value = nValue;
cc_free(payoutCond);
return(vout);
}

btc_pubkey *buf2pk(btc_pubkey *pk,uint8_t *buf33)
{
int32_t i; uint8_t *dest;
pk->compressed=true;
for (i=0; i<33; i++)
pk->pubkey[i] = buf33[i];
return(pk);
}

btc_pubkey *CCtxidaddr(btc_spv_client *client,btc_pubkey *pk,char *txidaddr,uint256 txid)
{
uint8_t buf33[33];
buf33[0] = 0x02;
btc_pubkey_init(pk);
endiancpy(&buf33[1],(uint8_t *)&txid,32);
buf2pk(pk,buf33);
btc_pubkey_getaddr_p2pkh(pk,client->chainparams,txidaddr);
return(pk);
}

bool IsPayToCryptoCondition(cstring *script)
{
vector *v=vector_new(sizeof(btc_script_op),btc_script_op_free_cb);
btc_script_get_ops(script,v);
for(int i=0;i<(int32_t)v->len;i++)
{
btc_script_op *op=vector_idx(v,i);
if (op->op==OP_CHECKCRYPTOCONDITION) return true;
}
return false;
}

#endif // NSPV_CCUTILS_H

Loading

0 comments on commit 6dcf98d

Please sign in to comment.