public ActionResult <PurchaseAccountRequest> StartPurchaseAccount([FromBody] PurchaseAccountRequest data)
 {
     try
     {
         MicroCoin.Transactions.TransferTransaction transaction = PurchaseAccountRequestToTransaction(data);
         data.Hash = (Hash)transaction.GetHash();
         return(Ok(data));
     }
     catch (MicroCoinRPCException e)
     {
         return(this.HandlerError(e));
     }
     catch (Exception e)
     {
         return(StatusCode(500, new MicroCoinError(ErrorCode.UnknownError, e.Message, "")));
     }
 }
        public async Task <IActionResult> CommitPurchaseAccount([FromBody] PurchaseAccountRequest data)
        {
            MicroCoin.Transactions.TransferTransaction transaction = null;
            try
            {
                transaction = PurchaseAccountRequestToTransaction(data);
            }
            catch (MicroCoinRPCException e)
            {
                return(this.HandlerError(e));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new MicroCoinError(ErrorCode.UnknownError, e.Message, "")));
            }
            Hash hash = transaction.GetHash();

            if (data.Signature != null)
            {
                transaction.Signature = new ECSignature
                {
                    R = (Hash)data.Signature.R.PadLeft(64, '0'),
                    S = (Hash)data.Signature.S.PadLeft(64, '0')
                };
                if (Utils.ValidateSignature(hash, transaction.Signature, transaction.AccountKey))
                {
                    using (var ms = new MemoryStream())
                    {
                        using (BinaryWriter bw = new BinaryWriter(ms, System.Text.Encoding.Default, true))
                        {
                            bw.Write(1);
                            bw.Write(6);
                        }
                        transaction.SaveToStream(ms);
                        ms.Position = 0;
                        Hash h    = ms.ToArray();
                        var  resp = await client.ExecuteOperationsAsync(h);

                        if (resp.Count() > 0 && resp.First().Errors == null)
                        {
                            var r  = resp.First();
                            var tr = new PurchaseAccount
                            {
                                FounderAccount = (uint)r.Account,
                                AccountNumber  = (uint)r.DestAccount,
                                Type           = r.Type.ToString(),
                                SubType        = r.SubType.ToString(),
                                Fee            = r.Fee,
                                Confirmations  = r.Maturation,
                                OpHash         = r.Ophash,
                                Balance        = r.Balance
                            };
                            return(Ok(tr));
                        }
                        else
                        {
                            return(BadRequest(new MicroCoinError(ErrorCode.InvalidOperation,
                                                                 resp.Count() > 0 ? resp.First().Errors : "Invalid transaction",
                                                                 "Your transaction is invalid"
                                                                 )));
                        }
                    }
                }
                else
                {
                    return(StatusCode(403, MicroCoinError.Error(ErrorCode.InvalidSignature)));
                }
            }
            return(BadRequest("Missing signature"));
        }
        private MicroCoin.Transactions.TransferTransaction PurchaseAccountRequestToTransaction(PurchaseAccountRequest data)
        {
            var          account = client.GetAccount(data.AccountNumber);
            var          founder = client.GetAccount(data.FounderAccount);
            string       pubkey  = founder.EncPubKey;
            PublicKeyDTO key     = client.DecodePubKey(pubkey, null);

            return(new MicroCoin.Transactions.TransferTransaction
            {
                Amount = (ulong)(account.Price),
                Fee = (ulong)(data.Fee * 10000M),
                SignerAccount = founder.AccountNumber,
                SellerAccount = account.SellerAccount,
                TargetAccount = account.AccountNumber,
                TransactionStyle = MicroCoin.Transactions.TransferTransaction.TransferType.BuyAccount,
                TransactionType = MicroCoin.Transactions.TransactionType.BuyAccount,
                NumberOfOperations = founder.NumOperations + 1,
                AccountKey = new ECKeyPair
                {
                    CurveType = (CurveType)key.KeyType,
                    PublicKey = new ECPoint
                    {
                        X = (Hash)key.X,
                        Y = (Hash)key.Y
                    }
                },
                AccountPrice = (ulong)(account.Price),
                NewAccountKey = new ECKeyPair
                {
                    CurveType = Enum.Parse <CurveType>(data.NewKey.CurveType, true),
                    PublicKey = new ECPoint
                    {
                        X = (Hash)data.NewKey.X.PadLeft(64, '0'),
                        Y = (Hash)data.NewKey.Y.PadLeft(64, '0')
                    }
                }
            });
        }