Skip to content

Commit da1672e

Browse files
committed
Merge branch '1.0-develop' of http://gitlab.topiatechnology.com/blockchain/eosRPCAdapater-java into 1.0-develop
# Conflicts: # src/main/java/io/topiacoin/eosrpcadapter/RPCChain.java
2 parents f27f37b + 969f991 commit da1672e

File tree

10 files changed

+265
-39
lines changed

10 files changed

+265
-39
lines changed

src/main/java/io/topiacoin/eosrpcadapter/Chain.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.topiacoin.eosrpcadapter.exceptions.ChainException;
44
import io.topiacoin.eosrpcadapter.messages.AccountInfo;
5+
import io.topiacoin.eosrpcadapter.messages.Action;
56
import io.topiacoin.eosrpcadapter.messages.BlockInfo;
67
import io.topiacoin.eosrpcadapter.messages.ChainInfo;
78
import io.topiacoin.eosrpcadapter.messages.Code;
@@ -52,7 +53,8 @@ TableRows getTableRows(String contract,
5253
TableRows getTableRows(String contract,
5354
String scope,
5455
String table,
55-
String key,
56+
Integer indexPosition,
57+
String keyType,
5658
String lowerBound,
5759
String upperBound,
5860
long limit,
@@ -80,6 +82,12 @@ Transaction createRawTransaction(String account,
8082
List<Transaction.Authorization> authorizations,
8183
Date expirationDate) throws ChainException;
8284

85+
Transaction createRawTransaction(Action action, Date expirationDate)
86+
throws ChainException;
87+
88+
Transaction createRawTransaction(List<Action> actions, Date expirationDate)
89+
throws ChainException;
90+
8391
Transaction createSetContractTransaction(String account, InputStream abi, InputStream wasm) throws ChainException;
8492

8593
String packTransaction(SignedTransaction transaction) throws ChainException;

src/main/java/io/topiacoin/eosrpcadapter/EOSRPCAdapter.java

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package io.topiacoin.eosrpcadapter;
22

3+
import io.topiacoin.eosrpcadapter.exceptions.ChainException;
34
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;
411
import org.apache.commons.io.IOUtils;
512
import org.apache.commons.lang.StringEscapeUtils;
613
import org.apache.commons.logging.Log;
@@ -18,11 +25,15 @@
1825
import java.net.URI;
1926
import java.net.URISyntaxException;
2027
import java.net.URL;
28+
import java.util.ArrayList;
29+
import java.util.Date;
30+
import java.util.List;
31+
import java.util.Map;
2132

2233
public class EOSRPCAdapter {
2334

2435
public static class EOSRPCResponse {
25-
public String response ;
36+
public String response;
2637
public HttpResponse error;
2738

2839
public EOSRPCResponse(String response) {
@@ -54,7 +65,7 @@ public String toString() {
5465
/**
5566
* Creates a new EOS RPC Adapter instance that will connect to the specified node and wallet URLs.
5667
*
57-
* @param nodeURL The URL of the EOS node to communicate with
68+
* @param nodeURL The URL of the EOS node to communicate with
5869
* @param walletURL The URL of the EOS wallet to communicate with
5970
*/
6071
public EOSRPCAdapter(URL nodeURL, URL walletURL) {
@@ -68,8 +79,8 @@ public EOSRPCAdapter(URL nodeURL, URL walletURL) {
6879
* @return An instance of the Wallet class
6980
*/
7081
public synchronized Wallet wallet() {
71-
if ( _wallet == null ) {
72-
if(eosWalletURL == null) {
82+
if (_wallet == null) {
83+
if (eosWalletURL == null) {
7384
_wallet = new JavaWallet();
7485
} else {
7586
_wallet = new RPCWallet(eosWalletURL, this);
@@ -84,7 +95,7 @@ public synchronized Wallet wallet() {
8495
* @return An instance of the Chain class
8596
*/
8697
public synchronized Chain chain() {
87-
if ( _chain == null ) {
98+
if (_chain == null) {
8899
_chain = new RPCChain(eosNodeURL, this);
89100
}
90101
return _chain;
@@ -96,16 +107,92 @@ public synchronized Chain chain() {
96107
* @return An instance of the Account History class.
97108
*/
98109
public synchronized AccountHistory accountHistory() {
99-
if ( _accountHistory == null ) {
110+
if (_accountHistory == null) {
100111
_accountHistory = new RPCAccountHistory(eosNodeURL, this);
101112
}
102113
return _accountHistory;
103114
}
104115

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+
105192
// -------- Package Scoped methods for raw communication with the RPC API --------
106193

107194
// 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 {
109196
try {
110197
HttpClient client = HttpClients.createDefault();
111198

@@ -115,7 +202,7 @@ EOSRPCResponse getRequest (URL url ) throws EOSException {
115202
HttpResponse response = client.execute(getRequest);
116203

117204
return validateResponse(response);
118-
} catch ( URISyntaxException e) {
205+
} catch (URISyntaxException e) {
119206
throw new EOSException("Communications Exception", e, null);
120207
} catch (ClientProtocolException e) {
121208
throw new EOSException("Communications Exception", e, null);
@@ -125,15 +212,15 @@ EOSRPCResponse getRequest (URL url ) throws EOSException {
125212
}
126213

127214
// 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 {
129216
return postRequest(url, rawData, false);
130217
}
131218

132219
// 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 {
134221
try {
135222
String requestData = rawData;
136-
if ( escapeQuotes ) {
223+
if (escapeQuotes) {
137224
requestData = StringEscapeUtils.escapeJavaScript(rawData);
138225
}
139226

@@ -146,7 +233,7 @@ EOSRPCResponse postRequest ( URL url, String rawData, boolean escapeQuotes) thro
146233
HttpResponse response = client.execute(postRequest);
147234

148235
return validateResponse(response);
149-
} catch ( URISyntaxException e) {
236+
} catch (URISyntaxException e) {
150237
throw new EOSException("Communications Exception", e, null);
151238
} catch (ClientProtocolException e) {
152239
throw new EOSException("Communications Exception", e, null);
@@ -157,7 +244,7 @@ EOSRPCResponse postRequest ( URL url, String rawData, boolean escapeQuotes) thro
157244

158245
EOSRPCResponse validateResponse(HttpResponse response) throws IOException, EOSException {
159246
EOSRPCResponse result;
160-
if ( response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
247+
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
161248
result = new EOSRPCResponse(IOUtils.toString(response.getEntity().getContent(), "UTF-8"));
162249
} else {
163250
result = new EOSRPCResponse(response);

src/main/java/io/topiacoin/eosrpcadapter/RPCChain.java

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.topiacoin.eosrpcadapter.exceptions.EOSException;
77
import io.topiacoin.eosrpcadapter.messages.Abi;
88
import io.topiacoin.eosrpcadapter.messages.AccountInfo;
9+
import io.topiacoin.eosrpcadapter.messages.Action;
910
import io.topiacoin.eosrpcadapter.messages.BlockInfo;
1011
import io.topiacoin.eosrpcadapter.messages.ChainInfo;
1112
import io.topiacoin.eosrpcadapter.messages.Code;
@@ -292,7 +293,7 @@ public TableRows getTableRows(String contract,
292293
String table,
293294
long limit,
294295
boolean json) throws ChainException {
295-
return getTableRows(contract, scope, table, null, "0", "-1", limit, json);
296+
return getTableRows(contract, scope, table, 1, null, "0", "-1", limit, json);
296297
}
297298

298299
@Override
@@ -303,14 +304,15 @@ public TableRows getTableRows(String contract,
303304
String upperBound,
304305
long limit,
305306
boolean json) throws ChainException {
306-
return getTableRows(contract, scope, table, null, lowerBound, upperBound, limit, json);
307+
return getTableRows(contract, scope, table, 1, null, lowerBound, upperBound, limit, json);
307308
}
308309

309310
@Override
310311
public TableRows getTableRows(String contract,
311312
String scope,
312313
String table,
313-
String key,
314+
Integer indexPosition,
315+
String keyType,
314316
String lowerBound,
315317
String upperBound,
316318
long limit,
@@ -325,8 +327,14 @@ public TableRows getTableRows(String contract,
325327
requestMap.put("scope", scope);
326328
requestMap.put("table", table);
327329
requestMap.put("limit", limit);
328-
if ( key != null ) {
329-
requestMap.put("table_key", key);
330+
if ( indexPosition != null ) {
331+
requestMap.put("index_position", indexPosition);
332+
if ( indexPosition > 1 && keyType == null ) {
333+
throw new IllegalArgumentException("Must specify keyType when using non-primary index");
334+
}
335+
if ( keyType != null ) {
336+
requestMap.put("key_type", keyType);
337+
}
330338
}
331339
requestMap.put("lower_bound", lowerBound);
332340
requestMap.put("upper_bound", upperBound);
@@ -565,7 +573,30 @@ public Transaction createRawTransaction(String account,
565573
Map args,
566574
List<String> scopes,
567575
List<Transaction.Authorization> authorizations,
568-
Date expirationDate) throws ChainException {
576+
Date expirationDate)
577+
throws ChainException {
578+
579+
return createRawTransaction(
580+
new Action(account, name, authorizations, args),
581+
expirationDate);
582+
}
583+
584+
@Override
585+
public Transaction createRawTransaction(Action action, Date expirationDate)
586+
throws ChainException {
587+
List<Action> actions = new ArrayList<>();
588+
actions.add(action);
589+
590+
return createRawTransaction(actions, expirationDate);
591+
}
592+
593+
@Override
594+
public Transaction createRawTransaction(List<Action> actions, Date expirationDate)
595+
throws ChainException {
596+
597+
if ( actions.size() == 0 ) {
598+
throw new IllegalArgumentException("Must specify at least one action to include in the transaction");
599+
}
569600

570601
TimeZone tz = TimeZone.getTimeZone("UTC");
571602
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@@ -577,15 +608,36 @@ public Transaction createRawTransaction(String account,
577608
BlockInfo blockInfo = getBlock(Long.toString(last_irreversible_block_num));
578609
long last_irreversible_block_prefix = blockInfo.ref_block_prefix;
579610

580-
TransactionBinArgs binArgsResponse = abiJsonToBin(account, name, args);
581-
String binArgs = binArgsResponse.binargs;
611+
ArrayList<Transaction.Action> txActions = new ArrayList<Transaction.Action>();
612+
613+
for ( Action action : actions ) {
614+
TransactionBinArgs binArgsResponse = abiJsonToBin(
615+
action.account,
616+
action.name,
617+
action.args);
618+
String binArgs = binArgsResponse.binargs;
582619

583-
Transaction.Action txAction = new Transaction.Action(account, name, authorizations, binArgs);
620+
Transaction.Action txAction = new Transaction.Action(
621+
action.account,
622+
action.name,
623+
action.authorizations,
624+
binArgs);
584625

585-
ArrayList<Transaction.Action> actions = new ArrayList<Transaction.Action>();
586-
actions.add(txAction);
626+
txActions.add(txAction);
627+
}
587628

588-
Transaction transaction = new Transaction(expDateString, last_irreversible_block_num, last_irreversible_block_prefix,0, 0, 0, null, actions, null, null, null);
629+
Transaction transaction = new Transaction(
630+
expDateString,
631+
last_irreversible_block_num,
632+
last_irreversible_block_prefix,
633+
0,
634+
0,
635+
0,
636+
null,
637+
txActions,
638+
null,
639+
null,
640+
null);
589641

590642
return transaction;
591643
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.topiacoin.eosrpcadapter.messages;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Action {
7+
8+
public String account;
9+
public String name;
10+
public List<Transaction.Authorization> authorizations;
11+
public Map<String,Object> args;
12+
13+
public Action() {
14+
}
15+
16+
public Action(String account,
17+
String name,
18+
List<Transaction.Authorization> authorizations,
19+
Map<String, Object> args) {
20+
this.account = account;
21+
this.name = name;
22+
this.authorizations = authorizations;
23+
this.args = args;
24+
}
25+
26+
}

src/main/java/io/topiacoin/eosrpcadapter/messages/BlockInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,12 @@ public static class TransactionInfo {
6565
public List<String> context_free_data;
6666
public String packed_trx;
6767
public Transaction transaction;
68+
69+
public TransactionInfo() {
70+
}
71+
72+
public TransactionInfo(String id) {
73+
this.id = id;
74+
}
6875
}
6976
}

0 commit comments

Comments
 (0)