示例#1
0
        public void FullExchange2()
        {
            var initiatorInputKey = new Key();
            var acceptorInputKey  = new Key();

            var offerKey  = new Key();
            var acceptKey = new Key();

            var initiator = new DLCTransactionBuilder(true, null, null, null, Network.RegTest);
            var acceptor  = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);

            var oracleInfo         = OracleInfo.Parse("e4d36e995ff4bba4da2b60ad907d61d36e120d6f7314a3c2a20c6e27a5cd850ff67f8f41718c86f05eb95fab308f5ed788a2a963124299154648f97124caa579");
            var requiredCollateral = initiator.Offer(oracleInfo.PubKey, oracleInfo.RValue,
                                                     new DiscretePayoffs()
            {
                new DiscretePayoff("Republicans", Money.Coins(0.4m)),
                new DiscretePayoff("Democrats", -Money.Coins(0.6m)),
                new DiscretePayoff("Smith", Money.Zero)
            }, new Timeouts()
            {
                ContractMaturity = 100,
                ContractTimeout  = 200
            });
            var fund1 = GetFundingPSBT(acceptorInputKey, requiredCollateral);
            var offer = initiator.FundOffer(offerKey, fund1);

            var payoff = acceptor.Accept(offer);
            var fund2  = GetFundingPSBT(initiatorInputKey, payoff.CalculateMinimumCollateral());
            var accept = acceptor.FundAccept(acceptKey, fund2);

            initiator.Sign1(accept);
            var fundPSBT = initiator.GetFundingPSBT();

            fundPSBT.SignWithKeys(initiatorInputKey);
            var sign = initiator.Sign2(offerKey, fundPSBT);

            acceptor.Finalize1(sign);
            fundPSBT = acceptor.GetFundingPSBT();
            fundPSBT.SignWithKeys(acceptorInputKey);
            var fullyVerified = acceptor.Finalize(fundPSBT);

            foreach (var i in fullyVerified.Inputs)
            {
                Assert.NotNull(i.WitScript);
            }

            var cet          = initiator.BuildCET(offer.ContractInfo[0].Outcome);
            var keyBytes     = Encoders.Hex.DecodeData("d1da46f96f0be50bce2bbabe6bc8633f448ec3f1d14715a0b086b68ed34e095d");
            var oracleSecret = new Key(keyBytes);

            var sig = oracleInfo.RValue.CreateSchnorrSignature(oracleSecret.ToECPrivKey());

            Assert.True(oracleInfo.PubKey.SigVerifyBIP340(sig, new DiscreteOutcome("Republicans").Hash));

            initiator.BuildSignedCET(offerKey, oracleSecret);

            this.testOutputHelper.WriteLine("----Final state------");
            testOutputHelper.WriteLine(JObject.Parse(initiator.ExportState()).ToString(Formatting.Indented));
            this.testOutputHelper.WriteLine("---------------------");
        }
示例#2
0
        protected override async Task InvokeAsyncBase(InvocationContext context)
        {
            var name = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("name")?.Trim();

            if (name is null)
            {
                throw new CommandOptionRequiredException("name");
            }
            if (await this.TryGetDLC(name) != null)
            {
                throw new CommandException("name", "This DLC already exists");
            }
            EventFullName evtName = context.GetEventName();
            var           oracle  = await GetOracle("eventfullname", evtName.OracleName);

            if (oracle?.PubKey is null)
            {
                throw new CommandException("eventfullname", "The specified oracle does not exists");
            }
            var evt = await GetEvent("eventfullname", evtName);

            var payoffsStr = context.ParseResult.CommandResult.GetArgumentValueOrDefault <List <string> >("payoff");

            if (payoffsStr is null || payoffsStr.Count == 0)
            {
                throw new CommandOptionRequiredException("payoff");
            }
            var payoffs = CreatePayoffs(payoffsStr);

            FixCasing(evt, payoffs);
            var builder = new DLCTransactionBuilder(true, null, null, null, Network);

            var timeout = new Timeouts()
            {
                ContractMaturity = 0,
                ContractTimeout  = Constants.NeverLockTime
            };

            if (context.ParseResult.HasOption("cetlocktime"))
            {
                timeout.ContractMaturity = new LockTime(context.ParseResult.ValueForOption <uint>("cetlocktime"));
            }
            if (context.ParseResult.HasOption("refundlocktime"))
            {
                timeout.ContractTimeout = new LockTime(context.ParseResult.ValueForOption <uint>("refundlocktime"));
            }
            var collateral = payoffs.CalculateMinimumCollateral();

            builder.Offer(oracle.PubKey, evt.EventId !.RValue, payoffs, timeout);
            var dlc = await Repository.NewDLC(evt.EventId, builder);

            await NameRepository.AsDLCNameRepository().SetMapping(name, dlc.LocalId);

            context.Console.Out.Write($"Offer created, you now need to setup the DLC sending {collateral} BTC to yourself. For more information, run `dlc show \"{name}\"`.");
        }
