Ejemplo n.º 1
0
        public UnsignedTransaction SendTransactionToExchage(dynamic transactionDto, string toAddress, string privateKey, string network, string token)
        {
            try
            {
                var      btcnetwork = GetNetwork(network);
                Endpoint endpoint   = Endpoint.BtcTest3;
                if (network.ToUpper() == "MAIN")
                {
                    endpoint = Endpoint.BtcMain;
                }
                Blockcypher          blockCypherLib = new Blockcypher(token, endpoint);
                BitcoinSecret        secret         = new BitcoinSecret(privateKey, btcnetwork);
                NBitcoin.Transaction tx             = NBitcoin.Transaction.Create(btcnetwork);
                TxIn input = new TxIn
                {
                    PrevOut   = new OutPoint(new uint256(transactionDto.TransactionID), transactionDto.Vout),
                    ScriptSig = secret.GetAddress(ScriptPubKeyType.Legacy).ScriptPubKey
                };
                tx.Inputs.Add(input);

                TxOut output      = new TxOut();
                Money fee         = Money.Coins(transactionDto.DefaultFees.GetValueOrDefault());
                Money totalAmount = Money.Coins(transactionDto.Amount);
                output.Value = totalAmount - fee;
                BitcoinAddress toaddress = BitcoinAddress.Create(toAddress, btcnetwork);
                output.ScriptPubKey = toaddress.ScriptPubKey;
                tx.Outputs.Add(output);

                var coins = tx.Inputs.Select(txin => new Coin(txin.PrevOut, new TxOut {
                    ScriptPubKey = txin.ScriptSig
                }));
                tx.Sign(secret, coins.ToArray());
                var response = blockCypherLib.PushTransaction(tx.ToHex()).Result;
                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }