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 void IsNep2_Should_DetectEncryptedKeys() { SecureString correctNep2Wallet = "6PYWLPXtLHZeqR1vpWtcfSt5Ubwba3Qv1zdeacd7347mBfXABa9WXbe3Yh".ToSecureString(); SecureString incorrectNep2Wallet = "KxmqXqdavQhziXNGborgiqy3JWjCmDHgJpKniMdFdnLHCBmCXnoU".ToSecureString(); Assert.IsTrue(WalletsHelper.IsNep2(correctNep2Wallet)); Assert.IsFalse(WalletsHelper.IsNep2(incorrectNep2Wallet)); }