This commit is contained in:
2023-11-28 11:36:18 +05:30
commit 3fe8517bab
482 changed files with 54087 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
namespace Web3Unity.Scripts.Library.ETHEREUEM.WebGL
{
public class GameLogger
{
private const string loggingUrl = "https://game-api-stg.chainsafe.io/logging/logEvent";
public static async Task<string> Log(string _chain, string _network, object _data)
{
using var webRequest = new UnityWebRequest(loggingUrl, "POST");
webRequest.timeout = -1;
var bodyRaw = System.Text.Encoding.UTF8.GetBytes($"chain={_chain}&network={_network}&gameData={_data}");
webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
webRequest.downloadHandler = new DownloadHandlerBuffer();
webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
await webRequest.SendWebRequest();
return webRequest.result switch
{
UnityWebRequest.Result.ProtocolError => webRequest.error,
_ => webRequest.downloadHandler.text
};
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b5e65eab7e134a94f90d090a2d8d8a89
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,232 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using GameData;
using UnityEngine;
using Web3Unity.Scripts.Library.ETHEREUEM.WebGL;
#if UNITY_WEBGL
public class Web3GL
{
[DllImport("__Internal")]
private static extern void SendContractJs(string method, string abi, string contract, string args, string value,
string gasLimit, string gasPrice);
[DllImport("__Internal")]
private static extern string SendContractResponse();
[DllImport("__Internal")]
private static extern void SetContractResponse(string value);
[DllImport("__Internal")]
private static extern void EcRecoverJS(string message, string signature);
[DllImport("__Internal")]
private static extern string EcRecoverResponse();
[DllImport("__Internal")]
private static extern void SendTransactionJs(string to, string value, string gasLimit, string gasPrice);
[DllImport("__Internal")]
private static extern void SendTransactionJsData(string to, string value, string gasPrice, string gasLimit,
string data);
[DllImport("__Internal")]
private static extern string SendTransactionResponse();
[DllImport("__Internal")]
private static extern void SetTransactionResponse(string value);
[DllImport("__Internal")]
private static extern void SetTransactionResponseData(string value);
[DllImport("__Internal")]
private static extern void SignMessage(string value);
[DllImport("__Internal")]
private static extern void HashMessage(string value);
[DllImport("__Internal")]
private static extern string SignMessageResponse();
[DllImport("__Internal")]
private static extern string HashMessageResponse();
[DllImport("__Internal")]
private static extern void SetSignMessageResponse(string value);
[DllImport("__Internal")]
private static extern void SetHashMessageResponse(string value);
[DllImport("__Internal")]
private static extern int GetNetwork();
// this function will create a metamask tx for user to confirm.
public static async Task<string> SendContract(string _method, string _abi, string _contract, string _args,
string _value, string _gasLimit = "", string _gasPrice = "")
{
// Set response to empty
SetContractResponse("");
SendContractJs(_method, _abi, _contract, _args, _value, _gasLimit, _gasPrice);
var data = new
{
Client = "WebGL",
Version = "v2",
ProjectID = PlayerPrefs.GetString("ProjectID"),
Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")),
Method = _method,
Address = _contract,
ABI = _abi,
ARGS = _args,
Value = _value,
GasLimit = _gasLimit,
GasPrice = _gasPrice
};
await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
var response = SendContractResponse();
while (response == "")
{
await new WaitForSeconds(1f);
response = SendContractResponse();
}
SetContractResponse("");
// check if user submmited or user rejected
if (response.Length == 66)
{
await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
return response;
}
throw new Exception(response);
}
public static async Task<string> SendTransaction(string _to, string _value, string _gasLimit = "",
string _gasPrice = "")
{
// Set response to empty
SetTransactionResponse("");
SendTransactionJs(_to, _value, _gasLimit, _gasPrice);
var data = new
{
Client = "WebGL",
Version = "v2",
ProjectID = PlayerPrefs.GetString("ProjectID"),
Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")).ToString(),
To = _to,
Value = _value,
GasLimit = _gasLimit,
GasPrice = _gasPrice
};
var response = SendTransactionResponse();
while (response == "")
{
await new WaitForSeconds(1f);
response = SendTransactionResponse();
}
SetTransactionResponse("");
// check if user submmited or user rejected
if (response.Length == 66)
{
await GameLogger.Log(PlayerPrefs.GetString("ChainId"), PlayerPrefs.GetString("RPC"), data);
return response;
}
throw new Exception(response);
}
public static async Task<string> SendTransactionData(string _to, string _value, string _gasPrice = "",
string _gasLimit = "", string _data = "")
{
// Set response to empty
SetTransactionResponse("");
SendTransactionJsData(_to, _value, _gasPrice, _gasLimit, _data);
var data = new
{
Client = "WebGL",
Version = "v2",
ProjectID = PlayerPrefs.GetString("ProjectID"),
Player = Sha3(PlayerPrefs.GetString("Account") + PlayerPrefs.GetString("ProjectID")),
To = _to,
Value = _value,
GasLimit = _gasLimit,
GasPrice = _gasPrice
};
var response = SendTransactionResponse();
Debug.Log("called from webgl" + response);
//Logging.SendGameData(data);
while (response == "")
{
await new WaitForSeconds(1f);
response = SendTransactionResponse();
}
SetTransactionResponse("");
// check if user submmited or user rejected
if (response.Length == 66)
{
return response;
}
throw new Exception(response);
}
public static async Task<string> Sign(string _message)
{
SignMessage(_message);
var response = SignMessageResponse();
while (response == "")
{
await new WaitForSeconds(1f);
response = SignMessageResponse();
}
// Set response to empty
SetSignMessageResponse("");
// check if user submmited or user rejected
if (response.Length == 132)
return response;
throw new Exception(response);
}
public static async Task<string> Sha3(string _message)
{
HashMessage(_message);
SetHashMessageResponse("");
try
{
await new WaitForSeconds(1f);
var response = HashMessageResponse();
return response;
}
catch (Exception e)
{
Debug.Log(e);
throw;
}
}
public static async Task<string> EcRecover(string _message, string _signature)
{
EcRecoverJS(_message, _signature);
EcRecoverResponse();
try
{
await new WaitForSeconds(1f);
var response = EcRecoverResponse();
return response;
}
catch (Exception e)
{
Debug.Log(e);
throw;
}
}
public static int Network()
{
return GetNetwork();
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5f471a8d48b5e844d9f6167058b8eebb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,67 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
#if UNITY_WEBGL
public class Web3GLLight
{
[DllImport("__Internal")]
private static extern void ClearResponseJs();
[DllImport("__Internal")]
private static extern string SendAsyncResponse();
[DllImport("__Internal")]
private static extern string SendAsyncError();
[DllImport("__Internal")]
private static extern void SendAsyncJs(string method, string parameters);
public static async Task<string> SendAsync(string method, string parameters)
{
ClearResponseJs();
SendAsyncJs(method, parameters);
var response = "";
var error = "";
while (response == "" && error == "")
{
await new WaitForSeconds(0.1f);
response = SendAsyncResponse();
error = SendAsyncError();
}
if (error != "")
{
var err = JsonConvert.DeserializeObject<WalletError>(error);
throw new WalletException(err!.Code, err.Message);
}
return response;
}
public class WalletError
{
[JsonProperty(PropertyName = "code")] public int Code { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
}
[Serializable]
public class WalletException : Exception
{
public int code;
public string message;
public WalletException(int code, string message)
{
this.code = code;
this.message = message;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2fcd90ef44624854aa5ae479c1df87af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,121 @@
mergeInto(LibraryManager.library, {
Web3Connect: function () {
window.web3gl.connect();
},
ConnectAccount: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.connectAccount) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.connectAccount, buffer, bufferSize);
return buffer;
},
SetConnectAccount: function (value) {
window.web3gl.connectAccount = value;
},
SendContractJs: function (method, abi, contract, args, value, gasLimit, gasPrice) {
window.web3gl.sendContract(
UTF8ToString(method),
UTF8ToString(abi),
UTF8ToString(contract),
UTF8ToString(args),
UTF8ToString(value),
UTF8ToString(gasLimit),
UTF8ToString(gasPrice)
);
},
SendContractResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.sendContractResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.sendContractResponse, buffer, bufferSize);
return buffer;
},
EcRecoverJS: function (message,signature) {
window.web3gl.ecRecover(
UTF8ToString(message),
UTF8ToString(signature)
);
},
EcRecoverResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.ecRecoverAddressResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.ecRecoverAddressResponse, buffer, bufferSize);
return buffer;
},
SetContractResponse: function (value) {
window.web3gl.sendContractResponse = value;
},
SendTransactionJs: function (to, value, gasLimit, gasPrice) {
window.web3gl.sendTransaction(
UTF8ToString(to),
UTF8ToString(value),
UTF8ToString(gasLimit),
UTF8ToString(gasPrice)
);
},
SendTransactionJsData: function (to, value, gasLimit, gasPrice, data) {
window.web3gl.sendTransactionData(
UTF8ToString(to),
UTF8ToString(value),
UTF8ToString(gasLimit),
UTF8ToString(gasPrice),
UTF8ToString(data)
);
},
SendTransactionResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.sendTransactionResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.sendTransactionResponse, buffer, bufferSize);
return buffer;
},
SetTransactionResponse: function (value) {
window.web3gl.sendTransactionResponse = value;
},
SetTransactionResponseData: function (value) {
window.web3gl.sendTransactionResponseData = value;
},
SignMessage: function (message) {
window.web3gl.signMessage(UTF8ToString(message));
},
HashMessage: function (message) {
window.web3gl.sha3Message(UTF8ToString(message));
},
SignMessageResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.signMessageResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.signMessageResponse, buffer, bufferSize);
return buffer;
},
HashMessageResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.hashMessageResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.hashMessageResponse, buffer, bufferSize);
return buffer;
},
SetSignMessageResponse: function (value) {
window.web3gl.signMessageResponse = value;
},
SetHashMessageResponse: function (value) {
window.web3gl.hashMessageResponse = value;
},
GetNetwork: function () {
return window.web3gl.networkId;
}
});

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: b12b190155856e14fbd9f0c367af9a55
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
WebGL: WebGL
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
mergeInto(LibraryManager.library, {
ClearResponseJs: function () {
window.web3gl.clearResponse();
},
SendAsyncJs: function (method, parameters) {
window.web3gl.sendAsync(
UTF8ToString(method),
UTF8ToString(parameters)
);
},
SendAsyncResponse: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.sendAsyncResponse) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.sendAsyncResponse, buffer, bufferSize);
return buffer;
},
SendAsyncError: function () {
var bufferSize = lengthBytesUTF8(window.web3gl.sendAsyncError) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(window.web3gl.sendAsyncError, buffer, bufferSize);
return buffer;
}
});

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 963e6e0df2159154bb03c6338828d46f
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
WebGL: WebGL
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant: