PrefixedChecksummedBytes: rename to EncodedPrivateKey and update class-level JavaDoc

This commit is contained in:
Andreas Schildbach 2022-08-12 02:02:27 +02:00
parent 64c619d2bf
commit 66b0cc8324
6 changed files with 19 additions and 29 deletions

View file

@ -28,7 +28,7 @@ import java.util.Arrays;
* <p> * <p>
* Note that this is not the same base58 as used by Flickr, which you may find referenced around the Internet. * Note that this is not the same base58 as used by Flickr, which you may find referenced around the Internet.
* <p> * <p>
* You may want to consider working with {@code org.bitcoinj.core.PrefixedChecksummedBytes} instead, which * You may want to consider working with {@code org.bitcoinj.core.EncodedPrivateKey} instead, which
* adds support for testing the prefix and suffix bytes commonly found in addresses. * adds support for testing the prefix and suffix bytes commonly found in addresses.
* <p> * <p>
* Satoshi explains: why base-58 instead of standard base-64 encoding? * Satoshi explains: why base-58 instead of standard base-64 encoding?

View file

@ -31,7 +31,7 @@ public class AddressFormatException extends IllegalArgumentException {
} }
/** /**
* This exception is thrown by {@link Base58}, {@link Bech32} and the {@code PrefixedChecksummedBytes} hierarchy of * This exception is thrown by {@link Base58}, {@link Bech32} and the {@code EncodedPrivateKey} hierarchy of
* classes when you try to decode data and a character isn't valid. You shouldn't allow the user to proceed in this * classes when you try to decode data and a character isn't valid. You shouldn't allow the user to proceed in this
* case. * case.
*/ */
@ -47,7 +47,7 @@ public class AddressFormatException extends IllegalArgumentException {
} }
/** /**
* This exception is thrown by {@link Base58}, {@link Bech32} and the {@code PrefixedChecksummedBytes} hierarchy of * This exception is thrown by {@link Base58}, {@link Bech32} and the {@code EncodedPrivateKey} hierarchy of
* classes when you try to decode data and the data isn't of the right size. You shouldn't allow the user to proceed * classes when you try to decode data and the data isn't of the right size. You shouldn't allow the user to proceed
* in this case. * in this case.
*/ */
@ -62,7 +62,7 @@ public class AddressFormatException extends IllegalArgumentException {
} }
/** /**
* This exception is thrown by {@link Base58}, {@link Bech32} and the {@code PrefixedChecksummedBytes} hierarchy of * This exception is thrown by {@link Base58}, {@link Bech32} and the {@code EncodedPrivateKey} hierarchy of
* classes when you try to decode data and the checksum isn't valid. You shouldn't allow the user to proceed in this * classes when you try to decode data and the checksum isn't valid. You shouldn't allow the user to proceed in this
* case. * case.
*/ */
@ -91,7 +91,7 @@ public class AddressFormatException extends IllegalArgumentException {
} }
/** /**
* This exception is thrown by the {@code PrefixedChecksummedBytes} hierarchy of classes when you try and decode an * This exception is thrown by the {@code EncodedPrivateKey} hierarchy of classes when you try and decode an
* address or private key with an invalid prefix (version header or human-readable part). You shouldn't allow the * address or private key with an invalid prefix (version header or human-readable part). You shouldn't allow the
* user to proceed in this case. * user to proceed in this case.
*/ */
@ -106,7 +106,7 @@ public class AddressFormatException extends IllegalArgumentException {
} }
/** /**
* This exception is thrown by the {@code PrefixedChecksummedBytes} hierarchy of classes when you try and decode an * This exception is thrown by the {@code EncodedPrivateKey} hierarchy of classes when you try and decode an
* address with a prefix (version header or human-readable part) that used by another network (usually: mainnet vs * address with a prefix (version header or human-readable part) that used by another network (usually: mainnet vs
* testnet). You shouldn't allow the user to proceed in this case as they are trying to send money across different * testnet). You shouldn't allow the user to proceed in this case as they are trying to send money across different
* chains, an operation that is guaranteed to destroy the money. * chains, an operation that is guaranteed to destroy the money.

View file

@ -30,7 +30,7 @@ import java.util.Arrays;
* bytes with a header byte and 4 checksum bytes at the end. If there are 33 private key bytes instead of 32, then * bytes with a header byte and 4 checksum bytes at the end. If there are 33 private key bytes instead of 32, then
* the last byte is a discriminator value for the compressed pubkey. * the last byte is a discriminator value for the compressed pubkey.
*/ */
public class DumpedPrivateKey extends PrefixedChecksummedBytes { public class DumpedPrivateKey extends EncodedPrivateKey {
/** /**
* Construct a private key from its Base58 representation. * Construct a private key from its Base58 representation.

View file

@ -23,23 +23,13 @@ import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* <p> * Some form of string-encoded private key. This form is useful for noting them down, e.g. on paper wallets.
* The following format is often used to represent some type of data (e.g. key or hash of key):
* </p>
*
* <pre>
* [prefix] [data bytes] [checksum]
* </pre>
* <p>
* and the result is then encoded with some variant of base. This format is most commonly used for addresses and private
* keys exported using Bitcoin Core's dumpprivkey command.
* </p>
*/ */
public abstract class PrefixedChecksummedBytes { public abstract class EncodedPrivateKey {
protected final NetworkParameters params; protected final NetworkParameters params;
protected final byte[] bytes; protected final byte[] bytes;
protected PrefixedChecksummedBytes(NetworkParameters params, byte[] bytes) { protected EncodedPrivateKey(NetworkParameters params, byte[] bytes) {
this.params = checkNotNull(params); this.params = checkNotNull(params);
this.bytes = checkNotNull(bytes); this.bytes = checkNotNull(bytes);
} }
@ -60,7 +50,7 @@ public abstract class PrefixedChecksummedBytes {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
PrefixedChecksummedBytes other = (PrefixedChecksummedBytes) o; EncodedPrivateKey other = (EncodedPrivateKey) o;
return this.params.equals(other.params) && Arrays.equals(this.bytes, other.bytes); return this.params.equals(other.params) && Arrays.equals(this.bytes, other.bytes);
} }
} }

View file

@ -22,8 +22,8 @@ import org.bitcoinj.base.utils.ByteUtils;
import org.bitcoinj.base.exceptions.AddressFormatException; import org.bitcoinj.base.exceptions.AddressFormatException;
import org.bitcoinj.base.Base58; import org.bitcoinj.base.Base58;
import org.bitcoinj.core.ECKey; import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.EncodedPrivateKey;
import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PrefixedChecksummedBytes;
import org.bitcoinj.base.Sha256Hash; import org.bitcoinj.base.Sha256Hash;
import org.bouncycastle.crypto.generators.SCrypt; import org.bouncycastle.crypto.generators.SCrypt;
@ -41,7 +41,7 @@ import static com.google.common.base.Preconditions.checkState;
* Implementation of <a href="https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki">BIP 38</a> * Implementation of <a href="https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki">BIP 38</a>
* passphrase-protected private keys. Currently, only decryption is supported. * passphrase-protected private keys. Currently, only decryption is supported.
*/ */
public class BIP38PrivateKey extends PrefixedChecksummedBytes { public class BIP38PrivateKey extends EncodedPrivateKey {
public final boolean ecMultiply; public final boolean ecMultiply;
public final boolean compressed; public final boolean compressed;
public final boolean hasLotAndSequence; public final boolean hasLotAndSequence;

View file

@ -34,13 +34,13 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
@RunWith(EasyMockRunner.class) @RunWith(EasyMockRunner.class)
public class PrefixedChecksummedBytesTest { public class EncodedPrivateKeyTest {
@Mock @Mock
NetworkParameters params; NetworkParameters params;
private static class PrefixedChecksummedBytesToTest extends PrefixedChecksummedBytes { private static class EncodedPrivateKeyToTest extends EncodedPrivateKey {
public PrefixedChecksummedBytesToTest(NetworkParameters params, byte[] bytes) { public EncodedPrivateKeyToTest(NetworkParameters params, byte[] bytes) {
super(params, bytes); super(params, bytes);
} }
@ -52,7 +52,7 @@ public class PrefixedChecksummedBytesTest {
@Test @Test
public void equalsContract() { public void equalsContract() {
EqualsVerifier.forClass(PrefixedChecksummedBytes.class) EqualsVerifier.forClass(EncodedPrivateKey.class)
.withPrefabValues(NetworkParameters.class, MainNetParams.get(), TestNet3Params.get()) .withPrefabValues(NetworkParameters.class, MainNetParams.get(), TestNet3Params.get())
.suppress(Warning.NULL_FIELDS) .suppress(Warning.NULL_FIELDS)
.suppress(Warning.TRANSIENT_FIELDS) .suppress(Warning.TRANSIENT_FIELDS)
@ -66,10 +66,10 @@ public class PrefixedChecksummedBytesTest {
expect(params.getAddressHeader()).andReturn(111).andReturn(0); expect(params.getAddressHeader()).andReturn(111).andReturn(0);
replay(params); replay(params);
PrefixedChecksummedBytes a = new PrefixedChecksummedBytesToTest(params, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); EncodedPrivateKey a = new EncodedPrivateKeyToTest(params, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
PrefixedChecksummedBytes b = new PrefixedChecksummedBytesToTest(params, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); EncodedPrivateKey b = new EncodedPrivateKeyToTest(params, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
} }
} }