Skip to content

Commit 1fa30ef

Browse files
authored
Merge branch 'master' into feat/config_api
2 parents b4afc64 + 66617f2 commit 1fa30ef

File tree

5 files changed

+621
-15
lines changed

5 files changed

+621
-15
lines changed

Assets/Plugins/Web3AuthSDK/Samples/Web3AuthSample.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
using UnityEngine;
55
using UnityEngine.UI;
66
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
78
using static Web3Auth;
9+
using Org.BouncyCastle.Asn1.Sec;
10+
using Org.BouncyCastle.Crypto.Parameters;
11+
using Org.BouncyCastle.Math;
12+
using Org.BouncyCastle.Math.EC;
13+
using Org.BouncyCastle.Security;
14+
using Org.BouncyCastle.Utilities.Encoders;
15+
using Org.BouncyCastle.Crypto.Digests;
816

917
public class Web3AuthSample : MonoBehaviour
1018
{
@@ -48,6 +56,12 @@ public class Web3AuthSample : MonoBehaviour
4856
[SerializeField]
4957
Button launchWalletServicesButton;
5058

59+
[SerializeField]
60+
Button signMessageButton;
61+
62+
[SerializeField]
63+
Button signResponseButton;
64+
5165
void Start()
5266
{
5367
var loginConfigItem = new LoginConfigItem()
@@ -81,7 +95,7 @@ void Start()
8195
}
8296
*/
8397
clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
84-
buildEnv = BuildEnv.PRODUCTION,
98+
buildEnv = BuildEnv.TESTING,
8599
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
86100
network = Web3Auth.Network.SAPPHIRE_DEVNET,
87101
sessionTime = 86400
@@ -94,11 +108,15 @@ void Start()
94108
logoutButton.gameObject.SetActive(false);
95109
mfaSetupButton.gameObject.SetActive(false);
96110
launchWalletServicesButton.gameObject.SetActive(false);
111+
signMessageButton.gameObject.SetActive(false);
112+
signResponseButton.gameObject.SetActive(false);
97113

98114
loginButton.onClick.AddListener(login);
99115
logoutButton.onClick.AddListener(logout);
100116
mfaSetupButton.onClick.AddListener(enableMFA);
101117
launchWalletServicesButton.onClick.AddListener(launchWalletServices);
118+
signMessageButton.onClick.AddListener(request);
119+
signResponseButton.onClick.AddListener(getSignResponse);
102120

103121
verifierDropdown.AddOptions(verifierList.Select(x => x.name).ToList());
104122
verifierDropdown.onValueChanged.AddListener(onVerifierDropDownChange);
@@ -116,6 +134,8 @@ private void onLogin(Web3AuthResponse response)
116134
logoutButton.gameObject.SetActive(true);
117135
mfaSetupButton.gameObject.SetActive(true);
118136
launchWalletServicesButton.gameObject.SetActive(true);
137+
signMessageButton.gameObject.SetActive(true);
138+
signResponseButton.gameObject.SetActive(true);
119139
}
120140

121141
private void onLogout()
@@ -125,6 +145,8 @@ private void onLogout()
125145
logoutButton.gameObject.SetActive(false);
126146
mfaSetupButton.gameObject.SetActive(false);
127147
launchWalletServicesButton.gameObject.SetActive(false);
148+
signMessageButton.gameObject.SetActive(false);
149+
signResponseButton.gameObject.SetActive(false);
128150

129151
loginResponseText.text = "";
130152
}
@@ -206,4 +228,61 @@ private void launchWalletServices() {
206228
};
207229
web3Auth.launchWalletServices(chainConfig);
208230
}
231+
232+
private void request() {
233+
var selectedProvider = verifierList[verifierDropdown.value].loginProvider;
234+
235+
var chainConfig = new ChainConfig()
236+
{
237+
chainId = "0x89",
238+
rpcTarget = "https://1rpc.io/matic",
239+
chainNamespace = Web3Auth.ChainNamespace.EIP155
240+
};
241+
242+
JArray paramsArray = new JArray
243+
{
244+
"Hello, World!",
245+
getPublicAddressFromPrivateKey(web3Auth.getPrivKey()),
246+
"Android"
247+
};
248+
249+
web3Auth.request(chainConfig, "personal_sign", paramsArray);
250+
}
251+
252+
public string getPublicAddressFromPrivateKey(string privateKeyHex)
253+
{
254+
byte[] privateKeyBytes = Hex.Decode(privateKeyHex);
255+
256+
// Create the EC private key parameters
257+
BigInteger privateKeyInt = new BigInteger(1, privateKeyBytes);
258+
var ecParams = SecNamedCurves.GetByName("secp256k1");
259+
ECPoint q = ecParams.G.Multiply(privateKeyInt);
260+
261+
// Get the public key bytes
262+
byte[] publicKeyBytes = q.GetEncoded(false).Skip(1).ToArray();
263+
264+
// Compute the Keccak-256 hash of the public key
265+
var digest = new KeccakDigest(256);
266+
byte[] hash = new byte[digest.GetDigestSize()];
267+
digest.BlockUpdate(publicKeyBytes, 0, publicKeyBytes.Length);
268+
digest.DoFinal(hash, 0);
269+
270+
// Take the last 20 bytes of the hash as the address
271+
byte[] addressBytes = hash.Skip(12).ToArray();
272+
string publicAddress = "0x" + BitConverter.ToString(addressBytes).Replace("-", "").ToLower();
273+
274+
return publicAddress;
275+
}
276+
277+
public void getSignResponse() {
278+
SignResponse signResponse = Web3Auth.getSignResponse();
279+
if (signResponse != null)
280+
{
281+
Debug.Log("Retrieved SignResponse: " + signResponse);
282+
}
283+
else
284+
{
285+
Debug.Log("SignResponse is null");
286+
}
287+
}
209288
}

0 commit comments

Comments
 (0)