示例#3
0
        public void FullExchange2()
        {
            var initiatorInputKey = new Key();
            var acceptorInputKey  = new Key();

            var fund1     = GetFundingPSBT(initiatorInputKey, Money.Coins(0.6m));
            var fund2     = GetFundingPSBT(acceptorInputKey, Money.Coins(0.4m));
            var initiator = new DLCTransactionBuilder(true, null, null, null, Network.RegTest);
            var acceptor  = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);

            var oracleInfo = OracleInfo.Parse("156c7d1c7922f0aa1168d9e21ac77ea88bbbe05e24e70a08bbe0519778f2e5daea3a68d8749b81682513b0479418d289d17e24d4820df2ce979f1a56a63ca525");
            var offer      = initiator.Offer(PSBTFundingTemplate.Parse(fund1), oracleInfo,
                                             ContractInfo.CreateContract(
                                                 ("Republicans_win", Money.Coins(1.0m)),
                                                 ("Democrats_win", Money.Coins(0m)),
                                                 ("other", Money.Coins(0.6m))), new Timeouts()
            {
                ContractMaturity = 100,
                ContractTimeout  = 200
            });
            var accept = acceptor.Accept(offer, PSBTFundingTemplate.Parse(fund2));

            initiator.VerifySign(accept);
            var fundPSBT = initiator.BuildFundingPSBT();

            fundPSBT.SignWithKeys(initiatorInputKey);
            var sign = initiator.EndSign(fundPSBT);

            acceptor.VerifySign(sign);
            fundPSBT = acceptor.BuildFundingPSBT();
            fundPSBT.SignWithKeys(acceptorInputKey);
            var psbt = acceptor.CombineFunding(fundPSBT);

            psbt = psbt.Finalize();
            var fullyVerified = psbt.ExtractTransaction();

            foreach (var i in fullyVerified.Inputs)
            {
                Assert.NotNull(i.WitScript);
            }

            var cet      = initiator.BuildCET(offer.ContractInfo[0].Outcome);
            var keyBytes = Encoders.Hex.DecodeData("39eabd151030f4f2d518fb8a8d00f679aa9e034c66263032a1245a04cfbc592b");
            //Array.Reverse(keyBytes);
            var oracleSecret = new Key(keyBytes);

            initiator.BuildSignedCET(oracleSecret);
        }
示例#4
0
        public void FullExchange()
        {
            var offerExample      = Parse <Messages.Offer>("Data/Offer2.json");
            var offerKey          = new Key();
            var acceptKey         = new Key();
            var initiatorInputKey = new Key();
            var acceptorInputKey  = new Key();
            var initiator         = new DLCTransactionBuilder(true, null, null, null, Network.RegTest);
            var requiredFund      = initiator.Offer(offerExample.OracleInfo.PubKey,
                                                    offerExample.OracleInfo.RValue,
                                                    DiscretePayoffs.CreateFromContractInfo(offerExample.ContractInfo, offerExample.TotalCollateral,
                                                                                           new[] { new DiscreteOutcome("Republicans_win"), new DiscreteOutcome("Democrats_win"), new DiscreteOutcome("other") }),
                                                    offerExample.Timeouts);
            var fund1          = GetFundingPSBT(initiatorInputKey, requiredFund);
            var offer          = initiator.FundOffer(offerKey, fund1);
            var acceptor       = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);
            var acceptorPayoff = acceptor.Accept(offer);
            var fund2          = GetFundingPSBT(acceptorInputKey, acceptorPayoff.CalculateMinimumCollateral());
            var accept         = acceptor.FundAccept(acceptKey, fund2);

            initiator.Sign1(accept);
            var fundPSBT = initiator.GetFundingPSBT();

            fundPSBT.SignWithKeys(initiatorInputKey);
            var sign = initiator.Sign2(offerKey, fundPSBT);

            acceptor.Finalize1(sign);
            fundPSBT = acceptor.GetFundingPSBT();
            fundPSBT.SignWithKeys(acceptorInputKey);
            var fullyVerified = acceptor.Finalize(fundPSBT);

            foreach (var i in fullyVerified.Inputs)
            {
                Assert.NotNull(i.WitScript);
            }
            fundPSBT = acceptor.GetFundingPSBT();
            if (fundPSBT.TryGetEstimatedFeeRate(out var estimated))
            {
                Assert.True(estimated > new FeeRate(1.0m), "Fee Rate of the funding PSBT are too low");
            }
        }
示例#5
0
        public void FullExchange()
        {
            var offerExample = Parse <Messages.Offer>("Data/Offer2.json");

            var initiatorInputKey = new Key();
            var acceptorInputKey  = new Key();

            var fund1     = GetFundingPSBT(initiatorInputKey, Money.Coins(0.6m));
            var fund2     = GetFundingPSBT(acceptorInputKey, Money.Coins(0.4m));
            var initiator = new DLCTransactionBuilder(true, null, null, null, Network.RegTest);
            var acceptor  = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);

            var offer  = initiator.Offer(PSBTFundingTemplate.Parse(fund1), offerExample.OracleInfo, offerExample.ContractInfo, offerExample.Timeouts);
            var accept = acceptor.Accept(offer, PSBTFundingTemplate.Parse(fund2));

            initiator.VerifySign(accept);
            var fundPSBT = initiator.BuildFundingPSBT();

            fundPSBT.SignWithKeys(initiatorInputKey);
            var sign = initiator.EndSign(fundPSBT);

            acceptor.VerifySign(sign);
            fundPSBT = acceptor.BuildFundingPSBT();
            fundPSBT.SignWithKeys(acceptorInputKey);
            fundPSBT = acceptor.CombineFunding(fundPSBT);
            fundPSBT = fundPSBT.Finalize();
            var fullyVerified = fundPSBT.ExtractTransaction();

            foreach (var i in fullyVerified.Inputs)
            {
                Assert.NotNull(i.WitScript);
            }
            if (fundPSBT.TryGetEstimatedFeeRate(out var estimated))
            {
                Assert.True(estimated > new FeeRate(1.0m), "Fee Rate of the funding PSBT are too low");
            }
        }