Skip to content

Commit bf07c48

Browse files
committed
Merge branch 'feature/contract' of github.com:ChainSQL/java-chainsql-api into feature/contract
2 parents 0f9587c + 1484f69 commit bf07c48

File tree

17 files changed

+309
-55
lines changed

17 files changed

+309
-55
lines changed

chainsql/src/main/java/com/peersafe/base/client/Client.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,9 +1515,9 @@ private void UnhexResult(Response response) {
15151515
for(int i=0; i<txs.length(); i++){
15161516
JSONObject tx = (JSONObject)txs.get(i);
15171517
Util.unHexData(tx.getJSONObject("tx"));
1518-
if(tx.has("meta")){
1519-
tx.remove("meta");
1520-
}
1518+
// if(tx.has("meta")){
1519+
// tx.remove("meta");
1520+
// }
15211521
}
15221522
}
15231523
}
@@ -2055,6 +2055,16 @@ public Request subscribeAccount(AccountID... accounts) {
20552055
request.json("accounts", accounts_arr);
20562056
return request;
20572057
}
2058+
2059+
public Request unsubscribeAccount(AccountID... accounts){
2060+
Request request = newRequest(Command.unsubscribe);
2061+
JSONArray accounts_arr = new JSONArray();
2062+
for (AccountID acc : accounts) {
2063+
accounts_arr.put(acc);
2064+
}
2065+
request.json("accounts", accounts_arr);
2066+
return request;
2067+
}
20582068

20592069

