4444import java .math .BigInteger ;
4545import java .security .NoSuchAlgorithmException ;
4646import java .util .ArrayList ;
47- import java .util .Arrays ;
47+ import java .util .Collections ;
4848import java .util .HashMap ;
4949import java .util .LinkedHashMap ;
5050import java .util .LinkedHashSet ;
@@ -170,7 +170,7 @@ public Wallet recoverFromMnemonic(@Nonnull String mnemonic, @Nonnull String defa
170170
171171 Wallet walletBody = new Wallet ();
172172 HDWallet hdWallet = HDWallet .recoverFromMnemonic (mnemonic , defaultAccountName );
173- walletBody .setHdWallets (Arrays . asList (hdWallet ));
173+ walletBody .setHdWallets (Collections . singletonList (hdWallet ));
174174
175175 walletBaseBody .setWalletBody (walletBody );
176176
@@ -349,16 +349,35 @@ private void saveNewWallet(String email) throws Exception {
349349 }
350350
351351 /**
352- * Saves wallet to server
352+ * Saves wallet to server and forces the upload of the user's addresses to allow notifications
353+ * to work correctly.
354+ *
355+ * @return True if save successful
356+ */
357+ public boolean saveAndSyncPubKeys () throws
358+ HDWalletException ,
359+ EncryptionException ,
360+ NoSuchAlgorithmException ,
361+ IOException {
362+ return save (true );
363+ }
364+
365+ /**
366+ * Saves wallet to server.
367+ *
353368 * @return True if save successful
354- * @throws HDWalletException
355- * @throws NoSuchAlgorithmException
356- * @throws EncryptionException
357- * @throws IOException
358369 */
359- public boolean save ()
360- throws HDWalletException , NoSuchAlgorithmException ,
361- EncryptionException , IOException {
370+ public boolean save () throws
371+ HDWalletException ,
372+ EncryptionException ,
373+ NoSuchAlgorithmException ,
374+ IOException {
375+ return save (false );
376+ }
377+
378+ private boolean save (boolean forcePubKeySync )
379+ throws HDWalletException , NoSuchAlgorithmException ,
380+ EncryptionException , IOException {
362381
363382 validateSave ();
364383 log .info ("Saving wallet" );
@@ -371,28 +390,48 @@ public boolean save()
371390
372391 //Save to server
373392 List <String > syncAddresses = null ;
374- if (walletBaseBody .isSyncPubkeys ()) {
375- syncAddresses = Tools .filterLegacyAddress (
376- LegacyAddress .NORMAL_ADDRESS ,
377- walletBaseBody .getWalletBody ().getLegacyAddressList ());
393+ if (walletBaseBody .isSyncPubkeys () || forcePubKeySync ) {
394+ syncAddresses = new ArrayList <>();
395+
396+ /*
397+ This matches what iOS is doing, but it seems to be massive overkill for mobile
398+ devices. I'm also filtering out archived accounts here because I don't see the point
399+ in sending them.
400+ */
401+ for (Account account : getPayload ().getHdWallets ().get (0 ).getAccounts ()) {
402+ if (!account .isArchived ()) {
403+ HDAccount hdAccount =
404+ getPayload ().getHdWallets ().get (0 ).getHDAccountFromAccountBody (account );
405+ int nextIndex = getNextReceiveAddressIndex (account );
406+
407+ syncAddresses .addAll (
408+ Tools .getReceiveAddressList (hdAccount , nextIndex , nextIndex + 20 ));
409+ }
410+ }
411+
412+ syncAddresses .addAll (
413+ Tools .filterLegacyAddress (
414+ LegacyAddress .NORMAL_ADDRESS ,
415+ walletBaseBody .getWalletBody ().getLegacyAddressList ()));
378416 }
417+
379418 Call <ResponseBody > call = walletApi .updateWallet (
380- walletBaseBody .getWalletBody ().getGuid (),
381- walletBaseBody .getWalletBody ().getSharedKey (),
382- syncAddresses ,
383- payloadWrapper .toJson (),
384- newPayloadChecksum ,
385- oldPayloadChecksum ,
386- BlockchainFramework .getDevice ());
419+ walletBaseBody .getWalletBody ().getGuid (),
420+ walletBaseBody .getWalletBody ().getSharedKey (),
421+ syncAddresses ,
422+ payloadWrapper .toJson (),
423+ newPayloadChecksum ,
424+ oldPayloadChecksum ,
425+ BlockchainFramework .getDevice ());
387426
388427 Response <ResponseBody > exe = call .execute ();
389- if (exe .isSuccessful ()) {
428+ if (exe .isSuccessful ()) {
390429 //set new checksum
391430 walletBaseBody .setPayloadChecksum (newPayloadChecksum );
392431
393432 return true ;
394- } else {
395- log .error ("Save unsuccessful" );
433+ } else {
434+ log .error ("Save unsuccessful: " + exe . errorBody (). string () );
396435 return false ;
397436 }
398437 }
0 commit comments