1- using Nethereum . Hex . HexConvertors . Extensions ;
2- using Nethereum . Signer ;
3- using Nethereum . Util ;
41using Org . BouncyCastle . Asn1 ;
2+ using Org . BouncyCastle . Asn1 . Sec ;
3+ using Org . BouncyCastle . Crypto . Digests ;
4+ using Org . BouncyCastle . Crypto . Parameters ;
5+ using Org . BouncyCastle . Crypto . Signers ;
6+ using Org . BouncyCastle . Math ;
7+ using Org . BouncyCastle . Utilities . Encoders ;
8+ using System . Runtime . InteropServices ;
59
610public class KeyStoreManagerUtils
711{
12+ #if UNITY_IOS
13+ [ DllImport ( "__Internal" ) ]
14+ extern static int web3auth_keystore_set ( string key , string value ) ;
15+
16+ [ DllImport ( "__Internal" ) ]
17+ extern static string web3auth_keystore_get ( string key ) ;
18+
19+ [ DllImport ( "__Internal" ) ]
20+ extern static int web3auth_keystore_delete ( string key ) ;
21+ #endif
822
923 public static string SESSION_ID = "sessionId" ;
1024 public static string IV_KEY = "ivKey" ;
@@ -13,40 +27,78 @@ public class KeyStoreManagerUtils
1327
1428 public static string getPubKey ( string sessionId )
1529 {
16- var privKey = new EthECKey ( sessionId ) ;
17- return privKey . GetPubKey ( ) . ToHex ( ) ;
30+ var domain = SecNamedCurves . GetByName ( "secp256k1" ) ;
31+ var parameters = new ECDomainParameters ( domain . Curve , domain . G , domain . H ) ;
32+
33+ var key = new ECPrivateKeyParameters ( new BigInteger ( sessionId , 16 ) , parameters ) ;
34+ var q = new ECPublicKeyParameters ( "EC" , domain . G . Multiply ( key . D ) , parameters ) . Q ;
35+
36+ return Hex . ToHexString ( domain . Curve . CreatePoint ( q . XCoord . ToBigInteger ( ) , q . YCoord . ToBigInteger ( ) ) . GetEncoded ( false ) ) ;
1837 }
1938
2039 static KeyStoreManagerUtils ( )
2140 {
41+ #if ! UNITY_IOS
2242 SecurePlayerPrefs . Init ( ) ;
43+ #endif
2344 }
2445
2546 public static void savePreferenceData ( string key , string value )
2647 {
48+ #if UNITY_IOS
49+ web3auth_keystore_set ( key , value ) ;
50+ #else
2751 SecurePlayerPrefs . SetString ( key , value ) ;
52+ #endif
2853 }
2954
3055 public static string getPreferencesData ( string key )
3156 {
57+ #if UNITY_IOS
58+ return web3auth_keystore_get ( key ) ;
59+ #else
3260 return SecurePlayerPrefs . GetString ( key ) ;
61+ #endif
3362 }
3463 public static void deletePreferencesData ( string key )
3564 {
65+ #if UNITY_IOS
66+ web3auth_keystore_delete ( key ) ;
67+ #else
3668 SecurePlayerPrefs . DeleteKey ( key ) ;
69+ #endif
3770 }
3871
3972 public static string getECDSASignature ( string privateKey , string data ) {
40- var derivedECKeyPair = new EthECKey ( privateKey ) ;
41- byte [ ] hashedData = new Sha3Keccack ( ) . CalculateHash ( System . Text . Encoding . UTF8 . GetBytes ( data ) ) ;
73+ var curve = SecNamedCurves . GetByName ( "secp256k1" ) ;
74+ var domain = new ECDomainParameters ( curve . Curve , curve . G , curve . N , curve . H ) ;
75+ var keyParameters = new ECPrivateKeyParameters ( new BigInteger ( privateKey , 16 ) , domain ) ;
76+
77+ var signer = new ECDsaSigner ( new HMacDsaKCalculator ( new Sha256Digest ( ) ) ) ;
78+ signer . Init ( true , keyParameters ) ;
79+
80+ var hashAlgorithm = new KeccakDigest ( 256 ) ;
81+ byte [ ] input = System . Text . Encoding . UTF8 . GetBytes ( data ) ;
82+ hashAlgorithm . BlockUpdate ( input , 0 , input . Length ) ;
83+
84+ byte [ ] messageHash = new byte [ 32 ] ;
85+ hashAlgorithm . DoFinal ( messageHash , 0 ) ;
86+
87+ var signature = signer . GenerateSignature ( messageHash ) ;
88+
89+ var r = signature [ 0 ] ;
90+ var s = signature [ 1 ] ;
91+
92+ var other = curve . Curve . Order . Subtract ( s ) ;
93+ if ( s . CompareTo ( other ) == 1 )
94+ s = other ;
4295
43- var signature = derivedECKeyPair . Sign ( hashedData ) ;
4496 var v = new Asn1EncodableVector ( ) ;
45- v . Add ( new DerInteger ( signature . R ) ) ;
46- v . Add ( new DerInteger ( signature . S ) ) ;
97+ v . Add ( new DerInteger ( r ) ) ;
98+ v . Add ( new DerInteger ( s ) ) ;
99+
100+ var derSignature = new DerSequence ( v ) . GetDerEncoded ( ) ;
47101
48- var der = new DerSequence ( v ) ;
49- var sigBytes = der . GetEncoded ( ) ;
50- return sigBytes . ToHexCompact ( ) ;
102+ return Hex . ToHexString ( derSignature ) ;
51103 }
52104}
0 commit comments