From 766a986abb72ea84317a405b93055abc3d1eaf22 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 13 May 2020 12:46:43 +0200 Subject: [PATCH] wallet encryption and decryption --- .../sparrowwallet/drongo/wallet/Keystore.java | 31 +++++++++++++++++-- .../sparrowwallet/drongo/wallet/Wallet.java | 29 +++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Keystore.java b/src/main/java/com/sparrowwallet/drongo/wallet/Keystore.java index f4a66f5..d9df900 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Keystore.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Keystore.java @@ -3,9 +3,8 @@ package com.sparrowwallet.drongo.wallet; import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.KeyDerivation; import com.sparrowwallet.drongo.Utils; -import com.sparrowwallet.drongo.crypto.ChildNumber; -import com.sparrowwallet.drongo.crypto.DeterministicKey; -import com.sparrowwallet.drongo.crypto.HDKeyDerivation; +import com.sparrowwallet.drongo.crypto.*; +import org.bouncycastle.crypto.params.KeyParameter; import java.util.List; @@ -146,4 +145,30 @@ public class Keystore { return keystore; } + + public boolean isEncrypted() { + return seed != null && seed.isEncrypted(); + } + + public void encrypt(String password) { + KeyCrypter keyCrypter = new ScryptKeyCrypter(); + encrypt(keyCrypter, keyCrypter.deriveKey(password)); + } + + public void encrypt(KeyCrypter keyCrypter, KeyParameter key) { + if(seed != null && !seed.isEncrypted()) { + seed = seed.encrypt(keyCrypter, key); + } + } + + public void decrypt(String password, String passphrase) { + KeyCrypter keyCrypter = new ScryptKeyCrypter(); + decrypt(keyCrypter, passphrase, keyCrypter.deriveKey(password)); + } + + public void decrypt(KeyCrypter keyCrypter, String passphrase, KeyParameter key) { + if(seed != null && seed.isEncrypted()) { + seed = seed.decrypt(keyCrypter, passphrase, key); + } + } } diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index 6cc0c7a..be34359 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -1,8 +1,11 @@ package com.sparrowwallet.drongo.wallet; +import com.sparrowwallet.drongo.crypto.KeyCrypter; +import com.sparrowwallet.drongo.crypto.ScryptKeyCrypter; import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; +import org.bouncycastle.crypto.params.KeyParameter; import java.util.ArrayList; import java.util.Collections; @@ -114,4 +117,30 @@ public class Wallet { } return copy; } + + public boolean isEncrypted() { + for(Keystore keystore : keystores) { + if(keystore.isEncrypted()) { + return true; + } + } + + return false; + } + + public void encrypt(String password) { + KeyCrypter keyCrypter = new ScryptKeyCrypter(); + KeyParameter key = keyCrypter.deriveKey(password); + for(Keystore keystore : keystores) { + keystore.encrypt(keyCrypter, key); + } + } + + public void decrypt(String password, String passphrase) { + KeyCrypter keyCrypter = new ScryptKeyCrypter(); + KeyParameter key = keyCrypter.deriveKey(password); + for(Keystore keystore : keystores) { + keystore.decrypt(keyCrypter, passphrase, key); + } + } }