mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-23 22:46:56 +01:00
Address: Fix Java serialization and add serialization test case.
This commit is contained in:
parent
8db92ede3b
commit
b1402afe9a
2 changed files with 39 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
/**
|
||||
* Copyright 2011 Google Inc.
|
||||
* Copyright 2014 Giannis Dzegoutanis
|
||||
* Copyright 2015 Andreas Schildbach
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -17,6 +18,10 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import org.bitcoinj.params.Networks;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
||||
|
@ -42,7 +47,7 @@ public class Address extends VersionedChecksummedBytes {
|
|||
*/
|
||||
public static final int LENGTH = 20;
|
||||
|
||||
private final NetworkParameters params;
|
||||
private transient NetworkParameters params;
|
||||
|
||||
/**
|
||||
* Construct an address from parameters, the address version, and the hash160 form. Example:<p>
|
||||
|
@ -176,4 +181,16 @@ public class Address extends VersionedChecksummedBytes {
|
|||
public Address clone() throws CloneNotSupportedException {
|
||||
return (Address) super.clone();
|
||||
}
|
||||
|
||||
// Java serialization
|
||||
|
||||
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
out.writeUTF(params.id);
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
params = NetworkParameters.fromID(in.readUTF());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@ import org.bitcoinj.script.Script;
|
|||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -34,6 +38,23 @@ public class AddressTest {
|
|||
static final NetworkParameters testParams = TestNet3Params.get();
|
||||
static final NetworkParameters mainParams = MainNetParams.get();
|
||||
|
||||
@Test
|
||||
public void testJavaSerialization() throws Exception {
|
||||
Address testAddress = new Address(testParams, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(os).writeObject(testAddress);
|
||||
VersionedChecksummedBytes testAddressCopy = (VersionedChecksummedBytes) new ObjectInputStream(
|
||||
new ByteArrayInputStream(os.toByteArray())).readObject();
|
||||
assertEquals(testAddress, testAddressCopy);
|
||||
|
||||
Address mainAddress = new Address(mainParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
os = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(os).writeObject(mainAddress);
|
||||
VersionedChecksummedBytes mainAddressCopy = (VersionedChecksummedBytes) new ObjectInputStream(
|
||||
new ByteArrayInputStream(os.toByteArray())).readObject();
|
||||
assertEquals(mainAddress, mainAddressCopy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringification() throws Exception {
|
||||
// Test a testnet address.
|
||||
|
|
Loading…
Add table
Reference in a new issue