From ae03da257a88e50c0778013e5305a196b488d741 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 29 Apr 2020 15:44:18 +0200 Subject: [PATCH] ensure keystore source and wallet model is always filled in --- drongo | 2 +- .../sparrow/external/ColdcardMultisig.java | 5 +++++ .../sparrow/external/ColdcardSinglesig.java | 3 +++ .../sparrow/external/Electrum.java | 17 +++++++++++++++++ .../sparrow/wallet/SettingsController.java | 12 ++++++++++-- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/drongo b/drongo index d740895b..cbffaf3e 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit d740895bd68f0ded97e382e5944feb26fa759875 +Subproject commit cbffaf3e416e0d94a01a9c00c99d9f2d61018d1e diff --git a/src/main/java/com/sparrowwallet/sparrow/external/ColdcardMultisig.java b/src/main/java/com/sparrowwallet/sparrow/external/ColdcardMultisig.java index 868f9ab2..bcf0860a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/external/ColdcardMultisig.java +++ b/src/main/java/com/sparrowwallet/sparrow/external/ColdcardMultisig.java @@ -9,6 +9,7 @@ import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; +import com.sparrowwallet.drongo.wallet.KeystoreSource; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletModel; import com.sparrowwallet.sparrow.storage.Storage; @@ -42,6 +43,8 @@ public class ColdcardMultisig implements MultisigWalletImport, KeystoreFileImpor ColdcardKeystore cck = Storage.getStorage().getGson().fromJson(reader, ColdcardKeystore.class); Keystore keystore = new Keystore("Coldcard " + cck.xfp); + keystore.setSource(KeystoreSource.HW_AIRGAPPED); + keystore.setWalletModel(WalletModel.COLDCARD); if(scriptType.equals(ScriptType.P2SH)) { keystore.setKeyDerivation(new KeyDerivation(cck.xfp, cck.p2sh_deriv)); @@ -113,6 +116,8 @@ public class ColdcardMultisig implements MultisigWalletImport, KeystoreFileImpor default: if (key.length() == 8 && Utils.isHex(key)) { Keystore keystore = new Keystore("Coldcard " + key); + keystore.setSource(KeystoreSource.HW_AIRGAPPED); + keystore.setWalletModel(WalletModel.COLDCARD); keystore.setKeyDerivation(new KeyDerivation(key, derivation)); keystore.setExtendedPublicKey(ExtendedPublicKey.fromDescriptor(value)); wallet.getKeystores().add(keystore); diff --git a/src/main/java/com/sparrowwallet/sparrow/external/ColdcardSinglesig.java b/src/main/java/com/sparrowwallet/sparrow/external/ColdcardSinglesig.java index fbd3f688..d69cb2a8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/external/ColdcardSinglesig.java +++ b/src/main/java/com/sparrowwallet/sparrow/external/ColdcardSinglesig.java @@ -8,6 +8,7 @@ import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; +import com.sparrowwallet.drongo.wallet.KeystoreSource; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletModel; @@ -82,6 +83,8 @@ public class ColdcardSinglesig implements KeystoreFileImport, SinglesigWalletImp if(!key.equals("m") && scriptType.getDefaultDerivationPath().startsWith(key)) { ExtendedPublicKey extPubKey = ExtendedPublicKey.fromDescriptor(value); Keystore keystore = new Keystore(); + keystore.setSource(KeystoreSource.HW_AIRGAPPED); + keystore.setWalletModel(WalletModel.COLDCARD); keystore.setKeyDerivation(new KeyDerivation(masterFingerprint, key)); keystore.setExtendedPublicKey(extPubKey); wallet.getKeystores().add(keystore); diff --git a/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java index 6c4ff1c9..d4a3f16c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java @@ -9,6 +9,7 @@ import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; +import com.sparrowwallet.drongo.wallet.KeystoreSource; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletModel; @@ -83,6 +84,20 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult for(ElectrumKeystore ek : ew.keystores.values()) { Keystore keystore = new Keystore(ek.label); + if("hardware".equals(ek.type)) { + keystore.setSource(KeystoreSource.HW_USB); + keystore.setWalletModel(WalletModel.fromType(ek.hw_type)); + if(keystore.getWalletModel() == null) { + throw new ImportException("Unknown hardware wallet type " + ek.hw_type); + } + } else if("bip32".equals(ek.type)) { + if(ek.xprv != null) { + keystore.setSource(KeystoreSource.SW_SEED); + } else { + keystore.setSource(KeystoreSource.SW_WATCH); + } + keystore.setWalletModel(WalletModel.ELECTRUM); + } keystore.setKeyDerivation(new KeyDerivation(ek.root_fingerprint, ek.derivation)); keystore.setExtendedPublicKey(ExtendedPublicKey.fromDescriptor(ek.xpub)); wallet.getKeystores().add(keystore); @@ -185,6 +200,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult public static class ElectrumKeystore { public String xpub; + public String xprv; public String hw_type; public String ckcc_xfp; public String root_fingerprint; @@ -192,5 +208,6 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult public String soft_device_id; public String type; public String derivation; + public String seed; } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index eaa685b4..f19687cd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -6,7 +6,9 @@ import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; +import com.sparrowwallet.drongo.wallet.KeystoreSource; import com.sparrowwallet.drongo.wallet.Wallet; +import com.sparrowwallet.drongo.wallet.WalletModel; import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.CopyableLabel; @@ -120,7 +122,10 @@ public class SettingsController extends WalletFormController implements Initiali while(walletForm.getWallet().getKeystores().stream().map(Keystore::getLabel).collect(Collectors.toList()).contains(name)) { name = "Keystore " + (++keystoreNameCount); } - walletForm.getWallet().getKeystores().add(new Keystore(name)); + Keystore keystore = new Keystore(name); + keystore.setSource(KeystoreSource.SW_WATCH); + keystore.setWalletModel(WalletModel.SPARROW); + walletForm.getWallet().getKeystores().add(keystore); } walletForm.getWallet().setKeystores(walletForm.getWallet().getKeystores().subList(0, numCosigners.intValue())); @@ -170,7 +175,10 @@ public class SettingsController extends WalletFormController implements Initiali if(wallet.getPolicyType() == null) { wallet.setPolicyType(PolicyType.SINGLE); wallet.setScriptType(ScriptType.P2WPKH); - wallet.getKeystores().add(new Keystore("Keystore 1")); + Keystore keystore = new Keystore("Keystore 1"); + keystore.setSource(KeystoreSource.SW_WATCH); + keystore.setWalletModel(WalletModel.SPARROW); + wallet.getKeystores().add(keystore); wallet.setDefaultPolicy(Policy.getPolicy(wallet.getPolicyType(), wallet.getScriptType(), wallet.getKeystores(), 1)); }