public static string GetKingAddressString()
        {
            if (KingAddress == null)
            {
                return(string.Empty);
            }

            return(HexByteConvertorExtensions.ToHex(KingAddress.ToArray(), prefix: true));
        }
Exemple #2
0
        async void OnSaveWalletClicked(object sender, EventArgs e)
        {
            txtpwd.Text = txtpwd.Text.Trim();
            if (txtpwd.Text.Trim() == "" || txtpwd.Text.Length < 8)
            {
                await DisplayAlert("Password Error!", "Password length cannot be less than 8", "OK");

                return;
            }
            (sender as Button).IsEnabled = false;
            try
            {
                bool answer = await DisplayAlert("Password Confirm!", "Password:"******"Yes", "No");

                if (answer)
                {
                    try
                    {
                        WalletInfo wi    = new WalletInfo();
                        var        ecKey = EthECKey.GenerateKey();
                        wi.PublicKey  = HexByteConvertorExtensions.ToHex(ecKey.GetPubKey());
                        wi.PrivateKey = ecKey.GetPrivateKey();

                        wi.Address = ecKey.GetPublicAddress().ToLower();
                        var scryptService = new KeyStoreScryptService();
                        wi.FJson = scryptService.EncryptAndGenerateKeyStoreAsJson(txtpwd.Text, ecKey.GetPrivateKeyAsBytes()
                                                                                  , wi.Address.Replace("0x", ""), scryptParams);
                        await App.Wdb.SaveWalletAsync(wi);

                        txtpwd.Text = "";
                        WalletBind();
                        await DisplayAlert("Tips", "Success", "OK");
                    }
                    catch (Exception ex)
                    {
                        await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
                    }
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
            }
            (sender as Button).IsEnabled = true;
        }
        private MiningParameters(string ethAddress,
                                 Function getMiningDifficulty,
                                 Function getMiningTarget,
                                 Function getChallengeNumber)
        {
            EthAddress = ethAddress;
            bool success = false;

            while (!success)
            {
                var exceptions = new List <System.AggregateException>();
                try
                {
                    Task.WaitAll(new Task[]
                    {
                        Task.Factory.StartNew(() =>
                        {
                            try { MiningDifficulty = new HexBigInteger(getMiningDifficulty.CallAsync <BigInteger>().Result); }
                            catch (System.AggregateException ex)
                            {
                                exceptions.Add(ex);
                                throw ex;
                            }
                        }),
                        Task.Factory.StartNew(() =>
                        {
                            try { MiningTarget = new HexBigInteger(getMiningTarget.CallAsync <BigInteger>().Result); }
                            catch (System.AggregateException ex)
                            {
                                exceptions.Add(ex);
                                throw ex;
                            }
                        }).
                        ContinueWith(task => MiningTargetByte32       = Utils.Numerics.FilterByte32Array(MiningTarget.Value.ToByteArray(littleEndian: false))).
                        ContinueWith(task => MiningTargetByte32String = Utils.Numerics.BigIntegerToByte32HexString(MiningTarget.Value)),
                        Task.Factory.StartNew(() =>
                        {
                            try { ChallengeNumberByte32 = Utils.Numerics.FilterByte32Array(getChallengeNumber.CallAsync <byte[]>().Result); }
                            catch (System.AggregateException ex)
                            {
                                exceptions.Add(ex);
                                throw ex;
                            }
                        }).
                        ContinueWith(Task => ChallengeNumber             = new HexBigInteger(HexByteConvertorExtensions.ToHex(ChallengeNumberByte32, prefix: true))).
                        ContinueWith(task => ChallengeNumberByte32String = Utils.Numerics.BigIntegerToByte32HexString(ChallengeNumber.Value))
                    });
                    success = true;
                }
                catch (System.Exception) { exceptions.ForEach(ex => Program.Print(ex.InnerExceptions[0].Message)); }
            }
        }
        private MiningParameters(string ethAddress,
                                 Function getMiningDifficulty,
                                 Function getMiningTarget,
                                 Function getChallengeNumber)
        {
            EthAddress = ethAddress;

            var retryCount = 0;
            var exceptions = new List <Exception>();

            while (retryCount < 10)
            {
                try
                {
                    MiningDifficulty = new HexBigInteger(getMiningDifficulty.CallAsync <BigInteger>().Result);
                    break;
                }
                catch (AggregateException ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex.InnerExceptions[0]);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
            }

            while (retryCount < 10)
            {
                try
                {
                    MiningTarget             = new HexBigInteger(getMiningTarget.CallAsync <BigInteger>().Result);
                    MiningTargetByte32       = Utils.Numerics.FilterByte32Array(MiningTarget.Value.ToByteArray(littleEndian: false));
                    MiningTargetByte32String = Utils.Numerics.BigIntegerToByte32HexString(MiningTarget.Value);
                    break;
                }
                catch (AggregateException ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex.InnerExceptions[0]);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
            }

            while (retryCount < 10)
            {
                try
                {
                    ChallengeNumberByte32       = Utils.Numerics.FilterByte32Array(getChallengeNumber.CallAsync <byte[]>().Result);
                    ChallengeNumber             = new HexBigInteger(HexByteConvertorExtensions.ToHex(ChallengeNumberByte32, prefix: true));
                    ChallengeNumberByte32String = Utils.Numerics.BigIntegerToByte32HexString(ChallengeNumber.Value);
                    break;
                }
                catch (AggregateException ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex.InnerExceptions[0]);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
                catch (Exception ex)
                {
                    retryCount++;
                    if (retryCount == 10)
                    {
                        exceptions.Add(ex);
                    }
                    else
                    {
                        Task.Delay(200).Wait();
                    }
                }
            }

            var exMessage = string.Join(Environment.NewLine, exceptions.Select(ex => ex.Message));

            if (exceptions.Any())
            {
                throw new Exception(exMessage);
            }
        }