Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Assets/Plugins/Web3AuthSDK/Editor/BuildPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProj
urlSchemes.AddString(uri.Scheme);

infoPlist.WriteToFile(infoPlistPath);


#endif
}
}
78 changes: 65 additions & 13 deletions Assets/Plugins/Web3AuthSDK/Keystore/KeyStoreManagerUtils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Signer;
using Nethereum.Util;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System.Runtime.InteropServices;

public class KeyStoreManagerUtils
{
#if UNITY_IOS
[DllImport("__Internal")]
extern static int web3auth_keystore_set(string key, string value);

[DllImport("__Internal")]
extern static string web3auth_keystore_get(string key);

[DllImport("__Internal")]
extern static int web3auth_keystore_delete(string key);
#endif

public static string SESSION_ID = "sessionId";
public static string IV_KEY = "ivKey";
Expand All @@ -13,40 +27,78 @@ public class KeyStoreManagerUtils

public static string getPubKey(string sessionId)
{
var privKey = new EthECKey(sessionId);
return privKey.GetPubKey().ToHex();
var domain = SecNamedCurves.GetByName("secp256k1");
var parameters = new ECDomainParameters(domain.Curve, domain.G, domain.H);

var key = new ECPrivateKeyParameters(new BigInteger(sessionId, 16), parameters);
var q = new ECPublicKeyParameters("EC", domain.G.Multiply(key.D), parameters).Q;

return Hex.ToHexString(domain.Curve.CreatePoint(q.XCoord.ToBigInteger(), q.YCoord.ToBigInteger()).GetEncoded(false));
}

static KeyStoreManagerUtils()
{
#if !UNITY_IOS
SecurePlayerPrefs.Init();
#endif
}

public static void savePreferenceData(string key, string value)
{
#if UNITY_IOS
web3auth_keystore_set(key, value);
#else
SecurePlayerPrefs.SetString(key, value);
#endif
}

public static string getPreferencesData(string key)
{
#if UNITY_IOS
return web3auth_keystore_get(key);
#else
return SecurePlayerPrefs.GetString(key);
#endif
}
public static void deletePreferencesData(string key)
{
#if UNITY_IOS
web3auth_keystore_delete(key);
#else
SecurePlayerPrefs.DeleteKey(key);
#endif
}

public static string getECDSASignature(string privateKey, string data){
var derivedECKeyPair = new EthECKey(privateKey);
byte[] hashedData = new Sha3Keccack().CalculateHash(System.Text.Encoding.UTF8.GetBytes(data));
var curve = SecNamedCurves.GetByName("secp256k1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
var keyParameters = new ECPrivateKeyParameters(new BigInteger(privateKey, 16), domain);

var signer = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest()));
signer.Init(true, keyParameters);

var hashAlgorithm = new KeccakDigest(256);
byte[] input = System.Text.Encoding.UTF8.GetBytes(data);
hashAlgorithm.BlockUpdate(input, 0, input.Length);

byte[] messageHash = new byte[32];
hashAlgorithm.DoFinal(messageHash, 0);

var signature = signer.GenerateSignature(messageHash);

var r = signature[0];
var s = signature[1];

var other = curve.Curve.Order.Subtract(s);
if (s.CompareTo(other) == 1)
s = other;

var signature = derivedECKeyPair.Sign(hashedData);
var v = new Asn1EncodableVector();
v.Add(new DerInteger(signature.R));
v.Add(new DerInteger(signature.S));
v.Add(new DerInteger(r));
v.Add(new DerInteger(s));

var derSignature = new DerSequence(v).GetDerEncoded();

var der = new DerSequence(v);
var sigBytes = der.GetEncoded();
return sigBytes.ToHexCompact();
return Hex.ToHexString(derSignature);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

This file was deleted.

Binary file not shown.
33 changes: 0 additions & 33 deletions Assets/Plugins/Web3AuthSDK/Nethereum/Nethereum.ABI.dll.meta

This file was deleted.

Binary file not shown.
33 changes: 0 additions & 33 deletions Assets/Plugins/Web3AuthSDK/Nethereum/Nethereum.Accounts.dll.meta

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
33 changes: 0 additions & 33 deletions Assets/Plugins/Web3AuthSDK/Nethereum/Nethereum.Contracts.dll.meta

This file was deleted.

Binary file not shown.
33 changes: 0 additions & 33 deletions Assets/Plugins/Web3AuthSDK/Nethereum/Nethereum.GnosisSafe.dll.meta

This file was deleted.

Binary file not shown.
33 changes: 0 additions & 33 deletions Assets/Plugins/Web3AuthSDK/Nethereum/Nethereum.Hex.dll.meta

This file was deleted.

Binary file not shown.
Loading