-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWeb3AuthSample.cs
321 lines (274 loc) · 10.7 KB
/
Web3AuthSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static Web3Auth;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Crypto.Digests;
public class Web3AuthSample : MonoBehaviour
{
List<LoginVerifier> verifierList = new List<LoginVerifier> {
new LoginVerifier("Google", AuthConnection.GOOGLE),
new LoginVerifier("Facebook", AuthConnection.FACEBOOK),
// new LoginVerifier("CUSTOM_VERIFIER", AuthConnection.CUSTOM_VERIFIER),
new LoginVerifier("Twitch", AuthConnection.TWITCH),
new LoginVerifier("Discord", AuthConnection.DISCORD),
new LoginVerifier("Reddit", AuthConnection.REDDIT),
new LoginVerifier("Apple", AuthConnection.APPLE),
new LoginVerifier("Github", AuthConnection.GITHUB),
new LoginVerifier("LinkedIn", AuthConnection.LINKEDIN),
new LoginVerifier("Twitter", AuthConnection.TWITTER),
new LoginVerifier("Line", AuthConnection.LINE),
new LoginVerifier("Email Passwordless", AuthConnection.EMAIL_PASSWORDLESS),
new LoginVerifier("SMS Passwordless", AuthConnection.SMS_PASSWORDLESS),
new LoginVerifier("Farcaster", AuthConnection.FARCASTER),
};
Web3Auth web3Auth;
[SerializeField]
InputField emailAddressField;
[SerializeField]
Dropdown verifierDropdown;
[SerializeField]
Button loginButton;
[SerializeField]
Text loginResponseText;
[SerializeField]
Button logoutButton;
[SerializeField]
Button mfaSetupButton;
[SerializeField]
Button launchWalletServicesButton;
[SerializeField]
Button signMessageButton;
[SerializeField]
Button signResponseButton;
void Start()
{
var authConnectionItem = new AuthConnectionConfig()
{
authConnectionId = "your_verifierid_from_web3auth_dashboard", // corresponds to `verifier`
authConnection = AuthConnection.GOOGLE,
clientId = "your_clientId_from_web3auth_dashboard"
};
var authConnectionConfig = new List<AuthConnectionConfig> { authConnectionItem };
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{
whiteLabel = new WhiteLabelData()
{
appName = "Web3Auth Sample App",
logoLight = null,
logoDark = null,
defaultLanguage = Language.en,
mode = ThemeModes.dark,
theme = new Dictionary<string, string>
{
{ "primary", "#FFBF00" }
}
},
// If using your own custom verifier, uncomment this code.
/*
,
loginConfig = new Dictionary<string, LoginConfigItem>
{
{"CUSTOM_VERIFIER", loginConfigItem}
}
*/
authConnectionConfig = new List<AuthConnectionConfig>()
{
new AuthConnectionConfig()
{
authConnectionId = "web3auth-auth0-email-passwordless-sapphire-devnet",
authConnection = AuthConnection.CUSTOM,
clientId = "d84f6xvbdV75VTGmHiMWfZLeSPk8M07C"
}
},
clientId = "BFuUqebV5I8Pz5F7a5A2ihW7YVmbv_OHXnHYDv6OltAD5NGr6e-ViNvde3U4BHdn6HvwfkgobhVu4VwC-OSJkik",
authBuildEnv = BuildEnv.TESTING,
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity"),
web3AuthNetwork = Web3Auth.Network.SAPPHIRE_DEVNET,
sessionTime = 86400
});
web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
web3Auth.onMFASetup += onMFASetup;
web3Auth.onSignResponse += onSignResponse;
web3Auth.onManageMFA += onManageMFA;
emailAddressField.gameObject.SetActive(false);
logoutButton.gameObject.SetActive(false);
mfaSetupButton.gameObject.SetActive(false);
launchWalletServicesButton.gameObject.SetActive(false);
signMessageButton.gameObject.SetActive(false);
signResponseButton.gameObject.SetActive(false);
loginButton.onClick.AddListener(login);
logoutButton.onClick.AddListener(logout);
mfaSetupButton.onClick.AddListener(enableMFA);
launchWalletServicesButton.onClick.AddListener(showWalletUI);
signMessageButton.onClick.AddListener(request);
signResponseButton.onClick.AddListener(manageMFA);
verifierDropdown.AddOptions(verifierList.Select(x => x.name).ToList());
verifierDropdown.onValueChanged.AddListener(onVerifierDropDownChange);
}
private void onLogin(Web3AuthResponse response)
{
loginResponseText.text = JsonConvert.SerializeObject(response, Formatting.Indented);
var userInfo = JsonConvert.SerializeObject(response.userInfo, Formatting.Indented);
Debug.Log(userInfo);
loginButton.gameObject.SetActive(false);
verifierDropdown.gameObject.SetActive(false);
emailAddressField.gameObject.SetActive(false);
logoutButton.gameObject.SetActive(true);
mfaSetupButton.gameObject.SetActive(true);
launchWalletServicesButton.gameObject.SetActive(true);
signMessageButton.gameObject.SetActive(true);
signResponseButton.gameObject.SetActive(true);
}
private void onLogout()
{
loginButton.gameObject.SetActive(true);
verifierDropdown.gameObject.SetActive(true);
logoutButton.gameObject.SetActive(false);
mfaSetupButton.gameObject.SetActive(false);
launchWalletServicesButton.gameObject.SetActive(false);
signMessageButton.gameObject.SetActive(false);
signResponseButton.gameObject.SetActive(false);
loginResponseText.text = "";
}
private void onMFASetup(bool response) {
Debug.Log("MFA Setup: " + response);
}
private void onSignResponse(SignResponse signResponse)
{
Debug.Log("Retrieved SignResponse: " + signResponse);
}
private void onManageMFA(bool response) {
Debug.Log("Manage MFA: " + response);
}
private void onVerifierDropDownChange(int selectedIndex)
{
if (verifierList[selectedIndex].authConnection == AuthConnection.EMAIL_PASSWORDLESS)
emailAddressField.gameObject.SetActive(true);
else
emailAddressField.gameObject.SetActive(false);
}
private void login()
{
var selectedProvider = verifierList[verifierDropdown.value].authConnection;
var options = new LoginParams()
{
authConnection = selectedProvider
};
if (selectedProvider == AuthConnection.EMAIL_PASSWORDLESS)
{
options.extraLoginOptions = new ExtraLoginOptions()
{
login_hint = emailAddressField.text
};
}
if (selectedProvider == AuthConnection.SMS_PASSWORDLESS)
{
options.extraLoginOptions = new ExtraLoginOptions()
{
login_hint = "+XX-XXXXXXXXXX"
};
}
web3Auth.login(options);
}
private void logout()
{
web3Auth.logout();
}
private void enableMFA()
{
var selectedProvider = verifierList[verifierDropdown.value].authConnection;
var options = new LoginParams()
{
authConnection = selectedProvider,
mfaLevel = MFALevel.MANDATORY
};
if (selectedProvider == AuthConnection.EMAIL_PASSWORDLESS)
{
options.extraLoginOptions = new ExtraLoginOptions()
{
login_hint = emailAddressField.text
};
}
web3Auth.enableMFA(options);
}
private void manageMFA()
{
var selectedProvider = verifierList[verifierDropdown.value].authConnection;
var options = new LoginParams()
{
authConnection = selectedProvider,
mfaLevel = MFALevel.MANDATORY
};
if (selectedProvider == AuthConnection.EMAIL_PASSWORDLESS)
{
options.extraLoginOptions = new ExtraLoginOptions()
{
login_hint = emailAddressField.text
};
}
web3Auth.manageMFA(options);
}
private void showWalletUI() {
var selectedProvider = verifierList[verifierDropdown.value].authConnection;
var chainConfig = new ChainConfig()
{
chainId = "0x1",
rpcTarget = "https://mainnet.infura.io/v3/daeee53504be4cd3a997d4f2718d33e0",
ticker = "ETH",
chainNamespace = Web3Auth.ChainNamespace.eip155
};
var chainConfigList = new List<ChainConfig> { chainConfig };
foreach (var config in chainConfigList)
{
Debug.Log($"Chain ID: {config.chainId}, RPC Target: {config.rpcTarget}, Ticker: {config.ticker}, Namespace: {config.chainNamespace}");
}
web3Auth.showWalletUI(chainConfigList, "0x1");
}
private void request() {
var selectedProvider = verifierList[verifierDropdown.value].authConnection;
var chainConfig = new ChainConfig()
{
chainId = "0x89",
rpcTarget = "https://1rpc.io/matic",
chainNamespace = Web3Auth.ChainNamespace.eip155
};
JArray paramsArray = new JArray
{
"Hello, World!",
getPublicAddressFromPrivateKey(web3Auth.getPrivKey()),
"Android"
};
web3Auth.request(chainConfig, "personal_sign", paramsArray);
}
public string getPublicAddressFromPrivateKey(string privateKeyHex)
{
byte[] privateKeyBytes = Hex.Decode(privateKeyHex);
// Create the EC private key parameters
BigInteger privateKeyInt = new BigInteger(1, privateKeyBytes);
var ecParams = SecNamedCurves.GetByName("secp256k1");
ECPoint q = ecParams.G.Multiply(privateKeyInt);
// Get the public key bytes
byte[] publicKeyBytes = q.GetEncoded(false).Skip(1).ToArray();
// Compute the Keccak-256 hash of the public key
var digest = new KeccakDigest(256);
byte[] hash = new byte[digest.GetDigestSize()];
digest.BlockUpdate(publicKeyBytes, 0, publicKeyBytes.Length);
digest.DoFinal(hash, 0);
// Take the last 20 bytes of the hash as the address
byte[] addressBytes = hash.Skip(12).ToArray();
string publicAddress = "0x" + BitConverter.ToString(addressBytes).Replace("-", "").ToLower();
return publicAddress;
}
}