Web3GL.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading.Tasks;
  4. using GameData;
  5. using UnityEngine;
  6. using Web3Unity.Scripts.Library.ETHEREUEM.WebGL;
  7. #if UNITY_WEBGL
  8. public class Web3GL
  9. {
  10. [DllImport("__Internal")]
  11. private static extern void SendContractJs(string method, string abi, string contract, string args, string value,
  12. string gasLimit, string gasPrice);
  13. [DllImport("__Internal")]
  14. private static extern string SendContractResponse();
  15. [DllImport("__Internal")]
  16. private static extern void SetContractResponse(string value);
  17. [DllImport("__Internal")]
  18. private static extern void EcRecoverJS(string message, string signature);
  19. [DllImport("__Internal")]
  20. private static extern string EcRecoverResponse();
  21. [DllImport("__Internal")]
  22. private static extern void SendTransactionJs(string to, string value, string gasLimit, string gasPrice);
  23. [DllImport("__Internal")]
  24. private static extern void SendTransactionJsData(string to, string value, string gasPrice, string gasLimit,
  25. string data);
  26. [DllImport("__Internal")]
  27. private static extern string SendTransactionResponse();
  28. [DllImport("__Internal")]
  29. private static extern void SetTransactionResponse(string value);
  30. [DllImport("__Internal")]
  31. private static extern void SetTransactionResponseData(string value);
  32. [DllImport("__Internal")]
  33. private static extern void SignMessage(string value);
  34. [DllImport("__Internal")]
  35. private static extern void HashMessage(string value);
  36. [DllImport("__Internal")]
  37. private static extern string SignMessageResponse();
  38. [DllImport("__Internal")]
  39. private static extern string HashMessageResponse();
  40. [DllImport("__Internal")]
  41. private static extern void SetSignMessageResponse(string value);
  42. [DllImport("__Internal")]
  43. private static extern void SetHashMessageResponse(string value);
  44. [DllImport("__Internal")]
  45. private static extern int GetNetwork();
  46. // this function will create a metamask tx for user to confirm.
  47. public static async Task<string> SendContract(string _method, string _abi, string _contract, string _args,
  48. string _value, string _gasLimit = "", string _gasPrice = "")
  49. {
  50. // Set response to empty
  51. SetContractResponse("");
  52. SendContractJs(_method, _abi, _contract, _args, _value, _gasLimit, _gasPrice);
  53. var data = new
  54. {
  55. Client = "WebGL",
  56. Version = "v2",
  57. ProjectID = PlayerPrefs.GetString("ProjectID"),
  58. Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")),
  59. Method = _method,
  60. Address = _contract,
  61. ABI = _abi,
  62. ARGS = _args,
  63. Value = _value,
  64. GasLimit = _gasLimit,
  65. GasPrice = _gasPrice
  66. };
  67. await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
  68. var response = SendContractResponse();
  69. while (response == "")
  70. {
  71. await new WaitForSeconds(1f);
  72. response = SendContractResponse();
  73. }
  74. SetContractResponse("");
  75. // check if user submmited or user rejected
  76. if (response.Length == 66)
  77. {
  78. await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
  79. return response;
  80. }
  81. throw new Exception(response);
  82. }
  83. public static async Task<string> SendTransaction(string _to, string _value, string _gasLimit = "",
  84. string _gasPrice = "")
  85. {
  86. // Set response to empty
  87. SetTransactionResponse("");
  88. SendTransactionJs(_to, _value, _gasLimit, _gasPrice);
  89. var data = new
  90. {
  91. Client = "WebGL",
  92. Version = "v2",
  93. ProjectID = PlayerPrefs.GetString("ProjectID"),
  94. Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")).ToString(),
  95. To = _to,
  96. Value = _value,
  97. GasLimit = _gasLimit,
  98. GasPrice = _gasPrice
  99. };
  100. var response = SendTransactionResponse();
  101. while (response == "")
  102. {
  103. await new WaitForSeconds(1f);
  104. response = SendTransactionResponse();
  105. }
  106. SetTransactionResponse("");
  107. // check if user submmited or user rejected
  108. if (response.Length == 66)
  109. {
  110. await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
  111. return response;
  112. }
  113. throw new Exception(response);
  114. }
  115. public static async Task<string> SendTransactionData(string _to, string _value, string _gasPrice = "",
  116. string _gasLimit = "", string _data = "")
  117. {
  118. // Set response to empty
  119. SetTransactionResponse("");
  120. SendTransactionJsData(_to, _value, _gasPrice, _gasLimit, _data);
  121. var data = new
  122. {
  123. Client = "WebGL",
  124. Version = "v2",
  125. ProjectID = PlayerPrefs.GetString("ProjectID"),
  126. Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")),
  127. To = _to,
  128. Value = _value,
  129. GasLimit = _gasLimit,
  130. GasPrice = _gasPrice
  131. };
  132. var response = SendTransactionResponse();
  133. Debug.Log("called from webgl" + response);
  134. //Logging.SendGameData(data);
  135. while (response == "")
  136. {
  137. await new WaitForSeconds(1f);
  138. response = SendTransactionResponse();
  139. }
  140. SetTransactionResponse("");
  141. // check if user submmited or user rejected
  142. if (response.Length == 66)
  143. {
  144. return response;
  145. }
  146. throw new Exception(response);
  147. }
  148. public static async Task<string> Sign(string _message)
  149. {
  150. SignMessage(_message);
  151. var response = SignMessageResponse();
  152. while (response == "")
  153. {
  154. await new WaitForSeconds(1f);
  155. response = SignMessageResponse();
  156. }
  157. // Set response to empty
  158. SetSignMessageResponse("");
  159. // check if user submmited or user rejected
  160. if (response.Length == 132)
  161. return response;
  162. throw new Exception(response);
  163. }
  164. public static async Task<string> Sha3(string _message)
  165. {
  166. HashMessage(_message);
  167. SetHashMessageResponse("");
  168. try
  169. {
  170. await new WaitForSeconds(1f);
  171. var response = HashMessageResponse();
  172. return response;
  173. }
  174. catch (Exception e)
  175. {
  176. Debug.Log(e);
  177. throw;
  178. }
  179. }
  180. public static async Task<string> EcRecover(string _message, string _signature)
  181. {
  182. EcRecoverJS(_message, _signature);
  183. EcRecoverResponse();
  184. try
  185. {
  186. await new WaitForSeconds(1f);
  187. var response = EcRecoverResponse();
  188. return response;
  189. }
  190. catch (Exception e)
  191. {
  192. Debug.Log(e);
  193. throw;
  194. }
  195. }
  196. public static int Network()
  197. {
  198. return GetNetwork();
  199. }
  200. }
  201. #endif