Exemple #1
0
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                case "--help":
                case "-h":
                    Console.WriteLine($"{Config.CurrencyName} - C# Wallet - v{ Config.Version }");
                    Console.WriteLine("=====   Commands   =====");
                    Console.WriteLine("--walletfile          : specifies the name of the wallet to open");
                    Console.WriteLine("--password            : the password to decrypt the wallet");
                    Console.WriteLine("--debug               : enables debugging output");
                    Console.WriteLine();
                    return;

                case "--walletfile":
                    if (i + 1 < args.Length)
                    {
                        currentFileName = args[++i];
                    }
                    break;

                case "--password":
                    if (i + 1 < args.Length)
                    {
                        currentPassword = args[++i];
                    }
                    break;
                }
            }

            Console.WriteLine("==============================");
            Console.WriteLine($"{Config.CurrencyName} - C# Wallet - v{ Config.Version }");
            Console.WriteLine("==============================");

            if (String.IsNullOrEmpty(currentPassword) || String.IsNullOrEmpty(currentFileName))
            {
                char responseChar = DisplayMenu(new Dictionary <char, string>
                {
                    { 'O', "Open Existing Wallet" },
                    { 'N', "Create new wallet" },
                    { 'R', "Restore Wallet" },
                    { 'X', "Exit" }
                });

                switch (Char.ToUpperInvariant(responseChar))
                {
                case 'O':
                    GetWalletAndPassword(false);
                    currentWallet = WalletContainer.Read(currentFileName, currentPassword);
                    Console.WriteLine($"Opened wallet: {currentFileName}");
                    break;

                case 'N':
                    GetWalletAndPassword(true);
                    currentWallet = new WalletContainer();
                    CreateNewWallet();
                    break;

                case 'R':
                    GetWalletAndPassword(true);
                    currentWallet = new WalletContainer();
                    CreateNewWalletFromSeed();
                    break;

                case 'X':
                    return;
                }
            }
            else
            {
                currentWallet = WalletContainer.Read(currentFileName, currentPassword);
            }

            new WalletSyncService(currentWallet, currentFileName, currentPassword).Start();

            while (true)
            {
                char mainResponseChar = DisplayMenu(new Dictionary <Char, String>
                {
                    { 'P', "Show pending transactions" },
                    { 'L', "List transfers" },
                    { 'B', "Balance" },
                    { 'T', "Transfer" },
                    { 'S', "Show seed" },
                    { 'R', "Rebuild wallet" },
                    { 'C', "Create new wallet" },
                    { 'A', "Add existing wallet" },
                    { 'D', "Delete wallet" },
                    { 'W', "List wallets" },
                    { 'N', "Name a wallet" },
                    { 'X', "Exit" }
                });

                switch (Char.ToUpperInvariant(mainResponseChar))
                {
                case 'P':
                case 'L':
                case 'T':
                    Console.WriteLine("Feature not yet implemented");
                    break;

                case 'B':
                    Console.WriteLine($"Current balance: { currentWallet.GetBalance() }");
                    break;

                case 'S':
                    Console.WriteLine("Addresses and seeds - DO NOT SHARE:");
                    foreach (Wallet wallet in currentWallet.Wallets)
                    {
                        Console.WriteLine("-------------------------------------------------------------------------------------------------");
                        Console.WriteLine($"Address: {wallet.Address}");
                        Console.WriteLine($"Seed: {wallet.Seed}");
                    }
                    Console.WriteLine("-------------------------------------------------------------------------------------------------");
                    break;

                case 'R':
                    foreach (Wallet wallet in currentWallet.Wallets)
                    {
                        wallet.Height = 0;
                        wallet.tokens.Clear();
                        wallet.Transactions.Clear();
                    }

                    currentWallet.Write(currentFileName, currentPassword);
                    break;

                case 'C':
                    CreateNewWallet();
                    break;

                case 'A':
                    CreateNewWalletFromSeed();
                    break;

                case 'D':
                    for (var i = 0; i < currentWallet.Wallets.Count; i++)
                    {
                        Console.WriteLine($"{i}: {currentWallet.Wallets[i].Address}");
                    }

                    Console.Write("Please choose a wallet number to delete: ");
                    string readString = Console.ReadLine();
                    if (!String.IsNullOrWhiteSpace(readString) && readString.All(Char.IsDigit))
                    {
                        int readInt = Convert.ToInt32(readString);
                        if (readInt < currentWallet.Wallets.Count)
                        {
                            Console.WriteLine($"Wallet number {readInt} was removed!");
                            currentWallet.Wallets.RemoveAt(readInt);
                        }
                    }
                    else
                    {
                        Console.WriteLine("You did not enter a valid wallet number to delete!");
                    }

                    break;

                case 'W':
                    for (var i = 0; i < currentWallet.Wallets.Count; i++)
                    {
                        Console.WriteLine($"{i}: {currentWallet.Wallets[i].Address}");
                    }
                    break;

                case 'N':
                    for (var i = 0; i < currentWallet.Wallets.Count; i++)
                    {
                        Console.WriteLine($"{i}: {currentWallet.Wallets[i].Address}");
                    }

                    Console.Write("Please choose a wallet number to rename: ");
                    string readNameString = Console.ReadLine();
                    if (!String.IsNullOrWhiteSpace(readNameString) && readNameString.All(Char.IsDigit))
                    {
                        int readInt = Convert.ToInt32(readNameString);
                        if (readInt < currentWallet.Wallets.Count)
                        {
                            Console.Write("Please enter the new name for the wallet: ");
                            readNameString = Console.ReadLine();

                            if (!String.IsNullOrWhiteSpace(readNameString))
                            {
                                currentWallet.Wallets[readInt].Name = readNameString;
                                currentWallet.Write(currentFileName, currentPassword);
                            }
                            else
                            {
                                Console.WriteLine("You did not enter a valid name!");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("You did not enter a valid wallet number to rename!");
                    }
                    break;

                case 'X':
                    return;
                }
            }
        }
        public void Start()
        {
            backgroundTask = Task.Factory.StartNew(async() =>
            {
                while (true)
                {
                    foreach (Wallet wallet in walletContainer.Wallets)
                    {
                        uint walletHeight = 0;
                        String address;

                        lock (walletContainer)
                        {
                            walletHeight = wallet.Height;
                            address      = wallet.Address;
                        }

                        try
                        {
                            string outputJson = await Comms.RequestJson("wallet/sync",
                                                                        new Dictionary <String, String>
                            {
                                { "height", walletHeight.ToString() },
                                { "address", address }
                            });

                            if (!String.IsNullOrWhiteSpace(outputJson))
                            {
                                Dictionary <String, List <RPCLedger> > grouped = new Dictionary <String, List <RPCLedger> >();

                                RPCWalletSyncObject syncObject = JsonConvert.DeserializeObject <RPCWalletSyncObject>(outputJson);
                                if (syncObject.transactions.Any())
                                {
                                    uint totalAdded = 0;
                                    uint totalSpent = 0;

                                    lock (walletContainer)
                                    {
                                        foreach (RPCLedger ledger in syncObject.transactions)
                                        {
                                            if (ledger.destination == address)
                                            {
                                                WalletToken token = new WalletToken();
                                                token.Token       = ledger.token;
                                                token.Value       = Convert.ToUInt32(ledger.token.Split('-')[2], 16);

                                                wallet.tokens[ledger.token] = token;

                                                totalAdded += token.Value.Value;
                                            }
                                            else
                                            {
                                                // is this a transfer out of a token which was owned by this wallet?
                                                if (wallet.tokens.ContainsKey(ledger.token))
                                                {
                                                    WalletToken token = wallet.tokens[ledger.token];
                                                    wallet.tokens.Remove(ledger.token);

                                                    if (!grouped.ContainsKey(ledger.transaction_group))
                                                    {
                                                        grouped[ledger.transaction_group] = new List <RPCLedger>();
                                                    }

                                                    grouped[ledger.transaction_group].Add(ledger);

                                                    totalSpent += token.Value.Value;
                                                }
                                            }
                                        }

                                        foreach (var transaction in grouped)
                                        {
                                            WalletTransaction newTransaction = new WalletTransaction();
                                            newTransaction.Ref         = transaction.Key;
                                            newTransaction.Date        = transaction.Value[0].date;
                                            newTransaction.Destination = transaction.Value[0].destination;

                                            foreach (RPCLedger ledger in transaction.Value)
                                            {
                                                newTransaction.Value += Convert.ToUInt32(ledger.token.Split('-')[2], 16);
                                            }

                                            wallet.Transactions.Add(newTransaction);
                                        }

                                        wallet.Height = (uint)syncObject.rowid;
                                    }

                                    walletContainer.Write(filePath, password);

                                    Console.WriteLine($"{(float)totalAdded / Config.DenominationDivider} {Config.CurrencyName} added to wallet");
                                    Console.WriteLine($"Value of spent tokens: {(float)totalSpent / Config.DenominationDivider}");
                                    Console.WriteLine("====================");
                                    Console.WriteLine($"Current balance: {wallet.GetBalance()}");
                                }
                                else
                                {
                                    Debug.WriteLine("Height: " + walletHeight);
                                    await Task.Delay(10000);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("An error occurred");
                        }
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }