From 8782688eeb4e2873bed3ab190a95cc0a735b9131 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Wed, 27 Sep 2023 10:22:39 -0700 Subject: [PATCH] BlockChainInfo: methods to convert chain String to/from Network Add static methods for converting the (non-standard) chain name/id String to/from a `BitcoinNetwork` enum. --- .../bitcoin/json/pojo/BlockChainInfo.java | 50 +++++++++++++++++++ .../bitcoin/jsonrpc/BitcoinClient.java | 22 +------- .../bitcoin/services/WalletAppKitService.java | 16 +----- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/cj-btc-json/src/main/java/org/consensusj/bitcoin/json/pojo/BlockChainInfo.java b/cj-btc-json/src/main/java/org/consensusj/bitcoin/json/pojo/BlockChainInfo.java index 3d999aef6..49a694401 100644 --- a/cj-btc-json/src/main/java/org/consensusj/bitcoin/json/pojo/BlockChainInfo.java +++ b/cj-btc-json/src/main/java/org/consensusj/bitcoin/json/pojo/BlockChainInfo.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import org.bitcoinj.base.BitcoinNetwork; +import org.bitcoinj.base.Network; import org.bitcoinj.base.Sha256Hash; import java.math.BigDecimal; @@ -68,4 +69,53 @@ public BigDecimal getVerificationProgress() { public byte[] getChainWork() { return chainWork; } + + /** + * Map a BlockChainInfo chain string to a Network. These strings are different from the standard values + * in {@link BitcoinNetwork#toString()}. + * @param info {@code BlockChainInfo} + * @return the matching network. + */ + public static Network chainToNetwork(BlockChainInfo info) { + Network network; + switch(info.getChain()) { + case "main": + network = BitcoinNetwork.MAINNET; + break; + case "test": + network = BitcoinNetwork.TESTNET; + break; + case "signet": + network = BitcoinNetwork.SIGNET; + break; + case "regtest": + network = BitcoinNetwork.REGTEST; + break; + default: + throw new RuntimeException("BlockChainInfo contains unrecognized Bitcoin network"); + } + return network; + } + + /** + * Map {@link BitcoinNetwork} to a chain-id string. + * Bitcoin Core returns strings that differ from {@link BitcoinNetwork#toString()}. + * @param network bitcoinj enum type + * @return Bitcoin Core-compatible chain string + */ + public static String networkToChainName(BitcoinNetwork network) { + String name; + switch(network) { + case MAINNET: + name = "main"; + break; + case TESTNET: + name = "test"; + break; + default: + name = network.toString(); + break; + }; + return name; + } } diff --git a/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java b/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java index ccdad9633..ebf4fbf83 100644 --- a/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java +++ b/cj-btc-jsonrpc/src/main/java/org/consensusj/bitcoin/jsonrpc/BitcoinClient.java @@ -246,27 +246,7 @@ synchronized int getServerVersion() { * @return A future for the server-side {@code Network} */ private CompletableFuture getNetworkFromServer() { - return getBlockChainInfoAsync().thenApply(info -> { - Network network; - switch(info.getChain()) { - case "main": - network = BitcoinNetwork.MAINNET; - break; - case "test": - network = BitcoinNetwork.TESTNET; - break; - case "signet": - network = BitcoinNetwork.SIGNET; - break; - case "regtest": - network = BitcoinNetwork.REGTEST; - break; - default: - throw new RuntimeException("Server returned unrecognized Bitcoin network"); - - } - return network; - }); + return getBlockChainInfoAsync().thenApply(BlockChainInfo::chainToNetwork); } /** diff --git a/cj-btc-services/src/main/java/org/consensusj/bitcoin/services/WalletAppKitService.java b/cj-btc-services/src/main/java/org/consensusj/bitcoin/services/WalletAppKitService.java index 20c404f72..004da7d48 100644 --- a/cj-btc-services/src/main/java/org/consensusj/bitcoin/services/WalletAppKitService.java +++ b/cj-btc-services/src/main/java/org/consensusj/bitcoin/services/WalletAppKitService.java @@ -274,7 +274,7 @@ public CompletableFuture getconnectioncount() { @Override public CompletableFuture getblockchaininfo() { - return result(new BlockChainInfo(chainName(network), // Chain ID + return result(new BlockChainInfo(BlockChainInfo.networkToChainName(network), // Chain ID kit.chain().getChainHead().getHeight(), // Block processed (same as headers for SPV) kit.chain().getChainHead().getHeight(), // Headers validated kit.chain().getChainHead().getHeader().getHash(), // Best block hash @@ -283,20 +283,6 @@ public CompletableFuture getblockchaininfo() { chainWork)); } - /** - * Map {@link BitcoinNetwork} to a chain-id string. - * Bitcoin Core returns strings that differ from {@link BitcoinNetwork#toString()}. - * @param network bitcoinj enum type - * @return Bitcoin Core-compatible chain string - */ - private String chainName(BitcoinNetwork network) { - return switch(network) { - case MAINNET -> "main"; - case TESTNET -> "test"; - case SIGNET, REGTEST -> network.toString(); - }; - } - @Override public CompletableFuture getnetworkinfo() { byte[] localServices = {};