From 21e2b57c78773f1b0706e7eff24b797d46a2daa8 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Fri, 20 Sep 2024 15:13:01 +0000 Subject: [PATCH] Create bitcoind specific RpcConfig --- .../java/bisq/wallets/json_rpc/RpcConfig.java | 26 +------- .../elementsd/LiquidWalletService.java | 20 ++++-- .../wallets/elementsd/LiquidWalletStore.java | 10 +-- .../AbstractBitcoindWalletService.java | 14 +++- .../bitcoind/BitcoinWalletService.java | 3 +- .../wallets/bitcoind/BitcoinWalletStore.java | 1 - .../java/bisq/wallets/bitcoind/RpcConfig.java | 64 +++++++++++++++++++ .../src/main/proto/wallets.core.proto | 0 8 files changed, 96 insertions(+), 42 deletions(-) create mode 100644 wallets/wallet/src/main/java/bisq/wallets/bitcoind/RpcConfig.java rename wallets/{bitcoind/json-rpc => wallet}/src/main/proto/wallets.core.proto (100%) diff --git a/wallets/bitcoind/json-rpc/src/main/java/bisq/wallets/json_rpc/RpcConfig.java b/wallets/bitcoind/json-rpc/src/main/java/bisq/wallets/json_rpc/RpcConfig.java index 5934cc8783..32c349509b 100644 --- a/wallets/bitcoind/json-rpc/src/main/java/bisq/wallets/json_rpc/RpcConfig.java +++ b/wallets/bitcoind/json-rpc/src/main/java/bisq/wallets/json_rpc/RpcConfig.java @@ -17,38 +17,14 @@ package bisq.wallets.json_rpc; -import bisq.common.proto.PersistableProto; import lombok.Builder; import lombok.Getter; @Builder @Getter -public final class RpcConfig implements PersistableProto { +public final class RpcConfig { private String hostname; private int port; private String user; private String password; - - @Override - public bisq.wallets.protobuf.RpcConfig toProto(boolean serializeForHash) { - return resolveProto(serializeForHash); - } - - @Override - public bisq.wallets.protobuf.RpcConfig.Builder getBuilder(boolean serializeForHash) { - return bisq.wallets.protobuf.RpcConfig.newBuilder() - .setHostname(hostname) - .setPort(port) - .setUser(user) - .setPassword(password); - } - - public static RpcConfig fromProto(bisq.wallets.protobuf.RpcConfig proto) { - return RpcConfig.builder() - .hostname(proto.getHostname()) - .port(proto.getPort()) - .user(proto.getUser()) - .password(proto.getPassword()) - .build(); - } } diff --git a/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletService.java b/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletService.java index d6de32f620..732bd2fb21 100644 --- a/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletService.java +++ b/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletService.java @@ -23,7 +23,7 @@ import bisq.persistence.Persistence; import bisq.persistence.PersistenceService; import bisq.wallets.bitcoind.AbstractBitcoindWalletService; -import bisq.wallets.json_rpc.RpcConfig; +import bisq.wallets.bitcoind.RpcConfig; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -47,19 +47,25 @@ public LiquidWalletService(PersistenceService persistenceService, } @Override - protected LiquidWallet createWallet(RpcConfig rpcConfig) { - return WalletFactory.createLiquidWallet(rpcConfig, walletName, persistableStore); + protected LiquidWallet createWallet(bisq.wallets.bitcoind.RpcConfig rpcConfig) { + return WalletFactory.createLiquidWallet(rpcConfig.toJsonRpcConfig(), walletName, persistableStore); } @Override - protected void persistRpcConfig(RpcConfig rpcConfig) { - persistableStore.setRpcConfig(Optional.of(rpcConfig)); + protected void persistRpcConfig(bisq.wallets.bitcoind.RpcConfig rpcConfig) { + persistableStore.setRpcConfig(Optional.of(rpcConfig.toJsonRpcConfig())); persist(); } @Override - protected Optional getRpcConfigFromPersistableStore() { - return persistableStore.getRpcConfig(); + protected Optional getRpcConfigFromPersistableStore() { + return persistableStore.getRpcConfig().map(jsonRpcConfig -> + RpcConfig.builder() + .hostname(jsonRpcConfig.getHostname()) + .port(jsonRpcConfig.getPort()) + .user(jsonRpcConfig.getUser()) + .password(jsonRpcConfig.getPassword()) + .build()); } @Override diff --git a/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletStore.java b/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletStore.java index 3aba8b2129..a89255a911 100644 --- a/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletStore.java +++ b/wallets/elementsd/src/main/java/bisq/wallets/elementsd/LiquidWalletStore.java @@ -51,8 +51,9 @@ public bisq.wallets.protobuf.LiquidWalletStore.Builder getBuilder(boolean serial bisq.wallets.protobuf.LiquidWalletStore.newBuilder() .addAllWalletAddresses(walletAddresses); - rpcConfig.ifPresent(config -> builder.setRpcConfig(config.toProto(serializeForHash))); - return builder; + throw new UnsupportedOperationException("Not implemented."); + /* rpcConfig.ifPresent(config -> builder.setRpcConfig(config.toProto(serializeForHash))); + return builder; */ } @Override @@ -61,9 +62,10 @@ public bisq.wallets.protobuf.LiquidWalletStore toProto(boolean serializeForHash) } public static LiquidWalletStore fromProto(bisq.wallets.protobuf.LiquidWalletStore proto) { - Optional rpcConfig = proto.hasRpcConfig() ? Optional.of(RpcConfig.fromProto(proto.getRpcConfig())) + throw new UnsupportedOperationException("Not implemented."); + /* Optional rpcConfig = proto.hasRpcConfig() ? Optional.of(RpcConfig.fromProto(proto.getRpcConfig())) : Optional.empty(); - return new LiquidWalletStore(rpcConfig, new ArrayList<>(proto.getWalletAddressesList())); + return new LiquidWalletStore(rpcConfig, new ArrayList<>(proto.getWalletAddressesList())); */ } @Override diff --git a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/AbstractBitcoindWalletService.java b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/AbstractBitcoindWalletService.java index 92cefdec7b..e4e2607190 100644 --- a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/AbstractBitcoindWalletService.java +++ b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/AbstractBitcoindWalletService.java @@ -31,7 +31,6 @@ import bisq.wallets.core.model.Transaction; import bisq.wallets.core.model.TransactionInfo; import bisq.wallets.core.model.Utxo; -import bisq.wallets.json_rpc.RpcConfig; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -115,8 +114,17 @@ public CompletableFuture shutdown() { // WalletService /////////////////////////////////////////////////////////////////////////////////////////////////// + @Override - public CompletableFuture initializeWallet(RpcConfig rpcConfig, Optional walletPassphrase) { + public CompletableFuture initializeWallet(bisq.wallets.json_rpc.RpcConfig jsonRpcConfig, + Optional walletPassphrase) { + RpcConfig rpcConfig = RpcConfig.builder() + .hostname(jsonRpcConfig.getHostname()) + .port(jsonRpcConfig.getPort()) + .user(jsonRpcConfig.getUser()) + .password(jsonRpcConfig.getPassword()) + .build(); + if (wallet.isEmpty()) { boolean isSuccess = verifyRpcConfigAndCreateWallet(Optional.of(rpcConfig)); @@ -242,7 +250,7 @@ private boolean verifyRpcConfigAndCreateWallet(Optional optionalRpcCo if (optionalRpcConfig.isEmpty()) return false; RpcConfig rpcConfig = optionalRpcConfig.get(); - boolean isValidRpcConfig = BitcoindDaemon.verifyRpcConfig(rpcConfig); + boolean isValidRpcConfig = BitcoindDaemon.verifyRpcConfig(rpcConfig.toJsonRpcConfig()); if (isValidRpcConfig) { T walletImpl = createWallet(rpcConfig); wallet = Optional.of(walletImpl); diff --git a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletService.java b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletService.java index a85a36cf11..49e9b039be 100644 --- a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletService.java +++ b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletService.java @@ -22,7 +22,6 @@ import bisq.persistence.DbSubDirectory; import bisq.persistence.Persistence; import bisq.persistence.PersistenceService; -import bisq.wallets.json_rpc.RpcConfig; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -63,7 +62,7 @@ public BitcoinWalletService(Config config, @Override protected BitcoinWallet createWallet(RpcConfig rpcConfig) { - return WalletFactory.createBitcoinWallet(rpcConfig, walletName, persistableStore); + return WalletFactory.createBitcoinWallet(rpcConfig.toJsonRpcConfig(), walletName, persistableStore); } @Override diff --git a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletStore.java b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletStore.java index 3bb580b717..e4e79f89bb 100644 --- a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletStore.java +++ b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/BitcoinWalletStore.java @@ -21,7 +21,6 @@ import bisq.common.proto.ProtoResolver; import bisq.common.proto.UnresolvableProtobufMessageException; import bisq.persistence.PersistableStore; -import bisq.wallets.json_rpc.RpcConfig; import com.google.protobuf.InvalidProtocolBufferException; import lombok.Getter; import lombok.Setter; diff --git a/wallets/wallet/src/main/java/bisq/wallets/bitcoind/RpcConfig.java b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/RpcConfig.java new file mode 100644 index 0000000000..3416323326 --- /dev/null +++ b/wallets/wallet/src/main/java/bisq/wallets/bitcoind/RpcConfig.java @@ -0,0 +1,64 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.wallets.bitcoind; + +import bisq.common.proto.PersistableProto; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public final class RpcConfig implements PersistableProto { + private String hostname; + private int port; + private String user; + private String password; + + @Override + public bisq.wallets.protobuf.RpcConfig toProto(boolean serializeForHash) { + return resolveProto(serializeForHash); + } + + @Override + public bisq.wallets.protobuf.RpcConfig.Builder getBuilder(boolean serializeForHash) { + return bisq.wallets.protobuf.RpcConfig.newBuilder() + .setHostname(hostname) + .setPort(port) + .setUser(user) + .setPassword(password); + } + + public static RpcConfig fromProto(bisq.wallets.protobuf.RpcConfig proto) { + return RpcConfig.builder() + .hostname(proto.getHostname()) + .port(proto.getPort()) + .user(proto.getUser()) + .password(proto.getPassword()) + .build(); + } + + public bisq.wallets.json_rpc.RpcConfig toJsonRpcConfig() { + return bisq.wallets.json_rpc.RpcConfig.builder() + .hostname(hostname) + .port(port) + .user(user) + .password(password) + .build(); + } +} + diff --git a/wallets/bitcoind/json-rpc/src/main/proto/wallets.core.proto b/wallets/wallet/src/main/proto/wallets.core.proto similarity index 100% rename from wallets/bitcoind/json-rpc/src/main/proto/wallets.core.proto rename to wallets/wallet/src/main/proto/wallets.core.proto