Commit abd74e7f authored by zeroleak's avatar zeroleak
Browse files

update for whirlpool-client

parent e5f09f06
......@@ -75,11 +75,6 @@
<artifactId>jtorctl</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.8.1</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -125,6 +125,7 @@ public class JavaStompClient implements IStompClient {
}
// set session twice, as we need it for subscribe
stompSession = session;
// notify connected
onConnectOnDisconnectListener.onMessage(null);
}
......
package com.samourai.whirlpool.cli.config;
import com.samourai.api.client.SamouraiApi;
import com.samourai.http.client.IHttpClient;
import com.samourai.stomp.client.IStompClientService;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.wallet.api.backend.BackendServer;
import com.samourai.wallet.util.FormatsUtilGeneric;
import com.samourai.whirlpool.client.utils.ClientUtils;
......@@ -26,7 +26,7 @@ public class CliConfig extends CliConfigFile {
IHttpClient httpClient,
IStompClientService stompClientService,
WhirlpoolWalletPersistHandler persistHandler,
SamouraiApi samouraiApi) {
BackendApi backendApi) {
// check valid
if (autoAggregatePostmix && StringUtils.isEmpty(autoTx0PoolId)) {
......@@ -35,7 +35,7 @@ public class CliConfig extends CliConfigFile {
WhirlpoolWalletConfig config =
super.computeWhirlpoolWalletConfig(
httpClient, stompClientService, persistHandler, samouraiApi);
httpClient, stompClientService, persistHandler, backendApi);
config.setAutoTx0PoolId(autoTx0PoolId);
return config;
}
......
package com.samourai.whirlpool.cli.config;
import com.samourai.api.client.SamouraiApi;
import com.samourai.http.client.IHttpClient;
import com.samourai.stomp.client.IStompClientService;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.whirlpool.cli.beans.CliProxy;
import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.utils.ClientUtils;
......@@ -384,12 +384,12 @@ public abstract class CliConfigFile {
IHttpClient httpClient,
IStompClientService stompClientService,
WhirlpoolWalletPersistHandler persistHandler,
SamouraiApi samouraiApi) {
BackendApi backendApi) {
String serverUrl = computeServerUrl();
NetworkParameters params = server.getParams();
WhirlpoolWalletConfig config =
new WhirlpoolWalletConfig(
httpClient, stompClientService, persistHandler, serverUrl, params, samouraiApi);
httpClient, stompClientService, persistHandler, serverUrl, params, backendApi);
if (!Strings.isEmpty(scode)) {
config.setScode(scode);
}
......
......@@ -2,6 +2,7 @@ package com.samourai.whirlpool.cli.services;
import com.google.common.primitives.Bytes;
import com.samourai.http.client.JavaHttpClient;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.wallet.api.pairing.PairingNetwork;
import com.samourai.wallet.api.pairing.PairingPayload;
import com.samourai.wallet.api.pairing.PairingVersion;
......@@ -117,17 +118,17 @@ public class CliWalletService extends WhirlpoolWalletService {
}
// backend connexion
SamouraiApiService samouraiApiService = computeSamouraiApiService(passphrase);
if (!samouraiApiService.testConnectivity()) {
BackendApi BackendApiService = computeBackendApiService(passphrase);
if (!BackendApiService.testConnectivity()) {
throw new NotifiableException(
"Unable to connect to wallet backend: " + samouraiApiService.getUrlBackend());
"Unable to connect to wallet backend: " + BackendApiService.getUrlBackend());
}
// open wallet
WhirlpoolWalletPersistHandler persistHandler = computePersistHandler(walletIdentifier);
WhirlpoolWalletConfig whirlpoolWalletConfig =
cliConfig.computeWhirlpoolWalletConfig(
httpClient, stompClientService, persistHandler, samouraiApiService);
httpClient, stompClientService, persistHandler, BackendApiService);
WhirlpoolWallet whirlpoolWallet = openWallet(whirlpoolWalletConfig, bip84w);
this.sessionWallet =
new CliWallet(
......@@ -141,13 +142,15 @@ public class CliWalletService extends WhirlpoolWalletService {
return sessionWallet;
}
private SamouraiApiService computeSamouraiApiService(String passphrase) throws Exception {
// decrypt apiKey if any
private BackendApi computeBackendApiService(String passphrase) throws Exception {
String backendUrl = cliConfig.computeBackendUrl();
// dojo apiKey
String dojoApiKey = cliConfig.computeBackendApiKey();
if (dojoApiKey != null) {
dojoApiKey = decryptApiKey(dojoApiKey, passphrase);
}
return new SamouraiApiService(httpClient, cliConfig, dojoApiKey);
return new BackendApi(httpClient, backendUrl, dojoApiKey);
}
private WhirlpoolWalletPersistHandler computePersistHandler(String walletIdentifier)
......
package com.samourai.whirlpool.cli.services;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.samourai.api.client.SamouraiApi;
import com.samourai.http.client.IHttpClient;
import com.samourai.wallet.api.backend.beans.RefreshTokenResponse;
import com.samourai.whirlpool.cli.config.CliConfig;
import com.samourai.whirlpool.cli.utils.OAuthUtils;
import java.lang.invoke.MethodHandles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SamouraiApiService extends SamouraiApi {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String ARG_ACCESS_TOKEN = "at";
private DecodedJWT accessToken;
private DecodedJWT refreshToken;
public SamouraiApiService(IHttpClient httpClient, CliConfig cliConfig, String backendApiKey) {
super(httpClient, cliConfig.computeBackendUrl(), backendApiKey);
}
/*@Override
protected Map<String, String> computeHeaders() throws Exception {
Map<String, String> headers = super.computeHeaders();
if (getApiKey() != null) {
// add auth token
headers.put("Authorization<", "Bearer " + computeAccessToken());
}
return headers;
}*/
@Override
protected String computeAuthUrl(String url) throws Exception {
url = super.computeAuthUrl(url);
if (getApiKey() == null) {
// no apiKey => no auth
return url;
}
url += (url.contains("?") ? "&" : "?") + ARG_ACCESS_TOKEN + "=" + computeAccessToken();
return url;
}
private String computeAccessToken() throws Exception {
if (accessToken != null) {
boolean valid = OAuthUtils.validate(accessToken);
if (log.isDebugEnabled()) {
log.debug(
"accessToken is "
+ (valid ? "VALID" : "EXPIRED")
+ ", expiresAt="
+ accessToken.getExpiresAt());
}
if (valid) {
// accessToken is valid
return accessToken.getToken();
}
}
newAccessToken();
return accessToken.getToken();
}
public void newAccessToken() throws Exception {
if (refreshToken != null) {
boolean valid = OAuthUtils.validate(refreshToken);
if (log.isDebugEnabled()) {
log.debug(
"refreshToken is "
+ (valid ? "VALID" : "EXPIRED")
+ ", expiresAt="
+ refreshToken.getExpiresAt());
}
if (valid) {
// refreshToken is valid => refresh
String accessTokenStr = tokenRefresh(refreshToken.getToken());
this.accessToken = OAuthUtils.decode(accessTokenStr);
return;
}
}
// authenticate
RefreshTokenResponse.Authorization auth = tokenAuthenticate();
this.accessToken = OAuthUtils.decode(auth.access_token);
this.refreshToken = OAuthUtils.decode(auth.refresh_token);
}
}
package com.samourai.whirlpool.cli.services;
import com.samourai.api.client.SamouraiApi;
import com.samourai.wallet.api.backend.SamouraiFeeTarget;
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.client.Bip84ApiWallet;
import com.samourai.wallet.client.Bip84Wallet;
......@@ -48,9 +48,9 @@ public class WalletAggregateService {
Bip84ApiWallet sourceWallet,
Bip84Wallet destinationWallet,
int feeSatPerByte,
SamouraiApi samouraiApi)
BackendApi backendApi)
throws Exception {
return doAggregate(sourceWallet, null, destinationWallet, feeSatPerByte, samouraiApi);
return doAggregate(sourceWallet, null, destinationWallet, feeSatPerByte, backendApi);
}
public boolean toAddress(
......@@ -61,9 +61,9 @@ public class WalletAggregateService {
"aggregate toAddress is disabled on mainnet for security reasons.");
}
int feeSatPerByte = cliWallet.getFee(SamouraiFeeTarget.BLOCKS_2);
SamouraiApi samouraiApi = cliWallet.getConfig().getSamouraiApi();
return doAggregate(sourceWallet, destinationAddress, null, feeSatPerByte, samouraiApi);
int feeSatPerByte = cliWallet.getFee(MinerFeeTarget.BLOCKS_2);
BackendApi backendApi = cliWallet.getConfig().getBackendApi();
return doAggregate(sourceWallet, destinationAddress, null, feeSatPerByte, backendApi);
}
private boolean doAggregate(
......@@ -71,7 +71,7 @@ public class WalletAggregateService {
String destinationAddress,
Bip84Wallet destinationWallet,
int feeSatPerByte,
SamouraiApi samouraiApi)
BackendApi backendApi)
throws Exception {
List<UnspentResponse.UnspentOutput> utxos = sourceWallet.fetchUtxos();
if (utxos.isEmpty()) {
......@@ -100,7 +100,7 @@ public class WalletAggregateService {
}
log.info("Aggregating " + subsetUtxos.size() + " utxos (pass #" + round + ")");
txAggregate(sourceWallet, subsetUtxos, toAddress, feeSatPerByte, samouraiApi);
txAggregate(sourceWallet, subsetUtxos, toAddress, feeSatPerByte, backendApi);
success = true;
ClientUtils.sleepRefreshUtxos(cliConfig.getServer().getParams());
......@@ -115,7 +115,7 @@ public class WalletAggregateService {
List<UnspentResponse.UnspentOutput> postmixUtxos,
String toAddress,
int feeSatPerByte,
SamouraiApi samouraiApi)
BackendApi backendApi)
throws Exception {
List<TransactionOutPoint> spendFromOutPoints = new ArrayList<>();
List<HD_Address> spendFromAddresses = new ArrayList<>();
......@@ -137,7 +137,7 @@ public class WalletAggregateService {
// broadcast
log.info(" • Broadcasting TxAggregate...");
String txHex = ClientUtils.getTxHex(txAggregate);
samouraiApi.pushTx(txHex);
backendApi.pushTx(txHex);
}
public boolean consolidateWallet(CliWallet cliWallet) throws Exception {
......@@ -149,21 +149,21 @@ public class WalletAggregateService {
Bip84ApiWallet premixWallet = cliWallet.getWalletPremix();
Bip84ApiWallet postmixWallet = cliWallet.getWalletPostmix();
int feeSatPerByte = cliWallet.getFee(SamouraiFeeTarget.BLOCKS_2);
SamouraiApi samouraiApi = cliWallet.getConfig().getSamouraiApi();
int feeSatPerByte = cliWallet.getFee(MinerFeeTarget.BLOCKS_2);
BackendApi backendApi = cliWallet.getConfig().getBackendApi();
log.info(" • Consolidating postmix -> deposit...");
toWallet(postmixWallet, depositWallet, feeSatPerByte, samouraiApi);
toWallet(postmixWallet, depositWallet, feeSatPerByte, backendApi);
log.info(" • Consolidating premix -> deposit...");
toWallet(premixWallet, depositWallet, feeSatPerByte, samouraiApi);
toWallet(premixWallet, depositWallet, feeSatPerByte, backendApi);
if (depositWallet.fetchUtxos().size() < 2) {
log.info(" • Consolidating deposit... nothing to aggregate.");
return false;
}
log.info(" • Consolidating deposit...");
boolean success = toWallet(depositWallet, depositWallet, feeSatPerByte, samouraiApi);
boolean success = toWallet(depositWallet, depositWallet, feeSatPerByte, backendApi);
return success;
}
}
package com.samourai.whirlpool.cli.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.lang.invoke.MethodHandles;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OAuthUtils {
private static Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static boolean validate(DecodedJWT token) {
// check expiration
boolean valid = token.getExpiresAt().after(new Date());
return valid;
}
public static DecodedJWT decode(String token) {
return JWT.decode(token);
}
}
......@@ -16,7 +16,7 @@ cli.torConfig.onionBackend = true
cli.apiKey =
cli.seed =
cli.seedAppendPassphrase = true
cli.persistDelay = 2
cli.persistDelay = 4
cli.refreshPoolsDelay = 30
cli.tx0MinConfirmations = 1
cli.proxy =
......
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