Example #1
0
        public async static Task <BitcoinTransactionInfo> GetBtcTransactionDetails(ConnectionState connectionState, CancellationToken token, TlsClientProtocol tls)
        {
            byte[] apdu = { 0xE0, 0xE0, 0x00, 0x00 };
            byte[] response;

            try
            {
                response = await SendWrappedAPDU(apdu, 11, connectionState, token, tls);
            }
            catch (Non9000SwException)
            {
                throw new TransactionNotActiveException();
            }

            BitcoinTransactionInfo transactionInfo = new BitcoinTransactionInfo();

            if (response[0] != 0x00)
            {
                transactionInfo.transactionTooBigToDisplay = true;
            }
            else
            {
                transactionInfo.transactionTooBigToDisplay = false;
            }

            transactionInfo.currentOffset  = Helper.MakeUint16(response, 1);
            transactionInfo.numberOfInputs = Helper.MakeUint32(response, 3);
            transactionInfo.remainingTime  = Helper.MakeUint32(response, 7);

            return(transactionInfo);
        }
Example #2
0
        public static string ParseBitcoinTransaction(BitcoinTransactionInfo info, byte[] transaction, Int64[] inputAmounts, ref uint timeout)
        {
            string htmlOutput        = "";
            Int64  totalInputAmount  = 0;
            Int64  totalOutputAmount = 0;

            if (info.transactionTooBigToDisplay == true)
            {
                throw new BtcParserException("Transaction too big to display.");
            }

            Transaction tx = new Transaction(BitConverter.ToString(transaction).Replace("-", ""));

            if (tx.Inputs.Count != inputAmounts.Length)
            {
                throw new BtcParserException("Invalid transaction.");
            }

            for (int i = 0; i < inputAmounts.Length; i++)
            {
                totalInputAmount += inputAmounts[i];
            }

            htmlOutput += "<b>Inputs:</b><br>";

            for (int i = 0; i < tx.Inputs.Count; i++)
            {
                string prevout = tx.Inputs[i].PrevOut.ToString();

                htmlOutput += "Prevout: " + prevout + "<br>" + "Amount: " + new Money(inputAmounts[i]).ToString() + "<br><br>";
            }

            htmlOutput += "<b>Outputs:</b><br>";

            for (int i = 0; i < tx.Outputs.Count; i++)
            {
                var script  = tx.Outputs[i].ScriptPubKey;
                var address = script.GetDestinationAddress(NBitcoin.Network.Main);

                htmlOutput += "Address: " + address + "<br>" + "Amount: " + tx.Outputs[i].Value.ToString() + "<br><br>";

                totalOutputAmount += tx.Outputs[i].Value.Satoshi;
            }

            Int64 fee = totalInputAmount - totalOutputAmount;

            htmlOutput += "<b>Fee:</b><br>";
            htmlOutput += new Money(fee).ToString();

            timeout = info.remainingTime / 1000;

            return(htmlOutput);
        }