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("---------------------"); }
protected override async Task InvokeAsyncBase(InvocationContext context) { var name = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("name")?.Trim(); if (name is null) { throw new CommandOptionRequiredException("name"); } var dlc = await GetDLC("name", name); if (dlc?.BuilderState is null || dlc?.OracleInfo is null) { throw new CommandException("name", "This DLC does not exist"); } var builder = new DLCTransactionBuilder(dlc.BuilderState.ToString(), Network); var psbt = context.ParsePSBT("signedpsbt", Network); if (!builder.State.IsInitiator) { context.AssertState("name", dlc, false, DLCNextStep.Fund, Network); var fullySigned = builder.Finalize(psbt); dlc.BuilderState = builder.ExportStateJObject(); await Repository.SaveDLC(dlc); context.WriteTransaction(fullySigned, builder.State.Funding?.FundCoin, Network); } else { context.AssertState("name", dlc, true, DLCNextStep.Fund, Network); var key = await Repository.GetKey(dlc.FundKeyPath !); try { var sign = builder.Sign2(key, psbt); dlc.Sign = sign; dlc.BuilderState = builder.ExportStateJObject(); await Repository.SaveDLC(dlc); context.WriteObject(sign, Repository.JsonSettings); } catch (Exception ex) { throw new CommandException("signedpsbt", $"Invalid PSBT. ({ex.Message})"); } } }
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"); } }