예제 #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
        public void CheckAttestationMatchBitcoinS()
        {
            var oracleInfo = OracleInfo.Parse("156c7d1c7922f0aa1168d9e21ac77ea88bbbe05e24e70a08bbe0519778f2e5daea3a68d8749b81682513b0479418d289d17e24d4820df2ce979f1a56a63ca525");

            Assert.True(oracleInfo.TryComputeSigpoint(new DiscreteOutcome("Democrat_win"), out var sigpoint));
            var attestation = Context.Instance.CreateECPrivKey(Encoders.Hex.DecodeData("77a5aabd716936411bbe19219bd0b261fae8f0524367268feb264e0a3b215766"));
            var pubKey      = attestation.CreatePubKey();

            Assert.Equal(sigpoint, pubKey);
        }
예제 #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 DLCTestVector(JObject testVector)
        {
            TestInputs       = testVector["testInputs"];
            Offer            = new Offer();
            Offer.OracleInfo = OracleInfo.Parse(
                TestInputs["params"]["oracleInfo"]["publicKey"].Value <string>() +
                TestInputs["params"]["oracleInfo"]["nonce"].Value <string>());
            var ci = new List <ContractInfo>();

            foreach (var jObj in (JArray)TestInputs["params"]["contractInfo"])
            {
                var info = new ContractInfo(new DiscreteOutcome(jObj["preImage"].Value <string>()), Money.Satoshis(jObj["localPayout"].Value <long>()));
                ci.Add(info);
                Assert.Equal(jObj["outcome"].Value <string>(), Encoders.Hex.EncodeData(info.Outcome.Hash));
            }
            Offer.ContractInfo = ci.ToArray();
            Offer.FeeRate      = new FeeRate(Money.Satoshis(TestInputs["params"]["feeRate"].Value <long>()), 1);
            Offer.ChainHash    = Network.RegTest.GenesisHash;
            Offer.Timeouts     = new Timeouts()
            {
                ContractMaturity = TestInputs["params"]["contractMaturityBound"].Value <uint>(),
                ContractTimeout  = TestInputs["params"]["contractTimeout"].Value <uint>()
            };
            ExpectedOfferTLV  = testVector["offer"].Value <string>();
            ExpectedAcceptTLV = testVector["accept"].Value <string>();
            ExpectedSignTLV   = testVector["sign"].Value <string>();
            JsonSerializerSettings settings = new JsonSerializerSettings();

            NBitcoin.JsonConverters.Serializer.RegisterFrontConverters(settings, Network.RegTest);
            UnsignedTxs = testVector["unsignedTxs"].ToObject <ExpectedTransactions>(JsonSerializer.Create(settings));
            SignedTxs   = testVector["signedTxs"].ToObject <ExpectedTransactions>(JsonSerializer.Create(settings));
            FillFundingInformation(Offer, "offerParams");
            OfferPrivateKey          = new Key(Encoders.Hex.DecodeData(TestInputs["offerParams"]["fundingPrivKey"].Value <string>()));
            Offer.PubKeys.FundingKey = OfferPrivateKey.PubKey;
            AcceptPrivateKey         = new Key(Encoders.Hex.DecodeData(TestInputs["acceptParams"]["fundingPrivKey"].Value <string>()));
        }