Exemple #1
0
        public async Task <IActionResult> BroadcastTransaction([FromBody] BroadcastTransactionRequest request)
        {
            if (request == null)
            {
                return(BadRequest(ErrorResponse.Create("Unable to deserialize request")));
            }

            var aggregate = await _operationRepository.GetOrDefault(request.OperationId);

            if (aggregate == null)
            {
                return(BadRequest(ErrorResponse.Create($"Operation {request.OperationId} not found")));
            }

            Transaction tx;

            try
            {
                tx = TransactionSerializer.Deserialize(request.SignedTransaction).transaction;
            }
            catch (InvalidTransactionException)
            {
                return(BadRequest(ErrorResponse.Create($"{nameof(request.SignedTransaction)} is invalid")));
            }

            if (aggregate.IsBroadcasted)
            {
                return(Conflict());
            }

            try
            {
                await _transactionBroadcaster.BroadcastTransaction(tx, aggregate);
            }
            catch (TransactionAlreadyBroadcastedException)
            {
                return(Conflict());
            }

            aggregate.OnBroadcasted(DateTime.UtcNow);
            await _operationRepository.Save(aggregate);

            return(Ok());
        }
        public IActionResult Sign([FromBody] SignTransactionRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ErrorResponseFactory.Create(ModelState)));
            }

            try
            {
                var txWrapper = TransactionSerializer.Deserialize(request.TransactionContext);
                var tx        = txWrapper.transaction;

                var keyPair = new KeyPair(Wallet.GetPrivateKeyFromWif(request.PrivateKeys.Single()));

                var signature = Transaction.Sign(keyPair, tx, false);

                var invocationScript   = NeoModules.Core.Helper.HexToBytes(("40" + signature.ToHexString()));
                var verificationScript = Helper.CreateSignatureRedeemScript(keyPair.PublicKey);

                tx.Witnesses = new[]
                {
                    new Witness
                    {
                        InvocationScript   = invocationScript,
                        VerificationScript = verificationScript
                    }
                };

                return(Ok(new SignedTransactionResponse
                {
                    SignedTransaction = TransactionSerializer.Serialize(tx, txWrapper.type)
                }));
            }

            catch (Exception e)
            {
                return(BadRequest(ErrorResponse.Create(e.ToAsyncString())));
            }
        }