Various cleanup

This commit is contained in:
Mr. Seven 2024-02-20 19:17:40 -05:00
parent 365e2edbf2
commit 9ded663e3b
15 changed files with 38 additions and 43 deletions

View file

@ -61,7 +61,7 @@ public class KeyDerivation {
String[] parsedNodes = path.replace("M", "").replace("m", "").split("/"); String[] parsedNodes = path.replace("M", "").replace("m", "").split("/");
for (String n : parsedNodes) { for (String n : parsedNodes) {
n = n.replaceAll(" ", ""); n = n.replaceAll(" ", "");
if (n.length() == 0) continue; if (n.isEmpty()) continue;
boolean isHard = n.endsWith("H") || n.endsWith("h") || n.endsWith("'"); boolean isHard = n.endsWith("H") || n.endsWith("h") || n.endsWith("'");
if (isHard) n = n.substring(0, n.length() - 1); if (isHard) n = n.substring(0, n.length() - 1);
if (n.equals("*")) n = Integer.toString(wildcardReplacement); if (n.equals("*")) n = Integer.toString(wildcardReplacement);

View file

@ -434,12 +434,12 @@ public class OutputDescriptor {
if(extendedKey.getKey().hasPrivKey()) { if(extendedKey.getKey().hasPrivKey()) {
ExtendedKey privateExtendedKey = extendedKey; ExtendedKey privateExtendedKey = extendedKey;
List<ChildNumber> derivation = keyDerivation.getDerivation(); List<ChildNumber> derivation = keyDerivation.getDerivation();
int depth = derivation.size() == 0 ? scriptType.getDefaultDerivation().size() : derivation.size(); int depth = derivation.isEmpty() ? scriptType.getDefaultDerivation().size() : derivation.size();
DeterministicKey prvKey = extendedKey.getKey(); DeterministicKey prvKey = extendedKey.getKey();
DeterministicKey pubKey = new DeterministicKey(prvKey.getPath(), prvKey.getChainCode(), prvKey.getPubKey(), depth, extendedKey.getParentFingerprint()); DeterministicKey pubKey = new DeterministicKey(prvKey.getPath(), prvKey.getChainCode(), prvKey.getPubKey(), depth, extendedKey.getParentFingerprint());
extendedKey = new ExtendedKey(pubKey, pubKey.getParentFingerprint(), extendedKey.getKeyChildNumber()); extendedKey = new ExtendedKey(pubKey, pubKey.getParentFingerprint(), extendedKey.getKeyChildNumber());
if(derivation.size() == 0) { if(derivation.isEmpty()) {
masterPrivateKeyMap.put(extendedKey, privateExtendedKey); masterPrivateKeyMap.put(extendedKey, privateExtendedKey);
} }
} }

View file

@ -10,7 +10,7 @@ import java.util.Arrays;
* This is not a string but a CharSequence that can be cleared of its memory. * This is not a string but a CharSequence that can be cleared of its memory.
* Important for handling passwords. Represents text that should be kept * Important for handling passwords. Represents text that should be kept
* confidential, such as by deleting it from computer memory when no longer * confidential, such as by deleting it from computer memory when no longer
* needed or garbaged collected. * needed or garbage collected.
*/ */
public class SecureString implements CharSequence { public class SecureString implements CharSequence {

View file

@ -160,22 +160,22 @@ public class Utils {
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */ /** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
public static long readUint32(byte[] bytes, int offset) { public static long readUint32(byte[] bytes, int offset) {
return (bytes[offset] & 0xffl) | return (bytes[offset] & 0xffL) |
((bytes[offset + 1] & 0xffl) << 8) | ((bytes[offset + 1] & 0xffL) << 8) |
((bytes[offset + 2] & 0xffl) << 16) | ((bytes[offset + 2] & 0xffL) << 16) |
((bytes[offset + 3] & 0xffl) << 24); ((bytes[offset + 3] & 0xffL) << 24);
} }
/** Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format. */ /** Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
public static long readInt64(byte[] bytes, int offset) { public static long readInt64(byte[] bytes, int offset) {
return (bytes[offset] & 0xffl) | return (bytes[offset] & 0xffL) |
((bytes[offset + 1] & 0xffl) << 8) | ((bytes[offset + 1] & 0xffL) << 8) |
((bytes[offset + 2] & 0xffl) << 16) | ((bytes[offset + 2] & 0xffL) << 16) |
((bytes[offset + 3] & 0xffl) << 24) | ((bytes[offset + 3] & 0xffL) << 24) |
((bytes[offset + 4] & 0xffl) << 32) | ((bytes[offset + 4] & 0xffL) << 32) |
((bytes[offset + 5] & 0xffl) << 40) | ((bytes[offset + 5] & 0xffL) << 40) |
((bytes[offset + 6] & 0xffl) << 48) | ((bytes[offset + 6] & 0xffL) << 48) |
((bytes[offset + 7] & 0xffl) << 56); ((bytes[offset + 7] & 0xffL) << 56);
} }
/** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */ /** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */
@ -197,10 +197,10 @@ public class Utils {
/** Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. */ /** Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. */
public static long readUint32FromStream(InputStream is) { public static long readUint32FromStream(InputStream is) {
try { try {
return (is.read() & 0xffl) | return (is.read() & 0xffL) |
((is.read() & 0xffl) << 8) | ((is.read() & 0xffL) << 8) |
((is.read() & 0xffl) << 16) | ((is.read() & 0xffL) << 16) |
((is.read() & 0xffl) << 24); ((is.read() & 0xffL) << 24);
} catch (IOException x) { } catch (IOException x) {
throw new RuntimeException(x); throw new RuntimeException(x);
} }

View file

@ -9,10 +9,10 @@ import java.util.List;
public class WatchWallet { public class WatchWallet {
private static final int LOOK_AHEAD_LIMIT = 500; private static final int LOOK_AHEAD_LIMIT = 500;
private String name; private final String name;
private OutputDescriptor outputDescriptor; private final OutputDescriptor outputDescriptor;
private HashMap<Address,List<ChildNumber>> addresses = new HashMap<>(LOOK_AHEAD_LIMIT*2); private final HashMap<Address, List<ChildNumber>> addresses = new HashMap<>(LOOK_AHEAD_LIMIT*2);
public WatchWallet(String name, String descriptor) { public WatchWallet(String name, String descriptor) {
this.name = name; this.name = name;

View file

@ -34,7 +34,7 @@ public class DeterministicHierarchy {
*/ */
public DeterministicKey get(List<ChildNumber> path) throws HDDerivationException { public DeterministicKey get(List<ChildNumber> path) throws HDDerivationException {
if(!keys.containsKey(path)) { if(!keys.containsKey(path)) {
if(path.size() == 0) { if(path.isEmpty()) {
throw new IllegalArgumentException("Can't derive the master key: nothing to derive from."); throw new IllegalArgumentException("Can't derive the master key: nothing to derive from.");
} }

View file

@ -160,7 +160,7 @@ public class DeterministicKey extends ECKey {
/** Returns the last element of the path returned by {@link DeterministicKey#getPath()} */ /** Returns the last element of the path returned by {@link DeterministicKey#getPath()} */
public ChildNumber getChildNumber() { public ChildNumber getChildNumber() {
return childNumberPath.size() == 0 ? ChildNumber.ZERO : childNumberPath.get(childNumberPath.size() - 1); return childNumberPath.isEmpty() ? ChildNumber.ZERO : childNumberPath.get(childNumberPath.size() - 1);
} }
public byte[] getChainCode() { public byte[] getChainCode() {

View file

@ -625,7 +625,7 @@ public class ECKey {
* format generated by signmessage/verifymessage RPCs and GUI menu options. They are intended for humans to verify * format generated by signmessage/verifymessage RPCs and GUI menu options. They are intended for humans to verify
* their communications with each other, hence the base64 format and the fact that the input is text. * their communications with each other, hence the base64 format and the fact that the input is text.
* *
* @param message Some piece of human readable text. * @param message Some piece of human-readable text.
* @param signatureBase64 The Bitcoin-format message signature in base64 * @param signatureBase64 The Bitcoin-format message signature in base64
* @param electrumFormat Whether to generate a key following Electrum's approach of regarding P2SH-P2WSH as the same as P2PKH uncompressed * @param electrumFormat Whether to generate a key following Electrum's approach of regarding P2SH-P2WSH as the same as P2PKH uncompressed
* @throws SignatureException If the public key could not be recovered or if there was a signature format error. * @throws SignatureException If the public key could not be recovered or if there was a signature format error.

View file

@ -132,7 +132,7 @@ public class Base58 {
* @return the decoded data bytes * @return the decoded data bytes
*/ */
public static byte[] decode(String input) { public static byte[] decode(String input) {
if (input.length() == 0) { if (input.isEmpty()) {
return new byte[0]; return new byte[0];
} }
// Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits). // Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits).

View file

@ -126,7 +126,7 @@ public class Bech32 {
/** Encode a Bech32 string. */ /** Encode a Bech32 string. */
public static String encode(String hrp, Encoding encoding, final byte[] values) { public static String encode(String hrp, Encoding encoding, final byte[] values) {
if(hrp.length() < 1) { if(hrp.isEmpty()) {
throw new ProtocolException("Human-readable part is too short"); throw new ProtocolException("Human-readable part is too short");
} }

View file

@ -393,7 +393,7 @@ public class Transaction extends ChildMessage {
} }
public void verify() throws VerificationException { public void verify() throws VerificationException {
if (inputs.size() == 0 || outputs.size() == 0) if (inputs.isEmpty() || outputs.isEmpty())
throw new VerificationException.EmptyInputsOrOutputs(); throw new VerificationException.EmptyInputsOrOutputs();
if (this.getMessageSize() > MAX_BLOCK_SIZE) if (this.getMessageSize() > MAX_BLOCK_SIZE)
throw new VerificationException.LargerThanMaxBlockSize(); throw new VerificationException.LargerThanMaxBlockSize();
@ -464,7 +464,7 @@ public class Transaction extends ChildMessage {
// This step has no purpose beyond being synchronized with Bitcoin Core's bugs. OP_CODESEPARATOR // This step has no purpose beyond being synchronized with Bitcoin Core's bugs. OP_CODESEPARATOR
// is a legacy holdover from a previous, broken design of executing scripts that shipped in Bitcoin 0.1. // is a legacy holdover from a previous, broken design of executing scripts that shipped in Bitcoin 0.1.
// It was seriously flawed and would have let anyone take anyone elses money. Later versions switched to // It was seriously flawed and would have let anyone take anyone else's money. Later versions switched to
// the design we use today where scripts are executed independently but share a stack. This left the // the design we use today where scripts are executed independently but share a stack. This left the
// OP_CODESEPARATOR instruction having no purpose as it was only meant to be used internally, not actually // OP_CODESEPARATOR instruction having no purpose as it was only meant to be used internally, not actually
// ever put into scripts. Deleting OP_CODESEPARATOR is a step that should never be required but if we don't // ever put into scripts. Deleting OP_CODESEPARATOR is a step that should never be required but if we don't

View file

@ -121,7 +121,7 @@ public class Bip39MnemonicCode {
throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words."); throw new MnemonicException.MnemonicLengthException("Word list size must be multiple of three words.");
} }
if (words.size() == 0) { if (words.isEmpty()) {
throw new MnemonicException.MnemonicLengthException("Word list is empty."); throw new MnemonicException.MnemonicLengthException("Word list is empty.");
} }

View file

@ -29,7 +29,7 @@ public class PresetUtxoSelector extends SingleSetUtxoSelector {
@Override @Override
public Collection<BlockTransactionHashIndex> select(long targetValue, Collection<OutputGroup> candidates) { public Collection<BlockTransactionHashIndex> select(long targetValue, Collection<OutputGroup> candidates) {
List<BlockTransactionHashIndex> flattenedCandidates = candidates.stream().flatMap(outputGroup -> outputGroup.getUtxos().stream()).collect(Collectors.toList()); List<BlockTransactionHashIndex> flattenedCandidates = candidates.stream().flatMap(outputGroup -> outputGroup.getUtxos().stream()).toList();
List<BlockTransactionHashIndex> utxos = new ArrayList<>(); List<BlockTransactionHashIndex> utxos = new ArrayList<>();
//Don't use equals() here as we don't want to consider height which may change as txes are confirmed //Don't use equals() here as we don't want to consider height which may change as txes are confirmed

View file

@ -20,7 +20,7 @@ public class SeedQR {
List<Integer> indexes = IntStream.iterate(0, i -> i + 4).limit((int)Math.ceil(seedQr.length() / 4.0)) List<Integer> indexes = IntStream.iterate(0, i -> i + 4).limit((int)Math.ceil(seedQr.length() / 4.0))
.mapToObj(i -> seedQr.substring(i, Math.min(i + 4, seedQr.length()))) .mapToObj(i -> seedQr.substring(i, Math.min(i + 4, seedQr.length())))
.map(Integer::parseInt) .map(Integer::parseInt)
.collect(Collectors.toList()); .toList();
List<String> words = new ArrayList<>(indexes.size()); List<String> words = new ArrayList<>(indexes.size());
for(Integer index : indexes) { for(Integer index : indexes) {

View file

@ -17,9 +17,13 @@ import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
public class WalletTest { public class WalletTest {
private static final String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
private static final String words2 = "chef huge whisper year move obscure post pepper play minute foster lawn";
@Test @Test
public void encryptTest() throws MnemonicException { public void encryptTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
wallet.setPolicyType(PolicyType.SINGLE); wallet.setPolicyType(PolicyType.SINGLE);
@ -107,7 +111,6 @@ public class WalletTest {
@Test @Test
public void p2pkhDerivationTest() throws MnemonicException { public void p2pkhDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
wallet.setPolicyType(PolicyType.SINGLE); wallet.setPolicyType(PolicyType.SINGLE);
@ -124,7 +127,6 @@ public class WalletTest {
@Test @Test
public void p2shP2wpkhDerivationTest() throws MnemonicException { public void p2shP2wpkhDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
wallet.setPolicyType(PolicyType.SINGLE); wallet.setPolicyType(PolicyType.SINGLE);
@ -141,7 +143,6 @@ public class WalletTest {
@Test @Test
public void p2wpkhDerivationTest() throws MnemonicException { public void p2wpkhDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
wallet.setPolicyType(PolicyType.SINGLE); wallet.setPolicyType(PolicyType.SINGLE);
@ -158,10 +159,8 @@ public class WalletTest {
@Test @Test
public void p2shDerivationTest() throws MnemonicException { public void p2shDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
String words2 = "chef huge whisper year move obscure post pepper play minute foster lawn";
DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
@ -185,10 +184,8 @@ public class WalletTest {
@Test @Test
public void p2shP2wshDerivationTest() throws MnemonicException { public void p2shP2wshDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
String words2 = "chef huge whisper year move obscure post pepper play minute foster lawn";
DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
@ -212,10 +209,8 @@ public class WalletTest {
@Test @Test
public void p2wshDerivationTest() throws MnemonicException { public void p2wshDerivationTest() throws MnemonicException {
String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
String words2 = "chef huge whisper year move obscure post pepper play minute foster lawn";
DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39); DeterministicSeed seed2 = new DeterministicSeed(words2, "", 0, DeterministicSeed.Type.BIP39);
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();