public async Task <IActionResult> Mine()
        {
            var proof = await _blockchain.ProofOfWorkAsync(_blockchain.LastBlock.Proof);

            await _blockchain.AddTransactionAsync(new Transaction {
                Sender    = "0",
                Recipient = _settings.NodeIdentifier,
                Amount    = 1
            });

            var block = await _blockchain.AddBlockAsync(proof, Blockchain.HashBlock(_blockchain.LastBlock));

            return(Ok(block));
        }
        public async Task <IActionResult> AddTransaction([FromBody] Transaction transaction)
        {
            var transactionFound = await _blockchain.GetTransactionByIdAsync(transaction.Id);

            if (transactionFound != null)
            {
                return(StatusCode((int)HttpStatusCode.Conflict, new
                {
                    Message = $"Transaction '{transaction.Id}' already exists"
                }));
            }

            try
            {
                Transaction newTransaction = await _blockchain.AddTransactionAsync(transaction);

                return(Ok(newTransaction));
            }
            catch (Exception ex)
            {
                if (ex is TransactionAssertionException)
                {
                    return(BadRequest(new
                    {
                        ex.Message
                    }));
                }
                throw;
            }
        }
示例#3
0
        public async Task <IActionResult> CreateTransaction([FromRoute] string id, [FromBody] CreateTransactionViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (await _operator.CheckWalletPasswordAsync(id, model.Password) == null)
            {
                return(NotFound());
            }

            try
            {
                Transaction newTransaction = await _operator.CreateTransactionAsync(id,
                                                                                    model.FromAddress.FromHex(),
                                                                                    model.ToAddress.FromHex(),
                                                                                    model.Amount,
                                                                                    model.ChangeAddress.FromHex()
                                                                                    );

                newTransaction.Check(_hashProvider, _coinSettings);

                Transaction transaction = await _blockchain.AddTransactionAsync(newTransaction);

                return(Created($"/operator/wallets/{id}/transactions", transaction));
            }
            catch (Exception ex)
            {
                if (ex is ArgumentException || ex is TransactionAssertionException)
                {
                    return(BadRequest(new
                    {
                        ex.Message
                    }));
                }

                throw;
            }
        }