use of com.sparrowwallet.drongo.crypto.EncryptedData in project sparrow by sparrowwallet.
the class KeystoreDao method updateKeystoreEncryption.
default void updateKeystoreEncryption(Keystore keystore) {
if (keystore.hasMasterPrivateExtendedKey()) {
MasterPrivateExtendedKey mpek = keystore.getMasterPrivateExtendedKey();
if (mpek.isEncrypted()) {
EncryptedData data = mpek.getEncryptedData();
updateMasterPrivateExtendedKey(null, null, data.getInitialisationVector(), data.getEncryptedBytes(), data.getKeySalt(), data.getEncryptionType().getDeriver().ordinal(), data.getEncryptionType().getCrypter().ordinal(), mpek.getCreationTimeSeconds(), mpek.getId());
} else {
updateMasterPrivateExtendedKey(mpek.getPrivateKey().getPrivKeyBytes(), mpek.getPrivateKey().getChainCode(), null, null, null, null, null, mpek.getCreationTimeSeconds(), mpek.getId());
}
}
if (keystore.hasSeed()) {
DeterministicSeed seed = keystore.getSeed();
if (seed.isEncrypted()) {
EncryptedData data = seed.getEncryptedData();
updateSeed(seed.getType().ordinal(), null, data.getInitialisationVector(), data.getEncryptedBytes(), data.getKeySalt(), data.getEncryptionType().getDeriver().ordinal(), data.getEncryptionType().getCrypter().ordinal(), seed.needsPassphrase(), seed.getCreationTimeSeconds(), seed.getId());
} else {
updateSeed(seed.getType().ordinal(), seed.getMnemonicString().asString(), null, null, null, null, null, seed.needsPassphrase(), seed.getCreationTimeSeconds(), seed.getId());
}
}
}
use of com.sparrowwallet.drongo.crypto.EncryptedData in project sparrow by sparrowwallet.
the class KeystoreMapper method map.
@Override
public Keystore map(ResultSet rs, StatementContext ctx) throws SQLException {
Keystore keystore = new Keystore(rs.getString("keystore.label"));
keystore.setId(rs.getLong("keystore.id"));
keystore.setSource(KeystoreSource.values()[rs.getInt("keystore.source")]);
keystore.setWalletModel(WalletModel.values()[rs.getInt("keystore.walletModel")]);
keystore.setKeyDerivation(new KeyDerivation(rs.getString("keystore.masterFingerprint"), rs.getString("keystore.derivationPath")));
keystore.setExtendedPublicKey(rs.getString("keystore.extendedPublicKey") == null ? null : ExtendedKey.fromDescriptor(rs.getString("keystore.extendedPublicKey")));
keystore.setExternalPaymentCode(rs.getString("keystore.externalPaymentCode") == null ? null : PaymentCode.fromString(rs.getString("keystore.externalPaymentCode")));
if (rs.getBytes("masterPrivateExtendedKey.privateKey") != null) {
MasterPrivateExtendedKey masterPrivateExtendedKey = new MasterPrivateExtendedKey(rs.getBytes("masterPrivateExtendedKey.privateKey"), rs.getBytes("masterPrivateExtendedKey.chainCode"));
masterPrivateExtendedKey.setId(rs.getLong("masterPrivateExtendedKey.id"));
keystore.setMasterPrivateExtendedKey(masterPrivateExtendedKey);
} else if (rs.getBytes("masterPrivateExtendedKey.encryptedBytes") != null) {
EncryptedData encryptedData = new EncryptedData(rs.getBytes("masterPrivateExtendedKey.initialisationVector"), rs.getBytes("masterPrivateExtendedKey.encryptedBytes"), rs.getBytes("masterPrivateExtendedKey.keySalt"), EncryptionType.Deriver.values()[rs.getInt("masterPrivateExtendedKey.deriver")], EncryptionType.Crypter.values()[rs.getInt("masterPrivateExtendedKey.crypter")]);
MasterPrivateExtendedKey masterPrivateExtendedKey = new MasterPrivateExtendedKey(encryptedData);
masterPrivateExtendedKey.setId(rs.getLong("masterPrivateExtendedKey.id"));
keystore.setMasterPrivateExtendedKey(masterPrivateExtendedKey);
}
if (rs.getString("seed.mnemonicString") != null) {
List<String> mnemonicCode = Arrays.asList(rs.getString("seed.mnemonicString").split(" "));
DeterministicSeed seed = new DeterministicSeed(mnemonicCode, rs.getBoolean("seed.needsPassphrase"), rs.getLong("seed.creationTimeSeconds"), DeterministicSeed.Type.values()[rs.getInt("seed.type")]);
seed.setId(rs.getLong("seed.id"));
keystore.setSeed(seed);
} else if (rs.getBytes("seed.encryptedBytes") != null) {
EncryptedData encryptedData = new EncryptedData(rs.getBytes("seed.initialisationVector"), rs.getBytes("seed.encryptedBytes"), rs.getBytes("seed.keySalt"), EncryptionType.Deriver.values()[rs.getInt("seed.deriver")], EncryptionType.Crypter.values()[rs.getInt("seed.crypter")]);
DeterministicSeed seed = new DeterministicSeed(encryptedData, rs.getBoolean("seed.needsPassphrase"), rs.getLong("seed.creationTimeSeconds"), DeterministicSeed.Type.values()[rs.getInt("seed.type")]);
seed.setId(rs.getLong("seed.id"));
keystore.setSeed(seed);
}
return keystore;
}
use of com.sparrowwallet.drongo.crypto.EncryptedData in project sparrow by sparrowwallet.
the class ECIESOutputStream method close.
@Override
public void close() throws IOException {
super.close();
byte[] unencrypted = ((ByteArrayOutputStream) out).toByteArray();
ECIESKeyCrypter keyCrypter = new ECIESKeyCrypter();
EncryptedData encryptedData = keyCrypter.encrypt(unencrypted, encryptionMagic, encryptionKey);
ByteStreams.copy(new ByteArrayInputStream(encryptedData.getEncryptedBytes()), encryptedStream);
encryptedStream.flush();
encryptedStream.close();
}
use of com.sparrowwallet.drongo.crypto.EncryptedData in project sparrow by sparrowwallet.
the class KeystoreDao method addKeystores.
default void addKeystores(Wallet wallet) {
for (int i = 0; i < wallet.getKeystores().size(); i++) {
Keystore keystore = wallet.getKeystores().get(i);
if (keystore.hasMasterPrivateExtendedKey()) {
MasterPrivateExtendedKey mpek = keystore.getMasterPrivateExtendedKey();
if (mpek.isEncrypted()) {
EncryptedData data = mpek.getEncryptedData();
long id = insertMasterPrivateExtendedKey(null, null, data.getInitialisationVector(), data.getEncryptedBytes(), data.getKeySalt(), data.getEncryptionType().getDeriver().ordinal(), data.getEncryptionType().getCrypter().ordinal(), mpek.getCreationTimeSeconds());
mpek.setId(id);
} else {
long id = insertMasterPrivateExtendedKey(mpek.getPrivateKey().getPrivKeyBytes(), mpek.getPrivateKey().getChainCode(), null, null, null, null, null, mpek.getCreationTimeSeconds());
mpek.setId(id);
}
}
if (keystore.hasSeed()) {
DeterministicSeed seed = keystore.getSeed();
if (seed.isEncrypted()) {
EncryptedData data = seed.getEncryptedData();
long id = insertSeed(seed.getType().ordinal(), null, data.getInitialisationVector(), data.getEncryptedBytes(), data.getKeySalt(), data.getEncryptionType().getDeriver().ordinal(), data.getEncryptionType().getCrypter().ordinal(), seed.needsPassphrase(), seed.getCreationTimeSeconds());
seed.setId(id);
} else {
long id = insertSeed(seed.getType().ordinal(), seed.getMnemonicString().asString(), null, null, null, null, null, seed.needsPassphrase(), seed.getCreationTimeSeconds());
seed.setId(id);
}
}
long id = insert(truncate(keystore.getLabel()), keystore.getSource().ordinal(), keystore.getWalletModel().ordinal(), keystore.hasMasterPrivateKey() ? null : keystore.getKeyDerivation().getMasterFingerprint(), keystore.getKeyDerivation().getDerivationPath(), keystore.hasMasterPrivateKey() ? null : keystore.getExtendedPublicKey().toString(), keystore.getExternalPaymentCode() == null ? null : keystore.getExternalPaymentCode().toString(), keystore.getMasterPrivateExtendedKey() == null ? null : keystore.getMasterPrivateExtendedKey().getId(), keystore.getSeed() == null ? null : keystore.getSeed().getId(), wallet.getId(), i);
keystore.setId(id);
}
}
use of com.sparrowwallet.drongo.crypto.EncryptedData in project sparrow by sparrowwallet.
the class ECIESInputStream method ensureDecrypted.
private synchronized void ensureDecrypted() throws IOException {
if (!decrypted) {
byte[] encryptedBytes = ByteStreams.toByteArray(in);
in.close();
ECIESKeyCrypter keyCrypter = new ECIESKeyCrypter();
byte[] decryptedBytes = keyCrypter.decrypt(new EncryptedData(encryptionMagic, encryptedBytes, null, null), decryptionKey);
in = new ByteArrayInputStream(decryptedBytes);
decrypted = true;
}
}
Aggregations