From 200f2368c65d5fd1dae8f6f7ce6307c2e09c16c4 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sat, 8 Aug 2015 16:33:29 +0200 Subject: [PATCH] AddressFormatException: Make unchecked. Base58 strings are rarely typed manually these days. --- .../java/org/bitcoinj/core/AddressFormatException.java | 5 +++-- .../main/java/org/bitcoinj/crypto/DeterministicKey.java | 7 ++----- core/src/test/java/org/bitcoinj/core/Base58Test.java | 4 ++-- core/src/test/java/org/bitcoinj/crypto/BIP32Test.java | 7 +++---- core/src/test/java/org/bitcoinj/script/ScriptTest.java | 4 ++-- .../src/main/java/wallettemplate/SendMoneyController.java | 3 --- 6 files changed, 12 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/AddressFormatException.java b/core/src/main/java/org/bitcoinj/core/AddressFormatException.java index 37a2bdef..86528254 100644 --- a/core/src/main/java/org/bitcoinj/core/AddressFormatException.java +++ b/core/src/main/java/org/bitcoinj/core/AddressFormatException.java @@ -1,5 +1,6 @@ -/** +/* * Copyright 2011 Google Inc. + * 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,7 +18,7 @@ package org.bitcoinj.core; @SuppressWarnings("serial") -public class AddressFormatException extends Exception { +public class AddressFormatException extends IllegalArgumentException { public AddressFormatException() { super(); } diff --git a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java index 442acb66..3272f749 100644 --- a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java +++ b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java @@ -498,13 +498,10 @@ public class DeterministicKey extends ECKey { /** * Deserialize a base-58-encoded HD Key. * @param parent The parent node in the given key's deterministic hierarchy. + * @throws IllegalArgumentException if the base58 encoded key could not be parsed. */ public static DeterministicKey deserializeB58(@Nullable DeterministicKey parent, String base58, NetworkParameters params) { - try { - return deserialize(params, Base58.decodeChecked(base58), parent); - } catch (AddressFormatException e) { - throw new IllegalArgumentException(e); - } + return deserialize(params, Base58.decodeChecked(base58), parent); } /** diff --git a/core/src/test/java/org/bitcoinj/core/Base58Test.java b/core/src/test/java/org/bitcoinj/core/Base58Test.java index 6a89a4d6..c6642bf1 100644 --- a/core/src/test/java/org/bitcoinj/core/Base58Test.java +++ b/core/src/test/java/org/bitcoinj/core/Base58Test.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -84,7 +84,7 @@ public class Base58Test extends TestCase { } @Test - public void testDecodeToBigInteger() throws AddressFormatException { + public void testDecodeToBigInteger() { byte[] input = Base58.decode("129"); assertEquals(new BigInteger(1, input), Base58.decodeToBigInteger("129")); } diff --git a/core/src/test/java/org/bitcoinj/crypto/BIP32Test.java b/core/src/test/java/org/bitcoinj/crypto/BIP32Test.java index 942ec8dd..58c7ef8d 100644 --- a/core/src/test/java/org/bitcoinj/crypto/BIP32Test.java +++ b/core/src/test/java/org/bitcoinj/crypto/BIP32Test.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2013 Matija Mazi. * Copyright 2014 Andreas Schildbach * @@ -17,7 +17,6 @@ package org.bitcoinj.crypto; -import org.bitcoinj.core.AddressFormatException; import org.bitcoinj.core.Base58; import com.google.common.base.Functions; import com.google.common.base.Joiner; @@ -127,7 +126,7 @@ public class BIP32Test { testVector(1); } - private void testVector(int testCase) throws AddressFormatException { + private void testVector(int testCase) { log.info("======= Test vector {}", testCase); HDWTestVector tv = tvs[testCase]; NetworkParameters params = MainNetParams.get(); @@ -146,7 +145,7 @@ public class BIP32Test { } } - private String testEncode(String what) throws AddressFormatException { + private String testEncode(String what) { return HEX.encode(Base58.decodeChecked(what)); } diff --git a/core/src/test/java/org/bitcoinj/script/ScriptTest.java b/core/src/test/java/org/bitcoinj/script/ScriptTest.java index 0467eaee..e60def8c 100644 --- a/core/src/test/java/org/bitcoinj/script/ScriptTest.java +++ b/core/src/test/java/org/bitcoinj/script/ScriptTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2011 Google Inc. * Copyright 2014 Andreas Schildbach * @@ -118,7 +118,7 @@ public class ScriptTest { } @Test - public void testCreateMultiSigInputScript() throws AddressFormatException { + public void testCreateMultiSigInputScript() { // Setup transaction and signatures ECKey key1 = DumpedPrivateKey.fromBase58(params, "cVLwRLTvz3BxDAWkvS3yzT9pUcTCup7kQnfT2smRjvmmm1wAP6QT").getKey(); ECKey key2 = DumpedPrivateKey.fromBase58(params, "cTine92s8GLpVqvebi8rYce3FrUYq78ZGQffBYCS1HmDPJdSTxUo").getKey(); diff --git a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java index ebf40587..6e38bb87 100644 --- a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java +++ b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java @@ -83,9 +83,6 @@ public class SendMoneyController { overlayUI.done(); } catch (ECKey.KeyIsEncryptedException e) { askForPasswordAndRetry(); - } catch (AddressFormatException e) { - // Cannot happen because we already validated it when the text field changed. - throw new RuntimeException(e); } }