Guest User

Untitled

a guest
Dec 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. import iroha.protocol.Commands;
  2. import iroha.protocol.TransactionOuterClass;
  3. import iroha.protocol.Endpoint;
  4. import iroha.protocol.Queries;
  5. import iroha.protocol.Queries.Query;
  6. import iroha.protocol.Queries.GetAssetInfo;
  7.  
  8. import iroha.protocol.QueryService_v1Grpc;
  9. import iroha.protocol.QueryService_v1Grpc.QueryService_v1BlockingStub;
  10. import iroha.protocol.CommandService_v1Grpc;
  11. import iroha.protocol.CommandService_v1Grpc.CommandService_v1BlockingStub;
  12. import iroha.protocol.Endpoint.TxStatus;
  13. import iroha.protocol.Endpoint.TxStatusRequest;
  14. import iroha.protocol.Endpoint.ToriiResponse;
  15. import iroha.protocol.QryResponses.QueryResponse;
  16. import iroha.protocol.QryResponses.AssetResponse;
  17. import iroha.protocol.QryResponses.Asset;
  18.  
  19. import com.google.protobuf.InvalidProtocolBufferException;
  20. import io.grpc.ManagedChannel;
  21. import io.grpc.ManagedChannelBuilder;
  22. import com.google.protobuf.ByteString;
  23. import com.google.protobuf.Descriptors.FieldDescriptor;
  24.  
  25. import java.nio.file.Paths;
  26. import java.nio.file.Files;
  27. import java.io.IOException;
  28. import java.math.BigInteger;
  29. import java.lang.Thread;
  30. import java.util.*;
  31.  
  32. import jp.co.soramitsu.iroha.*;
  33.  
  34. class TransactionExample {
  35. static {
  36. try {
  37. System.loadLibrary("irohajava");
  38. } catch (UnsatisfiedLinkError e) {
  39. System.err.println("Native code library failed to load. \n" + e);
  40. System.exit(1);
  41. }
  42. }
  43.  
  44. //--------------------------------------------------------------------------
  45. private static ModelCrypto crypto = new ModelCrypto();
  46. private static ModelTransactionBuilder txBuilder = new ModelTransactionBuilder();
  47. private static ModelQueryBuilder queryBuilder = new ModelQueryBuilder();
  48.  
  49. public static CommandService_v1BlockingStub stub;
  50. public static QueryService_v1BlockingStub queryStub;
  51.  
  52. // admin
  53. public static Keypair admin_keys;
  54. public static String admin_account_id = "admin@test";
  55.  
  56. // alice
  57. public static Keypair alice_keys;
  58. public static String alice_account_id = "alice@com";
  59.  
  60. // alice
  61. public static Keypair bob_keys;
  62. public static String bob_account_id = "bob@com";
  63.  
  64. public static long startQueryCounter = 1;
  65. //--------------------------------------------------------------------------
  66.  
  67. public static byte[] toByteArray(ByteVector blob) {
  68. byte bs[] = new byte[(int)blob.size()];
  69. for (int i = 0; i < blob.size(); ++i) {
  70. bs[i] = (byte)blob.get(i);
  71. }
  72. return bs;
  73. }
  74.  
  75. public static String readKeyFromFile(String path) {
  76. try {
  77. return new String(Files.readAllBytes(Paths.get(path)));
  78. } catch (IOException e) {
  79. System.err.println("Unable to read key files.\n " + e);
  80. System.exit(1);
  81. }
  82. return "";
  83. }
  84.  
  85. public static void sendTx(UnsignedTx utx, Keypair keypair) {
  86. // sign transaction and get its binary representation (Blob)
  87. ByteVector txblob = new ModelProtoTransaction(utx).signAndAddSignature(keypair).finish().blob();
  88.  
  89. // Convert ByteVector to byte array
  90. byte bs[] = toByteArray(txblob);
  91.  
  92. // create proto object
  93. TransactionOuterClass.Transaction protoTx = null;
  94. try {
  95. protoTx = TransactionOuterClass.Transaction.parseFrom(bs);
  96. } catch (InvalidProtocolBufferException e) {
  97. System.err.println("Exception while converting byte array to protobuf:" + e.getMessage());
  98. System.exit(1);
  99. }
  100. // Send transaction to iroha
  101. stub.torii(protoTx);
  102.  
  103. // wait to ensure transaction was processed
  104. try {
  105. Thread.sleep(10_000);
  106. }
  107. catch(InterruptedException ex) {
  108. Thread.currentThread().interrupt();
  109. }
  110.  
  111. // create status request
  112. System.out.println("Hash of the transaction: " + utx.hash().hex());
  113.  
  114. ByteVector txhash = utx.hash().blob();
  115. byte bshash[] = toByteArray(txhash);
  116.  
  117. TxStatusRequest request = TxStatusRequest.newBuilder().setTxHash(ByteString.copyFrom(bshash)).build();
  118. ToriiResponse response = stub.status(request);
  119. String status = response.getTxStatus().name();
  120.  
  121. System.out.println("Status of the transaction is: " + response.toString());
  122.  
  123. if (!status.equals("COMMITTED")) {
  124. System.err.println("Your transaction wasn't committed");
  125. System.exit(1);
  126. }
  127. }
  128.  
  129. public static QueryResponse sendQuery(UnsignedQuery uquery, Keypair keys) {
  130. ByteVector queryBlob = new ModelProtoQuery(uquery).signAndAddSignature(keys).finish().blob();
  131. byte bquery[] = toByteArray(queryBlob);
  132.  
  133. Query protoQuery = null;
  134. try {
  135. protoQuery = Query.parseFrom(bquery);
  136. } catch (InvalidProtocolBufferException e) {
  137. System.err.println("Exception while converting byte array to protobuf:" + e.getMessage());
  138. System.exit(1);
  139. }
  140.  
  141. QueryResponse queryResponse = queryStub.find(protoQuery);
  142.  
  143. System.out.println(queryResponse);
  144. return queryResponse;
  145. }
  146.  
  147. public static void prepare() {
  148. // build transaction (still unsigned)
  149. UnsignedTx utx = txBuilder.creatorAccountId(admin_account_id)
  150. .createdTime(BigInteger.valueOf(System.currentTimeMillis()))
  151. .createDomain("com", "user")
  152. .createAsset("dollar", "com", (short)2)
  153. .createAccount("alice", "com", alice_keys.publicKey())
  154. .addAssetQuantity("dollar#com", "100.00")
  155. .transferAsset("admin@test", "alice@com", "dollar#com", "", "100.00")
  156. .build();
  157. sendTx(utx, admin_keys);
  158. }
  159.  
  160. public static void setAliceSignatoryAndQuorum() {
  161. // build transaction (still unsigned)
  162. UnsignedTx utx = txBuilder.creatorAccountId(alice_account_id)
  163. .createdTime(BigInteger.valueOf(System.currentTimeMillis()))
  164. .addSignatory(alice_account_id, bob_keys.publicKey())
  165. .setAccountQuorum("alice@com", (short)2)
  166. .build();
  167. sendTx(utx, alice_keys);
  168. }
  169.  
  170. public static void createMstTransaction() {
  171. // build transaction (still unsigned)
  172. UnsignedTx utx = txBuilder.creatorAccountId(alice_account_id)
  173. .createdTime(BigInteger.valueOf(System.currentTimeMillis()))
  174. .quorum((short)2)
  175. .transferAsset("alice@com", "admin@test", "dollar#com", "", "100.00")
  176. .build();
  177. sendTx(utx, alice_keys);
  178. }
  179.  
  180. public static void getPendingTransactions() {
  181. // query result of transaction we've just sent
  182. UnsignedQuery uquery = queryBuilder.creatorAccountId(alice_account_id)
  183. .queryCounter(BigInteger.valueOf(startQueryCounter++))
  184. .createdTime(BigInteger.valueOf(System.currentTimeMillis()))
  185. .getPendingTransactions()
  186. .build();
  187. QueryResponse response = sendQuery(uquery, bob_keys);
  188.  
  189. List<TransactionOuterClass.Transaction> txs = response.getTransactionsPageResponse().getTransactionsList();
  190. for (TransactionOuterClass.Transaction tx : txs) {
  191. // jp.co.soramitsu.iroha.Transaction ptx = new jp.co.soramitsu.iroha.Transaction(tx);
  192. // UnsignedTx utx = new UnsignedTx(ptx);
  193. new Blob(tx.SerializeToArray());
  194. }
  195. }
  196.  
  197. public static void main(String[] args) {
  198. admin_keys = crypto.convertFromExisting(readKeyFromFile("../admin@test.pub"),
  199. readKeyFromFile("../admin@test.priv"));
  200.  
  201. alice_keys = crypto.convertFromExisting(readKeyFromFile("alice@com.pub"),
  202. readKeyFromFile("alice@com.priv"));
  203.  
  204. bob_keys = crypto.convertFromExisting(readKeyFromFile("bob@com.pub"),
  205. readKeyFromFile("bob@com.priv"));
  206.  
  207. ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext(true).build();
  208. stub = CommandService_v1Grpc.newBlockingStub(channel);
  209. queryStub = QueryService_v1Grpc.newBlockingStub(channel);
  210.  
  211. prepare();
  212. // setAliceSignatoryAndQuorum();
  213. // createMstTransaction();
  214. getPendingTransactions();
  215. }
  216. }
Add Comment
Please, Sign In to add comment