1
1
package io .topiacoin .eosrpcadapter ;
2
2
3
+ import io .topiacoin .eosrpcadapter .exceptions .ChainException ;
3
4
import io .topiacoin .eosrpcadapter .exceptions .EOSException ;
5
+ import io .topiacoin .eosrpcadapter .exceptions .WalletException ;
6
+ import io .topiacoin .eosrpcadapter .messages .Action ;
7
+ import io .topiacoin .eosrpcadapter .messages .ChainInfo ;
8
+ import io .topiacoin .eosrpcadapter .messages .RequiredKeys ;
9
+ import io .topiacoin .eosrpcadapter .messages .SignedTransaction ;
10
+ import io .topiacoin .eosrpcadapter .messages .Transaction ;
4
11
import org .apache .commons .io .IOUtils ;
5
12
import org .apache .commons .lang .StringEscapeUtils ;
6
13
import org .apache .commons .logging .Log ;
18
25
import java .net .URI ;
19
26
import java .net .URISyntaxException ;
20
27
import java .net .URL ;
28
+ import java .util .ArrayList ;
29
+ import java .util .Date ;
30
+ import java .util .List ;
31
+ import java .util .Map ;
21
32
22
33
public class EOSRPCAdapter {
23
34
24
35
public static class EOSRPCResponse {
25
- public String response ;
36
+ public String response ;
26
37
public HttpResponse error ;
27
38
28
39
public EOSRPCResponse (String response ) {
@@ -54,7 +65,7 @@ public String toString() {
54
65
/**
55
66
* Creates a new EOS RPC Adapter instance that will connect to the specified node and wallet URLs.
56
67
*
57
- * @param nodeURL The URL of the EOS node to communicate with
68
+ * @param nodeURL The URL of the EOS node to communicate with
58
69
* @param walletURL The URL of the EOS wallet to communicate with
59
70
*/
60
71
public EOSRPCAdapter (URL nodeURL , URL walletURL ) {
@@ -68,8 +79,8 @@ public EOSRPCAdapter(URL nodeURL, URL walletURL) {
68
79
* @return An instance of the Wallet class
69
80
*/
70
81
public synchronized Wallet wallet () {
71
- if ( _wallet == null ) {
72
- if (eosWalletURL == null ) {
82
+ if (_wallet == null ) {
83
+ if (eosWalletURL == null ) {
73
84
_wallet = new JavaWallet ();
74
85
} else {
75
86
_wallet = new RPCWallet (eosWalletURL , this );
@@ -84,7 +95,7 @@ public synchronized Wallet wallet() {
84
95
* @return An instance of the Chain class
85
96
*/
86
97
public synchronized Chain chain () {
87
- if ( _chain == null ) {
98
+ if (_chain == null ) {
88
99
_chain = new RPCChain (eosNodeURL , this );
89
100
}
90
101
return _chain ;
@@ -96,16 +107,92 @@ public synchronized Chain chain() {
96
107
* @return An instance of the Account History class.
97
108
*/
98
109
public synchronized AccountHistory accountHistory () {
99
- if ( _accountHistory == null ) {
110
+ if (_accountHistory == null ) {
100
111
_accountHistory = new RPCAccountHistory (eosNodeURL , this );
101
112
}
102
113
return _accountHistory ;
103
114
}
104
115
116
+
117
+ // -------- Convienence Methods --------
118
+
119
+ public Transaction .Response pushTransaction (String account ,
120
+ String name ,
121
+ Map args ,
122
+ List <String > scopes ,
123
+ List <Transaction .Authorization > authorizations ,
124
+ Date expirationDate ,
125
+ String walletName ) throws ChainException , WalletException {
126
+
127
+ Action action = new Action (account , name , authorizations , args ) ;
128
+
129
+ return pushTransaction (action , expirationDate , walletName );
130
+ }
131
+
132
+ public Transaction .Response pushTransaction (Action action ,
133
+ Date expirationDate ,
134
+ String walletName ) throws ChainException , WalletException {
135
+ List <Action > actions = new ArrayList <>();
136
+ actions .add (action );
137
+
138
+ List <String > walletNames = new ArrayList <>();
139
+ walletNames .add (walletName );
140
+
141
+ return pushTransaction (actions , expirationDate , walletNames );
142
+ }
143
+
144
+ public Transaction .Response pushTransaction (Action action ,
145
+ Date expirationDate ,
146
+ List <String > walletNames ) throws ChainException , WalletException {
147
+ List <Action > actions = new ArrayList <>();
148
+ actions .add (action );
149
+
150
+ return pushTransaction (actions , expirationDate , walletNames );
151
+ }
152
+
153
+ public Transaction .Response pushTransaction (List <Action > actions ,
154
+ Date expirationDate ,
155
+ String walletName ) throws ChainException , WalletException {
156
+ List <String > walletNames = new ArrayList <>();
157
+ walletNames .add (walletName );
158
+ return pushTransaction (actions , expirationDate , walletNames );
159
+ }
160
+
161
+ public Transaction .Response pushTransaction (List <Action > actions ,
162
+ Date expirationDate ,
163
+ List <String > walletNames ) throws ChainException , WalletException {
164
+
165
+ // Create the unsigned transaction
166
+ Transaction registerTX = chain ().createRawTransaction (
167
+ actions ,
168
+ expirationDate );
169
+
170
+ // Get the available Keys from the specified wallets
171
+ List <String > availableKeys = new ArrayList <>();
172
+ for ( String walletName : walletNames ) {
173
+ availableKeys .addAll (wallet ().getPublicKeys (walletName ));
174
+ }
175
+
176
+ // Determine which keys this transaction requires
177
+ RequiredKeys requiredKeys = chain ().getRequiredKeys (registerTX , availableKeys );
178
+
179
+ // Get the chain ID for this chain.
180
+ ChainInfo chainInfo = chain ().getInfo ();
181
+ String chainId = chainInfo .chain_id ;
182
+
183
+ // Sign the transaction for the target chain
184
+ SignedTransaction signedTx = wallet ().signTransaction (registerTX , requiredKeys .required_keys , chainId );
185
+
186
+ // Push the transaction to the chain
187
+ Transaction .Response response = chain ().pushTransaction (signedTx );
188
+
189
+ return response ;
190
+ }
191
+
105
192
// -------- Package Scoped methods for raw communication with the RPC API --------
106
193
107
194
// Send a Get Request to the Server and Return the response
108
- EOSRPCResponse getRequest (URL url ) throws EOSException {
195
+ EOSRPCResponse getRequest (URL url ) throws EOSException {
109
196
try {
110
197
HttpClient client = HttpClients .createDefault ();
111
198
@@ -115,7 +202,7 @@ EOSRPCResponse getRequest (URL url ) throws EOSException {
115
202
HttpResponse response = client .execute (getRequest );
116
203
117
204
return validateResponse (response );
118
- } catch ( URISyntaxException e ) {
205
+ } catch (URISyntaxException e ) {
119
206
throw new EOSException ("Communications Exception" , e , null );
120
207
} catch (ClientProtocolException e ) {
121
208
throw new EOSException ("Communications Exception" , e , null );
@@ -125,15 +212,15 @@ EOSRPCResponse getRequest (URL url ) throws EOSException {
125
212
}
126
213
127
214
// Send a Post Request to the Server and Return the response
128
- EOSRPCResponse postRequest (URL url , String rawData ) throws EOSException {
215
+ EOSRPCResponse postRequest (URL url , String rawData ) throws EOSException {
129
216
return postRequest (url , rawData , false );
130
217
}
131
218
132
219
// Send a Post Request to the Server, quoting the rawData, and Return the response
133
- EOSRPCResponse postRequest ( URL url , String rawData , boolean escapeQuotes ) throws EOSException {
220
+ EOSRPCResponse postRequest ( URL url , String rawData , boolean escapeQuotes ) throws EOSException {
134
221
try {
135
222
String requestData = rawData ;
136
- if ( escapeQuotes ) {
223
+ if (escapeQuotes ) {
137
224
requestData = StringEscapeUtils .escapeJavaScript (rawData );
138
225
}
139
226
@@ -146,7 +233,7 @@ EOSRPCResponse postRequest ( URL url, String rawData, boolean escapeQuotes) thro
146
233
HttpResponse response = client .execute (postRequest );
147
234
148
235
return validateResponse (response );
149
- } catch ( URISyntaxException e ) {
236
+ } catch (URISyntaxException e ) {
150
237
throw new EOSException ("Communications Exception" , e , null );
151
238
} catch (ClientProtocolException e ) {
152
239
throw new EOSException ("Communications Exception" , e , null );
@@ -157,7 +244,7 @@ EOSRPCResponse postRequest ( URL url, String rawData, boolean escapeQuotes) thro
157
244
158
245
EOSRPCResponse validateResponse (HttpResponse response ) throws IOException , EOSException {
159
246
EOSRPCResponse result ;
160
- if ( response .getStatusLine ().getStatusCode () >= 200 && response .getStatusLine ().getStatusCode () < 300 ) {
247
+ if (response .getStatusLine ().getStatusCode () >= 200 && response .getStatusLine ().getStatusCode () < 300 ) {
161
248
result = new EOSRPCResponse (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ));
162
249
} else {
163
250
result = new EOSRPCResponse (response );
0 commit comments