Exemplo n.º 1
0
        public async Task <OfferData> ProposeOffer(Proposal proposal, PubKey bobPubKey)
        {
            var key = new Key();

            _Repository.SaveKey(key);

            var initiator = GetChain(proposal.From.Chain);
            var taker     = GetChain(proposal.To.Chain);

            var preimage = new Preimage();

            _Repository.SavePreimage(preimage);

            var lockTimes = new[]
            {
                CalculateLockTime(proposal.From, TimeSpan.FromHours(2.0)),
                CalculateLockTime(proposal.To, TimeSpan.FromHours(1.0))
            };

            return(new OfferData()
            {
                Initiator = new OfferParty()
                {
                    Asset = proposal.From,
                    PubKey = key.PubKey
                },
                Taker = new OfferParty()
                {
                    Asset = proposal.To,
                    PubKey = bobPubKey
                },
                LockTime = await lockTimes[0].ConfigureAwait(false),
                CounterOfferLockTime = await lockTimes[1].ConfigureAwait(false),
                Hash = preimage.GetHash()
            });
        }
Exemplo n.º 2
0
        private async Task Propose(ProposeOptions o)
        {
            if (o.Proposition == null)
            {
                throw new FormatException();
            }

            var proposal = Proposal.Parse(o.Proposition);

            GetSupportedChain(proposal.From.Chain).EnsureIsSetup();
            GetSupportedChain(proposal.To.Chain).EnsureIsSetup();

            var key       = new PubKey(o.PubKey ?? "");
            var swapper   = CreateSwapper();
            var offerData = await swapper.ProposeOffer(proposal, key).ConfigureAwait(false);

            Console.WriteLine("Transfer this offer to the other party:");
            Console.WriteLine(Separator);
            DataToTransfer = offerData.ToString(false);
            Console.WriteLine(DataToTransfer);
            Console.WriteLine(Separator);

            Console.WriteLine();
            Console.WriteLine("Waiting for the other party to broadcast the counter offer...");
            Console.WriteLine();

            var counterOffer = offerData.CreateCounterOffer();

            blocked.Set();
            var txId = await swapper.WaitOfferAsync(counterOffer).ConfigureAwait(false);

            blocked.Reset();
            Console.WriteLine("Counter Offer broadcasted by the other party " + txId);

            txId = await swapper.BroadcastOffer(offerData, false).ConfigureAwait(false);

            Console.WriteLine("Offer broadcasted by us " + txId);

            Console.WriteLine();
            Console.WriteLine("Waiting confirmation of the offer and counter offer...");
            Console.WriteLine();

            var offerConfirmation = swapper.WaitOfferConfirmationAsync(offerData).ContinueWith(oo =>
            {
                Console.WriteLine("Offer confirmed");
            });
            var counterOfferConfirmation = swapper.WaitOfferConfirmationAsync(counterOffer).ContinueWith(oo =>
            {
                Console.WriteLine("Counter Offer confirmed");
            });

            blocked.Set();
            await Task.WhenAll(offerConfirmation, counterOfferConfirmation).ConfigureAwait(false);

            blocked.Reset();

            txId = await swapper.TakeOffer(counterOffer).ConfigureAwait(false);

            Console.WriteLine("Counter offer taken " + txId);
            Console.WriteLine();
            Console.WriteLine("Exchange complete!");
        }