From 2eba60251a44014e4f6e7a1358261397578be755 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Apr 2016 21:55:28 -0500 Subject: [PATCH] test --- iguana/SuperNET.c | 19 ++++++++------- iguana/SuperNET_keys.c | 43 ++++++++++++++++------------------ iguana/iguana_rpc.c | 2 +- iguana/iguana_wallet.c | 44 ++++++++++++++++++++++++++++------- iguana/tests/backupwallet | 1 + iguana/tests/dumpwallet | 1 + iguana/tests/encryptwallet | 2 +- iguana/tests/login | 2 +- iguana/tests/walletpassphrase | 3 ++- 9 files changed, 74 insertions(+), 43 deletions(-) create mode 100644 iguana/tests/backupwallet create mode 100755 iguana/tests/dumpwallet diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index 8c2999509..a2918adf8 100755 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -1381,23 +1381,25 @@ struct supernet_info *SuperNET_accountfind(cJSON *json) FOUR_STRINGS(SuperNET,login,handle,password,permanentfile,passphrase) { - char *str,*decryptstr = 0; cJSON *argjson; uint32_t expire = myinfo->expiration; + char savehandle[1024],savepassword[1024],savepermanentfile[1024],*str,*decryptstr = 0; cJSON *argjson; uint32_t expire = myinfo->expiration; if ( remoteaddr != 0 ) return(clonestr("{\"error\":\"no remote\"}")); + safecopy(savehandle,myinfo->handle,sizeof(myinfo->handle)); + safecopy(savepassword,myinfo->secret,sizeof(myinfo->secret)); + safecopy(savepermanentfile,myinfo->permanentfile,sizeof(myinfo->permanentfile)); if ( bits256_nonz(myinfo->persistent_priv) != 0 && (str= SuperNET_logout(IGUANA_CALLARGS)) != 0 ) free(str); myinfo->expiration = expire; - if ( handle != 0 ) - { + if ( handle != 0 && handle[0] != 0 ) safecopy(myinfo->handle,handle,sizeof(myinfo->handle)); - safecopy(myinfo->secret,password,sizeof(myinfo->secret)); - safecopy(myinfo->permanentfile,permanentfile,sizeof(myinfo->permanentfile)); - } + else safecopy(myinfo->handle,savehandle,sizeof(myinfo->handle)); + safecopy(myinfo->secret,savepassword,sizeof(myinfo->secret)); + safecopy(myinfo->permanentfile,savepermanentfile,sizeof(myinfo->permanentfile)); if ( (passphrase == 0 || passphrase[0] == 0) && (decryptstr= SuperNET_decryptjson(IGUANA_CALLARGS,password,permanentfile)) != 0 ) { if ( (argjson= cJSON_Parse(decryptstr)) != 0 ) { - printf("decrypted.(%s) exp.%u\n",decryptstr,myinfo->expiration); + //printf("decrypted.(%s) exp.%u\n",decryptstr,myinfo->expiration); if ( myinfo->decryptstr != 0 ) free(myinfo->decryptstr); myinfo->decryptstr = decryptstr; @@ -1405,6 +1407,7 @@ FOUR_STRINGS(SuperNET,login,handle,password,permanentfile,passphrase) { SuperNET_setkeys(myinfo,passphrase,(int32_t)strlen(passphrase),1); free_json(argjson); + myinfo->expiration = (uint32_t)(time(NULL) + 3600*24); return(SuperNET_activehandle(IGUANA_CALLARGS)); } else @@ -1422,7 +1425,7 @@ FOUR_STRINGS(SuperNET,login,handle,password,permanentfile,passphrase) if ( passphrase != 0 && passphrase[0] != 0 ) { SuperNET_setkeys(myinfo,passphrase,(int32_t)strlen(passphrase),1); - if ( (str= SuperNET_encryptjson(IGUANA_CALLARGS,password,permanentfile,passphrase)) != 0 ) + if ( (str= SuperNET_encryptjson(IGUANA_CALLARGS,myinfo->secret,myinfo->permanentfile,passphrase)) != 0 ) free(str); return(SuperNET_activehandle(IGUANA_CALLARGS)); } diff --git a/iguana/SuperNET_keys.c b/iguana/SuperNET_keys.c index d1cfc4bed..9f2e215cd 100755 --- a/iguana/SuperNET_keys.c +++ b/iguana/SuperNET_keys.c @@ -135,7 +135,7 @@ int32_t SuperNET_savejsonfile(char *fname,bits256 privkey,bits256 destpubkey,cJS //sprintf(fname,"confs/iguana.%llu",(long long)wallet2shared.txid); if ( (ciphered= SuperNET_cipher(0,0,json,0,privkey,destpubkey,confstr)) != 0 ) { - printf("ciphered.save (%s) <- (%s)\n",fname,confstr); + //printf("ciphered.save (%s) <- (%s)\n",fname,confstr); if ( (fp= fopen(fname,"wb")) != 0 ) { fwrite(ciphered,1,strlen(ciphered)+1,fp); @@ -176,9 +176,9 @@ int32_t SuperNET_userkeys(char *passphrase,int32_t passsize,char *fname2fa,int32 return(-1); } -cJSON *SuperNET_decryptedjson(char *passphrase,int32_t passsize,bits256 wallethash,char *fname2fa,int32_t fnamesize,bits256 wallet2priv) +cJSON *SuperNET_decryptedjson(char *destfname,char *passphrase,int32_t passsize,bits256 wallethash,char *fname2fa,int32_t fnamesize,bits256 wallet2priv) { - long allocsize; cJSON *filejson,*msgjson=0,*json=0; char *confstr=0,*deciphered,fname[512],str[65]; + long allocsize; cJSON *filejson,*msgjson=0,*json=0; char *confstr=0,*deciphered,str[65]; bits256 wallet2shared,wallet2pub; int32_t first,second; msgjson = 0; first = (bits256_nonz(wallethash) != 0 && bits256_cmp(wallethash,GENESIS_PRIVKEY) != 0); @@ -200,17 +200,17 @@ cJSON *SuperNET_decryptedjson(char *passphrase,int32_t passsize,bits256 walletha wallethash = GENESIS_PRIVKEY; wallet2shared = SuperNET_wallet2shared(wallethash,wallet2priv); wallet2pub = curve25519(wallet2shared,curve25519_basepoint9()); - sprintf(fname,"confs/%s",bits256_str(str,wallet2pub)); - //printf("fname.(%s) wallet2shared.%s\n",fname,bits256_str(str,wallet2pub)); - if ( (confstr= OS_filestr(&allocsize,fname)) != 0 ) + sprintf(destfname,"confs/%s",bits256_str(str,wallet2pub)); + //printf("fname.(%s) wallet2pub.%s < [%s, %s]\n",destfname,bits256_str(str,wallet2pub),passphrase,fname2fa); + if ( (confstr= OS_filestr(&allocsize,destfname)) != 0 ) { if ( (filejson= cJSON_Parse(confstr)) != 0 ) { - printf("confstr.(%s)\n",confstr); + //printf("confstr.(%s)\n",confstr); if ( (deciphered= SuperNET_decipher(0,0,0,0,wallet2shared,curve25519(wallethash,curve25519_basepoint9()),jstr(filejson,"result"))) != 0 ) { if ( (json= cJSON_Parse(deciphered)) == 0 ) - printf("cant decipher (%s) [%s]\n",fname,confstr); + printf("cant decipher (%s) [%s]\n",destfname,confstr); else { if ( (msgjson= cJSON_Parse(jstr(json,"message"))) == 0 ) @@ -220,16 +220,16 @@ cJSON *SuperNET_decryptedjson(char *passphrase,int32_t passsize,bits256 walletha } free_json(filejson); } - } else printf("couldnt load (%s)\n",fname); + } else printf("couldnt load (%s)\n",destfname); } else { - sprintf(fname,"confs/iguana.conf"); - if ( (confstr= OS_filestr(&allocsize,fname)) != 0 ) + sprintf(destfname,"confs/iguana.conf"); + if ( (confstr= OS_filestr(&allocsize,destfname)) != 0 ) { if ( (json= cJSON_Parse(confstr)) != 0 ) msgjson = json; - } else printf("couldnt open (%s)\n",fname); + } else printf("couldnt open (%s)\n",destfname); } if ( msgjson != 0 ) msgjson = jduplicate(msgjson); @@ -251,7 +251,7 @@ int32_t _SuperNET_encryptjson(char *destfname,char *passphrase,int32_t passsize, wallet2shared = SuperNET_wallet2shared(wallethash,wallet2priv); wallet2pub = curve25519(wallet2shared,curve25519_basepoint9()); sprintf(destfname,"confs/%s",bits256_str(str,wallet2pub)); - printf("SAVE ARGJSON.(%s)\n",jprint(argjson,0)); + //printf("SAVE ARGJSON.(%s) [%s, %s] -> destfname.(%s)\n",jprint(argjson,0),passphrase,fname2fa,destfname); //printf("shared.%llx -> pub.%s\n",(long long)wallet2shared.txid,bits256_str(str,wallet2pub)); SuperNET_savejsonfile(destfname,wallethash,wallet2pub,argjson); return(0); @@ -320,7 +320,7 @@ char *SuperNET_keysinit(struct supernet_info *myinfo,char *argjsonstr) wallethash = wallet2priv = GENESIS_PRIVKEY; coinargs = SuperNET_parsemainargs(myinfo,&wallethash,&wallet2priv,argjsonstr); //printf("wallethash.%s 2.(%s)\n",bits256_str(str,wallethash),bits256_str(str2,wallet2priv)); - if ( (msgjson= SuperNET_decryptedjson(passphrase,sizeof(passphrase),wallethash,fname2fa,sizeof(fname2fa),wallet2priv)) != 0 ) + if ( (msgjson= SuperNET_decryptedjson(destfname,passphrase,sizeof(passphrase),wallethash,fname2fa,sizeof(fname2fa),wallet2priv)) != 0 ) { SuperNET_parsemyinfo(myinfo,msgjson); free_json(msgjson); @@ -398,7 +398,7 @@ THREE_STRINGS(SuperNET,encryptjson,password,permanentfile,payload) TWO_STRINGS(SuperNET,decryptjson,password,permanentfile) { - char pass[8192],fname2[1023]; cJSON *retjson,*obj; bits256 wallethash,wallet2priv; + char pass[8192],fname2[1023],destfname[1024]; cJSON *retjson; bits256 wallethash,wallet2priv; safecopy(pass,password,sizeof(pass)); safecopy(fname2,permanentfile,sizeof(fname2)); wallethash = wallet2priv = GENESIS_PRIVKEY; @@ -406,15 +406,12 @@ TWO_STRINGS(SuperNET,decryptjson,password,permanentfile) wallethash = bits256_conv(pass); if ( strlen(fname2) == sizeof(wallet2priv)*2 && is_hexstr(fname2,(int32_t)sizeof(bits256)*2) > 0 ) wallet2priv = bits256_conv(fname2); - printf("decrypt.(%s %s)\n",pass,fname2); - if ( (retjson= SuperNET_decryptedjson(pass,sizeof(pass),wallethash,fname2,sizeof(fname2),wallet2priv)) != 0 ) + if ( (retjson= SuperNET_decryptedjson(destfname,pass,sizeof(pass),wallethash,fname2,sizeof(fname2),wallet2priv)) != 0 ) { - if ( 0 ) - { - obj = jduplicate(jobj(retjson,"payload")); - jdelete(retjson,"payload"); - jadd(retjson,"result",obj); - } + //printf("decrypt pass.(%s) fname2.(%s) -> destfname.(%s)\n",pass,fname2,destfname); + //obj = jduplicate(jobj(retjson,"payload")); + //jdelete(retjson,"payload"); + //jadd(retjson,"result",obj); return(jprint(retjson,1)); } else return(clonestr("{\"error\":\"couldnt decrypt json file\"}")); diff --git a/iguana/iguana_rpc.c b/iguana/iguana_rpc.c index d593818cf..cb8a8a02f 100755 --- a/iguana/iguana_rpc.c +++ b/iguana/iguana_rpc.c @@ -359,7 +359,7 @@ static char *importwallet(RPCARGS) static char *walletpassphrase(RPCARGS) { - return(sglue3(0,CALLGLUE,"bitcoinrpc","walletpassphrase","passphrase",params[0],"permanentfile",params[2],"timeout",params[1])); + return(sglue3(0,CALLGLUE,"bitcoinrpc","walletpassphrase","password",params[0],"permanentfile",params[2],"timeout",params[1])); } static char *walletpassphrasechange(RPCARGS) diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index a0564f3bb..c52e3cc3d 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -189,6 +189,8 @@ void iguana_walletlock(struct supernet_info *myinfo) memset(&myinfo->persistent_priv,0,sizeof(myinfo->persistent_priv)); memset(myinfo->secret,0,sizeof(myinfo->secret)); memset(myinfo->permanentfile,0,sizeof(myinfo->permanentfile)); + if ( myinfo->decryptstr != 0 ) + free(myinfo->decryptstr), myinfo->decryptstr = 0; myinfo->expiration = 0; //printf("wallet locked\n"); } @@ -335,8 +337,8 @@ int32_t iguana_loginsave(struct supernet_info *myinfo,struct iguana_info *coin,c if ( (passphrase= jstr(loginjson,"passphrase")) != 0 ) { _SuperNET_encryptjson(destfname,passphrase,0,myinfo->permanentfile,0,loginjson); + //printf("loginsave.(%s) <= (%s)\n",destfname,newstr); //iguana_walletlock(myinfo); - printf("loginsave.(%s) <= (%s)\n",destfname,newstr); } free_json(loginjson); return(0); @@ -348,7 +350,9 @@ int32_t iguana_payloadupdate(struct supernet_info *myinfo,struct iguana_info *co cJSON *retjson,*accountobj,*payload,*obj; char *newstr; int32_t retval = -1; if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - payload = cJSON_DetachItemFromObject(retjson,"payload"); + if ( account == 0 || account[0] == 0 ) + account = "default"; + payload = cJSON_DetachItemFromObject(retjson,"wallet"); if ( payload == 0 ) payload = cJSON_CreateObject(); if ( (accountobj= jobj(payload,account)) != 0 && (obj= jobj(accountobj,waddr->coinaddr)) != 0 ) @@ -357,10 +361,15 @@ int32_t iguana_payloadupdate(struct supernet_info *myinfo,struct iguana_info *co free_json(payload); return(0); } - jaddbits256(payload,waddr->coinaddr,waddr->privkey); - jadd(retjson,"payload",payload); + if ( accountobj == 0 ) + { + accountobj = cJSON_CreateObject(); + jaddbits256(accountobj,waddr->coinaddr,waddr->privkey); + jadd(payload,account,accountobj); + } else jaddbits256(accountobj,waddr->coinaddr,waddr->privkey); + jadd(retjson,"wallet",payload); newstr = jprint(retjson,1); - printf("newstr.(%s)\n",newstr); + //printf("newstr.(%s)\n",newstr); retval = iguana_loginsave(myinfo,coin,newstr); free(newstr); } else printf("iguana_payloadupdate: error parsing.(%s)\n",retstr); @@ -676,11 +685,30 @@ ZERO_ARGS(bitcoinrpc,repairwallet) STRING_ARG(bitcoinrpc,dumpwallet,filename) { + char *retstr,*walletstr; cJSON *retjson,*walletobj,*strobj; if ( remoteaddr != 0 ) return(clonestr("{\"error\":\"no remote\"}")); if ( myinfo->expiration != 0 ) - return(SuperNET_login(IGUANA_CALLARGS,myinfo->handle,myinfo->secret,myinfo->permanentfile,0)); - else return(clonestr("{\"error\":\"wallet is locked, cant backup\"}")); + { + if ( (retstr= SuperNET_login(IGUANA_CALLARGS,myinfo->handle,myinfo->secret,myinfo->permanentfile,0)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (walletstr= myinfo->decryptstr) != 0 ) + { + myinfo->decryptstr = 0; + if ( (strobj= cJSON_Parse(walletstr)) != 0 ) + { + if ( (walletobj= jobj(strobj,"wallet")) != 0 ) + jadd(retjson,"wallet",jduplicate(walletobj)); + free_json(strobj); + } + } + return(jprint(retjson,1)); + } else printf("cant parse retstr.(%s)\n",retstr); + } else return(clonestr("{\"error\":\"couldnt decrypt wallet\"}")); + } + return(clonestr("{\"error\":\"wallet is locked, cant backup\"}")); } // RZXuGgmzABFpXRmGJet8AbJoqVGEs27WgdvkSSXUMg7en8jjBW2m 2016-03-26T18:40:06Z reserve=1 # addr=GRVaqhY6XVWGeEabEEx5gE7mAQ7EYQi5JV @@ -713,7 +741,7 @@ STRING_ARG(bitcoinrpc,backupwallet,filename) loginstr = myinfo->decryptstr, myinfo->decryptstr = 0; if ( (retjson= cJSON_Parse(loginstr)) != 0 ) { - if ( (payloadstr= jstr(retjson,"payload")) != 0 ) + if ( (payloadstr= jstr(retjson,"wallet")) != 0 ) retstr = iguana_payloadsave(filename,payloadstr); free_json(retjson); } diff --git a/iguana/tests/backupwallet b/iguana/tests/backupwallet new file mode 100644 index 000000000..c7a3b1e8a --- /dev/null +++ b/iguana/tests/backupwallet @@ -0,0 +1 @@ +curl --url "http://127.0.0.1:7778" --data "{\"method\":\"backupwallet\",\"params\":[\"testwallet\"]}" diff --git a/iguana/tests/dumpwallet b/iguana/tests/dumpwallet new file mode 100755 index 000000000..5ec708ec9 --- /dev/null +++ b/iguana/tests/dumpwallet @@ -0,0 +1 @@ +curl --url "http://127.0.0.1:7778" --data "{\"method\":\"dumpwallet\",\"params\":[]}" diff --git a/iguana/tests/encryptwallet b/iguana/tests/encryptwallet index a06a5151b..1af7926be 100755 --- a/iguana/tests/encryptwallet +++ b/iguana/tests/encryptwallet @@ -1 +1 @@ -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"bitcoinrpc\",\"method\":\"encryptwallet\",\"passphrase\":\"$1\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"bitcoinrpc\",\"method\":\"encryptwallet\",\"passphrase\":\"test\"}" diff --git a/iguana/tests/login b/iguana/tests/login index d7cea6e4c..bd706185e 100755 --- a/iguana/tests/login +++ b/iguana/tests/login @@ -1 +1 @@ -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"SuperNET\",\"method\":\"login\",\"passphrase\":\"$1\"}" +curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"SuperNET\",\"method\":\"login\",\"passphrase\":\"test\"}" diff --git a/iguana/tests/walletpassphrase b/iguana/tests/walletpassphrase index 77b410143..347d3fd52 100755 --- a/iguana/tests/walletpassphrase +++ b/iguana/tests/walletpassphrase @@ -1 +1,2 @@ -curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"bitcoinrpc\",\"method\":\"walletpassphrase\",\"passphrase\":\"$1\",\"timeout\":$2}" +curl --url "http://127.0.0.1:7778" --data "{\"method\":\"walletpassphrase\",\"params\":[\"test\", 600]}" +#curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"bitcoinrpc\",\"method\":\"walletpassphrase\",\"password\":\"test\",\"timeout\":300}"