public async Task <IActionResult> AddCommitmentPunishmentPair([FromQuery] string commitment,
                                                                      [FromQuery] string punishment, [FromQuery] bool overwrite = false)
        {
            if (string.IsNullOrEmpty(commitment) || string.IsNullOrEmpty(punishment))
            {
                return(BadRequest("Passed parameters should not be null or empty"));
            }
            else
            {
                try
                {
                    Transaction cTx = new Transaction(commitment);
                    Transaction pTx = new Transaction(punishment);

                    using (OffchainMonitorContext context = new OffchainMonitorContext())
                    {
                        var matchedCommitment = (from c in context.Commitments
                                                 where c.CommitmentTxId == cTx.GetHash().ToString()
                                                 select c).FirstOrDefault();

                        if (matchedCommitment != null)
                        {
                            if (overwrite == false)
                            {
                                return(BadRequest("To overwrite an existing punishment for existing commitment id, the overwrite flag should bet set to true"));
                            }
                            else
                            {
                                matchedCommitment.Commitment = commitment;
                                matchedCommitment.Punishment = punishment;
                            }
                        }
                        else
                        {
                            var newlyAddedCommitment = new CommitmentEntity
                            {
                                Commitment     = commitment,
                                CommitmentTxId = cTx.GetHash().ToString(),
                                Punishment     = punishment
                            };

                            await context.Commitments.AddAsync(newlyAddedCommitment);
                        }

                        await context.SaveChangesAsync();
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }

                return(Ok());
            }
        }
        public async Task CheckCommitmentBroadcast()
        {
            using (OffchainMonitorContext context = new OffchainMonitorContext())
            {
                var commitmentsToSearchFor = (from c in context.Commitments
                                              where c.Punished == false
                                              select c).ToArray();

                if (commitmentsToSearchFor.Count() > 0)
                {
                    for (int i = 0; i < commitmentsToSearchFor.Count(); i++)
                    {
                        var  c     = commitmentsToSearchFor[i];
                        bool found = false;
                        try
                        {
                            await rpcBitcoinClient.GetTransactionHex(c.CommitmentTxId);

                            found = true;
                        }
                        catch (RPCException exp)
                        {
                            if (!exp.Message.Contains("No such mempool or blockchain transaction."))
                            {
                                throw exp;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        catch (Exception exp)
                        {
                            throw exp;
                        }

                        if (found)
                        {
                            try
                            {
                                await rpcBitcoinClient.BroadcastTransaction(new Transaction(c.Punishment), new Guid());

                                c.Punished = true;
                            }
                            catch (Exception exp)
                            {
                                throw exp;
                            }
                        }
                    }
                }

                await context.SaveChangesAsync();
            }
        }
Beispiel #3
0
        public async Task Set <T>(string key, T value)
        {
            using (OffchainMonitorContext context = new OffchainMonitorContext())
            {
                var settingRecords = from record in context.Settings where record.Key == key select record;
                if (settingRecords.Count() > 0)
                {
                    context.Settings.RemoveRange(settingRecords);
                }

                context.Settings.Add(new SettingsEntity {
                    Key = key.ToString(), Value = value.ToString()
                });

                await context.SaveChangesAsync();
            }
        }
Beispiel #4
0
        public async Task <IActionResult> SetSettingsValue([FromQuery] string key, [FromQuery] string value)
        {
            key = key.Trim();
            if (string.IsNullOrEmpty(key))
            {
                return(BadRequest("Key should not be null or empty."));
            }

            if (string.IsNullOrEmpty(value))
            {
                return(BadRequest("Value should not be null or empty."));
            }

            if (key.ToLower() == "multisig")
            {
                BitcoinAddress address = null;
                try
                {
                    address = BitcoinAddress.Create(value);
                }
                catch (Exception exp)
                {
                    throw new Exception("Some problem in parsing the provided address.", exp);
                }
            }

            using (OffchainMonitorContext context = new OffchainMonitorContext())
            {
                var settingRecords = from record in context.Settings
                                     where record.Key == key.ToLower()
                                     select record;
                if (settingRecords.Count() > 0)
                {
                    context.Settings.RemoveRange(settingRecords);
                }

                context.Settings.Add(new SettingsEntity {
                    Key = key, Value = value
                });

                await context.SaveChangesAsync();
            }

            return(Ok());
        }
        public async Task <IActionResult> AddCommitmentPunishmentPairOldDesign([FromQuery] string commitment,
                                                                               [FromQuery] string punishment)
        {
            if (string.IsNullOrEmpty(commitment) || string.IsNullOrEmpty(punishment))
            {
                return(BadRequest("Passed parameters should not be null or empty"));
            }
            else
            {
                Transaction cTx = new Transaction(commitment);
                Transaction pTx = new Transaction(punishment);

                try
                {
                    using (OffchainMonitorContext context = new OffchainMonitorContext())
                    {
                        var newlyAddedCommitment = new CommitmentEntity
                        {
                            Commitment = commitment,
                            Punishment = punishment
                        };

                        bool found    = false;
                        var  multisig = await settingsRepository.Get <string>("multisig");

                        var netwok = await Helper.GetNetwork(settingsRepository);

                        for (int i = 0; i < cTx.Inputs.Count; i++)
                        {
                            var prevHash = cTx.Inputs[i].PrevOut.Hash.ToString();
                            var prevTx   = await bitcoinTransactionService.GetTransaction(prevHash);

                            var prevOut = cTx.Inputs[i].PrevOut;
                            if (prevTx.Outputs[prevOut.N].ScriptPubKey.GetDestinationAddress(netwok).ToString()
                                == multisig)
                            {
                                found = true;

                                var existingMultisigOutput = (from o in context.MultisigOutputs
                                                              where o.TransactionId == prevOut.Hash.ToString() && o.OutputNumber == prevOut.N
                                                              select o).FirstOrDefault();
                                if (existingMultisigOutput == null)
                                {
                                    var newMultisigOutput = new MultisigOutputEntity();
                                    newMultisigOutput.TransactionId = prevOut.Hash.ToString();
                                    newMultisigOutput.OutputNumber  = (int)prevOut.N;
                                    newMultisigOutput.LastSeen      = DateTime.UtcNow;
                                    await context.MultisigOutputs.AddAsync(newMultisigOutput);

                                    existingMultisigOutput = newMultisigOutput;
                                }

                                existingMultisigOutput.LastSeen = DateTime.UtcNow;
                                existingMultisigOutput.Commitments.Add(newlyAddedCommitment);

                                newlyAddedCommitment.CommitmentOutput = existingMultisigOutput;
                                await context.Commitments.AddAsync(newlyAddedCommitment);
                            }
                        }

                        if (!found)
                        {
                            return(BadRequest(string.Format("The provide transaction does not pay to multisig:{0}", multisig)));
                        }

                        await context.SaveChangesAsync();
                    }
                    return(Ok());
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            }
        }