public SwitcheoAuthenticationProvider(ApiCredentials credentials, BlockchainType keyType) : base(new ApiCredentials(new PrivateKey(EnsureHexFormat(credentials.PrivateKey.Key, credentials.PrivateKey?.Passphrase)))) { if (this.CanSign) { if (keyType == BlockchainType.Qtum || keyType == BlockchainType.Ethereum) { throw new NotImplementedException(); } try { this.KeyType = keyType; SecureString readablePrivateKey = credentials.PrivateKey.Key; // Decrypting private key if Nep2 format was provided if (WalletsHelper.IsNep2(credentials.PrivateKey.Key)) { readablePrivateKey = Nep2.Decrypt(credentials.PrivateKey.Key.GetString(), credentials.PrivateKey.Passphrase.GetString()).Result.ToHexString().ToSecureString(); } // Extracting wallet informations (public key, script hash, address and fixed address) this.WalletInformations = WalletsHelper.GetWalletInformations(readablePrivateKey, keyType); } catch (Exception) { throw privateKeyException; } } }
private static SecureString EnsureHexFormat(SecureString privateKey, SecureString passphrase = null) { try { //TODO: BlockchainType.Qtum, BlockchainType.Ethereum ... SecureString _privateKey = privateKey; if (WalletsHelper.IsNep2(privateKey) && passphrase != null) { _privateKey = Nep2.Decrypt(privateKey.GetString(), passphrase.GetString()).Result.ToHexString().ToSecureString(); } else { if (privateKey != null && privateKey.Length > 0) { if (!WalletsHelper.IsHex(privateKey.GetString())) { _privateKey = WalletsHelper.ConvertToHex(privateKey); } } } return(_privateKey); } catch (Exception) { throw privateKeyException; } }
public static KeyPair GetKeysFromNep2(string nep2, string passphrase, ScryptParameters scryptParameters) { byte[] privateKey = Nep2.DecryptKey(nep2, passphrase, scryptParameters); KeyPair key = new KeyPair(privateKey); Array.Clear(privateKey, 0, privateKey.Length); return(key); }
public static void DecryptTest2() { var decryptedWif = Nep2.Decrypt("6PYN6mjwYfjPUuYT3Exajvx25UddFVLpCw4bMsmtLdnKwZ9t1Mi3CfKe8S", "Satoshi") .Result; var wifBytes = Wallet.GetPrivateKeyFromWif("KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7"); Assert.Equal(decryptedWif, wifBytes); }
public static void DecryptTest1() { var decryptedWif = Nep2.Decrypt("6PYVPVe1fQznphjbUxXP9KZJqPMVnVwCx5s5pr5axRJ8uHkMtZg97eT5kL", "TestingOneTwoThree") .Result; var wifBytes = Wallet.GetPrivateKeyFromWif("L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP"); Assert.Equal(decryptedWif, wifBytes); }
public static void EncryptTest1() { string password = "******"; var wif = "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP"; var wifBytes = Wallet.GetPrivateKeyFromWif(wif); var wifHexString = wifBytes.ToHexString().ToUpper(); var encrypted = Nep2.Encrypt(wif, password, ScryptParameters.Default).Result; Assert.Equal("CBF4B9F70470856BB4F40F80B87EDB90865997FFEE6DF315AB166D713AF433A5", wifHexString); Assert.Equal("6PYVPVe1fQznphjbUxXP9KZJqPMVnVwCx5s5pr5axRJ8uHkMtZg97eT5kL", encrypted); }
public static void EncryptTest2() { string password = "******"; var wif = "KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7"; var wifBytes = Wallet.GetPrivateKeyFromWif(wif); var wifHexString = wifBytes.ToHexString().ToUpper(); var encrypted = Nep2.Encrypt(wif, password, ScryptParameters.Default).Result; Assert.Equal("09C2686880095B1A4C249EE3AC4EEA8A014F11E6F986D0B5025AC1F39AFBD9AE", wifHexString); Assert.Equal("6PYN6mjwYfjPUuYT3Exajvx25UddFVLpCw4bMsmtLdnKwZ9t1Mi3CfKe8S", encrypted); }