Beispiel #1
0
        public async Task <bool> WaitOfferTakenAsync(OfferData offerData, CancellationToken cancellation = default(CancellationToken))
        {
            var decodedTxById = new Dictionary <uint256, JObject>();
            var initiator     = GetChain(offerData.Initiator.Asset.Chain);

            var redeemHash = offerData.CreateRedeemScript().Hash;

            int offset    = 0;
            int takeCount = 10;

            while (true)
            {
                cancellation.ThrowIfCancellationRequested();
                var transactions = await initiator.RPCClient.SendCommandAsync(RPCOperations.listtransactions, "*", takeCount, offset, true).ConfigureAwait(false);

                offset += takeCount;

                //If we reach end of the list, start over
                if (transactions.Result == null || ((JArray)transactions.Result).Count() == 0)
                {
                    offset = 0;
                    await Task.Delay(1000, cancellation).ConfigureAwait(false);

                    continue;
                }

                bool startOver = false;
                //Check if the preimage is present
                foreach (var tx in ((JArray)transactions.Result))
                {
                    int confirmation = 0;
                    if (tx["confirmations"] != null)
                    {
                        confirmation = tx["confirmations"].Value <int>();
                    }
                    //Old transaction, skip
                    if (confirmation > 144)
                    {
                        startOver = true;
                        continue;
                    }
                    //Coinbase we can't have the preimage
                    var category = tx["category"]?.Value <string>();
                    if (category == "immature" || category == "generate")
                    {
                        continue;
                    }

                    var     txId  = new uint256(tx["txid"].Value <string>());
                    JObject txObj = null;
                    if (!decodedTxById.TryGetValue(txId, out txObj))
                    {
                        var getTransaction = await initiator.RPCClient.SendCommandAsync("gettransaction", txId.ToString(), true);

                        var decodeRawTransaction = await initiator.RPCClient.SendCommandAsync("decoderawtransaction", getTransaction.Result["hex"]);

                        txObj = (JObject)decodeRawTransaction.Result;
                        decodedTxById.Add(txId, txObj);
                    }
                    foreach (var input in txObj["vin"])
                    {
                        var scriptSig = input["scriptSig"]["asm"].Value <string>();
                        var sigParts  = scriptSig.Split(' ').ToList();
                        // Add the hash in txin if segwit
                        if (input["txinwitness"] is JArray scriptWitness)
                        {
                            sigParts.AddRange(scriptWitness.Select(c => c.Value <string>()));
                        }

                        foreach (var sigPart in sigParts)
                        {
                            if (!HexEncoder.IsWellFormed(sigPart))
                            {
                                continue;
                            }
                            var preimage = new Preimage(Encoders.Hex.DecodeData(sigPart));
                            if (preimage.GetHash() == offerData.Hash)
                            {
                                _Repository.SavePreimage(preimage);
                                return(true);
                            }
                        }
                    }
                }

                //If we reach end of the list, start over
                if (startOver)
                {
                    offset = 0;
                    await Task.Delay(1000, cancellation).ConfigureAwait(false);

                    continue;
                }
            }
        }
Beispiel #2
0
        public async Task <uint256> TakeOffer(OfferData offer)
        {
            var taker     = GetChain(offer.Taker.Asset.Chain);
            var initiator = GetChain(offer.Initiator.Asset.Chain);

            var preimage = _Repository.GetPreimage(offer.Hash);

            if (preimage == null)
            {
                throw new InvalidOperationException("Unknown preimage");
            }
            var key = _Repository.GetPrivateKey(offer.Taker.PubKey);

            if (key == null)
            {
                throw new InvalidOperationException("Unknown pubkey");
            }

            var offerOutpoint = _Repository.GetOffer(offer.CreateScriptPubkey());

            if (offerOutpoint == null)
            {
                throw new InvalidOperationException("Unknown offer");
            }

            var destination = await initiator.RPCClient.GetNewAddressAsync().ConfigureAwait(false);

            var tx = new Transaction();

            tx.AddInput(new TxIn(offerOutpoint, offer.TakeOffer(new TransactionSignature(key.Sign(uint256.One), SigHash.All), preimage)));
            tx.AddOutput(new TxOut(offer.Initiator.Asset.Amount, destination));
            var fee = (await GetFeeAsync(initiator).ConfigureAwait(false)).GetFee(tx.GetVirtualSize());

            tx.Outputs[0].Value -= fee;

            var offerCoin = new Coin(offerOutpoint, new TxOut(offer.Initiator.Asset.Amount, offer.CreateScriptPubkey())).ToScriptCoin(offer.CreateRedeemScript());
            var sig       = tx.Inputs.AsIndexedInputs().First().Sign(key, offerCoin, SigHash.All);

            tx.Inputs[0].ScriptSig = offer.TakeOffer(sig, preimage);
            await initiator.RPCClient.SendRawTransactionAsync(tx).ConfigureAwait(false);

            return(tx.GetHash());
        }