mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2026-08-11 17:18:01 +00:00
ecies input and output streams
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.sparrowwallet.sparrow.io;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.sparrowwallet.drongo.crypto.ECKey;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class ECIESInputStream extends FilterInputStream {
|
||||
private boolean decrypted;
|
||||
private final ECKey decryptionKey;
|
||||
private final byte[] encryptionMagic;
|
||||
|
||||
public ECIESInputStream(InputStream in, ECKey decryptionKey, byte[] encryptionMagic) {
|
||||
super(in);
|
||||
|
||||
if(in == null || decryptionKey == null || encryptionMagic == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
this.decryptionKey = decryptionKey;
|
||||
this.encryptionMagic = encryptionMagic;
|
||||
}
|
||||
|
||||
public ECIESInputStream(InputStream in, ECKey decryptionKey) {
|
||||
this(in, decryptionKey, "BIE1".getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
ensureDecrypted();
|
||||
return super.read();
|
||||
}
|
||||
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
ensureDecrypted();
|
||||
return super.read(b, off, len);
|
||||
}
|
||||
|
||||
private synchronized void ensureDecrypted() throws IOException {
|
||||
if(!decrypted) {
|
||||
byte[] encrypted = ByteStreams.toByteArray(in);
|
||||
in.close();
|
||||
in = new ByteArrayInputStream(decryptionKey.decryptEcies(encrypted, encryptionMagic));
|
||||
decrypted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.sparrowwallet.sparrow.io;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.sparrowwallet.drongo.crypto.ECKey;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class ECIESOutputStream extends FilterOutputStream {
|
||||
private final ECKey encryptionKey;
|
||||
private final byte[] encryptionMagic;
|
||||
private final OutputStream encryptedStream;
|
||||
|
||||
public ECIESOutputStream(OutputStream out, ECKey encryptionKey, byte[] encryptionMagic) {
|
||||
super(new ByteArrayOutputStream());
|
||||
|
||||
encryptedStream = out;
|
||||
|
||||
if(encryptionKey == null || encryptionMagic == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
this.encryptionKey = encryptionKey;
|
||||
this.encryptionMagic = encryptionMagic;
|
||||
}
|
||||
|
||||
public ECIESOutputStream(OutputStream in, ECKey encryptionKey) {
|
||||
this(in, encryptionKey, "BIE1".getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
byte[] unencrypted = ((ByteArrayOutputStream)out).toByteArray();
|
||||
byte[] encryptedWallet = encryptionKey.encryptEcies(unencrypted, encryptionMagic);
|
||||
ByteStreams.copy(new ByteArrayInputStream(encryptedWallet), encryptedStream);
|
||||
encryptedStream.flush();
|
||||
encryptedStream.close();
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult
|
||||
ScriptType scriptType = null;
|
||||
|
||||
for(ElectrumKeystore ek : ew.keystores.values()) {
|
||||
Keystore keystore = new Keystore(ek.label);
|
||||
Keystore keystore = new Keystore(ek.label != null ? ek.label : "Electrum " + ek.root_fingerprint);
|
||||
if("hardware".equals(ek.type)) {
|
||||
keystore.setSource(KeystoreSource.HW_USB);
|
||||
keystore.setWalletModel(WalletModel.fromType(ek.hw_type));
|
||||
@@ -98,8 +98,9 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult
|
||||
}
|
||||
keystore.setWalletModel(WalletModel.ELECTRUM);
|
||||
}
|
||||
ExtendedPublicKey xPub = ExtendedPublicKey.fromDescriptor(ek.xpub);
|
||||
keystore.setKeyDerivation(new KeyDerivation(ek.root_fingerprint, ek.derivation));
|
||||
keystore.setExtendedPublicKey(ExtendedPublicKey.fromDescriptor(ek.xpub));
|
||||
keystore.setExtendedPublicKey(xPub);
|
||||
wallet.getKeystores().add(keystore);
|
||||
|
||||
ExtendedPublicKey.XpubHeader xpubHeader = ExtendedPublicKey.XpubHeader.fromXpub(ek.xpub);
|
||||
|
||||
Reference in New Issue
Block a user