Commit e6809b0b authored by zeroleak's avatar zeroleak
Browse files

use backendApi.fetchWallet()

parent f3c4ed74
......@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.samourai.code.whirlpool</groupId>
<artifactId>whirlpool-client-cli</artifactId>
<version>develop-SNAPSHOT</version>
<version>fetchWallet-SNAPSHOT</version>
<name>whirlpool-client-cli</name>
<properties>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
......@@ -17,7 +17,7 @@
<dependency>
<groupId>io.samourai.code.whirlpool</groupId>
<artifactId>whirlpool-client</artifactId>
<version>develop-SNAPSHOT</version>
<version>fetchWallet-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
......
package com.samourai.whirlpool.cli.api.protocol.beans;
import com.samourai.wallet.api.backend.beans.UnspentResponse;
import com.samourai.wallet.api.backend.beans.UnspentOutput;
import com.samourai.whirlpool.client.mix.listener.MixStep;
import com.samourai.whirlpool.client.wallet.beans.*;
......@@ -26,7 +26,7 @@ public class ApiUtxo {
private Long lastActivityElapsed;
public ApiUtxo(WhirlpoolUtxo whirlpoolUtxo, int mixsTargetMin) {
UnspentResponse.UnspentOutput utxo = whirlpoolUtxo.getUtxo();
UnspentOutput utxo = whirlpoolUtxo.getUtxo();
this.hash = utxo.tx_hash;
this.index = utxo.tx_output_n;
this.value = utxo.value;
......
......@@ -26,7 +26,6 @@ import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.cli.wallet.CliWallet;
import com.samourai.whirlpool.client.exception.NotifiableException;
import com.samourai.whirlpool.client.utils.ClientUtils;
import com.samourai.whirlpool.client.wallet.WhirlpoolDataService;
import com.samourai.whirlpool.client.wallet.WhirlpoolWallet;
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletService;
......@@ -135,10 +134,9 @@ public class CliWalletService extends WhirlpoolWalletService {
String walletStateFileName = computeIndexFile(walletIdentifier).getAbsolutePath();
String utxoConfigFileName = computeUtxosFile(walletIdentifier).getAbsolutePath();
WhirlpoolDataService dataService = new WhirlpoolDataService(config);
WhirlpoolWallet whirlpoolWallet =
computeWhirlpoolWallet(dataService, bip84w, walletStateFileName, utxoConfigFileName);
computeWhirlpoolWallet(config, bip84w, walletStateFileName, utxoConfigFileName);
CliWallet cliWallet =
new CliWallet(
whirlpoolWallet,
......
......@@ -2,7 +2,7 @@ package com.samourai.whirlpool.cli.services;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.wallet.api.backend.MinerFeeTarget;
import com.samourai.wallet.api.backend.beans.UnspentResponse;
import com.samourai.wallet.api.backend.beans.UnspentOutput;
import com.samourai.wallet.client.Bip84Wallet;
import com.samourai.wallet.hd.HD_Address;
import com.samourai.wallet.segwit.bech32.Bech32UtilGeneric;
......@@ -15,6 +15,7 @@ import com.samourai.whirlpool.client.wallet.beans.WhirlpoolAccount;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java8.util.Lists;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutPoint;
......@@ -72,7 +73,8 @@ public class WalletAggregateService {
int feeSatPerByte,
BackendApi backendApi)
throws Exception {
List<UnspentResponse.UnspentOutput> utxos = backendApi.fetchUtxos(sourceWallet.getZpub());
List<UnspentOutput> utxos =
Lists.of(backendApi.fetchWallet(sourceWallet.getZpub()).unspent_outputs);
if (utxos.isEmpty()) {
// maybe you need to declare zpub as bip84 with /multiaddr?bip84=
log.info("AggregateWallet result: no utxo to aggregate");
......@@ -87,7 +89,7 @@ public class WalletAggregateService {
int round = 0;
int offset = 0;
while (offset < utxos.size()) {
List<UnspentResponse.UnspentOutput> subsetUtxos = new ArrayList<>();
List<UnspentOutput> subsetUtxos = new ArrayList<>();
offset = AGGREGATED_UTXOS_PER_TX * round;
for (int i = offset; i < (offset + AGGREGATED_UTXOS_PER_TX) && i < utxos.size(); i++) {
subsetUtxos.add(utxos.get(i));
......@@ -109,7 +111,7 @@ public class WalletAggregateService {
private void txAggregate(
Bip84Wallet sourceWallet,
List<UnspentResponse.UnspentOutput> postmixUtxos,
List<UnspentOutput> postmixUtxos,
String toAddress,
int feeSatPerByte,
BackendApi backendApi)
......@@ -118,7 +120,7 @@ public class WalletAggregateService {
List<HD_Address> spendFromAddresses = new ArrayList<>();
// spend
for (UnspentResponse.UnspentOutput utxo : postmixUtxos) {
for (UnspentOutput utxo : postmixUtxos) {
spendFromOutPoints.add(utxo.computeOutpoint(params));
spendFromAddresses.add(sourceWallet.getAddressAt(utxo));
}
......
......@@ -4,13 +4,16 @@ import com.samourai.http.client.HttpUsage;
import com.samourai.http.client.JavaHttpClient;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.wallet.api.backend.BackendServer;
import com.samourai.wallet.api.backend.MinerFeeTarget;
import com.samourai.wallet.api.backend.beans.MultiAddrResponse;
import com.samourai.wallet.api.backend.beans.UnspentResponse;
import com.samourai.wallet.api.backend.beans.UnspentOutput;
import com.samourai.wallet.api.backend.beans.WalletResponse;
import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.test.AbstractTest;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java8.util.Lists;
import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
......@@ -43,7 +46,7 @@ public class BackendApiTest extends AbstractTest {
public void fetchAddress() throws Exception {
String zpub = VPUB_1;
MultiAddrResponse.Address address = backendApi.fetchAddress(zpub);
assertEquals(address, zpub, 63, 7, 0);
assertAddressEquals(address, zpub, 63, 7, 1000000);
}
@Test
......@@ -54,25 +57,46 @@ public class BackendApiTest extends AbstractTest {
for (String zpub : zpubs) {
Assertions.assertTrue(addresses.containsKey(zpub));
}
assertEquals(addresses.get(VPUB_1), VPUB_1, 63, 7, 0);
assertEquals(addresses.get(VPUB_2), VPUB_2, 0, 0, 0);
assertAddressEquals(addresses.get(VPUB_1), VPUB_1, 63, 7, 1000000);
assertAddressEquals(addresses.get(VPUB_2), VPUB_2, 0, 0, 0);
}
@Test
public void fetchUtxos() throws Exception {
String zpub = VPUB_1;
List<UnspentResponse.UnspentOutput> unspentOutputs = backendApi.fetchUtxos(zpub);
Assertions.assertEquals(0, unspentOutputs.size());
List<UnspentOutput> unspentOutputs = Lists.of(backendApi.fetchWallet(zpub).unspent_outputs);
Assertions.assertEquals(1, unspentOutputs.size());
}
@Test
public void fetchUtxosMulti() throws Exception {
String[] zpubs = new String[] {VPUB_1, VPUB_2};
List<UnspentResponse.UnspentOutput> unspentOutputs = backendApi.fetchUtxos(zpubs);
Assertions.assertEquals(0, unspentOutputs.size());
List<UnspentOutput> unspentOutputs = Lists.of(backendApi.fetchWallet(zpubs).unspent_outputs);
Assertions.assertEquals(1, unspentOutputs.size());
}
private void assertEquals(
@Test
public void fetchWallet() throws Exception {
String[] zpubs = new String[] {VPUB_1, VPUB_2};
WalletResponse walletResponse = backendApi.fetchWallet(zpubs);
Assertions.assertTrue(walletResponse.unspent_outputs.length > 0);
Map<String, WalletResponse.Address> addressesMap = walletResponse.getAddressesMap();
assertAddressEquals(addressesMap.get(VPUB_1), VPUB_1, 63, 7, 1000000);
assertAddressEquals(addressesMap.get(VPUB_2), VPUB_2, 0, 0, 0);
Assertions.assertTrue(walletResponse.txs.length > 0);
Assertions.assertNotNull(walletResponse.info.latest_block.hash);
Assertions.assertTrue(walletResponse.info.latest_block.height > 0);
Assertions.assertTrue(walletResponse.info.latest_block.time > 0);
for (MinerFeeTarget minerFeeTarget : MinerFeeTarget.values()) {
Assertions.assertTrue(walletResponse.info.fees.get(minerFeeTarget.getValue()) > 0);
}
}
private void assertAddressEquals(
MultiAddrResponse.Address address,
String zpub,
int accountIndex,
......@@ -83,4 +107,16 @@ public class BackendApiTest extends AbstractTest {
Assertions.assertEquals(finalBalance, address.final_balance);
Assertions.assertEquals(zpub, address.address);
}
private void assertAddressEquals(
WalletResponse.Address address,
String zpub,
int accountIndex,
int changeIndex,
int finalBalance) {
Assertions.assertEquals(accountIndex, address.account_index);
Assertions.assertEquals(changeIndex, address.change_index);
Assertions.assertEquals(finalBalance, address.final_balance);
Assertions.assertEquals(zpub, address.address);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment