|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using TMPro; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Thirdweb.Unity.Examples |
| 7 | +{ |
| 8 | + public class EcosystemWalletManager : MonoBehaviour |
| 9 | + { |
| 10 | + public TMP_InputField EcosystemWalletIdInputField; |
| 11 | + public TMP_InputField EcosystemWalletPartnerIdInputField; |
| 12 | + public TMP_InputField ClientIdInputField; |
| 13 | + public TMP_InputField BundleIdInputField; |
| 14 | + public TMP_InputField EmailInputField; |
| 15 | + public TMP_InputField PhoneInputField; |
| 16 | + |
| 17 | + public TMP_Text LogText; |
| 18 | + |
| 19 | + private ThirdwebClient _client; |
| 20 | + private EcosystemWallet _ecosystemWallet; |
| 21 | + |
| 22 | + private void Awake() |
| 23 | + { |
| 24 | + EcosystemWalletIdInputField.text = "ecosystem.bonfire-development"; |
| 25 | + EcosystemWalletPartnerIdInputField.text = "02077ab1-59be-4b62-a768-6c6609da7864"; |
| 26 | + ClientIdInputField.text = "3cbee06cf6521a47257c1a3d1ff860ad"; |
| 27 | + BundleIdInputField.text = ""; |
| 28 | + } |
| 29 | + |
| 30 | + public async void InitializeEcosystemWallet() |
| 31 | + { |
| 32 | + var clientId = ClientIdInputField.text; |
| 33 | + var bundleId = BundleIdInputField.text; |
| 34 | + |
| 35 | + if (string.IsNullOrEmpty(clientId)) |
| 36 | + { |
| 37 | + Log("Please enter Client Id"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + bundleId = string.IsNullOrEmpty(bundleId) ? null : bundleId; |
| 42 | + |
| 43 | + var email = string.IsNullOrEmpty(EmailInputField.text) ? null : EmailInputField.text; |
| 44 | + var phone = string.IsNullOrEmpty(PhoneInputField.text) ? null : PhoneInputField.text; |
| 45 | + |
| 46 | + if (email == null && phone == null) |
| 47 | + { |
| 48 | + Log("Please enter Email or Phone"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + var ecosystemWalletId = EcosystemWalletIdInputField.text; |
| 53 | + var ecosystemWalletPartnerId = EcosystemWalletPartnerIdInputField.text; |
| 54 | + |
| 55 | + if (string.IsNullOrEmpty(ecosystemWalletId)) |
| 56 | + { |
| 57 | + Log("Please enter Ecosystem Wallet Id"); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + ecosystemWalletPartnerId = string.IsNullOrEmpty(ecosystemWalletPartnerId) ? null : ecosystemWalletPartnerId; |
| 62 | + |
| 63 | + _client = ThirdwebClient.Create( |
| 64 | + clientId: clientId, |
| 65 | + bundleId: bundleId, |
| 66 | + httpClient: Application.platform == RuntimePlatform.WebGLPlayer ? new Helpers.UnityThirdwebHttpClient() : new ThirdwebHttpClient(), |
| 67 | + headers: new Dictionary<string, string> |
| 68 | + { |
| 69 | + { "x-sdk-name", Application.platform == RuntimePlatform.WebGLPlayer ? "UnitySDK_WebGL" : "UnitySDK" }, |
| 70 | + { "x-sdk-os", Application.platform.ToString() }, |
| 71 | + { "x-sdk-platform", "unity" }, |
| 72 | + { "x-sdk-version", ThirdwebManager.THIRDWEB_UNITY_SDK_VERSION }, |
| 73 | + { "x-client-id", clientId }, |
| 74 | + { "x-bundle-id", clientId } |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + _ecosystemWallet = await EcosystemWallet.Create( |
| 79 | + client: _client, |
| 80 | + ecosystemId: ecosystemWalletId, |
| 81 | + ecosystemPartnerId: ecosystemWalletPartnerId, |
| 82 | + email: email, |
| 83 | + phoneNumber: phone, |
| 84 | + storageDirectoryPath: Path.Combine(Application.persistentDataPath, "Thirdweb", "EcosystemWallet") |
| 85 | + ); |
| 86 | + |
| 87 | + Log($"Ecosystem Wallet Initialized."); |
| 88 | + } |
| 89 | + |
| 90 | + public async void Login() |
| 91 | + { |
| 92 | + if (_ecosystemWallet == null) |
| 93 | + { |
| 94 | + Log("Please initialize Ecosystem Wallet first."); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + string address; |
| 99 | + if (await _ecosystemWallet.IsConnected()) |
| 100 | + { |
| 101 | + address = await _ecosystemWallet.GetAddress(); |
| 102 | + Log($"Connected with address: {address}"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + _ = await _ecosystemWallet.SendOTP(); |
| 107 | + _ecosystemWallet = await EcosystemWalletModal.LoginWithOtp(_ecosystemWallet); |
| 108 | + |
| 109 | + address = await _ecosystemWallet.GetAddress(); |
| 110 | + Log($"Connected with address: {address}"); |
| 111 | + } |
| 112 | + |
| 113 | + public async void PersonalSign() |
| 114 | + { |
| 115 | + if (_ecosystemWallet == null) |
| 116 | + { |
| 117 | + Log("Please initialize Ecosystem Wallet first."); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + var message = "Hello World!"; |
| 122 | + var signature = await _ecosystemWallet.PersonalSign(message); |
| 123 | + Log($"Personal Sign: {signature}"); |
| 124 | + } |
| 125 | + |
| 126 | + public async void SignTransaction() |
| 127 | + { |
| 128 | + if (_ecosystemWallet == null) |
| 129 | + { |
| 130 | + Log("Please initialize Ecosystem Wallet first."); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + var address = await _ecosystemWallet.GetAddress(); |
| 135 | + var nonce = await _ecosystemWallet.GetTransactionCount(chainId: 421614, blocktag: "pending"); |
| 136 | + var tx = new ThirdwebTransactionInput(chainId: 421614, from: address, to: address, nonce: nonce, gas: 100000, gasPrice: 200000000, value: 0, data: "0x"); |
| 137 | + var signature = await _ecosystemWallet.SignTransaction(tx); |
| 138 | + Log($"Transaction Signed: {signature}"); |
| 139 | + } |
| 140 | + |
| 141 | + private void Log(string message) |
| 142 | + { |
| 143 | + LogText.text = message; |
| 144 | + Debug.Log(message); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments