Ejemplo n.º 1
0
        void AddFeeInput(WalletAddr address, ChainTx ctx, uint nIn, string sender, BigInteger fee, bool updateInputsAndOutputs)
        {
            // add fee input
            var i = db.TxInputGet(ctx.TxId, nIn);

            if (i == null)
            {
                i         = new TxInput(ctx.TxId, sender, nIn, fee);
                i.ChainTx = ctx;
                if (sender == address.Address)
                {
                    i.WalletAddr = address;
                }
                db.TxInputs.Add(i);
            }
            else if (i.WalletAddr == null && i.Addr == address.Address)
            {
                logger.LogInformation($"Updating input (fee) with no address: {ctx.TxId}, {i.N}, {address.Address}");
                i.WalletAddr = address;
                db.TxInputs.Update(i);
            }
            else if (updateInputsAndOutputs && i.Amount != fee)
            {
                logger.LogInformation($"Updating input (fee) amount: {ctx.TxId}, {i.N}, {address.Address} (from {i.Amount} to {fee}");
                i.Amount = fee;
                db.TxInputs.Update(i);
            }
        }
Ejemplo n.º 2
0
        void AddFeeInput(ChainTx ctx, uint nIn, string sender, BigInteger fee)
        {
            // add fee input
            var i = db.TxInputGet(ctx.TxId, nIn);

            if (i == null)
            {
                i         = new TxInput(ctx.TxId, sender, nIn, fee);
                i.ChainTx = ctx;
                db.TxInputs.Add(i);
            }
        }
Ejemplo n.º 3
0
        WalletTx AddOutgoingTx(string from, TransferTransaction signedTx, WalletTag tagFor)
        {
            var date    = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var address = db.AddrGet(from);
            var amount  = asset.AmountToLong(signedTx.Amount);
            var fee     = asset.AmountToLong(signedTx.Fee);

            logger.LogDebug("outgoing tx: amount: {0}, fee: {1}", amount, fee);
            // create chain tx
            var ctx = new ChainTx(signedTx.GenerateId(), date, fee, -1, 0);

            ctx.NetworkStatus = new ChainTxNetworkStatus(ctx, ChainTxStatus.Unconfirmed, date, signedTx.GetBytes());
            db.ChainTxs.Add(ctx);
            db.ChainTxNetworkStatus.Add(ctx.NetworkStatus);
            // create tx inputs
            var i = new TxInput(signedTx.GenerateId(), from /*signedTx.Sender is null*/, 0, amount);

            i.ChainTx    = ctx;
            i.WalletAddr = address;
            db.TxInputs.Add(i);
            i            = new TxInput(signedTx.GenerateId(), from /*signedTx.Sender is null*/, 1, fee);
            i.ChainTx    = ctx;
            i.WalletAddr = address;
            db.TxInputs.Add(i);
            // create tx output
            var o = new TxOutput(signedTx.GenerateId(), signedTx.Recipient, 0, amount);

            o.ChainTx = ctx;
            db.TxOutputs.Add(o);
            if (tagFor != null)
            {
                db.TxOutputsForTag.Add(new TxOutputForTag {
                    TxOutput = o, Tag = tagFor
                });
            }
            // create wallet tx
            var wtx = new WalletTx {
                ChainTx = ctx, Address = address, Direction = WalletDirection.Outgoing
            };

            db.WalletTxs.Add(wtx);
            return(wtx);
        }
Ejemplo n.º 4
0
        void AddOutputInputAndWalletTx(WalletAddr address, ChainTx ctx, uint nOut, string sender, string recipient, BigInteger amount)
        {
            // add output
            var o = db.TxOutputGet(ctx.TxId, nOut);

            if (o == null)
            {
                o         = new TxOutput(ctx.TxId, recipient, nOut, amount);
                o.ChainTx = ctx;
                if (recipient == address.Address)
                {
                    o.WalletAddr = address;
                }
                db.TxOutputs.Add(o);
            }
            else if (o.WalletAddr == null && o.Addr == address.Address)
            {
                logger.LogInformation($"Updating output with no address: {ctx.TxId}, {o.N}, {address.Address}");
                o.WalletAddr = address;
                db.TxOutputs.Update(o);
            }
            // add input (so we can see who its from)
            var i = db.TxInputGet(ctx.TxId, nOut);

            if (i == null)
            {
                i         = new TxInput(ctx.TxId, sender, nOut, amount);
                i.ChainTx = ctx;
                db.TxInputs.Add(i);
            }
            // add / update wallet tx
            var dir = recipient == address.Address ? WalletDirection.Incomming : WalletDirection.Outgoing;
            var wtx = db.TxGet(address, ctx, dir);

            if (wtx == null)
            {
                wtx = new WalletTx {
                    ChainTx = ctx, Address = address, Direction = dir, State = WalletTxState.None
                };
                db.WalletTxs.Add(wtx);
            }
        }
