/// <summary>
        /// Creates new profile with given name
        /// </summary>
        /// <param name="name">name of profile </param>
        /// <returns>new account</returns>
        public async Task <Profile> CreateProfile(string name)
        {
            ErrorCallbackProvider.ReportInfo("Generating user account.");
            string password = await UserInputProvider.RequestInput(name, null, eUserInputType.kPassword, "new password");

            Tuple <string, byte[]> accountTuple = KeyStoreUtils.CreateProfile(name, password, ProfilesDir);
            Profile profile = new KeyStoreProfile(name, new HoardID(accountTuple.Item1), accountTuple.Item2);

            accountTuple.Item2.Initialize();
            return(profile);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads account information for the user
        /// </summary>
        /// <param name="userInputProvider">Provider with user credentials</param>
        /// <param name="filename">filename of the file with account to load</param>
        /// <param name="profilesDir">folder where the key store files are stored</param>
        /// <returns>description making an account</returns>
        public static async Task <ProfileDesc> LoadProfile(IUserInputProvider userInputProvider, string filename, string profilesDir)
        {
            if (!Directory.Exists(profilesDir))
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0:1}", profilesDir, filename));
            }

            var profileFiles = Directory.GetFiles(profilesDir, filename);

            if (profileFiles.Length == 0)
            {
                throw new HoardException(string.Format("Profile doesn't exists: {0:1}", profilesDir, filename));
            }
            ErrorCallbackProvider.ReportInfo(string.Format("Loading profiles {0}", profileFiles[0]));

            string json = null;

            using (var reader = File.OpenText(profileFiles[0]))
            {
                json = await reader.ReadToEndAsync();
            }
            var details = JObject.Parse(json);

            if (details == null)
            {
                throw new HoardException(string.Format("Can't parse json: {0}", json));
            }

            string address = details["address"].Value <string>();
            string name    = "";

            if (details["name"] != null)
            {
                name = details["name"].Value <string>();
            }
            string password = await userInputProvider.RequestInput(name, new HoardID(address), eUserInputType.kPassword, address);

            var keyStoreService = new Nethereum.KeyStore.KeyStoreService();

            Nethereum.Signer.EthECKey key = null;

            try
            {
                key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(password, json), true);
                return(new ProfileDesc(name, key.GetPublicAddress(), key.GetPrivateKeyAsBytes()));
            }
            catch (Exception e)
            {
                throw new HoardException("Incorrect password", e);
            }
        }
        public async Task EncryptAndSaveCredentials(HoardID userID, IUserInputProvider credentials)
        {
            var pathToFile = PathToID(userID);
            var pass       = await credentials.RequestInput("Password", userID, eUserInputType.kPassword, "");

            var encrypted = EncryptPassword(userID, pass);

            using (var file = new StreamWriter(pathToFile, true))
            {
                var converted = await Task.Run(() => JsonConvert.SerializeObject(encrypted));

                await file.WriteAsync(converted);
            }
        }
Exemplo n.º 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()));
        }
Exemplo n.º 5
0
        internal async Task <object> SendRequestAsyncNoLock(object request)
        {
            await WriteRequestAsync(request);

            var response = await ReadResponseAsync();

            while (response is PinMatrixRequest || response is ButtonRequest)
            {
                if (response is PinMatrixRequest)
                {
                    var pin = await pinInputProvider.RequestInput(null, null, eUserInputType.kPIN, "Trezor PIN request");

                    response = await PinMatrixAckAsync(pin);
                }
                else if (response is ButtonRequest)
                {
                    response = await ButtonAckAsync();
                }
            }

            return(response);
        }
Exemplo n.º 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));
        }