mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-08-01 12:31:23 +00:00
BIP38PrivateKey: Fix Java serialization.
This commit is contained in:
@@ -24,6 +24,10 @@ import com.lambdaworks.crypto.SCrypt;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.text.Normalizer;
|
||||
@@ -37,7 +41,7 @@ import static com.google.common.base.Preconditions.checkState;
|
||||
*/
|
||||
public class BIP38PrivateKey extends VersionedChecksummedBytes {
|
||||
|
||||
public final NetworkParameters params;
|
||||
public transient NetworkParameters params;
|
||||
public final boolean ecMultiply;
|
||||
public final boolean compressed;
|
||||
public final boolean hasLotAndSequence;
|
||||
@@ -175,4 +179,16 @@ public class BIP38PrivateKey extends VersionedChecksummedBytes {
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(super.hashCode(), params);
|
||||
}
|
||||
|
||||
// Java serialization
|
||||
|
||||
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
out.writeUTF(params.getId());
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
params = NetworkParameters.fromID(in.readUTF());
|
||||
}
|
||||
}
|
||||
|
@@ -151,12 +151,21 @@ public class BIP38PrivateKeyTest {
|
||||
|
||||
@Test
|
||||
public void testJavaSerialization() throws Exception {
|
||||
BIP38PrivateKey key = new BIP38PrivateKey(TESTNET, "6PfMmVHn153N3x83Yiy4Nf76dHUkXufe2Adr9Fw5bewrunGNeaw2QCpifb");
|
||||
BIP38PrivateKey testKey = new BIP38PrivateKey(TESTNET,
|
||||
"6PfMmVHn153N3x83Yiy4Nf76dHUkXufe2Adr9Fw5bewrunGNeaw2QCpifb");
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(os).writeObject(key);
|
||||
BIP38PrivateKey keyCopy = (BIP38PrivateKey) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()))
|
||||
.readObject();
|
||||
assertEquals(key, keyCopy);
|
||||
new ObjectOutputStream(os).writeObject(testKey);
|
||||
BIP38PrivateKey testKeyCopy = (BIP38PrivateKey) new ObjectInputStream(
|
||||
new ByteArrayInputStream(os.toByteArray())).readObject();
|
||||
assertEquals(testKey, testKeyCopy);
|
||||
|
||||
BIP38PrivateKey mainKey = new BIP38PrivateKey(MAINNET,
|
||||
"6PfMmVHn153N3x83Yiy4Nf76dHUkXufe2Adr9Fw5bewrunGNeaw2QCpifb");
|
||||
os = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(os).writeObject(mainKey);
|
||||
BIP38PrivateKey mainKeyCopy = (BIP38PrivateKey) new ObjectInputStream(
|
||||
new ByteArrayInputStream(os.toByteArray())).readObject();
|
||||
assertEquals(mainKey, mainKeyCopy);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -168,5 +177,4 @@ public class BIP38PrivateKeyTest {
|
||||
assertEquals(a, b);
|
||||
assertNotSame(a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user