Skip to content

Commit

Permalink
Merge pull request #595 from alvasw/migrate_to_new_json_rpc_lib
Browse files Browse the repository at this point in the history
Migrate Bitcoin Core and Elements Core to JSON-RPC lib
  • Loading branch information
alvasw authored Dec 12, 2022
2 parents f402e2e + 579013d commit b674e90
Show file tree
Hide file tree
Showing 120 changed files with 817 additions and 832 deletions.
1 change: 1 addition & 0 deletions wallets/bitcoind/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation("bisq:persistence")

implementation project(':core')
implementation project(':json-rpc')

implementation libs.google.guava
implementation libs.jackson.databind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import bisq.wallets.bitcoind.zmq.ZmqWallet;
import bisq.wallets.core.RpcConfig;
import bisq.wallets.core.Wallet;
import bisq.wallets.core.exceptions.WalletInitializationFailedException;
import bisq.wallets.core.model.AddressType;
import bisq.wallets.core.model.Transaction;
import bisq.wallets.core.model.Utxo;
import lombok.Getter;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Optional;

Expand All @@ -51,12 +49,7 @@ public BitcoinWallet(String walletName,
ZmqConnection zmqConnection) {
this.receiveAddresses = receiveAddresses;
this.zmqConnection = zmqConnection;

try {
wallet = new BitcoindWallet(daemon, rpcConfig, walletName);
} catch (MalformedURLException e) {
throw new WalletInitializationFailedException("Couldn't initialize BitcoinWalletService", e);
}
this.wallet = new BitcoindWallet(daemon, rpcConfig, walletName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import bisq.wallets.bitcoind.zmq.ZmqConnection;
import bisq.wallets.bitcoind.zmq.ZmqConnectionFactory;
import bisq.wallets.core.RpcConfig;
import bisq.wallets.core.rpc.DaemonRpcClient;
import bisq.wallets.core.rpc.RpcClientFactory;
import bisq.wallets.json_rpc.JsonRpcClient;

import java.util.List;

Expand All @@ -38,13 +38,13 @@ public static BitcoinWallet createBitcoinWallet(RpcConfig rpcConfig,
}

private static BitcoindDaemon createBitcoindDaemon(RpcConfig rpcConfig) {
DaemonRpcClient rpcClient = RpcClientFactory.createLegacyDaemonRpcClient(rpcConfig);
JsonRpcClient rpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
return new BitcoindDaemon(rpcClient);
}

private static ZmqConnection initializeBitcoindZeroMq(BitcoindDaemon bitcoindDaemon) {
var bitcoindZeroMq = ZmqConnectionFactory.createForBitcoind(bitcoindDaemon);
List<BitcoindGetZmqNotificationsResponse> zmqNotifications = bitcoindDaemon.getZmqNotifications();
List<BitcoindGetZmqNotificationsResponse.Entry> zmqNotifications = bitcoindDaemon.getZmqNotifications();
bitcoindZeroMq.initialize(zmqNotifications);
return bitcoindZeroMq;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@
import bisq.wallets.bitcoind.rpc.responses.BitcoindFinalizePsbtResponse;
import bisq.wallets.bitcoind.rpc.responses.BitcoindGetZmqNotificationsResponse;
import bisq.wallets.core.RpcConfig;
import bisq.wallets.core.exceptions.InvalidRpcCredentialsException;
import bisq.wallets.core.exceptions.RpcCallFailureException;
import bisq.wallets.core.rpc.DaemonRpcClient;
import bisq.wallets.core.rpc.RpcClientFactory;
import bisq.wallets.json_rpc.JsonRpcClient;
import bisq.wallets.json_rpc.RpcCallFailureException;
import bisq.wallets.json_rpc.exceptions.InvalidRpcCredentialsException;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class BitcoindDaemon {
private final DaemonRpcClient rpcClient;
private final JsonRpcClient rpcClient;

public BitcoindDaemon(DaemonRpcClient rpcClient) {
public BitcoindDaemon(JsonRpcClient rpcClient) {
this.rpcClient = rpcClient;
}

Expand Down Expand Up @@ -66,19 +65,19 @@ private void createOrLoadWallet(String walletName, Optional<String> passphrase,
public String combinePsbt(List<String> txs) {
var request = new BitcoindCombinePsbtRpcCall.Request(txs);
var rpcCall = new BitcoindCombinePsbtRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall).getResult();
}

public BitcoindDecodeRawTransactionResponse decodeRawTransaction(String txInHex) {
var request = new BitcoindDecodeRawTransactionRpcCall.Request(txInHex);
var rpcCall = new BitcoindDecodeRawTransactionRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall);
}

public BitcoindFinalizePsbtResponse finalizePsbt(String psbt) {
var request = new BitcoindFinalizePsbtRpcCall.Request(psbt);
var rpcCall = new BitcoindFinalizePsbtRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall);
}

public List<String> generateToAddress(int numberOfBlocksToMine, String addressOfMiner) {
Expand All @@ -87,58 +86,56 @@ public List<String> generateToAddress(int numberOfBlocksToMine, String addressOf
.address(addressOfMiner)
.build();
var rpcCall = new BitcoindGenerateToAddressRpcCall(request);
String[] blockHashes = rpcClient.invokeAndValidate(rpcCall);
return Arrays.asList(blockHashes);
return rpcClient.call(rpcCall).getResult();
}

public String getRawTransaction(String txId) {
var request = new BitcoindGetRawTransactionRpcCall.Request(txId);
var rpcCall = new BitcoindGetRawTransactionRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall).getResult();
}

public String getTxOutProof(List<String> txIds) {
var request = new BitcoindGetTxOutProofRpcCall.Request(txIds);
var rpcCall = new BitcoindGetTxOutProofRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall).getResult();
}

public List<BitcoindGetZmqNotificationsResponse> getZmqNotifications() {
public List<BitcoindGetZmqNotificationsResponse.Entry> getZmqNotifications() {
var rpcCall = new BitcoindGetZmqNotificationsRpcCall();
BitcoindGetZmqNotificationsResponse[] responses = rpcClient.invokeAndValidate(rpcCall);
return Arrays.asList(responses);
BitcoindGetZmqNotificationsResponse responses = rpcClient.call(rpcCall);
return responses.getResult();
}

public List<String> listWallets() {
return listWalletsWithRpcClient(rpcClient);
}

private static List<String> listWalletsWithRpcClient(DaemonRpcClient rpcClient) {
private static List<String> listWalletsWithRpcClient(JsonRpcClient rpcClient) {
var rpcCall = new BitcoindListWalletsRpcCall();
String[] wallets = rpcClient.invokeAndValidate(rpcCall);
return Arrays.asList(wallets);
return rpcClient.call(rpcCall).getResult();
}

public String sendRawTransaction(String hexString) {
var request = new BitcoindSendRawTransactionRpcCall.Request(hexString);
var rpcCall = new BitcoindSendRawTransactionRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
return rpcClient.call(rpcCall).getResult();
}

public void stop() {
var rpcCall = new BitcoindStopRpcCall();
rpcClient.invokeAndValidate(rpcCall);
rpcClient.call(rpcCall);
}

public void unloadWallet(String walletName) {
var request = new BitcoindUnloadWalletRpcCall.Request(walletName);
var rpcCall = new BitcoindUnloadWalletRpcCall(request);
rpcClient.invokeAndValidate(rpcCall);
rpcClient.call(rpcCall);
}

public static boolean verifyRpcConfig(RpcConfig rpcConfig) {
try {
DaemonRpcClient rpcClient = RpcClientFactory.createLegacyDaemonRpcClient(rpcConfig);
JsonRpcClient rpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig);
listWalletsWithRpcClient(rpcClient); // Makes a listwallets RPC call
return true;
} catch (InvalidRpcCredentialsException e) {
Expand All @@ -147,7 +144,7 @@ public static boolean verifyRpcConfig(RpcConfig rpcConfig) {
}

private boolean doesWalletExist(RpcCallFailureException e) {
return e.getCause().getMessage().contains("Database already exists.");
return e.getMessage().contains("Database already exists.");
}

private void createWallet(String walletName, String passphrase, boolean descriptors, boolean disablePrivateKeys, boolean blank) {
Expand All @@ -160,12 +157,12 @@ private void createWallet(String walletName, String passphrase, boolean descript
.build();

var rpcCall = new BitcoindCreateWalletRpcCall(request);
rpcClient.invokeAndValidate(rpcCall);
rpcClient.call(rpcCall);
}

private void loadWallet(String walletName) {
var request = new BitcoindLoadWalletRpcCall.Request(walletName);
var rpcCall = new BitcoindLoadWalletRpcCall(request);
rpcClient.invokeAndValidate(rpcCall);
rpcClient.call(rpcCall);
}
}
Loading

0 comments on commit b674e90

Please sign in to comment.