wallet encryption and decryption

This commit is contained in:
Craig Raw 2020-05-13 12:46:43 +02:00
parent b951f79cfd
commit 766a986abb
2 changed files with 57 additions and 3 deletions

View file

@ -3,9 +3,8 @@ package com.sparrowwallet.drongo.wallet;
import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.ExtendedKey;
import com.sparrowwallet.drongo.KeyDerivation; import com.sparrowwallet.drongo.KeyDerivation;
import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.crypto.ChildNumber; import com.sparrowwallet.drongo.crypto.*;
import com.sparrowwallet.drongo.crypto.DeterministicKey; import org.bouncycastle.crypto.params.KeyParameter;
import com.sparrowwallet.drongo.crypto.HDKeyDerivation;
import java.util.List; import java.util.List;
@ -146,4 +145,30 @@ public class Keystore {
return 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);
}
}
} }

View file

@ -1,8 +1,11 @@
package com.sparrowwallet.drongo.wallet; 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.Policy;
import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.ScriptType;
import org.bouncycastle.crypto.params.KeyParameter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -114,4 +117,30 @@ public class Wallet {
} }
return copy; 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);
}
}
} }