Skip to content
This repository was archived by the owner on Apr 20, 2022. It is now read-only.

Commit e3545b1

Browse files
authored
Merge pull request #112 from blockchain/development
Development
2 parents f583787 + b3556ff commit e3545b1

File tree

2 files changed

+92
-30
lines changed

2 files changed

+92
-30
lines changed

src/main/java/info/blockchain/wallet/payload/PayloadManager.java

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import java.math.BigInteger;
4545
import java.security.NoSuchAlgorithmException;
4646
import java.util.ArrayList;
47-
import java.util.Arrays;
47+
import java.util.Collections;
4848
import java.util.HashMap;
4949
import java.util.LinkedHashMap;
5050
import 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
}

src/main/java/info/blockchain/wallet/util/Tools.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package info.blockchain.wallet.util;
22

33
import com.google.common.primitives.UnsignedBytes;
4+
45
import info.blockchain.wallet.api.PersistentUrls;
56
import info.blockchain.wallet.bip44.HDAccount;
67
import info.blockchain.wallet.payload.data.LegacyAddress;
7-
import java.math.BigInteger;
8-
import java.util.ArrayList;
9-
import java.util.Collections;
10-
import java.util.Comparator;
11-
import java.util.List;
12-
import javax.annotation.Nonnull;
8+
139
import org.apache.commons.lang3.ArrayUtils;
1410
import org.bitcoinj.core.AddressFormatException;
1511
import org.bitcoinj.core.Base58;
@@ -19,6 +15,14 @@
1915
import org.bitcoinj.core.TransactionOutput;
2016
import org.bitcoinj.params.MainNetParams;
2117

18+
import java.math.BigInteger;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.Comparator;
22+
import java.util.List;
23+
24+
import javax.annotation.Nonnull;
25+
2226
public class Tools {
2327

2428
public static Transaction applyBip69 (Transaction transaction) {
@@ -136,4 +140,23 @@ public static List<String> getAddressList(int chain, String xpub, int startIndex
136140

137141
return list;
138142
}
143+
144+
/**
145+
* Returns a list of receive addresses between two points on the chain.
146+
*
147+
* @param account The {@link HDAccount} that you wish to derive addresses from
148+
* @param startIndex The starting index, probably the next available index
149+
* @param endIndex The finishing index, an arbitrary number away from the starting point
150+
* @return A non-null List of addresses as Strings
151+
*/
152+
public static List<String> getReceiveAddressList(HDAccount account, int startIndex, int endIndex) {
153+
List<String> list = new ArrayList<>();
154+
155+
for (int i = startIndex; i < endIndex; i++) {
156+
list.add(account.getReceive().getAddressAt(i).getAddressString());
157+
}
158+
159+
return list;
160+
}
161+
139162
}

0 commit comments

Comments
 (0)