コード例 #1
0
ファイル: TLVTests.cs プロジェクト: dgarage/NDLC
        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);
            }
        }
コード例 #2
0
ファイル: TLVTests.cs プロジェクト: dgarage/NDLC
 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);
     }
 }
コード例 #3
0
        protected override Task InvokeAsyncBase(InvocationContext context)
        {
            var signedMessageBase64 = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("signed")?.Trim();

            if (signedMessageBase64 is null)
            {
                throw new CommandOptionRequiredException("signed");
            }

            var  type         = GetTLVType(signedMessageBase64);
            bool wellFormated = false;

            try
            {
                if (type == Accept.TLVType)
                {
                    var accept = Accept.ParseFromTLV(signedMessageBase64, Network);
                    wellFormated = true;
                    return(HandleAccept(context, accept));
                }
                else if (type == Sign.TLVType)
                {
                    var sign = Sign.ParseFromTLV(signedMessageBase64, Network);
                    wellFormated = true;
                    return(HandleSign(context, sign));
                }
                else
                {
                    throw new CommandException("signed", "Invalid signed message (Invalid TLV type)");
                }
            }
            catch (CommandException)
            {
                throw;
            }
            catch (Exception ex) when(!wellFormated)
            {
                throw new CommandException("signed", $"Invalid signed message ({ex.Message})");
            }
        }