ecies input and output streams

This commit is contained in:
Craig Raw 2020-04-30 12:14:25 +02:00
parent 14aa328f6d
commit 981b379615
10 changed files with 161 additions and 6 deletions

View file

@ -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;
}
}
}

View file

@ -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();
}
}

View file

@ -83,7 +83,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult
ScriptType scriptType = null; ScriptType scriptType = null;
for(ElectrumKeystore ek : ew.keystores.values()) { 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)) { if("hardware".equals(ek.type)) {
keystore.setSource(KeystoreSource.HW_USB); keystore.setSource(KeystoreSource.HW_USB);
keystore.setWalletModel(WalletModel.fromType(ek.hw_type)); keystore.setWalletModel(WalletModel.fromType(ek.hw_type));
@ -98,8 +98,9 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult
} }
keystore.setWalletModel(WalletModel.ELECTRUM); keystore.setWalletModel(WalletModel.ELECTRUM);
} }
ExtendedPublicKey xPub = ExtendedPublicKey.fromDescriptor(ek.xpub);
keystore.setKeyDerivation(new KeyDerivation(ek.root_fingerprint, ek.derivation)); keystore.setKeyDerivation(new KeyDerivation(ek.root_fingerprint, ek.derivation));
keystore.setExtendedPublicKey(ExtendedPublicKey.fromDescriptor(ek.xpub)); keystore.setExtendedPublicKey(xPub);
wallet.getKeystores().add(keystore); wallet.getKeystores().add(keystore);
ExtendedPublicKey.XpubHeader xpubHeader = ExtendedPublicKey.XpubHeader.fromXpub(ek.xpub); ExtendedPublicKey.XpubHeader xpubHeader = ExtendedPublicKey.XpubHeader.fromXpub(ek.xpub);

View file

