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 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); }