Exemplo n.º 1
0
        async Task LockAsync(byte[] passphrase)
        {
            await lockLock.WaitAsync();

            try {
                if (IsLocked)
                {
                    throw new OperationException("Wallet is already locked");
                }

                await EncryptAsync(passphrase);

                Array.Clear(EncryptionKey, 0, EncryptionKey.Length);
                EncryptionKey = new byte[] { };

                await privateKeyLock.WaitAsync();

                try {
                    PrivateKeys.Clear();                     // TODO: Is this safe?
                }
                finally {
                    privateKeyLock.Release();
                }

                LockTimer.Stop();

                IsLocked = true;
                DidLock(this, new EventArgs());
            }
            finally {
                lockLock.Release();
            }
        }
        public override async Task <decimal> GetExchangeRateAsync(Money.CurrencyType c1, Money.CurrencyType c2)
        {
            await exchangeRateLock.WaitAsync();

            try {
                if (ExchangeRateCache.ContainsKey(c2))
                {
                    return(1m / (await GetExchangeRateAsync(c2, c1)));
                }

                if (!ExchangeRateCache.ContainsKey(c1))
                {
                    ExchangeRateCache.Add(c1, new Dictionary <Money.CurrencyType, ExchangeRateInfo>());
                }

                if (ExchangeRateCache[c1].ContainsKey(c2))
                {
                    if (ExchangeRateCache[c1][c2].Age <= MaxExchangeRateAge)
                    {
                        return(ExchangeRateCache[c1][c2].Rate);
                    }

                    ExchangeRateCache[c1].Remove(c2);
                }

                decimal rate = await base.GetExchangeRateAsync(c1, c2);

                ExchangeRateCache[c1].Add(c2, new ExchangeRateInfo(rate));
                return(rate);
            }
            finally {
                exchangeRateLock.Release();
            }
        }
Exemplo n.º 3
0
        async Task <string> SendRPCAsync(string methodName, params object[] args)
        {
            var jsonArgs = new List <string>();

            foreach (var arg in args)
            {
                if (arg is string)
                {
                    jsonArgs.Add(String.Format("\"{0}\"", arg));
                }
                else
                {
                    jsonArgs.Add(arg.ToString());
                }
            }

            var rpc = String.Format("{{\"id\": 1, \"method\": \"{0}\", \"params\": [{1}]}}", methodName, String.Join(", ", jsonArgs));

            await ClientLock.WaitAsync();

            try {
                var writer = new StreamWriter(ClientStream);
                await writer.WriteLineAsync(rpc);

                await writer.FlushAsync();

                var reader = new StreamReader(ClientStream);
                return(await reader.ReadLineAsync());
            }
            finally {
                ClientLock.Release();
            }
        }
Exemplo n.º 4
0
        public async Task <Wallet> CreateWallet(FileInfo walletFile)
        {
            Wallet newWallet     = null;
            var    creationSheet = new WalletCreationSheetController(walletFile);
            var    creationLock  = new SemaphoreLite();

            await creationLock.WaitAsync();

            creationSheet.ShowSheet(Window);
            creationSheet.DidCreate += (sender, e) => newWallet = e.Wallet;
            creationSheet.DidClose  += (sender, e) => creationLock.Release();

            await creationLock.WaitAsync();

            creationLock.Release();
            return(newWallet);
        }
Exemplo n.º 5
0
        async Task <string> GetContentAsync(string path)
        {
            await clientLock.WaitAsync();

            try {
                return(await Client.GetAsync(new Uri(Address, path)));
            }
            finally {
                clientLock.Release();
            }
        }
Exemplo n.º 6
0
        public async Task ImportKeyAsync(PrivateKeyDetails key, bool isPublic = true)
        {
            if (IsLocked)
            {
                throw new LockedException();
            }
            if (isPublic)
            {
                await ImportReadOnlyAddressAsync(key);
            }

            await privateKeyLock.WaitAsync();

            try {
                PrivateKeys.Add(key);
            }
            finally {
                privateKeyLock.Release();
            }
            await SaveAsync();
        }
        public override async Task <Money> GetCachedBalanceAsync(Address address, ulong startHeight = 0)
        {
            await balanceCacheLock.WaitAsync();

            try {
                if (!BalanceCache.ContainsKey(address))
                {
                    BalanceCache.Add(address, await base.GetCachedBalanceAsync(address, startHeight));
                }
                return(BalanceCache[address]);
            }
            finally {
                balanceCacheLock.Release();
            }
        }
        public async override Task <Transaction> GetTransactionAsync(Transaction.Info info)
        {
            await txCacheLock.WaitAsync();

            try {
                if (!TransactionCache.ContainsKey(info.Hash))
                {
                    TransactionCache.Add(info.Hash, await base.GetTransactionAsync(info));
                }
                return(TransactionCache[info.Hash]);
            }
            finally {
                txCacheLock.Release();
            }
        }
Exemplo n.º 9
0
        public async Task SaveAsync(FileInfo file)
        {
            await saveLock.WaitAsync();

            try {
                if (IsLocked)
                {
                    throw new LockedException();
                }

                if (file != null)
                {
                    FileInfo backup = file.WithExtension("bak");
                    FileInfo temp   = file.WithExtension("new");

                    temp.Delete();
                    backup.Delete();
                    file.MoveTo(backup);

                    if (!IsLocked)
                    {
                        await EncryptAsync(EncryptionKey);
                    }

                    // TODO: Set proper file permissions
                    using (var stream = FileStream.Create(file, FileMode.CreateNew)) {
                        using (var writer = new StreamWriter(stream)) {
                            await WriteAsync(writer);
                        }
                    }
                }
            }
            finally {
                saveLock.Release();
            }
        }