Esempio n. 1
0
        public async Task dlc_test()
        {
            await foreach (var vector in DLCTestVector.ReadVectors())
            {
                Assert.Equal(vector.ExpectedOfferTLV, Encoders.Hex.EncodeData(vector.Offer.ToTLV()));
                var accept = new Accept();
                accept.TemporaryContractId = vector.Offer.GetTemporaryContractId();
                vector.FillFundingInformation(accept, "acceptParams");
                accept.PubKeys.FundingKey = vector.AcceptPrivateKey.PubKey;
                var acceptor = new DLCTransactionBuilder(false, null, null, null, Network.RegTest);
                acceptor.Accept(vector.Offer, accept.TotalCollateral);
                Assert.True(acceptor.State.TemporaryContractId);
                var builtAccept = acceptor.FundAccept(vector.AcceptPrivateKey, accept.CreateSetupPSBT(Network.RegTest));
                accept.CetSigs = builtAccept.CetSigs;
                // this signature is non deterministic...
                accept.CetSigs.RefundSig = Accept.ParseFromTLV(vector.ExpectedAcceptTLV, Network.RegTest).CetSigs.RefundSig;
                Assert.Equal(vector.ExpectedAcceptTLV, Encoders.Hex.EncodeData(accept.ToTLV()));

                var actualFundingTransaction = acceptor.GetFundingTransaction();
                var actualRefundTransaction  = acceptor.BuildRefund();
                var actualCETs = new Transaction[vector.Offer.ContractInfo.Length];
                for (int i = 0; i < vector.Offer.ContractInfo.Length; i++)
                {
                    actualCETs[i] = acceptor.BuildCET(vector.Offer.ContractInfo[i].Outcome);
                }
                var offerer = new DLCTransactionBuilder(true, vector.Offer, null, null, Network.RegTest);
                offerer.Sign1(accept);
                var fundingPSBT = offerer.GetFundingPSBT();
                FillSignatures(fundingPSBT, vector.SignedTxs.FundingTx);
                var sign = offerer.Sign2(vector.OfferPrivateKey, fundingPSBT);
                // this signature is non deterministic...
                sign.CetSigs.RefundSig = Sign.ParseFromTLV(vector.ExpectedSignTLV, Network.RegTest).CetSigs.RefundSig;
                Assert.Equal(vector.ExpectedSignTLV, Encoders.Hex.EncodeData(sign.ToTLV()));
                Assert.Equal(
                    RemoveForDebug(vector.UnsignedTxs.FundingTx.ToString()),
                    RemoveForDebug(actualFundingTransaction.ToString()));
                Assert.Equal(
                    RemoveForDebug(vector.UnsignedTxs.RefundTx.ToString()),
                    RemoveForDebug(actualRefundTransaction.ToString()));
                for (int i = 0; i < actualCETs.Length; i++)
                {
                    Assert.Equal(
                        RemoveForDebug(vector.UnsignedTxs.Cets[i].ToString()),
                        RemoveForDebug(actualCETs[i].ToString()));
                }

                Assert.NotNull(offerer.State.ContractId);
                Assert.Equal(offerer.State.ContractId, acceptor.State.ContractId);
                Assert.False(offerer.State.TemporaryContractId);
            }
        }
Esempio n. 2
0
 public async Task CanConvertMessagesToTLV()
 {
     await foreach (var vector in DLCTestVector.ReadVectors())
     {
         var parsedOffer = Offer.ParseFromTLV(vector.ExpectedOfferTLV, Network.RegTest);
         var actualHex   = Encoders.Hex.EncodeData(parsedOffer.ToTLV());
         Assert.Equal(vector.ExpectedOfferTLV, actualHex);
         var parsedAccept = Accept.ParseFromTLV(vector.ExpectedAcceptTLV, Network.RegTest);
         actualHex = Encoders.Hex.EncodeData(parsedAccept.ToTLV());
         Assert.Equal(vector.ExpectedAcceptTLV, actualHex);
         var parsedSign = Sign.ParseFromTLV(vector.ExpectedSignTLV, Network.RegTest);
         actualHex = Encoders.Hex.EncodeData(parsedSign.ToTLV());
         Assert.Equal(vector.ExpectedSignTLV, actualHex);
     }
 }