Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement and test api method 'getunusedbsqaddress' #4798

Merged
merged 8 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apitest/docs/build-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ To run all test cases in a package:

To run a single test case:

$ ./gradlew :apitest:test --tests "bisq.apitest.method.GetBalanceTest" -DrunApiTests=true
$ ./gradlew :apitest:test --tests "bisq.apitest.method.wallet.GetBalanceTest" -DrunApiTests=true

To run test cases from Intellij, add two JVM arguments to your JUnit launchers:

Expand Down
5 changes: 5 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.GetTradeRequest;
import bisq.proto.grpc.GetUnusedBsqAddressRequest;
import bisq.proto.grpc.KeepFundsRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.MarketPriceRequest;
Expand Down Expand Up @@ -107,6 +108,10 @@ protected final GetBalanceRequest createBalanceRequest() {
return GetBalanceRequest.newBuilder().build();
}

protected final GetUnusedBsqAddressRequest createGetUnusedBsqAddressRequest() {
return GetUnusedBsqAddressRequest.newBuilder().build();
}

protected final SetWalletPasswordRequest createSetWalletPasswordRequest(String password) {
return SetWalletPasswordRequest.newBuilder().setPassword(password).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package bisq.apitest.method.wallet;

import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.NetworkParameters;

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static bisq.apitest.Scaffold.BitcoinCoreApp.bitcoind;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_MAINNET;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_REGTEST;
import static org.bitcoinj.core.NetworkParameters.PAYMENT_PROTOCOL_ID_TESTNET;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;



import bisq.apitest.method.MethodTest;

// @Disabled
@Slf4j
@TestMethodOrder(OrderAnnotation.class)
public class BsqWalletTest extends MethodTest {

@BeforeAll
public static void setUp() {
startSupportingApps(false,
true,
bitcoind,
seednode,
alicedaemon);
}

@Test
@Order(1)
public void testGetUnusedBsqAddress() {
var request = createGetUnusedBsqAddressRequest();

String address = grpcStubs(alicedaemon).walletsService.getUnusedBsqAddress(request).getAddress();
assertFalse(address.isEmpty());
assertTrue(address.startsWith("B"));

NetworkParameters networkParameters = LegacyAddress.getParametersFromAddress(address.substring(1));
String addressNetwork = networkParameters.getPaymentProtocolId();
assertNotEquals(PAYMENT_PROTOCOL_ID_MAINNET, addressNetwork);
// TODO Fix bug(?) causing the regtest bsq address network to be evaluated as 'testnet' here.
assertTrue(addressNetwork.equals(PAYMENT_PROTOCOL_ID_TESTNET)
|| addressNetwork.equals(PAYMENT_PROTOCOL_ID_REGTEST));
}

@AfterAll
public static void tearDown() {
tearDownScaffold();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.apitest.method;
package bisq.apitest.method.wallet;

import bisq.proto.grpc.GetBalanceRequest;

Expand All @@ -36,6 +36,10 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;



import bisq.apitest.method.MethodTest;

@Disabled
@Slf4j
@TestMethodOrder(OrderAnnotation.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bisq.apitest.method;
package bisq.apitest.method.wallet;

import io.grpc.StatusRuntimeException;

Expand All @@ -18,6 +18,10 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;



import bisq.apitest.method.MethodTest;

@SuppressWarnings("ResultOfMethodCallIgnored")
@Disabled
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


import bisq.apitest.method.MethodTest;
import bisq.apitest.method.WalletProtectionTest;
import bisq.apitest.method.wallet.WalletProtectionTest;

@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
Expand Down
21 changes: 17 additions & 4 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import bisq.proto.grpc.GetOffersRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.GetTradeRequest;
import bisq.proto.grpc.GetUnusedBsqAddressRequest;
import bisq.proto.grpc.GetVersionRequest;
import bisq.proto.grpc.KeepFundsRequest;
import bisq.proto.grpc.LockWalletRequest;
Expand Down Expand Up @@ -90,6 +91,7 @@ private enum Method {
getbalance,
getaddressbalance,
getfundingaddresses,
getunusedbsqaddress,
lockwallet,
unlockwallet,
removewalletpassword,
Expand Down Expand Up @@ -205,6 +207,12 @@ public static void run(String[] args) {
out.println(formatAddressBalanceTbl(reply.getAddressBalanceInfoList()));
return;
}
case getunusedbsqaddress: {
var request = GetUnusedBsqAddressRequest.newBuilder().build();
var reply = walletsService.getUnusedBsqAddress(request);
out.println(reply.getAddress());
return;
}
case createoffer: {
if (nonOptionArgs.size() < 9)
throw new IllegalArgumentException("incorrect parameter count,"
Expand All @@ -223,6 +231,7 @@ public static void run(String[] args) {
marketPriceMargin = new BigDecimal(nonOptionArgs.get(7));
else
fixedPrice = nonOptionArgs.get(7);

var securityDeposit = new BigDecimal(nonOptionArgs.get(8));

var request = CreateOfferRequest.newBuilder()
Expand Down Expand Up @@ -283,7 +292,8 @@ public static void run(String[] args) {
}
case takeoffer: {
if (nonOptionArgs.size() < 3)
throw new IllegalArgumentException("incorrect parameter count, expecting offer id, payment acct id");
throw new IllegalArgumentException("incorrect parameter count, "
+ " expecting offer id, payment acct id");

var offerId = nonOptionArgs.get(1);
var paymentAccountId = nonOptionArgs.get(2);
Expand All @@ -297,7 +307,8 @@ public static void run(String[] args) {
}
case gettrade: {
if (nonOptionArgs.size() < 2)
throw new IllegalArgumentException("incorrect parameter count, expecting trade id, [,showcontract = true|false]");
throw new IllegalArgumentException("incorrect parameter count, "
+ " expecting trade id [,showcontract = true|false]");

var tradeId = nonOptionArgs.get(1);
var showContract = false;
Expand Down Expand Up @@ -352,7 +363,8 @@ public static void run(String[] args) {
}
case withdrawfunds: {
if (nonOptionArgs.size() < 3)
throw new IllegalArgumentException("incorrect parameter count, expecting trade id, bitcoin wallet address");
throw new IllegalArgumentException("incorrect parameter count, "
+ " expecting trade id, bitcoin wallet address");

var tradeId = nonOptionArgs.get(1);
var address = nonOptionArgs.get(2);
Expand Down Expand Up @@ -485,6 +497,7 @@ private static void printHelp(OptionParser parser, PrintStream stream) {
stream.format(rowFormat, "getbalance", "", "Get server wallet balance");
stream.format(rowFormat, "getaddressbalance", "address", "Get server wallet address balance");
stream.format(rowFormat, "getfundingaddresses", "", "Get BTC funding addresses");
stream.format(rowFormat, "getunusedbsqaddress", "", "Get unused BSQ address");
stream.format(rowFormat, "createoffer", "payment acct id, buy | sell, currency code, \\", "Create and place an offer");
stream.format(rowFormat, "", "amount (btc), min amount, use mkt based price, \\", "");
stream.format(rowFormat, "", "fixed price (btc) | mkt price margin (%), \\", "");
Expand All @@ -493,7 +506,7 @@ private static void printHelp(OptionParser parser, PrintStream stream) {
stream.format(rowFormat, "getoffer", "offer id", "Get current offer with id");
stream.format(rowFormat, "getoffers", "buy | sell, currency code", "Get current offers");
stream.format(rowFormat, "takeoffer", "offer id", "Take offer with id");
stream.format(rowFormat, "gettrade", "trade id [,showcontract]", "Get trade summary or full contract");
stream.format(rowFormat, "gettrade", "trade id [,showcontract = true|false]", "Get trade summary or full contract");
stream.format(rowFormat, "confirmpaymentstarted", "trade id", "Confirm payment started");
stream.format(rowFormat, "confirmpaymentreceived", "trade id", "Confirm payment received");
stream.format(rowFormat, "keepfunds", "trade id", "Keep received funds in Bisq wallet");
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/api/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ public List<AddressBalanceInfo> getFundingAddresses() {
return walletsService.getFundingAddresses();
}

public String getUnusedBsqAddress() {
return walletsService.getUnusedBsqAddress();
}

public void setWalletPassword(String password, String newPassword) {
walletsService.setWalletPassword(password, newPassword);
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/bisq/core/api/CoreWalletsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.api.model.AddressBalanceInfo;
import bisq.core.btc.Balances;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.WalletsManager;

Expand Down Expand Up @@ -55,6 +56,7 @@ class CoreWalletsService {

private final Balances balances;
private final WalletsManager walletsManager;
private final BsqWalletService bsqWalletService;
private final BtcWalletService btcWalletService;

@Nullable
Expand All @@ -66,9 +68,11 @@ class CoreWalletsService {
@Inject
public CoreWalletsService(Balances balances,
WalletsManager walletsManager,
BsqWalletService bsqWalletService,
BtcWalletService btcWalletService) {
this.balances = balances;
this.walletsManager = walletsManager;
this.bsqWalletService = bsqWalletService;
this.btcWalletService = btcWalletService;
}

Expand Down Expand Up @@ -134,6 +138,10 @@ List<AddressBalanceInfo> getFundingAddresses() {
.collect(Collectors.toList());
}

String getUnusedBsqAddress() {
return bsqWalletService.getUnusedBsqAddressAsString();
}

int getNumConfirmationsForMostRecentTransaction(String addressString) {
Address address = getAddressEntry(addressString).getAddress();
TransactionConfidence confidence = btcWalletService.getConfidenceForAddress(address);
Expand Down
42 changes: 42 additions & 0 deletions core/src/main/java/bisq/core/api/model/BalancesInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bisq.core.api.model;

import bisq.common.Payload;

import lombok.Getter;

@Getter
public class BalancesInfo implements Payload {

private final BsqBalanceInfo bsqBalanceInfo;
private final BtcBalanceInfo btcBalanceInfo;

public BalancesInfo(BsqBalanceInfo bsqBalanceInfo, BtcBalanceInfo btcBalanceInfo) {
this.bsqBalanceInfo = bsqBalanceInfo;
this.btcBalanceInfo = btcBalanceInfo;
}

///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////

@Override
public bisq.proto.grpc.BalancesInfo toProtoMessage() {
return bisq.proto.grpc.BalancesInfo.newBuilder()
.setBsqBalanceInfo(bsqBalanceInfo.toProtoMessage())
.setBtcBalanceInfo(btcBalanceInfo.toProtoMessage())
.build();
}

public static BalancesInfo fromProto(bisq.proto.grpc.BalancesInfo proto) {
return new BalancesInfo(BsqBalanceInfo.fromProto(proto.getBsqBalanceInfo()),
BtcBalanceInfo.fromProto(proto.getBtcBalanceInfo()));
}

@Override
public String toString() {
return "BalancesInfo{" + "\n" +
" " + bsqBalanceInfo.toString() + "\n" +
", " + btcBalanceInfo.toString() + "\n" +
'}';
}
}
Loading