From c51c0dc776abce3e41d18b1dda61dfc5ae5d1b52 Mon Sep 17 00:00:00 2001 From: Bernard Ladenthin Date: Fri, 22 Mar 2024 10:24:01 +0100 Subject: [PATCH] Prevent massive log output. Support 20 bytes scripts. Prevent 32 bytes scripts. --- .../bitcoinaddressfinder/AddressTxtLine.java | 5 ++++ .../bitcoinaddressfinder/FinderTest.java | 3 ++ .../bitcoinaddressfinder/LogLevelChange.java | 30 +++++++++++++++++++ .../staticaddresses/StaticP2SHAddress.java | 13 +++----- .../StaticUnsupportedAddress.java | 15 ++++++++++ 5 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/test/java/net/ladenthin/bitcoinaddressfinder/LogLevelChange.java diff --git a/src/main/java/net/ladenthin/bitcoinaddressfinder/AddressTxtLine.java b/src/main/java/net/ladenthin/bitcoinaddressfinder/AddressTxtLine.java index d61386d..1a323c8 100644 --- a/src/main/java/net/ladenthin/bitcoinaddressfinder/AddressTxtLine.java +++ b/src/main/java/net/ladenthin/bitcoinaddressfinder/AddressTxtLine.java @@ -77,9 +77,14 @@ public AddressToCoin fromLine(String line, KeyUtility keyUtility) { return null; } else if (address.startsWith("bc1")) { // bitcoin Bech32 (P2WSH or P2WPKH) or P2TR + // supported (20 bytes): https://privatekeys.pw/address/bitcoin/bc1qazcm763858nkj2dj986etajv6wquslv8uxwczt SegwitAddress segwitAddress = SegwitAddress.fromBech32(keyUtility.networkParameters, address); byte[] hash = segwitAddress.getHash(); ByteBuffer hash160 = keyUtility.byteBufferUtility.byteArrayToByteBuffer(hash); + if (hash160.limit() != PublicKeyBytes.HASH160_SIZE) { + // unsupported (32 bytes): https://privatekeys.pw/bitcoin/address/bc1qp762gmkychywl4elnuyuwph68hqw0uc2jkzu3ax48zfjkskslpsq8p66gf + return null; + } return new AddressToCoin(hash160, amount); } else if (address.startsWith("fc1")) { // feathercoin Bech32 (P2WSH or P2WPKH) diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java index 1e230a2..ac3b3e5 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java @@ -126,6 +126,9 @@ public void shutdownAndAwaitTermination_producersSetAndInitialized_shutdownCalle // Attention: Change the duration. Finder.AWAIT_DURATION_TERMINATE = AwaitTimeTests.AWAIT_DURATION; + // Attention: During the long duration, this test produce a lot of debug and warn output, prevent it by set the log details + new LogLevelChange().turnOff(); + // arrange CFinder cFinder = new CFinder(); final CProducerJava cProducerJava = new CProducerJava(); diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/LogLevelChange.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/LogLevelChange.java new file mode 100644 index 0000000..df19080 --- /dev/null +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/LogLevelChange.java @@ -0,0 +1,30 @@ +// @formatter:off +/** + * Copyright 2024 Bernard Ladenthin bernard.ladenthin@gmail.com + * + * 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. + * + */ +// @formatter:on +package net.ladenthin.bitcoinaddressfinder; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import org.slf4j.LoggerFactory; + +public class LogLevelChange { + public void turnOff() { + final Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + logger.setLevel(Level.OFF); + } +} diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticP2SHAddress.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticP2SHAddress.java index b3d1ca4..10a176c 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticP2SHAddress.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticP2SHAddress.java @@ -33,17 +33,12 @@ public enum StaticP2SHAddress implements PublicAddress { * https://privatekeys.pw/bitcoin/address/3MSvaVbVFFLML86rt5eqgA9SvW23upaXdY */ BitcoinAndLitecoinDeprecated("3MSvaVbVFFLML86rt5eqgA9SvW23upaXdY", "d8b83ad7bf8795b9ff61464fcf06f156c28e3e1f"), + + // bitcoin Bech32 (P2WSH) (Pay to Witness Script Hash) with 20 bytes /** - * P2TR (Pay-to-Taproot) - * https://bitcoin.stackexchange.com/questions/110995/how-can-i-find-samples-for-p2tr-transactions-on-mainnet + * https://privatekeys.pw/address/bitcoin/bc1qazcm763858nkj2dj986etajv6wquslv8uxwczt */ - BitcoinP2TR("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297", "a37c3903c8d0db6512e2b40b0dffa05e5a3ab73603ce8c9c4b7771e5412328f9"), - // - /** - * P2SH (Pay to Witness Script Hash) - * https://privatekeys.pw/bitcoin/address/bc1qp762gmkychywl4elnuyuwph68hqw0uc2jkzu3ax48zfjkskslpsq8p66gf - */ - BitcoinP2WSH("bc1qp762gmkychywl4elnuyuwph68hqw0uc2jkzu3ax48zfjkskslpsq8p66gf", "0fb4a46ec4c5c8efd73f9f09c706fa3dc0e7f30a9585c8f4d538932b42d0f860"), + BitcoinP2WSH_20_BYTES("bc1qazcm763858nkj2dj986etajv6wquslv8uxwczt", "e8b1bf6a27a1e76929b229f595f64cd381c87d87"), /** * https://privatekeys.pw/dogecoin/address/A8c3xNz2mqsDLFwv5KL5fpH12QEwDaoTXo */ diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticUnsupportedAddress.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticUnsupportedAddress.java index 73b46a8..7bbe7fe 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticUnsupportedAddress.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/staticaddresses/StaticUnsupportedAddress.java @@ -43,6 +43,21 @@ public enum StaticUnsupportedAddress implements PublicAddress { * https://privatekeys.pw/bitcoin-cash/address/d-414f1537e051163a5a558d5e9ee37439 */ BitcoinP2MS("d-414f1537e051163a5a558d5e9ee37439"), + + // Scripts with 32 bytes + /** + * bitcoin Bech32 (P2WSH) (Pay to Witness Script Hash) with 32 bytes + * https://privatekeys.pw/bitcoin/address/bc1qp762gmkychywl4elnuyuwph68hqw0uc2jkzu3ax48zfjkskslpsq8p66gf + * The hash of this script has 32 bytes: 0fb4a46ec4c5c8efd73f9f09c706fa3dc0e7f30a9585c8f4d538932b42d0f860 + */ + BitcoinP2WSH_32_BYTES("bc1qp762gmkychywl4elnuyuwph68hqw0uc2jkzu3ax48zfjkskslpsq8p66gf"), + // P2TR + /** + * P2TR (Pay-to-Taproot) with 32 bytes + * https://bitcoin.stackexchange.com/questions/110995/how-can-i-find-samples-for-p2tr-transactions-on-mainnet + * The hash of this script has 32 bytes: a37c3903c8d0db6512e2b40b0dffa05e5a3ab73603ce8c9c4b7771e5412328f9 + */ + BitcoinP2TR("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297"), // feathercoin Bech32 (P2WSH or P2WPKH) /**