From ff22a1aedbc554b5036eafa25f987ff12fded467 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 21:30:42 -1100 Subject: [PATCH 01/21] Priv2addr --- src/cc/CCinclude.h | 2 + src/cc/CCutils.cpp | 24 ++- src/cc/sudoku.cpp | 476 +++++++++++++++++++++++++++++++++++++++++++++ src/key.cpp | 16 ++ src/key.h | 1 + 5 files changed, 517 insertions(+), 2 deletions(-) create mode 100644 src/cc/sudoku.cpp diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 66c001969..5d847800a 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -42,6 +42,7 @@ one other technical note is that komodod has the insight-explorer extensions bui #include #include "../script/standard.h" #include "../base58.h" +#include "../key.h" #include "../core_io.h" #include "../script/sign.h" #include "../wallet/wallet.h" @@ -179,6 +180,7 @@ int32_t oracle_format(uint256 *hashp,int64_t *valp,char *str,uint8_t fmt,uint8_t CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv); // CCutils +bool priv2addr(char *coinaddr,uint8_t buf33[33],uint8_t priv32[32]); CPubKey buf2pk(uint8_t *buf33); void endiancpy(uint8_t *dest,uint8_t *src,int32_t len); uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv,int32_t entropyvout,int32_t usevout); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 766ce1188..6dff5d3cd 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -442,6 +442,20 @@ bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t n return(true); } +bool priv2addr(char *coinaddr,uint8_t *buf33,uint8_t priv32[32]) +{ + CKey priv; CPubKey pk; int32_t i; uint8_t *src; + priv.SetKey32(priv32); + pk = priv.GetPubKey(); + if ( buf33 != 0 ) + { + src = (uint8_t *)pk.begin(); + for (i=0; i<33; i++) + buf33[i] = src[i]; + } + return(Getscriptaddress(coinaddr, CScript() << ParseHex(HexStr(pk)) << OP_CHECKSIG)); +} + std::vector Mypubkey() { extern uint8_t NOTARY_PUBKEY33[33]; @@ -456,7 +470,7 @@ std::vector Mypubkey() bool Myprivkey(uint8_t myprivkey[]) { - char coinaddr[64]; std::string strAddress; char *dest; int32_t i,n; CBitcoinAddress address; CKeyID keyID; CKey vchSecret; + char coinaddr[64],checkaddr[64]; std::string strAddress; char *dest; int32_t i,n; CBitcoinAddress address; CKeyID keyID; CKey vchSecret; uint8_t buf33[33]; if ( Getscriptaddress(coinaddr,CScript() << Mypubkey() << OP_CHECKSIG) != 0 ) { n = (int32_t)strlen(coinaddr); @@ -477,7 +491,13 @@ bool Myprivkey(uint8_t myprivkey[]) fprintf(stderr,"0x%02x, ",myprivkey[i]); fprintf(stderr," found privkey for %s!\n",dest); } - return(true); + if ( priv2addr(checkaddr,buf33,myprivkey) != 0 ) + { + if ( buf2pk(buf33) == Mypubkey() && strcmp(checkaddr,coinaddr) == 0 ) + return(true); + else printf("mismatched privkey -> addr %s vs %s\n",checkaddr,coinaddr); + } + return(false); } #endif } diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp new file mode 100644 index 000000000..a79e3d161 --- /dev/null +++ b/src/cc/sudoku.cpp @@ -0,0 +1,476 @@ + +// start https://github.com/mentalmove/SudokuGenerator +// +// main.c +// SudokuGenerator +// +// Malte Pagel +// + +#include +#include +#include +#include +#include + + +#define SMALL_LINE 3 +#define LINE 9 +#define TOTAL 81 + +#define LIMIT 16777216 + +#define SHOW_SOLVED 1 + + +struct dimensions_collection { + int row; + int column; + int small_square; +}; + + +static int indices[TOTAL]; +static int riddle[TOTAL]; +static int solved[TOTAL]; +static int unsolved[TOTAL]; +static int tries_to_set = 0; +static int taking_back; +static int global_unset_count = 0; + + +struct dimensions_collection get_collection(int); +int contains_element(int*, int, int); +void get_horizontal(int, int*); +void get_vertical(int, int*); +void get_square(int, int*); +int set_values(int, int); +void take_back(int); + +int show_solution(int*); + + +int show_solution (int* solution) { + + int i; + int counter = 0; + + printf( " -----------------------------------\n" ); + + for ( i = 0; i < TOTAL; i++ ) { + if ( i % LINE == 0 ) + printf( "|" ); + + if ( solution[i] ) { + printf( " %d ", solution[i]); + counter++; + } + else + printf( " "); + + if ( i % LINE == (LINE - 1) ) { + printf( "|\n" ); + if ( i != (TOTAL - 1) ) { + if ( i % (SMALL_LINE * LINE) == (SMALL_LINE * LINE - 1) ) + printf( "|-----------+-----------+-----------|\n" ); + else + printf( "|- - - - - -|- - - - - -|- - - - - -|\n" ); + } + } + else { + if ( i % SMALL_LINE == (SMALL_LINE - 1) ) + printf( "|"); + else + printf( ":" ); + } + } + + printf( " -----------------------------------" ); + + return counter; +} + + +/** + * Takes a position inside the large square and returns + * - the row number + * - the column number + * - the small square number + * where this position is situated in + */ +struct dimensions_collection get_collection (int index) { + struct dimensions_collection ret; + + ret.row = (int) (index / LINE); + ret.column = index % LINE; + ret.small_square = SMALL_LINE * (int) (ret.row / SMALL_LINE) + (int) (ret.column / SMALL_LINE); + + return ret; +} + +/** + * Is 'the_element' in 'the_array'? + */ +int contains_element (int* the_array, int the_element, int length) { + for ( int i = 0; i < length; i++ ) + if ( the_array[i] == the_element ) + return 1; + return 0; +} + +/** + * Sets all members of row 'row' + */ +void get_horizontal (int row, int* ret) { + int j = 0; + for ( int i = (row * LINE); i < (row * LINE) + LINE; i++ ) + ret[j++] = riddle[i]; +} +/** + * Sets all members of column 'col' + */ +void get_vertical (int col, int* ret) { + int j = 0; + for ( int i = col; i < TOTAL; i += LINE ) + ret[j++] = riddle[i]; +} +/** + * Sets all members of small square 'which' + */ +void get_square (int which, int* ret) { + for ( int i = 0; i < SMALL_LINE; i++ ) + for ( int j = 0; j < SMALL_LINE; j++ ) + ret[SMALL_LINE * i + j] = riddle[LINE * i + which * SMALL_LINE + j + ((int) (which / SMALL_LINE) * (SMALL_LINE - 1) * LINE)]; +} + +/** + * Recursive function: + * Try for each position the numbers from 1 to LINE + * (except 'forbidden_number' if given). + * If all numbers collide with already set numbers, move is bad. + * If a number doesn't collide with already set numbers, + * - move is bad if next move collides with the already set numbers + * (including actual one) + * - move is good if it's the last one + */ +int set_values (int index, int forbidden_number) { + + if ( taking_back && tries_to_set > (2 * LIMIT) ) + return 1; + + int real_index = indices[index]; + struct dimensions_collection blocks = get_collection(real_index); + int elements[LINE]; + + for ( int i = 1; i <= LINE; i++ ) { + if ( forbidden_number && i == forbidden_number ) + continue; + + tries_to_set++; + + get_horizontal(blocks.row, elements); + if ( contains_element(elements, i, LINE) ) + continue; + + get_vertical(blocks.column, elements); + if ( contains_element(elements, i, LINE) ) + continue; + + get_square(blocks.small_square, elements); + if ( contains_element(elements, i, LINE) ) + continue; + + riddle[real_index] = i; + + if ( index == (TOTAL - 1) || set_values((index + 1), 0) ) + return 1; + } + + riddle[real_index] = 0; + + return 0; +} + +/** + * Some steps to hide unnecessary numbers: + * a) Define last piece as 'special piece' + * b) Remember this piece's value + * c) Try to create riddle from this position on, + * but forbid the value of the special piece + * d) I) If operation fails, define the piece before the special piece + * as 'special piece' and continue with b) + * II) If operation is possible, reset 'special piece' + * and put it to start of list + * e) Stop if all pieces are tried or calculation limit is reached + */ +void take_back (int unset_count) { + + global_unset_count++; + + int i; + + int tmp = riddle[indices[TOTAL - unset_count]]; + int redundant = set_values((TOTAL - unset_count), tmp); + + if ( !redundant ) { + unsolved[indices[TOTAL - unset_count]] = 0; + take_back(++unset_count); + } + else { + riddle[indices[TOTAL - unset_count]] = tmp; + for ( i = 1; i < unset_count; i++ ) + riddle[indices[TOTAL - unset_count + i]] = 0; + + for ( i = (TOTAL - unset_count); i > 0; i-- ) + indices[i] = indices[i - 1]; + indices[0] = tmp; + + if ( global_unset_count < TOTAL && tries_to_set < LIMIT ) + take_back(unset_count); + } +} + + +int sudoku(uint8_t solved9[LINE][LINE],uint8_t unsolved9[LINE][LINE],uint32_t srandi) +{ + int i, j, random, small_rows, small_cols, tmp, redundant,ind; + int multi_raw[LINE][LINE]; + + memset(indices,0,sizeof(indices)); + memset(solved,0,sizeof(solved)); + memset(unsolved,0,sizeof(unsolved)); + tries_to_set = 0; + taking_back = 0; + global_unset_count = 0; + + //time_t t; + //time(&t); + srand(srandi); + + /** + * Initialization: + * Fields are set to 0 ( i.e. we dont' know the number yet) + */ + for ( i = 0; i < TOTAL; i++ ) + riddle[i] = 0; + + /** + * Second initialization: + * LINE times numbers from 0 to (LINE - 1), + * i.e. every square + */ + int big_rows_array[] = {0, 1, 2}; + int big_cols_array[] = {0, 1, 2}; + random = rand() % 4; + switch (random) { + case 1: + big_rows_array[0] = 2; + big_rows_array[1] = 1; + big_rows_array[2] = 0; + break; + case 2: + big_cols_array[0] = 2; + big_cols_array[1] = 1; + big_cols_array[2] = 0; + break; + case 3: + big_rows_array[0] = 2; + big_rows_array[1] = 1; + big_rows_array[2] = 0; + big_cols_array[0] = 2; + big_cols_array[1] = 1; + big_cols_array[2] = 0; + } + int big_rows, big_cols, big_rows_index, big_cols_index, start_value; + i = 0; + j = 0; + for ( big_rows_index = 0; big_rows_index < SMALL_LINE; big_rows_index++ ) { + big_rows = big_rows_array[big_rows_index]; + for ( big_cols_index = 0; big_cols_index < SMALL_LINE; big_cols_index++ ) { + big_cols = big_cols_array[big_cols_index]; + start_value = big_rows * LINE * SMALL_LINE + (big_cols * SMALL_LINE); + for ( small_rows = 0; small_rows < SMALL_LINE; small_rows++ ) + for ( small_cols = 0; small_cols < SMALL_LINE; small_cols++ ) + multi_raw[i][j++] = small_rows * LINE + small_cols + start_value; + i++; + j = 0; + } + } + + + /** + * Randomization for every element of multi_raw. + * Suffle only inside squares + */ + for ( i = 0; i < LINE; i++ ) { + for ( j = 0; j < LINE; j++ ) { + random = rand() % LINE; + if ( j == random ) + continue; + tmp = multi_raw[i][j]; + multi_raw[i][j] = multi_raw[i][random]; + multi_raw[i][random] = tmp; + } + } + + /** + * Linearization + */ + for ( i = 0; i < LINE; i++ ) + for ( j = 0; j < LINE; j++ ) + indices[i * LINE + j] = multi_raw[i][j]; + + + /** + * Setting numbers, start with the first one. + * Variable 'redundant' is needed only for formal reasons + */ + taking_back = 0; + redundant = set_values(0, 0); + + + memcpy(solved, riddle, (TOTAL * sizeof(int))); + memcpy(unsolved, riddle, (TOTAL * sizeof(int))); + + + /** + * Exchanging some (few) indices for more randomized game + */ + int random2; + for ( i = (LINE - 1); i > 0; i-- ) { + for ( j = 0; j < (int) (sqrt(i)); j++ ) { + + if ( !(rand() % ((int) (i * sqrt(i)))) || !(LINE - j) ) + continue; + + random = i * LINE + (int) (rand() % (LINE - j)); + random2 = rand() % TOTAL; + + if ( random == random2 ) + continue; + + tmp = indices[random]; + indices[random] = indices[random2]; + indices[random2] = tmp; + } + } + + + tries_to_set = 0; + taking_back = 1; + take_back(1); + + + if ( SHOW_SOLVED ) { + printf( "\n\n" ); + redundant = show_solution(solved); + } + + int counter = show_solution(unsolved); + printf( "\t *** %d numbers left *** \n", counter ); + ind = 0; + for (i=0; i=0; j--) + { + x *= 9; + x += vals9[i][j]-1; + } + if ( i < 8 ) + keyvals[i] = x; + else + { + for (j=0; j<8; j++) + keyvals[j] += SUDOKU_NINETH * (vals9[i][j]-1); + } + } + for (i=ind=0; i<8; i++) + { + privkey[ind++] = ((keyvals[i] >> 24) & 0xff); + privkey[ind++] = ((keyvals[i] >> 16) & 0xff); + privkey[ind++] = ((keyvals[i] >> 8) & 0xff); + privkey[ind++] = (keyvals[i] & 0xff); + } +} + +void sudoku_gen(uint8_t key32[32],uint8_t unsolved[9][9],uint32_t srandi) +{ + uint8_t vals9[9][9],uniq9[9][9]; int32_t i,j; + sudoku(vals9,unsolved,srandi); + sudoku_privkey(key32,vals9); + sudoku_privkeydisp(key32); +} diff --git a/src/key.cpp b/src/key.cpp index 3b6fed81b..6e2370dc2 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -179,6 +179,22 @@ void CKey::MakeNewKey(bool fCompressedIn) { fCompressed = fCompressedIn; } +int32_t CKey::SetKey32(uint8_t Key32[32]) +{ + memcpy(vch,Key32,32); + fCompressed = true; + if ( Check(vch) == 0 ) + { + fValid = false; + return(-1); + } + else + { + fValid = true; + return(0); + } +} + bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) { if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), &privkey[0], privkey.size())) return false; diff --git a/src/key.h b/src/key.h index c41208003..857e8a8ae 100644 --- a/src/key.h +++ b/src/key.h @@ -128,6 +128,7 @@ public: //! Generate a new private key using a cryptographic PRNG. void MakeNewKey(bool fCompressed); + int32_t SetKey32(uint8_t Key32[32]); /** * Convert the private key to a CPrivKey (serialized OpenSSL private key data). From c7fa58b74a2d212cb90baca5c34c18fcac230e40 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 22:16:22 -1100 Subject: [PATCH 02/21] Check cclib addr --- src/cc/CCcustom.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 577332b63..7d6d80043 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -245,7 +245,7 @@ uint8_t CClibCCpriv[32] = { 0x57, 0xcf, 0x49, 0x71, 0x7d, 0xb4, 0x15, 0x1b, 0x4f int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) { - CPubKey pk; uint8_t pub33[33]; char CCaddr[64]; + CPubKey pk; uint8_t pub33[33],check33[33]; char CCaddr[64],checkaddr[64],str[67]; if ( evalcode == EVAL_FIRSTUSER ) // eventually make a hashchain for each evalcode { cp->evalcode = evalcode; @@ -258,7 +258,14 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) if ( strcmp(cp->normaladdr,CClibNormaladdr) != 0 ) fprintf(stderr,"CClib_initcp addr mismatch %s vs %s\n",cp->normaladdr,CClibNormaladdr); GetCCaddress(cp,cp->unspendableCCaddr,pk); - return(0); + if ( priv2addr(checkaddr,check33,myprivkey) != 0 ) + { + if ( buf2pk(check33) == pk && strcmp(checkaddr,cp->normaladdr) == 0 ) + { + fprinf(stderr,"verified evalcode.%d %s %s\n",cp->evalcode,checkaddr,pubkey33_str(str,pub33)); + return(0); + } else fprintf(stderr,"CClib_initcp mismatched privkey -> addr %s vs %s\n",checkaddr,cp->normaladdr); + } } return(-1); } From 980a5efbafaeddff9d33f8dae54636a612c0c40f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 22:22:54 -1100 Subject: [PATCH 03/21] Syntax --- src/cc/CCcustom.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 7d6d80043..e60ed2997 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -258,11 +258,11 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) if ( strcmp(cp->normaladdr,CClibNormaladdr) != 0 ) fprintf(stderr,"CClib_initcp addr mismatch %s vs %s\n",cp->normaladdr,CClibNormaladdr); GetCCaddress(cp,cp->unspendableCCaddr,pk); - if ( priv2addr(checkaddr,check33,myprivkey) != 0 ) + if ( priv2addr(checkaddr,check33,cp->CCpriv) != 0 ) { if ( buf2pk(check33) == pk && strcmp(checkaddr,cp->normaladdr) == 0 ) { - fprinf(stderr,"verified evalcode.%d %s %s\n",cp->evalcode,checkaddr,pubkey33_str(str,pub33)); + fprintf(stderr,"verified evalcode.%d %s %s\n",cp->evalcode,checkaddr,pubkey33_str(str,pub33)); return(0); } else fprintf(stderr,"CClib_initcp mismatched privkey -> addr %s vs %s\n",checkaddr,cp->normaladdr); } From 886003602724ed4a3ae9ac8058f7013ac770b458 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 22:36:34 -1100 Subject: [PATCH 04/21] Autocalc all cclib cp --- src/cc/CCcustom.cpp | 24 ++++++++++++++++++++---- src/wallet/rpcwallet.cpp | 19 +++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index e60ed2997..e00b5a0a0 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -245,13 +245,13 @@ uint8_t CClibCCpriv[32] = { 0x57, 0xcf, 0x49, 0x71, 0x7d, 0xb4, 0x15, 0x1b, 0x4f int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) { - CPubKey pk; uint8_t pub33[33],check33[33]; char CCaddr[64],checkaddr[64],str[67]; + CPubKey pk; int32_t i; uint8_t pub33[33],check33[33],hash[32]; char CCaddr[64],checkaddr[64],str[67]; + cp->evalcode = evalcode; + cp->ismyvin = IsCClibInput; + memcpy(cp->CCpriv,CClibCCpriv,32); if ( evalcode == EVAL_FIRSTUSER ) // eventually make a hashchain for each evalcode { - cp->evalcode = evalcode; - cp->ismyvin = IsCClibInput; strcpy(cp->CChexstr,CClibCChexstr); - memcpy(cp->CCpriv,CClibCCpriv,32); decode_hex(pub33,33,cp->CChexstr); pk = buf2pk(pub33); Getscriptaddress(cp->normaladdr,CScript() << ParseHex(HexStr(pk)) << OP_CHECKSIG); @@ -267,6 +267,22 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) } else fprintf(stderr,"CClib_initcp mismatched privkey -> addr %s vs %s\n",checkaddr,cp->normaladdr); } } + else + { + for (i=EVAL_FIRSTUSER; iCCpriv,32); + memcpy(cp->CCpriv,hash,32); + } + if ( priv2addr(cp->normaladdr,pub33,cp->CCpriv) != 0 ) + { + pk = buf2pk(pub33); + for (i=0; i<33; i++) + sprintf(&cp->CChexstr[i*2],"%02x",pub33[i]); + cp->CChexstr[i*2] = 0; + GetCCaddress(cp,cp->unspendableCCaddr,pk); + } + } return(-1); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 0ffc370e2..b799c6672 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5327,13 +5327,20 @@ UniValue channelsaddress(const UniValue& params, bool fHelp) UniValue cclibaddress(const UniValue& params, bool fHelp) { struct CCcontract_info *cp,C; std::vector pubkey; - cp = CCinit(&C,EVAL_FIRSTUSER); - if ( fHelp || params.size() > 1 ) - throw runtime_error("cclibaddress [pubkey]\n"); + if ( fHelp || params.size() > 2 ) + throw runtime_error("cclibaddress [evalcode] [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - if ( params.size() == 1 ) - pubkey = ParseHex(params[0].get_str().c_str()); + cp = CCinit(&C,EVAL_FIRSTUSER); + if ( params.size() >= 1 ) + { + evalcode = atoi(params[0].get_str().c_str()); + if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) + throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); + cp = CCinit(&C,evalcode); + if ( params.size() == 2 ) + pubkey = ParseHex(params[1].get_str().c_str()); + } return(CCaddress(cp,(char *)"CClib",pubkey)); } @@ -7695,4 +7702,4 @@ UniValue test_heirmarker(const UniValue& params, bool fHelp) cp = CCinit(&C, EVAL_HEIR); return(FinalizeCCTx(0, cp, mtx, myPubkey, 10000, opret)); -} \ No newline at end of file +} From 534c5ee516354074a029643cb48b3f4534cf5f4e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 22:38:06 -1100 Subject: [PATCH 05/21] Err --- src/wallet/rpcwallet.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b799c6672..2d4ba25d7 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5326,21 +5326,22 @@ UniValue channelsaddress(const UniValue& params, bool fHelp) UniValue cclibaddress(const UniValue& params, bool fHelp) { - struct CCcontract_info *cp,C; std::vector pubkey; + struct CCcontract_info *cp,C; std::vector pubkey; uint8_t evalcode = EVAL_FIRSTUSER; if ( fHelp || params.size() > 2 ) throw runtime_error("cclibaddress [evalcode] [pubkey]\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - cp = CCinit(&C,EVAL_FIRSTUSER); if ( params.size() >= 1 ) { evalcode = atoi(params[0].get_str().c_str()); if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); - cp = CCinit(&C,evalcode); if ( params.size() == 2 ) pubkey = ParseHex(params[1].get_str().c_str()); } + cp = CCinit(&C,evalcode); + if ( cp == 0 ) + throw runtime_error("error creating *cp\n"); return(CCaddress(cp,(char *)"CClib",pubkey)); } From 42a362165ff2f8a209ee74e2ca6f84ac2c047d3f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 22:43:52 -1100 Subject: [PATCH 06/21] Add [evalcode] to cclib roc --- src/cc/CCcustom.cpp | 1 + src/wallet/rpcwallet.cpp | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index e00b5a0a0..477487390 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -281,6 +281,7 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) sprintf(&cp->CChexstr[i*2],"%02x",pub33[i]); cp->CChexstr[i*2] = 0; GetCCaddress(cp,cp->unspendableCCaddr,pk); + return(0); } } return(-1); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2d4ba25d7..c6370d762 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5347,25 +5347,39 @@ UniValue cclibaddress(const UniValue& params, bool fHelp) UniValue cclibinfo(const UniValue& params, bool fHelp) { - struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_FIRSTUSER); + struct CCcontract_info *cp,C; uint8_t evalcode = EVAL_FIRSTUSER; if ( fHelp || params.size() > 0 ) throw runtime_error("cclibinfo\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + if ( params.size() >= 1 ) + { + evalcode = atoi(params[0].get_str().c_str()); + if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) + throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); + } + cp = CCinit(&C,evalcode); return(CClib_info(cp)); } UniValue cclib(const UniValue& params, bool fHelp) { - struct CCcontract_info *cp,C; char *method; cJSON *jsonparams; + struct CCcontract_info *cp,C; char *method; cJSON *jsonparams=0; uint8_t evalcode; cp = CCinit(&C,EVAL_FIRSTUSER); - if ( fHelp || params.size() > 2 ) - throw runtime_error("cclib method [JSON params]\n"); + if ( fHelp || params.size() > 3 ) + throw runtime_error("cclib method [evalcode] [JSON params]\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); method = (char *)params[0].get_str().c_str(); - jsonparams = cJSON_Parse(params[1].get_str().c_str()); + if ( params.size() >= 1 ) + { + evalcode = atoi(params[1].get_str().c_str()); + if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) + throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); + if ( params.size() == 2 ) + jsonparams = cJSON_Parse(params[2].get_str().c_str()); + } + cp = CCinit(&C,evalcode); return(CClib(cp,method,jsonparams)); } From 087d590f36b5e62db7508f2807df9d6cca372e9e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 23:35:17 -1100 Subject: [PATCH 07/21] Link in sudoku --- src/cc/Makefile | 2 +- src/cc/cclib.cpp | 79 ++++++++++++++++++++++++++++++++++++----------- src/cc/makecclib | 2 +- src/cc/sudoku.cpp | 52 +++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 20 deletions(-) diff --git a/src/cc/Makefile b/src/cc/Makefile index 52db18fca..97798e41e 100644 --- a/src/cc/Makefile +++ b/src/cc/Makefile @@ -13,7 +13,7 @@ $(info $(OS)) TARGET = ../libcc.so TARGET_DARWIN = ../libcc.dylib TARGET_WIN = ../libcc.dll -SOURCES = cclib.cpp +SOURCES = cclib.cpp sudoku.cpp #HEADERS = $(shell echo ../cryptoconditions/include/*.h) all: $(TARGET) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 971258d90..2875ce4b0 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -28,24 +28,63 @@ #include "crosschain.h" #define FAUCET2SIZE COIN +#defein EVAL_FAUCET2 EVAL_FIRSTUSER +#define EVAL_SUDOKU 17 + +std::string MYCCLIBNAME = (char *)"sudoku"; +char *CClib_name() { return((char *)MYCCLIBNAME.c_str()); } struct CClib_rpcinfo { - char *method,*help; + char *CCname,*method,*help; int32_t numrequiredargs,maxargs; // frontloaded with required - uint8_t funcid; + uint8_t funcid,evalcode; } CClib_methods[] = { - { (char *)"faucet2_fund", (char *)"amount", 1, 1, 'F' }, - { (char *)"faucet2_get", (char *)"", 0, 0, 'G' }, + { (char *)"faucet2", (char *)"fund", (char *)"amount", 1, 1, 'F', EVAL_FAUCET2 }, + { (char *)"faucet2", (char *)"get", (char *)"", 0, 0, 'G', EVAL_FAUCET2 }, + { (char *)"sudoku", (char *)"gen", (char *)"amount", 1, 1, 'G', EVAL_SUDOKU }, + { (char *)"sudoku", (char *)"txidinfo", (char *)"txid", 1, 1, 'T', EVAL_SUDOKU }, + { (char *)"sudoku", (char *)"pending", (char *)"", 0, 0, 'U', EVAL_SUDOKU }, + { (char *)"sudoku", (char *)"solution", (char *)"solution timestamps[]", 2, 2, 'S', EVAL_SUDOKU }, }; -std::string MYCCLIBNAME = (char *)"faucet2"; - -char *CClib_name() { return((char *)MYCCLIBNAME.c_str()); } - std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *params); +UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params); + +UniValue CClib_method(struct CCcontract_info *cp,char *method,cJSON *params) +{ + UniValue result(UniValue::VOBJ); + if ( cp->evalcode == EVAL_SUDOKU ) + { + if ( strcmp(method,"txidinfo") == 0 ) + return(sudoku_txidinfo(cp,params)); + else if ( strcmp(method,"gen") == 0 ) + return(sudoku_generate(cp,params)); + else if ( strcmp(method,"solution") == 0 ) + return(sudoku_solution(cp,params)); + else if ( strcmp(method,"pending") == 0 ) + return(sudoku_pending(cp,params)); + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","invalid sudoku method")); + result.push_back(Pair("method",method)); + return(result); + } + } + else + { + result.push_back(Pair("result","error")); + result.push_back(Pair("error","only sudoku supported for now")); + result.push_back(Pair("evalcode",(int)cp->evalcode)); + return(result); + } +} UniValue CClib_info(struct CCcontract_info *cp) { @@ -55,6 +94,7 @@ UniValue CClib_info(struct CCcontract_info *cp) for (i=0; i= 128 ) obj.push_back(Pair("funcid",CClib_methods[i].funcid)); else @@ -63,7 +103,8 @@ UniValue CClib_info(struct CCcontract_info *cp) str[1] = 0; obj.push_back(Pair("funcid",str)); } - obj.push_back(Pair("name",CClib_methods[i].method)); + obj.push_back(Pair("name",CClib_methods[i].CCname)); + obj.push_back(Pair("method",CClib_methods[i].method)); obj.push_back(Pair("help",CClib_methods[i].help)); obj.push_back(Pair("params_required",CClib_methods[i].numrequiredargs)); obj.push_back(Pair("params_max",CClib_methods[i].maxargs)); @@ -78,13 +119,16 @@ UniValue CClib(struct CCcontract_info *cp,char *method,cJSON *params) UniValue result(UniValue::VOBJ); int32_t i; std::string rawtx; for (i=0; ievalcode == CClib_methods[i].evalcode && strcmp(method,CClib_methods[i].method) == 0 ) { - result.push_back(Pair("result","success")); - result.push_back(Pair("method",CClib_methods[i].method)); - rawtx = CClib_rawtxgen(cp,CClib_methods[i].funcid,params); - result.push_back(Pair("rawtx",rawtx)); - return(result); + if ( cp->evalcode == EVAL_FAUCET2 ) + { + result.push_back(Pair("result","success")); + result.push_back(Pair("method",CClib_methods[i].method)); + rawtx = CClib_rawtxgen(cp,CClib_methods[i].funcid,params); + result.push_back(Pair("rawtx",rawtx)); + return(result); + } else return(CClib_method(cp,method,params)); } } result.push_back(Pair("result","error")); @@ -234,7 +278,6 @@ int64_t AddCClibInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK return(totalinputs); } - std::string Faucet2Fund(struct CCcontract_info *cp,uint64_t txfee,int64_t funds) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); @@ -289,14 +332,14 @@ std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *para if ( inputs > nValue ) CCchange = (inputs - nValue - txfee); if ( CCchange != 0 ) - mtx.vout.push_back(MakeCC1vout(EVAL_FIRSTUSER,CCchange,cclibpk)); + mtx.vout.push_back(MakeCC1vout(EVAL_FAUCET2,CCchange,cclibpk)); mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); fprintf(stderr,"start at %u\n",(uint32_t)time(NULL)); j = rand() & 0xfffffff; for (i=0; i<1000000; i++,j++) { tmpmtx = mtx; - rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_FIRSTUSER << (uint8_t)'G' << j)); + rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_FAUCET2 << (uint8_t)'G' << j)); if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 ) { len >>= 1; diff --git a/src/cc/makecclib b/src/cc/makecclib index 47aec377d..6131148f1 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1 +1 @@ -gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../cclib.so cclib.cpp +gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../cclib.so cclib.cpp sudoku.cpp diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index a79e3d161..6e54d0709 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -474,3 +474,55 @@ void sudoku_gen(uint8_t key32[32],uint8_t unsolved[9][9],uint32_t srandi) sudoku_privkey(key32,vals9); sudoku_privkeydisp(key32); } + +//////////////////////// start of CClib interface +#include +#include + +#include "primitives/block.h" +#include "primitives/transaction.h" +#include "script/cc.h" +#include "cc/eval.h" +#include "cc/utils.h" +#include "cc/CCinclude.h" +#include "main.h" +#include "chain.h" +#include "core_io.h" +#include "crosschain.h" + +UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) +{ + UniValue result(UniValue::VOBJ); + if ( params != 0 ) + printf("params.(%s)\n",jprint(params,0)); + result.push_back(Pair("result","success")); + return(result); +} + +UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params) +{ + UniValue result(UniValue::VOBJ); + if ( params != 0 ) + printf("params.(%s)\n",jprint(params,0)); + result.push_back(Pair("result","success")); + return(result); +} + +UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params) +{ + UniValue result(UniValue::VOBJ); + if ( params != 0 ) + printf("params.(%s)\n",jprint(params,0)); + result.push_back(Pair("result","success")); + return(result); +} + +UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params) +{ + UniValue result(UniValue::VOBJ); + if ( params != 0 ) + printf("params.(%s)\n",jprint(params,0)); + result.push_back(Pair("result","success")); + return(result); +} + From 6999011a90e945a716cd9a1e96006a3cbd83e8d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 23:37:17 -1100 Subject: [PATCH 08/21] #include sudoku.cpp as linker errors --- src/cc/Makefile | 2 +- src/cc/cclib.cpp | 3 +++ src/cc/makecclib | 2 +- src/cc/sudoku.cpp | 13 ------------- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/cc/Makefile b/src/cc/Makefile index 97798e41e..3e988f279 100644 --- a/src/cc/Makefile +++ b/src/cc/Makefile @@ -13,7 +13,7 @@ $(info $(OS)) TARGET = ../libcc.so TARGET_DARWIN = ../libcc.dylib TARGET_WIN = ../libcc.dll -SOURCES = cclib.cpp sudoku.cpp +SOURCES = cclib.cpp #HEADERS = $(shell echo ../cryptoconditions/include/*.h) all: $(TARGET) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 2875ce4b0..06982fe92 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -358,3 +358,6 @@ std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *para } else fprintf(stderr,"cant find faucet inputs\n"); return(""); } + +#include "sudoku.cpp" + diff --git a/src/cc/makecclib b/src/cc/makecclib index 6131148f1..47aec377d 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1 +1 @@ -gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../cclib.so cclib.cpp sudoku.cpp +gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../cclib.so cclib.cpp diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 6e54d0709..c5cbc1b31 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -476,19 +476,6 @@ void sudoku_gen(uint8_t key32[32],uint8_t unsolved[9][9],uint32_t srandi) } //////////////////////// start of CClib interface -#include -#include - -#include "primitives/block.h" -#include "primitives/transaction.h" -#include "script/cc.h" -#include "cc/eval.h" -#include "cc/utils.h" -#include "cc/CCinclude.h" -#include "main.h" -#include "chain.h" -#include "core_io.h" -#include "crosschain.h" UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) { From 2c7d564a908534530727dabf22f940c29f7e5afc Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 23:38:14 -1100 Subject: [PATCH 09/21] Define --- src/cc/cclib.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 06982fe92..4405cda87 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -28,7 +28,7 @@ #include "crosschain.h" #define FAUCET2SIZE COIN -#defein EVAL_FAUCET2 EVAL_FIRSTUSER +#define EVAL_FAUCET2 EVAL_FIRSTUSER #define EVAL_SUDOKU 17 std::string MYCCLIBNAME = (char *)"sudoku"; From 72c4217d3904592f8bdbb4b4268ca208e49ddc53 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 23:54:41 -1100 Subject: [PATCH 10/21] +print --- src/wallet/rpcwallet.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c6370d762..1ea66bb5e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5174,11 +5174,20 @@ int32_t ensure_CCrequirements() { CCerror = ""; if ( NOTARY_PUBKEY33[0] == 0 ) + { + fprintf(stderr,"no -pubkey set\n"); return(-1); + } else if ( GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) == 0 ) + { + fprintf(stderr,"no -addressindex\n"); return(-1); + } else if ( GetBoolArg("-spentindex", DEFAULT_SPENTINDEX) == 0 ) + { + fprintf(stderr,"no -spentindex\n"); return(-1); + } else return(0); } From 47294fb7362fc94b3c6cca6e93f4fda7bcf85a87 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 28 Jan 2019 23:59:13 -1100 Subject: [PATCH 11/21] -pubkey --- src/komodo_utils.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 96c9cd922..5b3a6cd39 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1671,6 +1671,7 @@ void komodo_args(char *argv0) KOMODO_DEALERNODE = GetArg("-dealer",0); if ( strlen(NOTARY_PUBKEY.c_str()) == 66 ) { + decode_hex(NOTARY_PUBKEY33,33,NOTARY_PUBKEY.c_str()); USE_EXTERNAL_PUBKEY = 1; if ( IS_KOMODO_NOTARY == 0 ) { From 7b24fb0d45cec28654833f7264cc50b4dc8848c8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:00:17 -1100 Subject: [PATCH 12/21] (Char *) --- src/komodo_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 5b3a6cd39..bcd95e698 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -1671,7 +1671,7 @@ void komodo_args(char *argv0) KOMODO_DEALERNODE = GetArg("-dealer",0); if ( strlen(NOTARY_PUBKEY.c_str()) == 66 ) { - decode_hex(NOTARY_PUBKEY33,33,NOTARY_PUBKEY.c_str()); + decode_hex(NOTARY_PUBKEY33,33,(char *)NOTARY_PUBKEY.c_str()); USE_EXTERNAL_PUBKEY = 1; if ( IS_KOMODO_NOTARY == 0 ) { From 812d9235ebc476516c339b235c69d4e782facff9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:04:50 -1100 Subject: [PATCH 13/21] Test --- src/wallet/rpcwallet.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1ea66bb5e..bc10da7bc 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5384,7 +5384,10 @@ UniValue cclib(const UniValue& params, bool fHelp) { evalcode = atoi(params[1].get_str().c_str()); if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) + { + printf("evalcode.%d vs (%d, %d)\n",evalcode,EVAL_FIRSTUSER,EVAL_LASTUSER); throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); + } if ( params.size() == 2 ) jsonparams = cJSON_Parse(params[2].get_str().c_str()); } From a614b0555fa02c176a570c007701b86f4ddbba0a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:10:42 -1100 Subject: [PATCH 14/21] Prints --- src/cc/CCcustom.cpp | 1 + src/cc/sudoku.cpp | 4 ++++ src/wallet/rpcwallet.cpp | 10 ++-------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cc/CCcustom.cpp b/src/cc/CCcustom.cpp index 477487390..0b150e6e0 100644 --- a/src/cc/CCcustom.cpp +++ b/src/cc/CCcustom.cpp @@ -281,6 +281,7 @@ int32_t CClib_initcp(struct CCcontract_info *cp,uint8_t evalcode) sprintf(&cp->CChexstr[i*2],"%02x",pub33[i]); cp->CChexstr[i*2] = 0; GetCCaddress(cp,cp->unspendableCCaddr,pk); + printf("evalcode.%d initialized\n",evalcode); return(0); } } diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index c5cbc1b31..6f9d2d1d1 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -483,6 +483,7 @@ UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("method","txidinfo")); return(result); } @@ -492,6 +493,7 @@ UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("method","gen")); return(result); } @@ -501,6 +503,7 @@ UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("method","solution")); return(result); } @@ -510,6 +513,7 @@ UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("method","pending")); return(result); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index bc10da7bc..c0fe4caa8 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5361,20 +5361,13 @@ UniValue cclibinfo(const UniValue& params, bool fHelp) throw runtime_error("cclibinfo\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); - if ( params.size() >= 1 ) - { - evalcode = atoi(params[0].get_str().c_str()); - if ( evalcode < EVAL_FIRSTUSER || evalcode > EVAL_LASTUSER ) - throw runtime_error("evalcode not between EVAL_FIRSTUSER and EVAL_LASTUSER\n"); - } cp = CCinit(&C,evalcode); return(CClib_info(cp)); } UniValue cclib(const UniValue& params, bool fHelp) { - struct CCcontract_info *cp,C; char *method; cJSON *jsonparams=0; uint8_t evalcode; - cp = CCinit(&C,EVAL_FIRSTUSER); + struct CCcontract_info *cp,C; char *method; cJSON *jsonparams=0; uint8_t evalcode = EVAL_FIRSTUSER; if ( fHelp || params.size() > 3 ) throw runtime_error("cclib method [evalcode] [JSON params]\n"); if ( ensure_CCrequirements() < 0 ) @@ -5391,6 +5384,7 @@ UniValue cclib(const UniValue& params, bool fHelp) if ( params.size() == 2 ) jsonparams = cJSON_Parse(params[2].get_str().c_str()); } + printf("evalcode.%d\n",evalcode); cp = CCinit(&C,evalcode); return(CClib(cp,method,jsonparams)); } From 946808ba40d1117372f39be7ac1c8767d1a81e6d Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:16:49 -1100 Subject: [PATCH 15/21] libcc.so --- src/cc/makecclib | 2 +- src/cc/sudoku.cpp | 4 ++++ src/wallet/rpcwallet.cpp | 1 - 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/makecclib b/src/cc/makecclib index 47aec377d..0aecab072 100755 --- a/src/cc/makecclib +++ b/src/cc/makecclib @@ -1 +1 @@ -gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../cclib.so cclib.cpp +gcc -std=c++11 -I../univalue/include -I../cryptoconditions/include -I../cryptoconditions/src -I../cryptoconditions/src/asn -I.. -I. -fPIC -shared -c -o ../libcc.so cclib.cpp diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 6f9d2d1d1..62a7ea91c 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -483,6 +483,7 @@ UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","txidinfo")); return(result); } @@ -493,6 +494,7 @@ UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","gen")); return(result); } @@ -503,6 +505,7 @@ UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","solution")); return(result); } @@ -513,6 +516,7 @@ UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params) if ( params != 0 ) printf("params.(%s)\n",jprint(params,0)); result.push_back(Pair("result","success")); + result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","pending")); return(result); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c0fe4caa8..970dfa3cf 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5384,7 +5384,6 @@ UniValue cclib(const UniValue& params, bool fHelp) if ( params.size() == 2 ) jsonparams = cJSON_Parse(params[2].get_str().c_str()); } - printf("evalcode.%d\n",evalcode); cp = CCinit(&C,evalcode); return(CClib(cp,method,jsonparams)); } From db4e2fc5380b5d520b481d64bde57ad103c3c743 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:20:14 -1100 Subject: [PATCH 16/21] Hex --- src/cc/sudoku.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 62a7ea91c..f03fbd69b 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -496,6 +496,7 @@ UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params) result.push_back(Pair("result","success")); result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","gen")); + result.push_back(Pair("hex","deadbeef")); return(result); } From ca2a5e048bb61857a2d630d6b906d35831e0d690 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:48:59 -1100 Subject: [PATCH 17/21] sudoku_gen --- src/cc/cclib.cpp | 18 +++++++++--------- src/cc/sudoku.cpp | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/cc/cclib.cpp b/src/cc/cclib.cpp index 4405cda87..8a7d054f7 100644 --- a/src/cc/cclib.cpp +++ b/src/cc/cclib.cpp @@ -51,24 +51,24 @@ CClib_methods[] = }; std::string CClib_rawtxgen(struct CCcontract_info *cp,uint8_t funcid,cJSON *params); -UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params); -UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params); -UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params); -UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_txidinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_solution(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); +UniValue sudoku_pending(uint64_t txfee,struct CCcontract_info *cp,cJSON *params); UniValue CClib_method(struct CCcontract_info *cp,char *method,cJSON *params) { - UniValue result(UniValue::VOBJ); + UniValue result(UniValue::VOBJ); uint64_t txfee = 10000; if ( cp->evalcode == EVAL_SUDOKU ) { if ( strcmp(method,"txidinfo") == 0 ) - return(sudoku_txidinfo(cp,params)); + return(sudoku_txidinfo(txfee,cp,params)); else if ( strcmp(method,"gen") == 0 ) - return(sudoku_generate(cp,params)); + return(sudoku_generate(txfee,cp,params)); else if ( strcmp(method,"solution") == 0 ) - return(sudoku_solution(cp,params)); + return(sudoku_solution(txfee,cp,params)); else if ( strcmp(method,"pending") == 0 ) - return(sudoku_pending(cp,params)); + return(sudoku_pending(txfee,cp,params)); else { result.push_back(Pair("result","error")); diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index f03fbd69b..5132d7c98 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -477,7 +477,7 @@ void sudoku_gen(uint8_t key32[32],uint8_t unsolved[9][9],uint32_t srandi) //////////////////////// start of CClib interface -UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) +UniValue sudoku_txidinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result(UniValue::VOBJ); if ( params != 0 ) @@ -488,19 +488,43 @@ UniValue sudoku_txidinfo(struct CCcontract_info *cp,cJSON *params) return(result); } -UniValue sudoku_generate(struct CCcontract_info *cp,cJSON *params) +CScript sudoku_genopret(uint8_t unsolved[9][9]) { - UniValue result(UniValue::VOBJ); + CScript opret; uint8_t evalcode = EVAL_SUDOKU; + opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'G' << unsolved); + return(opret); +} + +UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) +{ + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + UniValue result(UniValue::VOBJ); CPubKey sudokupk,pk; uint8_t privkey[32],unsolved[9][9],pub33[33]; uint32_t srandi; uint256 hash; char coinaddr[64]; uint64_t inputsum,amount; std::string hex; if ( params != 0 ) + { printf("params.(%s)\n",jprint(params,0)); + amount = jdouble(jitem(params,0),0) * COIN + 0.0000000049; + } else amount = COIN; result.push_back(Pair("result","success")); result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","gen")); - result.push_back(Pair("hex","deadbeef")); + hash = chainActive.LastTip()->GetHash(); + memcpy(&srandi,&hash,sizeof(srandi)) + srandi ^= (uint32_t)time(NULL); + sudoku_gen(privkey,unsolved,srandi); + priv2addr(coinaddr,pub33,privkey); + pk = buf2pk(pub33); + sudokupk = GetUnspendable(cp,0); + inputsum = amount + 2*txfee; + mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,sudokupk)); + mtx.vout.push_back(MakeCC1vout(cp->evalcode,inputsum - 2*txfee,pk)); + rawtx = FinalizeCCTx(0,cp,mtx,sudokupk,txfee,sudoku_genopret(unsolved)); + result.push_back(Pair("srand",srandi)); + result.push_back(Pair("amount",ValueFromAmount(amount))); + result.push_back(Pair("hex",rawtx)); return(result); } -UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params) +UniValue sudoku_solution(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result(UniValue::VOBJ); if ( params != 0 ) @@ -511,7 +535,7 @@ UniValue sudoku_solution(struct CCcontract_info *cp,cJSON *params) return(result); } -UniValue sudoku_pending(struct CCcontract_info *cp,cJSON *params) +UniValue sudoku_pending(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { UniValue result(UniValue::VOBJ); if ( params != 0 ) From fb3246204be4c24a683b5666674c371bd69eae7b Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:50:17 -1100 Subject: [PATCH 18/21] Syntax --- src/cc/sudoku.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 5132d7c98..50a04e22e 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -498,7 +498,7 @@ CScript sudoku_genopret(uint8_t unsolved[9][9]) UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params) { CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - UniValue result(UniValue::VOBJ); CPubKey sudokupk,pk; uint8_t privkey[32],unsolved[9][9],pub33[33]; uint32_t srandi; uint256 hash; char coinaddr[64]; uint64_t inputsum,amount; std::string hex; + UniValue result(UniValue::VOBJ); CPubKey sudokupk,pk; uint8_t privkey[32],unsolved[9][9],pub33[33]; uint32_t srandi; uint256 hash; char coinaddr[64]; uint64_t inputsum,amount; std::string rawtx; if ( params != 0 ) { printf("params.(%s)\n",jprint(params,0)); @@ -508,7 +508,7 @@ UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","gen")); hash = chainActive.LastTip()->GetHash(); - memcpy(&srandi,&hash,sizeof(srandi)) + memcpy(&srandi,&hash,sizeof(srandi)); srandi ^= (uint32_t)time(NULL); sudoku_gen(privkey,unsolved,srandi); priv2addr(coinaddr,pub33,privkey); @@ -518,7 +518,7 @@ UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params mtx.vout.push_back(MakeCC1vout(cp->evalcode,txfee,sudokupk)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,inputsum - 2*txfee,pk)); rawtx = FinalizeCCTx(0,cp,mtx,sudokupk,txfee,sudoku_genopret(unsolved)); - result.push_back(Pair("srand",srandi)); + result.push_back(Pair("srand",(int)srandi)); result.push_back(Pair("amount",ValueFromAmount(amount))); result.push_back(Pair("hex",rawtx)); return(result); From d4e8f4afa9e99eb4f6f0fd79556b8ddde21f1385 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:51:00 -1100 Subject: [PATCH 19/21] GetBlockHash() --- src/cc/sudoku.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 50a04e22e..770620565 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -507,7 +507,7 @@ UniValue sudoku_generate(uint64_t txfee,struct CCcontract_info *cp,cJSON *params result.push_back(Pair("result","success")); result.push_back(Pair("name","sudoku")); result.push_back(Pair("method","gen")); - hash = chainActive.LastTip()->GetHash(); + hash = chainActive.LastTip()->GetBlockHash(); memcpy(&srandi,&hash,sizeof(srandi)); srandi ^= (uint32_t)time(NULL); sudoku_gen(privkey,unsolved,srandi); From 91f3a0fcbd0dfb3eeb5c5aec9b45d507d3d6f1a0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:52:52 -1100 Subject: [PATCH 20/21] Gen opret --- src/cc/sudoku.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 770620565..42f5791f8 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -490,8 +490,11 @@ UniValue sudoku_txidinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params CScript sudoku_genopret(uint8_t unsolved[9][9]) { - CScript opret; uint8_t evalcode = EVAL_SUDOKU; - opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'G' << unsolved); + CScript opret; uint8_t evalcode = EVAL_SUDOKU; std::vector data; + for (i=0; i<9; i++) + for (j=0; j<9; j++) + data.push_back(unsolved[i][j]); + opret << OP_RETURN << E_MARSHAL(ss << evalcode << 'G' << data); return(opret); } From 0d81615f616b645f78fabe9634d79e69805a56d1 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 29 Jan 2019 00:53:27 -1100 Subject: [PATCH 21/21] i,j --- src/cc/sudoku.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc/sudoku.cpp b/src/cc/sudoku.cpp index 42f5791f8..20d11567a 100644 --- a/src/cc/sudoku.cpp +++ b/src/cc/sudoku.cpp @@ -490,7 +490,7 @@ UniValue sudoku_txidinfo(uint64_t txfee,struct CCcontract_info *cp,cJSON *params CScript sudoku_genopret(uint8_t unsolved[9][9]) { - CScript opret; uint8_t evalcode = EVAL_SUDOKU; std::vector data; + CScript opret; uint8_t evalcode = EVAL_SUDOKU; std::vector data; int32_t i,j; for (i=0; i<9; i++) for (j=0; j<9; j++) data.push_back(unsolved[i][j]);