WebGLSignVerifyExample.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. #if UNITY_WEBGL
  5. public class WebGLSignVerifyExample : MonoBehaviour
  6. {
  7. public string message = "hello";
  8. public Text textHashedMessage;
  9. public Text textSignedHash;
  10. public Text verifyAddress;
  11. async public void OnHashMessage()
  12. {
  13. try
  14. {
  15. string hashedMessage = await Web3GL.Sha3(message);
  16. textHashedMessage.text = hashedMessage;
  17. Debug.Log("Hashed Message: " + hashedMessage);
  18. string signHashed = await Web3GL.Sign(hashedMessage);
  19. Debug.Log("Signed Hashed: " + signHashed);
  20. textSignedHash.text = signHashed;
  21. ParseSignatureFunction(signHashed);
  22. string verify = await Web3GL.EcRecover(hashedMessage, signHashed);
  23. verifyAddress.text = verify;
  24. Debug.Log("Verify Address: " + verifyAddress.text);
  25. }
  26. catch (Exception e)
  27. {
  28. Debug.LogException(e, this);
  29. }
  30. }
  31. public void ParseSignatureFunction(string sig)
  32. {
  33. string signature = sig;
  34. string r = signature.Substring(0, 66);
  35. Debug.Log("R:" + r);
  36. string s = "0x" + signature.Substring(66, 64);
  37. Debug.Log("S: " + s);
  38. int v = int.Parse(signature.Substring(130, 2), System.Globalization.NumberStyles.HexNumber);
  39. Debug.Log("V: " + v);
  40. }
  41. }
  42. #endif