Ejemplo n.º 5
0
        WalletTx AddOutgoingTx(string from, string signedTx, WalletTag tagFor)
        {
            var address = db.AddrGet(from);
            var tx      = new Transaction(signedTx.HexToByteArray());
            var date    = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var fee     = HexBigIntegerConvertorExtensions.HexToBigInteger(tx.GasLimit.ToHex(), false) * HexBigIntegerConvertorExtensions.HexToBigInteger(tx.GasPrice.ToHex(), false);
            var amount  = HexBigIntegerConvertorExtensions.HexToBigInteger(tx.Value.ToHex(), false);

            logger.LogDebug("outgoing tx: amount: {0}, fee: {1}", amount, fee);
            // create chain tx
            var ctx = new ChainTx(tx.Hash.ToHex(true), date, fee, -1, 0);

            db.ChainTxs.Add(ctx);
            var networkStatus = new ChainTxNetworkStatus(ctx, ChainTxStatus.Unconfirmed, date, signedTx.HexToByteArray());

            db.ChainTxNetworkStatus.Add(networkStatus);
            // create tx input
            var i = new TxInput(tx.Hash.ToHex(true), from, 0, amount + fee);

            i.ChainTx    = ctx;
            i.WalletAddr = address;
            db.TxInputs.Add(i);
            // create tx output
            var o = new TxOutput(tx.Hash.ToHex(true), tx.ReceiveAddress.ToHex(true), 0, amount);

            o.ChainTx = ctx;
            db.TxOutputs.Add(o);
            if (tagFor != null)
            {
                db.TxOutputsForTag.Add(new TxOutputForTag {
                    TxOutput = o, Tag = tagFor
                });
            }
            // create wallet tx
            var wtx = new WalletTx {
                ChainTx = ctx, Address = address, Direction = WalletDirection.Outgoing
            };

            db.WalletTxs.Add(wtx);
            return(wtx);
        }
Ejemplo n.º 6
0
        IEnumerable <WalletTx> AddOutgoingTx(Transaction tx, List <CoinSpend> spents, List <CoinOutput> outputs, BigInteger fee, WalletTag tagFor)
        {
            var txid = tx.GetHash().ToString();

            logger.LogDebug("outgoing tx: outputs: {0}, fee: {1}", outputs.Select(o => o.Amount), fee);
            var date = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            // create chain tx
            var ctx = db.ChainTxGet(txid);

            if (ctx == null)
            {
                ctx = new ChainTx(txid, date, fee, -1, 0);
                db.ChainTxs.Add(ctx);
            }
            if (ctx.NetworkStatus == null)
            {
                var networkStatus = new ChainTxNetworkStatus(ctx, ChainTxStatus.Unconfirmed, date, tx.ToBytes());
                db.ChainTxNetworkStatus.Add(networkStatus);
            }
            // create tx inputs
            uint n = 0;

            foreach (var spent in spents)
            {
                if (db.TxInputGet(txid, n) == null)
                {
                    var i = new TxInput(txid, spent.From, n, spent.Coin.Amount.Satoshi);
                    i.ChainTx    = ctx;
                    i.WalletAddr = spent.Addr;
                    db.TxInputs.Add(i);
                }
                n++;
            }
            // create tx outputs
            n = 0;
            foreach (var output in outputs)
            {
                if (db.TxOutputGet(txid, n) == null)
                {
                    var o = new TxOutput(txid, output.To, n, output.Amount);
                    o.ChainTx = ctx;
                    db.TxOutputs.Add(o);
                    if (tagFor != null)
                    {
                        db.TxOutputsForTag.Add(new TxOutputForTag {
                            TxOutput = o, Tag = tagFor
                        });
                    }
                }
                n++;
            }
            // create wallet txs
            var wtxs = new List <WalletTx>();

            foreach (var spent in spents)
            {
                if (spent.Addr == null)
                {
                    continue;
                }
                var wtx = db.TxGet(spent.Addr, ctx, WalletDirection.Outgoing);
                if (wtx == null)
                {
                    wtx = new WalletTx {
                        ChainTx = ctx, Address = spent.Addr, Direction = WalletDirection.Outgoing, State = WalletTxState.None
                    };
                    db.WalletTxs.Add(wtx);
                }
                wtxs.Add(wtx);
            }
            return(wtxs);
        }
