4
4
import com .samourai .wallet .api .backend .beans .MultiAddrResponse ;
5
5
import com .samourai .wallet .api .backend .beans .RefreshTokenResponse ;
6
6
import com .samourai .wallet .api .backend .beans .UnspentResponse ;
7
+ import com .samourai .wallet .util .oauth .OAuthApi ;
8
+ import com .samourai .wallet .util .oauth .OAuthManager ;
7
9
import org .apache .commons .lang3 .StringUtils ;
8
10
import org .slf4j .Logger ;
9
11
import org .slf4j .LoggerFactory ;
10
12
11
13
import java .util .*;
12
14
13
- public class BackendApi {
15
+ public class BackendApi implements OAuthApi {
14
16
private Logger log = LoggerFactory .getLogger (BackendApi .class );
15
17
16
18
private static final String URL_UNSPENT = "/unspent?active=" ;
17
19
private static final String URL_MULTIADDR = "/multiaddr?active=" ;
18
20
private static final String URL_INIT_BIP84 = "/xpub" ;
19
- private static final String URL_FEES = "/fees" ;
21
+ private static final String URL_MINER_FEES = "/fees" ;
20
22
private static final String URL_PUSHTX = "/pushtx/" ;
21
23
private static final String URL_GET_AUTH_LOGIN = "/auth/login" ;
22
24
private static final String URL_GET_AUTH_REFRESH = "/auth/refresh" ;
23
25
24
26
private IBackendClient httpClient ;
25
27
private String urlBackend ;
26
- private String apiKey ;
28
+ private OAuthManager oAuthManager ;
27
29
28
- public BackendApi (IBackendClient httpClient , String urlBackend , String apiKey ) {
30
+ public BackendApi (IBackendClient httpClient , String urlBackend , OAuthManager oAuthManager ) {
29
31
this .httpClient = httpClient ;
30
32
this .urlBackend = urlBackend ;
31
- this .apiKey = apiKey ; // may be null
33
+ this .oAuthManager = oAuthManager ; // may be null
34
+ if (log .isDebugEnabled ()) {
35
+ String oAuthStr = oAuthManager != null ? "yes" : "no" ;
36
+ log .debug ("urlBackend=" + urlBackend + ", oAuth=" + oAuthStr );
37
+ }
32
38
}
33
39
34
40
public List <UnspentResponse .UnspentOutput > fetchUtxos (String zpub ) throws Exception {
@@ -92,14 +98,14 @@ public void initBip84(String zpub) throws Exception {
92
98
httpClient .postUrlEncoded (url , Void .class , headers , postBody );
93
99
}
94
100
95
- public SamouraiFee fetchFees () throws Exception {
96
- String url = computeAuthUrl (urlBackend + URL_FEES );
101
+ public MinerFee fetchMinerFee () throws Exception {
102
+ String url = computeAuthUrl (urlBackend + URL_MINER_FEES );
97
103
Map <String ,String > headers = computeHeaders ();
98
104
Map <String , Integer > feeResponse = httpClient .getJson (url , Map .class , headers );
99
105
if (feeResponse == null ) {
100
- throw new Exception ("Invalid fee response from server" );
106
+ throw new Exception ("Invalid miner fee response from server" );
101
107
}
102
- return new SamouraiFee (feeResponse );
108
+ return new MinerFee (feeResponse );
103
109
}
104
110
105
111
public void pushTx (String txHex ) throws Exception {
@@ -130,13 +136,48 @@ public void pushTx(String txHex) throws Exception {
130
136
}
131
137
}
132
138
133
- protected RefreshTokenResponse .Authorization tokenAuthenticate () throws Exception {
139
+ public boolean testConnectivity () {
140
+ try {
141
+ fetchMinerFee ();
142
+ return true ;
143
+ } catch (Exception e ) {
144
+ log .error ("" , e );
145
+ return false ;
146
+ }
147
+ }
148
+
149
+ protected Map <String ,String > computeHeaders () throws Exception {
150
+ Map <String ,String > headers = new HashMap <String , String >();
151
+ if (oAuthManager != null ) {
152
+ // add auth token
153
+ headers .put ("Authorization<" , "Bearer " + oAuthManager .computeAccessToken (this ));
154
+ }
155
+ return headers ;
156
+ }
157
+
158
+ protected String computeAuthUrl (String url ) throws Exception {
159
+ // override for auth support
160
+ return url ;
161
+ }
162
+
163
+ protected IBackendClient getHttpClient () {
164
+ return httpClient ;
165
+ }
166
+
167
+ public String getUrlBackend () {
168
+ return urlBackend ;
169
+ }
170
+
171
+ // OAuthAPI
172
+
173
+ @ Override
174
+ public RefreshTokenResponse .Authorization oAuthAuthenticate (String apiKey ) throws Exception {
134
175
String url = getUrlBackend () + URL_GET_AUTH_LOGIN ;
135
176
if (log .isDebugEnabled ()) {
136
177
log .debug ("tokenAuthenticate" );
137
178
}
138
179
Map <String , String > postBody = new HashMap <String , String >();
139
- postBody .put ("apikey" , getApiKey () );
180
+ postBody .put ("apikey" , apiKey );
140
181
RefreshTokenResponse response =
141
182
getHttpClient ().postUrlEncoded (url , RefreshTokenResponse .class , null , postBody );
142
183
@@ -146,13 +187,14 @@ protected RefreshTokenResponse.Authorization tokenAuthenticate() throws Exceptio
146
187
return response .authorizations ;
147
188
}
148
189
149
- protected String tokenRefresh (String refreshToken ) throws Exception {
190
+ @ Override
191
+ public String oAuthRefresh (String refreshTokenStr ) throws Exception {
150
192
String url = getUrlBackend () + URL_GET_AUTH_REFRESH ;
151
193
if (log .isDebugEnabled ()) {
152
194
log .debug ("tokenRefresh" );
153
195
}
154
196
Map <String , String > postBody = new HashMap <String , String >();
155
- postBody .put ("rt" , refreshToken );
197
+ postBody .put ("rt" , refreshTokenStr );
156
198
RefreshTokenResponse response =
157
199
getHttpClient ().postUrlEncoded (url , RefreshTokenResponse .class , null , postBody );
158
200
@@ -161,26 +203,4 @@ protected String tokenRefresh(String refreshToken) throws Exception {
161
203
}
162
204
return response .authorizations .access_token ;
163
205
}
164
-
165
- protected Map <String ,String > computeHeaders () throws Exception {
166
- Map <String ,String > headers = new HashMap <String , String >();
167
- return headers ;
168
- }
169
-
170
- protected String computeAuthUrl (String url ) throws Exception {
171
- // override for auth support
172
- return url ;
173
- }
174
-
175
- protected IBackendClient getHttpClient () {
176
- return httpClient ;
177
- }
178
-
179
- protected String getApiKey () {
180
- return apiKey ;
181
- }
182
-
183
- public String getUrlBackend () {
184
- return urlBackend ;
185
- }
186
206
}
0 commit comments