From 88dfde3c67edd7c4bd249afee4b3661741be1b0e Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 1 Dec 2020 10:17:29 +0200 Subject: [PATCH] import address labels from electrum wallets --- .../sparrowwallet/sparrow/io/Electrum.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java index 795ebb70..15546a64 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java @@ -4,7 +4,9 @@ import com.google.gson.*; import com.google.gson.reflect.TypeToken; import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.KeyDerivation; +import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Utils; +import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.crypto.*; import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; @@ -91,6 +93,10 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport } } + if(key.equals("addresses")) { + ew.addresses = gson.fromJson(map.get(key), ElectrumAddresses.class); + } + if(key.equals("verified_tx3")) { JsonObject jsonObject = (JsonObject)map.get(key); for(String txKey : jsonObject.keySet()) { @@ -195,7 +201,24 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport blockTransaction.setLabel(ew.labels.get(key)); } } catch(Exception e) { - //not a tx, probably an address + //not a tx - try an address + if(ew.addresses != null) { + try { + Address address = Address.fromString(key); + Map> keyPurposes = Map.of(KeyPurpose.RECEIVE, ew.addresses.receiving, KeyPurpose.CHANGE, ew.addresses.change); + for(KeyPurpose keyPurpose : keyPurposes.keySet()) { + WalletNode purposeNode = wallet.getNode(keyPurpose); + purposeNode.fillToIndex(keyPurposes.get(keyPurpose).size() - 1); + for(WalletNode addressNode : purposeNode.getChildren()) { + if(address.equals(wallet.getAddress(addressNode))) { + addressNode.setLabel(ew.labels.get(key)); + } + } + } + } catch(Exception ex) { + //not an address + } + } } } @@ -342,6 +365,7 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport public String wallet_type; public String seed_type; public Boolean use_encryption; + public ElectrumAddresses addresses; public Map labels = new LinkedHashMap<>(); public Map transactions = new HashMap<>(); } @@ -360,4 +384,9 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport public String passphrase; public Integer pw_hash_version; } + + public static class ElectrumAddresses { + public List receiving; + public List change; + } }