@ -11,7 +11,7 @@ import org.junit.Test;
import java.io.*; import java.io.*;
public class ColdcardMultisigTest extends ImportExportTest { public class ColdcardMultisigTest extends IoTest {
@Test @Test
public void importKeystore1() throws ImportException { public void importKeystore1() throws ImportException {
ColdcardMultisig ccMultisig = new ColdcardMultisig(); ColdcardMultisig ccMultisig = new ColdcardMultisig();

View file

@ -6,7 +6,7 @@ import com.sparrowwallet.drongo.wallet.Wallet;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class ColdcardSinglesigTest extends ImportExportTest { public class ColdcardSinglesigTest extends IoTest {
@Test @Test
public void testImport() throws ImportException { public void testImport() throws ImportException {
ColdcardSinglesig ccSingleSig = new ColdcardSinglesig(); ColdcardSinglesig ccSingleSig = new ColdcardSinglesig();

View file

@ -0,0 +1,28 @@
package com.sparrowwallet.sparrow.io;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.wallet.Wallet;
import org.junit.Assert;
import org.junit.Test;
import java.util.zip.InflaterInputStream;
public class ECIESInputStreamTest extends IoTest {
@Test
public void decrypt() throws ImportException {
Electrum electrum = new Electrum();
ECKey decryptionKey = ECKey.createKeyPbkdf2HmacSha512("pass");
Wallet wallet = electrum.importWallet(new InflaterInputStream(new ECIESInputStream(getInputStream("electrum-encrypted"), decryptionKey)));
Assert.assertEquals(PolicyType.SINGLE, wallet.getPolicyType());
Assert.assertEquals(ScriptType.P2WPKH, wallet.getScriptType());
Assert.assertEquals(1, wallet.getDefaultPolicy().getNumSignaturesRequired());
Assert.assertEquals("pkh(electrum05aba071)", wallet.getDefaultPolicy().getMiniscript().getScript());
Assert.assertEquals("05aba071", wallet.getKeystores().get(0).getKeyDerivation().getMasterFingerprint());
Assert.assertEquals("m/0'", wallet.getKeystores().get(0).getKeyDerivation().getDerivationPath());
Assert.assertEquals("xpub67vv394epQsLhdjNGx7dfgURicP7XwBMuHPTVAMdXcXhDuC9VP8SqVvh2cYqKWm9xoUd6YynWK8JzRcXpmeuZFRH7i1kt8fR9GXoJSiHk1E", wallet.getKeystores().get(0).getExtendedPublicKey().toString());
Assert.assertTrue(wallet.isValid());
}
}

View file

@ -0,0 +1,38 @@
package com.sparrowwallet.sparrow.io;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.policy.PolicyType;
import com.sparrowwallet.drongo.protocol.ScriptType;
import com.sparrowwallet.drongo.wallet.Wallet;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
public class ECIESOutputStreamTest extends IoTest {
@Test
public void encrypt() throws ImportException, ExportException {
Electrum electrum = new Electrum();
ECKey decryptionKey = ECKey.createKeyPbkdf2HmacSha512("pass");
Wallet wallet = electrum.importWallet(new InflaterInputStream(new ECIESInputStream(getInputStream("electrum-encrypted"), decryptionKey)));
ECKey encyptionKey = ECKey.fromPublicOnly(decryptionKey);
ByteArrayOutputStream dummyFileOutputStream = new ByteArrayOutputStream();
electrum.exportWallet(wallet, new DeflaterOutputStream(new ECIESOutputStream(dummyFileOutputStream, encyptionKey)));
ByteArrayInputStream dummyFileInputStream = new ByteArrayInputStream(dummyFileOutputStream.toByteArray());
wallet = electrum.importWallet(new InflaterInputStream(new ECIESInputStream(dummyFileInputStream, decryptionKey)));
Assert.assertEquals(PolicyType.SINGLE, wallet.getPolicyType());
Assert.assertEquals(ScriptType.P2WPKH, wallet.getScriptType());
Assert.assertEquals(1, wallet.getDefaultPolicy().getNumSignaturesRequired());
Assert.assertEquals("pkh(electrum05aba071)", wallet.getDefaultPolicy().getMiniscript().getScript());
Assert.assertEquals("05aba071", wallet.getKeystores().get(0).getKeyDerivation().getMasterFingerprint());
Assert.assertEquals("m/0'", wallet.getKeystores().get(0).getKeyDerivation().getDerivationPath());
Assert.assertEquals("xpub67vv394epQsLhdjNGx7dfgURicP7XwBMuHPTVAMdXcXhDuC9VP8SqVvh2cYqKWm9xoUd6YynWK8JzRcXpmeuZFRH7i1kt8fR9GXoJSiHk1E", wallet.getKeystores().get(0).getExtendedPublicKey().toString());
Assert.assertTrue(wallet.isValid());
}
}

View file

@ -11,7 +11,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
public class ElectrumTest extends ImportExportTest { public class ElectrumTest extends IoTest {
@Test @Test
public void testSinglesigImport() throws ImportException { public void testSinglesigImport() throws ImportException {
Electrum electrum = new Electrum(); Electrum electrum = new Electrum();

View file

@ -2,7 +2,7 @@ package com.sparrowwallet.sparrow.io;
import java.io.InputStream; import java.io.InputStream;
public class ImportExportTest { public class IoTest {
protected InputStream getInputStream(String filename) { protected InputStream getInputStream(String filename) {
return this.getClass().getResourceAsStream("/com/sparrowwallet/sparrow/io/" + filename); return this.getClass().getResourceAsStream("/com/sparrowwallet/sparrow/io/" + filename);

View file

@ -0,0 +1 @@
QklFMQNn9crHbQuMAnus+PuzTNaAo5bakJojG4CTAaL0dR76ZDSHLuzWWcIDDeUjHJiL3x1D08PHxVHcq2bfo64jcoKn2WoncmubkuPAiGgI6cI3Pj/6Crb7KhqdpG22QOESIDUXsBjUk93qsEr7nTTPnMSfSBChZcdhb4FC7nCF3BL/Jr2ah+p1XgvNcnnSZCxn7DOJODt9n6AoV5qD1e55Kv/vlfzOmIJhKqdVAhLql8IWcwMl82b0W49Dv0gnTaFWJSvNGseNXOs6xE+nXxbn1+T4+SQQprbRH5QR/z7ftCVOEime8QdjOqhPjebSFJeMWkNBDqHhsOuFWF2xM/1q+MLeneihr+hNamBMxcFITkzPyU+faySWJJmXrnd252UnFjzayPT8PajLTXyJpqT5X8Ac2+3rb0NkQwddRjT7pOCUYH48l1iXeUzykJ78K0JNm3kTaOjUGwQEcxdU8uq9Jt9hBVOjcQHBHZZ0rsl2oudLtNRb6WYMWEjEYbDgq9DWMgU1l2T+pYzuLmUYNS8B5vKBmNlq4DdobpNc94OvZ6NDcgUPog2pv5kEuU/1eIIQW2ZNe6WUeFyqj7PTtl2sBUI9s/CY7Phi2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