Ejemplo n.º 7
0
        void UpdateTxs(WalletAddr address)
        {
            //TODO - add attachment
            // - read the 'data' field (https://ethereum.stackexchange.com/questions/2466/how-do-i-send-an-arbitary-message-to-an-ethereum-address)

            var blockNumTask = web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();

            blockNumTask.Wait();
            var blockNum = (long)blockNumTask.Result.Value;
            var json     = scanClient.DownloadString(gethTxScanAddress + "/list_transactions/" + address.Address);
            var scantxs  = JsonConvert.DeserializeObject <List <scantx> >(json);

            foreach (var scantx in scantxs)
            {
                long confirmations = 0;
                if (scantx.block_num > 0)
                {
                    confirmations = blockNum - scantx.block_num;
                }
                // add/update chain tx
                var ctx = db.ChainTxGet(scantx.txid);
                if (ctx == null)
                {
                    ctx = new ChainTx(scantx.txid, scantx.date, -1, scantx.block_num, confirmations);
                    db.ChainTxs.Add(ctx);
                }
                else
                {
                    ctx.Height        = scantx.block_num;
                    ctx.Confirmations = confirmations;
                    db.ChainTxs.Update(ctx);
                }
                var status = confirmations > 0 ? ChainTxStatus.Confirmed : ChainTxStatus.Unconfirmed;
                if (ctx.NetworkStatus == null)
                {
                    //TODO: we need txdata from scantx, to be able to rebroadcast if necessary
                    //var networkStatus = new ChainTxNetworkStatus(ctx, status, 0, scantx.txdata/*TODO*/);
                    //db.ChainTxNetworkStatus.Add(networkStatus);
                }
                else
                {
                    // transaction update comes from our trusted node so we will override any status we have set manually
                    ctx.NetworkStatus.Status = status;
                    db.ChainTxNetworkStatus.Update(ctx.NetworkStatus);
                }
                // add output
                var o = db.TxOutputGet(scantx.txid, 0);
                if (o == null)
                {
                    o         = new TxOutput(scantx.txid, scantx.to, 0, BigInteger.Parse(scantx.value));
                    o.ChainTx = ctx;
                    if (scantx.to == address.Address)
                    {
                        o.WalletAddr = address;
                    }
                    db.TxOutputs.Add(o);
                }
                else if (o.WalletAddr == null && o.Addr == address.Address)
                {
                    logger.LogInformation($"Updating output with no address: {scantx.txid}, {o.N}, {address.Address}");
                    o.WalletAddr = address;
                    db.TxOutputs.Update(o);
                }
                // add input (so we can see who its from)
                var i = db.TxInputGet(scantx.txid, 0);
                if (i == null)
                {
                    i         = new TxInput(scantx.txid, scantx.from_, 0, BigInteger.Parse(scantx.value) /*+ fee*/); //TODO: add fee
                    i.ChainTx = ctx;
                    db.TxInputs.Add(i);
                }
                // add / update wallet tx
                var dir = scantx.to == address.Address ? WalletDirection.Incomming : WalletDirection.Outgoing;
                var wtx = db.TxGet(address, ctx, dir);
                if (wtx == null)
                {
                    wtx = new WalletTx {
                        ChainTx = ctx, Address = address, Direction = dir, State = WalletTxState.None
                    };
                    db.WalletTxs.Add(wtx);
                }
            }
        }