コード例 #1
0
        public async Task <IActionResult> GetOfferAPI(string offerId)
        {
            var entry = await AtomicSwapRepository.GetEntry(offerId);

            if (entry == null ||
                (entry.Status != XSwapStatus.WaitingTaker && entry.Role == XSwapRole.Maker) ||
                (entry.Status != XSwapStatus.WaitingEscrow && entry.Role == XSwapRole.Taker))
            {
                return(NotFound());
            }
            return(Json(entry.Offer));
        }
コード例 #2
0
        public async Task <IActionResult> TakeOfferAPI(string offerId, [FromBody] AtomicSwapTakeRequest request)
        {
            var entry = await AtomicSwapRepository.GetEntry(offerId);

            if (entry == null || entry.Status != XSwapStatus.WaitingTaker)
            {
                return(NotFound());
            }
            // TODO atomically take the offer
            var client = AtomicSwapClientFactory.Create(request.TakerUri);
            AtomicSwapTakeResponse response = null;

            try
            {
                using (var cts = new CancellationTokenSource())
                {
                    cts.CancelAfter(5000);
                    var takerOffer = await client.GetOffer(cts.Token);

                    if (takerOffer.MarketMakerUri != entry.Offer.MarketMakerUri)
                    {
                        return(NotFound());
                    }
                }
            }
            catch { }

            entry.Partner           = request.TakerUri.DnsSafeHost;
            entry.OtherUri          = request.TakerUri;
            entry.Status            = XSwapStatus.WaitingEscrow;
            entry.Sent.MyKey        = new Key();
            entry.Sent.OtherKey     = request.MakerSentCryptoPubkey;
            entry.Received.MyKey    = new Key();
            entry.Received.OtherKey = request.MakerReceivedCryptoPubkey;
            entry.Preimage          = new Preimage();
            entry.Hash = entry.Preimage.GetHash();

            response = new AtomicSwapTakeResponse()
            {
                Hash = entry.Preimage.GetHash(),
                MakerReceivedCryptoPubkey = entry.Received.MyKey.PubKey,
                MakerSentCryptoPubkey     = entry.Received.MyKey.PubKey
            };

            await AtomicSwapRepository.UpdateEntry(offerId, entry);

            return(Json(response));
        }