From c929bc3e0450c0ba37828ce49d57bc35623ea3d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 22 Jan 2017 19:24:41 +0200 Subject: [PATCH] test --- src/rpcclient.cpp | 2 +- src/wallet/rpcwallet.cpp | 207 ++++++++++++++++++++------------------- 2 files changed, 107 insertions(+), 102 deletions(-) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 5b89b18c4..933b550a5 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -113,7 +113,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "notaries", 1 }, { "minerids", 1 }, { "kvsearch", 1 }, - { "kvupdate", 4 }, + //{ "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9791fe4cf..ddb099453 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -494,6 +494,112 @@ uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 privkey); int32_t komodo_kvduration(uint32_t flags); uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase); +Value kvupdate(const Array& params, bool fHelp) +{ + static uint256 zeroes; + CWalletTx wtx; Object ret; + uint8_t keyvalue[IGUANA_MAXSCRIPTSIZE],opretbuf[IGUANA_MAXSCRIPTSIZE]; int32_t i,coresize,haveprivkey,duration,opretlen,height; uint16_t keylen,valuesize=0,refvaluesize=0; uint8_t *key,*value=0; uint32_t flags,tmpflags,n; struct komodo_kv *ptr; uint64_t fee; uint256 privkey,pubkey,refpubkey,sig; + if (fHelp || params.size() < 3 ) + throw runtime_error("kvupdate key value flags/passphrase"); + if (!EnsureWalletIsAvailable(fHelp)) + return 0; + haveprivkey = 0; + memset(&sig,0,sizeof(sig)); + memset(&privkey,0,sizeof(privkey)); + memset(&pubkey,0,sizeof(pubkey)); + if ( (n= (int32_t)params.size()) >= 3 ) + { + flags = atoi(params[2].get_str().c_str()); + printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); + } else flags = 0; + if ( n >= 4 ) + { + privkey = komodo_kvprivkey(&pubkey,(char *)params[3].get_str().c_str()); + haveprivkey = 1; + flags |= 1; + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&privkey)[i]); + printf(" priv, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); + //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); + } + LOCK2(cs_main, pwalletMain->cs_wallet); + if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) + { + key = (uint8_t *)params[0].get_str().c_str(); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + value = (uint8_t *)params[1].get_str().c_str(); + valuesize = (int32_t)strlen(params[1].get_str().c_str()); + } + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) + { + if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) + { + if ( refpubkey != pubkey ) + { + ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); + return ret; + } + printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); + sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); + } + } + ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); + height = chainActive.Tip()->nHeight; + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); + ret.push_back(Pair("height", (int64_t)height)); + duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; + ret.push_back(Pair("expiration", (int64_t)(height+duration))); + ret.push_back(Pair("flags",(int64_t)flags)); + ret.push_back(Pair("key",params[0].get_str())); + ret.push_back(Pair("keylen",(int64_t)keylen)); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + ret.push_back(Pair("value",params[1].get_str())); + ret.push_back(Pair("valuesize",valuesize)); + } + iguana_rwnum(1,&keyvalue[0],sizeof(keylen),&keylen); + iguana_rwnum(1,&keyvalue[2],sizeof(valuesize),&valuesize); + iguana_rwnum(1,&keyvalue[4],sizeof(height),&height); + iguana_rwnum(1,&keyvalue[8],sizeof(flags),&flags); + memcpy(&keyvalue[12],key,keylen); + if ( value != 0 ) + memcpy(&keyvalue[12 + keylen],value,valuesize); + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); + if ( haveprivkey != 0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; + coresize += 32; + if ( refvaluesize >=0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; + coresize += 32; + } + } + if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) + opretlen++; + //for (i=0; i= 3 ) - { - flags = atoi(params[2].get_str().c_str()); - printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); - } else flags = 0; - //if ( params.size() >= 4 ) - { - privkey = komodo_kvprivkey(&pubkey,(char *)params[2].get_str().c_str()); - haveprivkey = 1; - flags |= 1; - printf("have privkey derived from (%s)\n",(char *)params[2].get_str().c_str()); - //printf("flags.%d (%s)\n",flags,params[2].get_str().c_str()); - } - LOCK2(cs_main, pwalletMain->cs_wallet); - if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) - { - key = (uint8_t *)params[0].get_str().c_str(); - if ( params.size() >= 2 && params[1].get_str().c_str() != 0 ) - { - value = (uint8_t *)params[1].get_str().c_str(); - valuesize = (int32_t)strlen(params[1].get_str().c_str()); - } - memcpy(keyvalue,key,keylen); - if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) - { - if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) - { - if ( refpubkey != pubkey ) - { - ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); - return ret; - } - printf("calc sig for keylen.%d + valuesize.%d\n",keylen,valuesize); - sig = komodo_kvsig(keyvalue,keylen+valuesize,privkey); - } - } - ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); - height = chainActive.Tip()->nHeight; - if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) - ret.push_back(Pair("owner",refpubkey.GetHex())); - ret.push_back(Pair("height", (int64_t)height)); - duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; - ret.push_back(Pair("expiration", (int64_t)(height+duration))); - ret.push_back(Pair("flags",(int64_t)flags)); - ret.push_back(Pair("key",params[0].get_str())); - ret.push_back(Pair("keylen",(int64_t)keylen)); - if ( params.size() >= 2 && params[1].get_str().c_str() != 0 ) - { - ret.push_back(Pair("value",params[1].get_str())); - ret.push_back(Pair("valuesize",valuesize)); - } - iguana_rwnum(1,&keyvalue[0],sizeof(keylen),&keylen); - iguana_rwnum(1,&keyvalue[2],sizeof(valuesize),&valuesize); - iguana_rwnum(1,&keyvalue[4],sizeof(height),&height); - iguana_rwnum(1,&keyvalue[8],sizeof(flags),&flags); - memcpy(&keyvalue[12],key,keylen); - if ( value != 0 ) - memcpy(&keyvalue[12 + keylen],value,valuesize); - coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); - if ( haveprivkey != 0 ) - { - for (i=0; i<32; i++) - keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; - coresize += 32; - if ( refvaluesize >=0 ) - { - for (i=0; i<32; i++) - keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; - coresize += 32; - } - } - if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) - opretlen++; - //for (i=0; i