20602070
/**

chainsql/src/main/java/com/peersafe/base/client/transactions/TransactionManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public Request submitSigned(final ManagedTxn txn){
137137
final Request req = client.newRequest(Command.submit);
138138
// tx_blob is a hex string, right o' the bat
139139
req.json("tx_blob", txn.tx_blob);
140+
req.json("ca_pem",txn.ca_pem);
140141

141142
//System.out.println("before request");
142143
req.once(Request.OnSuccess.class, new Request.OnSuccess() {

chainsql/src/main/java/com/peersafe/base/core/coretypes/Blob.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public Field getField() {
9999
static public BlobField ExpireCode = blobField(Field.ExpireCode);
100100
static public BlobField CreateCode = blobField(Field.CreateCode);
101101

102+
static public BlobField Certificate = blobField(Field.Certificate);
103+
102104
static public BlobField MemoType = blobField(Field.MemoType);
103105
static public BlobField MemoData = blobField(Field.MemoData);
104106
static public BlobField MemoFormat = blobField(Field.MemoFormat);

chainsql/src/main/java/com/peersafe/base/core/fields/Field.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public enum Field {
136136
MemoType(12, Type.Blob),
137137
MemoData(13, Type.Blob),
138138
MemoFormat(14, Type.Blob),
139-
139+
Certificate(15, Type.Blob),
140140
Fulfillment(16,Type.Blob),
141141
Condition(17,Type.Blob),
142142
MasterSignature(18,Type.Blob),

chainsql/src/main/java/com/peersafe/base/core/formats/TxFormat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void addCommonFields() {
4848
put(Field.Sequence, Requirement.REQUIRED);
4949
put(Field.Fee, Requirement.REQUIRED);
5050
put(Field.SigningPubKey, Requirement.REQUIRED);
51+
put(Field.Certificate, Requirement.OPTIONAL);
5152

5253
put(Field.Flags, Requirement.OPTIONAL);
5354
put(Field.SourceTag, Requirement.OPTIONAL);

chainsql/src/main/java/com/peersafe/base/core/types/known/tx/signed/SignedTransaction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.peersafe.base.core.types.known.tx.Transaction;
1818
import com.peersafe.base.crypto.ecdsa.IKeyPair;
1919
import com.peersafe.base.crypto.ecdsa.Seed;
20+
import com.peersafe.chainsql.util.Util;
2021

2122
public class SignedTransaction {
2223
private SignedTransaction(Transaction of) {
@@ -30,6 +31,7 @@ public SignedTransaction(SignedTransaction st){
3031
this.signingData = st.signingData;
3132
this.previousSigningData = st.previousSigningData;
3233
this.tx_blob = st.tx_blob;
34+
this.ca_pem = st.ca_pem;
3335
}
3436
// This will eventually be private
3537
@Deprecated
@@ -42,6 +44,8 @@ public SignedTransaction() {}
4244
public byte[] previousSigningData;
4345
public String tx_blob;
4446

47+
public String ca_pem;// CA
48+
4549
public void multiSign(String base58Secret){
4650
multiSign(Seed.fromBase58(base58Secret).keyPair());
4751
}

chainsql/src/main/java/com/peersafe/chainsql/contract/Contract.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ private List<Type> executeCall(
115115
JSONObject objTx = prepareCallParam(function);
116116

117117
JSONObject ret = this.chainsql.connection.client.contractCall(objTx);
118-
if(ret.has("error")){
119-
throw new ContractCallException(ret.getString("error"));
118+
if(ret.has("error") && ret.has("error_message")){
119+
throw new ContractCallException(ret.getString("error"),ret.getString("error_message"));
120+
}else if(ret.has("error")){
121+
throw new ContractCallException(ret.getString("error"));
120122
}
123+
121124
return FunctionReturnDecoder.decode(ret.getString("contract_call_result"), function.getOutputParameters());
122125
}
123126

@@ -277,6 +280,11 @@ JSONObject prepareSigned() {
277280
return Util.errorObject("Exception occured:Json not prepared");
278281
}
279282
mTxJson.put("Account",this.connection.address);
283+
284+
if (this.connection.userCert != null) {
285+
String sCert = Util.toHexString(this.connection.userCert);
286+
mTxJson.put("Certificate", sCert);
287+
}
280288

281289
Transaction tx = toTransaction(mTxJson,TransactionType.Contract);
282290
signed = tx.sign(this.connection.secret);
@@ -339,11 +347,17 @@ private static <T extends Contract> T create(
339347
if(cb == null) {
340348
JSONObject obj = contract.submit(SyncCond.validate_success);
341349
String contractAddress = null;
350+
342351
if(obj.has("status") && obj.getString("status").equals("validate_success")) {
343352
JSONObject tx = c.connection.client.getTransaction(obj.getString("tx_hash"));
344353
contractAddress = Util.getNewAccountFromTx(tx);
345354
contract.setContractAddress(contractAddress);
346-
}else{
355+
}else if(obj.has("status") && obj.getString("status").equals("validate_timeout")){
356+
357+
System.out.println(obj);
358+
throw new TransactionException("deploy validate_timeout");
359+
}
360+
else{
347361
if(obj.has("error_message")){
348362
if(obj.has("error_code"))
349363
throw new TransactionException(obj.getString("error_message"),obj.getInt("error_code"));

chainsql/src/main/java/com/peersafe/chainsql/contract/exception/ContractCallException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* Exception resulting from issues calling methods on Smart Contracts.
55
*/
66
public class ContractCallException extends RuntimeException {
7-
public int error_code;
8-
public ContractCallException(String message) {
9-
super(message);
7+
public String error;
8+
public ContractCallException(String error) {
9+
super(error);
1010
}
11-
public ContractCallException(String message,int error_code) {
12-
super(message);
13-
this.error_code = error_code;
11+
public ContractCallException(String error,String error_message) {
12+
super(error_message);
13+
this.error = error;
1414
}
1515
public ContractCallException(String message, Throwable cause) {
1616
super(message, cause);

chainsql/src/main/java/com/peersafe/chainsql/core/Chainsql.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,25 @@ public class Chainsql extends Submit {
6363
* @param secret Account secret,start with a lower case 'x'.
6464
*/
6565
public void as(String address, String secret) {
66+
67+
68+
JSONObject retAddress = generateAddress(secret);
69+
if(retAddress.has("address") && !address.equals( retAddress.getString("address") )){
70+
System.err.println("Exception: address and secret not match !");
71+
}
72+
6673
this.connection.address = address;
6774
this.connection.secret = secret;
6875
this.connection.scope = address;
6976
}
7077

78+
79+
public void useCert(String userCert) {
80+
this.connection.userCert = userCert;
81+
82+
}
83+
84+
7185
/**
7286
* Assigning table owner.
7387
* @param address Address of table owner.
@@ -394,6 +408,18 @@ JSONObject prepareSigned() {
394408
}
395409
mTxJson.put("Account",this.connection.address);
396410

411+
if(mTxJson.getInt("OpType") == Constant.opType.get("t_grant") &&
412+
!this.connection.address.equals(connection.scope)){
413+
mTxJson.put("Owner", connection.scope);
414+
}
415+
416+
417+
if (this.connection.userCert != null) {
418+
String sCert = Util.toHexString(this.connection.userCert);
419+
mTxJson.put("Certificate", sCert);
420+
}
421+
422+
397423
//for cross chain
398424
if(crossChainArgs != null){
399425
mTxJson.put("TxnLgrSeq", crossChainArgs.txnLedgerSeq);

chainsql/src/main/java/com/peersafe/chainsql/core/Ripple.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
import java.math.BigInteger;
88
import java.util.logging.Level;
99

10+
import com.peersafe.base.core.coretypes.*;
11+
import org.bouncycastle.util.encoders.Hex;
1012
import org.json.JSONObject;
1113

1214
import com.peersafe.base.client.Client;
1315
import com.peersafe.base.client.requests.Request;
14-
import com.peersafe.base.core.coretypes.AccountID;
15-
import com.peersafe.base.core.coretypes.Amount;
16-
import com.peersafe.base.core.coretypes.Currency;
17-
import com.peersafe.base.core.coretypes.RippleDate;
1816
import com.peersafe.base.core.serialized.enums.TransactionType;
1917
import com.peersafe.base.core.types.known.tx.Transaction;
2018
import com.peersafe.chainsql.util.Util;
@@ -44,17 +42,28 @@ protected JSONObject prepareSigned() {
4442
if(mTxJson.toString().equals("{}")) {
4543
return Util.errorObject("Exception occured");
4644
}
45+
46+
4747
mTxJson.put("Account",this.connection.address);
4848

49+
if (this.connection.userCert != null) {
50+
String sCert = Util.toHexString(this.connection.userCert);
51+
mTxJson.put("Certificate", sCert);
52+
}
53+
54+
4955
String sType = mTxJson.get("TransactionType").toString();
5056
if(sType.isEmpty()) {
5157
return Util.errorObject("Exception occured, no exist TransactionType");
5258
}
5359
//
5460
TransactionType type = TransactionType.translate.fromString(sType);
5561
Transaction payment = toTransaction(mTxJson, type);
62+
63+
5664

5765
signed = payment.sign(this.connection.secret);
66+
5867

5968
return Util.successObject();
6069
} catch (Exception e) {

chainsql/src/main/java/com/peersafe/chainsql/core/Submit.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,37 @@ private void onSubmitError(Response res) {
300300
JSONObject obj = new JSONObject();
301301

302302
obj.put("status", "error");
303-
if(res.result.has("engine_result_message"))
304-
obj.put("error_message", res.result.getString("engine_result_message"));
305-
if(res.result.has("engine_result_message_detail"))
306-
obj.put("error_message",res.result.getString("engine_result_message_detail"));
307-
308-
if(res.result.has("engine_result")) {
309-
obj.put("error", res.result.getString("engine_result"));
310-
}
311-
312-
if(res.result.has("engine_result_code")){
313-
obj.put("error_code", res.result.getInt("engine_result_code"));
314-
}
315-
if(res.result.has("tx_json")){
316-
obj.put("tx_json", res.result.getJSONObject("tx_json"));
317-
}
303+
304+
305+
if(res.result == null){
306+
307+
res.result = new JSONObject();
308+
if (res.message.has("result")) {
309+
res.result = res.message.getJSONObject("result");
310+
} else {
311+
obj.put("error_message", res.message.getString("error_message"));
312+
}
313+
314+
}
315+
316+
if(res.result.has("engine_result_message"))
317+
obj.put("error_message", res.result.getString("engine_result_message"));
318+
if(res.result.has("engine_result_message_detail"))
319+
obj.put("error_message",res.result.getString("engine_result_message_detail"));
320+
321+
if(res.result.has("engine_result")) {
322+
obj.put("error", res.result.getString("engine_result"));
323+
}
324+
325+
if(res.result.has("engine_result_code")){
326+
obj.put("error_code", res.result.getInt("engine_result_code"));
327+
}
328+
if(res.result.has("tx_json")){
329+
obj.put("tx_json", res.result.getJSONObject("tx_json"));
330+
}
331+
332+
333+
318334
if(sync || cb != null) {
319335
unSubscribeTx(signed.hash.toString());
320336
if(cb != null) {

chainsql/src/main/java/com/peersafe/chainsql/core/Table.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private JSONObject prepareSQLStatement() {
264264

265265
txjson.put("Account", this.connection.address);
266266

267+
267268
//for cross chain
268269
if(crossChainArgs != null){
269270
txjson.put("TxnLgrSeq", crossChainArgs.txnLedgerSeq);

chainsql/src/main/java/com/peersafe/chainsql/manager/EventManager.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class EventManager {
2727
public boolean onTbMessage;
2828
public boolean onTxMessage;
2929
public boolean onContractMessage;
30+
//订阅响应
3031
public boolean onSubRet;
3132
private HashMap<String,Callback> mapCache;
3233
private HashMap<String,byte[]> mapPass;
@@ -62,10 +63,13 @@ public void init(Connection connection) {
6263
* Resubscribe automatically after reconnected.
6364
*/
6465
public void reSubscribe(){
65-
int ownerLen = this.connection.address.length();
6666
for(String key : mapTableCache.keySet()){
67-
String name = key.substring(0,key.length() - ownerLen);
68-
String owner = key.substring(key.length() - ownerLen);
67+
String[] keys = key.split(";");
68+
if(keys.length != 2){
69+
continue;
70+
}
71+
String name = keys[0];
72+
String owner = keys[1];
6973

7074
JSONObject messageTx = new JSONObject();
7175
messageTx.put("command", "subscribe");
@@ -101,7 +105,7 @@ private void onChainsqlSubRet() {
101105
@Override
102106
public void called(JSONObject args) {
103107
if(args.has("owner") && args.has("tablename")) {
104-
String key = args.getString("tablename") + args.getString("owner");
108+
String key = args.getString("tablename") + ";" + args.getString("owner");
105109
makeCallback(key,args.getJSONObject("result"));
106110
}
107111
if(args.has("transaction")) {
@@ -138,7 +142,7 @@ public void called(JSONObject args) {
138142
onChainsqlSubRet();
139143
this.onSubRet = true;
140144
}
141-
this.mapTableCache.put(name + owner,cb);
145+
this.mapTableCache.put(name +";" + owner,cb);
142146
}
143147

144148
/**
@@ -226,7 +230,7 @@ public void unsubscribeTable(String name, String owner,Callback<JSONObject> cb)
226230
messageTx.put("tablename", name);
227231
this.connection.client.subscriptions.addMessage(messageTx);
228232

229-
String key = name + owner;
233+
String key = name +";" + owner;
230234

231235
JSONObject obj = new JSONObject();
232236
if(this.mapTableCache.containsKey(key)) {
@@ -336,7 +340,7 @@ public void called(JSONObject res) {
336340
private void onTBMessage(JSONObject data){
337341
String owner = data.getString("owner");
338342
String name = data.getString("tablename");
339-
String key = name + owner;
343+
String key = name + ";" + owner;
340344
onChainsqlMessage(data,key,owner,name);
341345
}
342346

chainsql/src/main/java/com/peersafe/chainsql/net/Connection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public class Connection implements Closeable {
1414
public String scope;
1515
public Client client;
1616

17+
18+
public String userCert;
19+
20+
21+
1722
/**
1823
* Connect to a websocket address.
1924
* @param url Websocket url.

0 commit comments

Comments
 (0)