mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-11 17:55:53 +00:00
Make Address (& super & subs) Cloneable
* Implement Cloneable in VersionedChecksummedBytes * Override clone() in VersionedChecksummedBytes * Override clone() in Address * Add Unit test file for VersionedChecksummedBytes * Add clone unit tests for clone for all subclasses of VersionedChecksummedBytes TODO: Consider overriding clone() in DumpedPrivateKey and BIP38PrivateKey
This commit is contained in:
parent
336b0f6aa2
commit
3456e896ec
@ -168,4 +168,12 @@ public class Address extends VersionedChecksummedBytes {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation narrows the return type to <code>Address</code>.
|
||||
*/
|
||||
@Override
|
||||
public Address clone() throws CloneNotSupportedException {
|
||||
return (Address) super.clone();
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ import com.google.common.base.Objects;
|
||||
* <p>and the result is then Base58 encoded. This format is used for addresses, and private keys exported using the
|
||||
* dumpprivkey command.</p>
|
||||
*/
|
||||
public class VersionedChecksummedBytes implements Serializable {
|
||||
public class VersionedChecksummedBytes implements Serializable, Cloneable {
|
||||
protected final int version;
|
||||
protected byte[] bytes;
|
||||
|
||||
@ -82,6 +82,19 @@ public class VersionedChecksummedBytes implements Serializable {
|
||||
/**
|
||||
* Returns the "version" or "header" byte: the first byte of the data. This is used to disambiguate what the
|
||||
* contents apply to, for example, which network the key or address is valid on.
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* This implementation narrows the return type to <code>VersionedChecksummedBytes</code>
|
||||
* and allows subclasses to throw <code>CloneNotSupportedException</code> even though it
|
||||
* is never thrown by this implementation.
|
||||
*/
|
||||
@Override
|
||||
public VersionedChecksummedBytes clone() throws CloneNotSupportedException {
|
||||
return (VersionedChecksummedBytes) super.clone();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return A positive number between 0 and 255.
|
||||
*/
|
||||
|
@ -168,4 +168,14 @@ public class AddressTest {
|
||||
Address address = Address.fromP2SHScript(mainParams, p2shScript);
|
||||
assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloning() throws Exception {
|
||||
Address a = new Address(testParams, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
|
||||
Address b = a.clone();
|
||||
|
||||
assertEquals(a, b);
|
||||
assertNotSame(a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@ -38,4 +39,15 @@ public class DumpedPrivateKeyTest {
|
||||
.readObject();
|
||||
assertEquals(key, keyCopy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloning() throws Exception {
|
||||
DumpedPrivateKey a = new DumpedPrivateKey(MainNetParams.get(), new ECKey().getPrivKeyBytes(), true);
|
||||
// TODO: Consider overriding clone() in DumpedPrivateKey to narrow the type
|
||||
DumpedPrivateKey b = (DumpedPrivateKey) a.clone();
|
||||
|
||||
assertEquals(a, b);
|
||||
assertNotSame(a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright 2014 BitcoinJ Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class VersionedChecksummedBytesTest {
|
||||
static final NetworkParameters testParams = TestNet3Params.get();
|
||||
static final NetworkParameters mainParams = MainNetParams.get();
|
||||
|
||||
@Test
|
||||
public void stringification() throws Exception {
|
||||
// Test a testnet address.
|
||||
VersionedChecksummedBytes a = new VersionedChecksummedBytes(testParams.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
|
||||
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
|
||||
|
||||
VersionedChecksummedBytes b = new VersionedChecksummedBytes(mainParams.getAddressHeader(), HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
|
||||
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloning() throws Exception {
|
||||
VersionedChecksummedBytes a = new VersionedChecksummedBytes(testParams.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
|
||||
VersionedChecksummedBytes b = a.clone();
|
||||
|
||||
assertEquals(a, b);
|
||||
assertNotSame(a, b);
|
||||
}
|
||||
|
||||
}
|
@ -28,6 +28,7 @@ import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
public class BIP38PrivateKeyTest {
|
||||
|
||||
@ -157,4 +158,15 @@ public class BIP38PrivateKeyTest {
|
||||
.readObject();
|
||||
assertEquals(key, keyCopy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cloning() throws Exception {
|
||||
BIP38PrivateKey a = new BIP38PrivateKey(TESTNET, "6PfMmVHn153N3x83Yiy4Nf76dHUkXufe2Adr9Fw5bewrunGNeaw2QCpifb");
|
||||
// TODO: Consider overriding clone() in BIP38PrivateKey to narrow the type
|
||||
BIP38PrivateKey b = (BIP38PrivateKey) a.clone();
|
||||
|
||||
assertEquals(a, b);
|
||||
assertNotSame(a, b);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user