Example #1
0
        public static bool importKeystore(string keystore, string password, bool makeActive = true)
        {
            var    service    = new Nethereum.KeyStore.KeyStoreService();
            var    sK         = service.DecryptKeyStoreFromJson(password, keystore);
            string privateKey = "0x" + System.BitConverter.ToString(sK).Replace("-", "");
            string address    = Nethereum.Signer.EthECKey.GetPublicAddress(privateKey);

            aggregateWallet.Add(address, privateKey);
            if (makeActive)
            {
                _currentAddress = address;
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Decrypt and setup wallet from Keystore
        /// </summary>
        /// <param name="PK">Password</param>
        /// <returns>The Wallet</returns>
        public WalletData importFromKeyStore(string keyStore, string password)
        {
            var service = new Nethereum.KeyStore.KeyStoreService();
            var key     = new Nethereum.Signer.EthECKey(
                service.DecryptKeyStoreFromJson(password, keyStore),
                true);

            var address = EthECKey.GetPublicAddress(key.GetPrivateKey());

            _wallet = new WalletData {
                address = address, PK = key.GetPrivateKey()
            };
            _web3 = new Web3(new Nethereum.Web3.Accounts.Account(_wallet.PK), NodeUrl);
            return(_wallet);
        }
Example #3
0
        /// <summary>
        /// Decrypt and setup wallet from Keystore
        /// </summary>
        /// <param name="keyStore">Encrypted Keystore</param>
        /// <param name="password">Password used on encryption</param>
        /// <returns>The Wallet</returns>
        public WalletData importFromKeyStore(string keyStore, string password)
        {
            var service = new Nethereum.KeyStore.KeyStoreService();
            var key     = new Nethereum.Signer.EthECKey(
                service.DecryptKeyStoreFromJson(password, keyStore),
                true);

            var address = EthECKey.GetPublicAddress(key.GetPrivateKey());

            _wallet = new WalletData {
                address = address, PK = key.GetPrivateKey()
            };
            _web3 = new Web3(new Nethereum.Web3.Accounts.Account(_wallet.PK, _chainId), _nodeUrl);
            _paymentChannelsContract = _web3.Eth.GetContract(PaymentChannelsABI, _paymentChannelsContractAddress);
            _validatorsContract      = _web3.Eth.GetContract(ValidatorsABI, _validatorsContractAddress);
            return(_wallet);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userInputProvider"></param>
        /// <param name="id"></param>
        /// <param name="profilesDir"></param>
        /// <param name="passwordNeeded"></param>
        /// <returns></returns>
        public static async Task DeleteProfile(IUserInputProvider userInputProvider, HoardID id, string profilesDir, bool passwordNeeded)
        {
            if (!Directory.Exists(profilesDir))
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString()));
            }

            string[] files           = Directory.GetFiles(profilesDir, "*.keystore");
            var      keyStoreService = new Nethereum.KeyStore.KeyStoreService();

            foreach (string file in files)
            {
                StreamReader jsonReader = new StreamReader(file);
                JObject      jobj       = JObject.Parse(jsonReader.ReadToEnd());
                jsonReader.Close();
                JToken valueAddress;
                if (jobj.TryGetValue("address", out valueAddress))
                {
                    HoardID actualId = new HoardID(valueAddress.Value <string>());
                    if (id == actualId)
                    {
                        Nethereum.Signer.EthECKey key = null;
                        if (passwordNeeded)
                        {
                            string password = await userInputProvider.RequestInput(null, id, eUserInputType.kPassword, valueAddress.Value <string>());

                            try
                            {
                                key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(password, jobj.ToString()), true);
                            }
                            catch (Exception e)
                            {
                                throw new HoardException("Incorrect password", e);
                            }
                        }
                        File.Delete(file);
                        return;
                    }
                }
            }

            throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString()));
        }
Example #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <param name="profilesDir"></param>
        /// <returns></returns>
        public static string ChangePassword(HoardID id, string oldPassword, string newPassword, string profilesDir)
        {
            if (!Directory.Exists(profilesDir))
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString()));
            }

            string[] files           = Directory.GetFiles(profilesDir, "*.keystore");
            var      keyStoreService = new Nethereum.KeyStore.KeyStoreService();

            foreach (string file in files)
            {
                StreamReader jsonReader = new StreamReader(file);
                JObject      jobj       = JObject.Parse(jsonReader.ReadToEnd());
                jsonReader.Close();
                JToken valueAddress;
                JToken name;
                if (jobj.TryGetValue("address", out valueAddress) && jobj.TryGetValue("name", out name))
                {
                    HoardID actualId = new HoardID(valueAddress.Value <string>());
                    if (id == actualId)
                    {
                        string newFile = null;
                        try
                        {
                            var key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(oldPassword, jobj.ToString()), true);
                            newFile = CreateAccountKeyStoreFile(key, newPassword, name.Value <string>(), profilesDir);
                        }
                        catch (Exception e)
                        {
                            throw new HoardException("Incorrect password", e);
                        }
                        if (newFile != null)
                        {
                            File.Delete(file);
                        }
                        return(newFile);
                    }
                }
            }
            throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString()));
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userInputProvider"></param>
        /// <param name="addressOrName"></param>
        /// <param name="profilesDir"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static async Task <ProfileDesc> RequestProfile(IUserInputProvider userInputProvider, string addressOrName, string profilesDir, string password = null)
        {
            if (!Directory.Exists(profilesDir))
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0}", addressOrName));
            }

            var profileFiles = Directory.GetFiles(profilesDir, "*.keystore");

            if (profileFiles.Length == 0)
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0}", addressOrName));
            }

            string providedAddress = addressOrName;

            if (!providedAddress.StartsWith("0x"))
            {
                providedAddress = "0x" + providedAddress;
            }
            bool isValidAddress = Nethereum.Util.AddressUtil.Current.IsValidEthereumAddressHexFormat(providedAddress);

            if (isValidAddress == false)
            {
                throw new HoardException(string.Format("{0} is not a valid ethereum address", providedAddress));
            }

            foreach (var fullPath in profileFiles)
            {
                string fileName = Path.GetFileName(fullPath);
                if ((fileName != null) && (fileName != System.String.Empty))
                {
                    string json    = File.ReadAllText(fullPath);
                    var    details = JObject.Parse(json);
                    if (details == null)
                    {
                        continue;
                    }
                    string address     = details["address"].Value <string>();
                    string profileName = "";
                    if (details["name"] != null)
                    {
                        profileName = details["name"].Value <string>();
                    }
                    if (((isValidAddress == true) && (address == providedAddress)) || ((isValidAddress == false) && (profileName == addressOrName)))
                    {
                        ErrorCallbackProvider.ReportInfo(string.Format("Loading account {0}", fileName));
                        string pswd = null;
                        if (password == null)
                        {
                            pswd = await userInputProvider.RequestInput(profileName, new HoardID(address), eUserInputType.kPassword, address);
                        }
                        else
                        {
                            pswd = password;
                        }
                        var keyStoreService           = new Nethereum.KeyStore.KeyStoreService();
                        Nethereum.Signer.EthECKey key = null;
                        try
                        {
                            key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(pswd, json), true);
                            return(new ProfileDesc(profileName, key.GetPublicAddress(), key.GetPrivateKeyAsBytes()));
                        }
                        catch (Exception e)
                        {
                            throw new HoardException("Incorrect password", e);
                        }
                    }
                }
            }

            throw new HoardException(string.Format("Failed to request profile: {0}", providedAddress));
        }