public SolverClientSession SetClientSignedTransaction(uint160 channelId, Transaction transaction, Script redeemDestination)
        {
            AssertState(TumblerClientSessionStates.WaitingClientTransaction);
            var expectedTxout = BuildClientEscrowTxOut();
            var output        = transaction.Outputs.AsIndexedOutputs().Single(o => o.TxOut.ScriptPubKey == expectedTxout.ScriptPubKey && o.TxOut.Value == expectedTxout.Value);
            var solver        = new SolverClientSession(Parameters.CreateSolverParamaters());

            solver.ConfigureEscrowedCoin(channelId, new Coin(output).ToScriptCoin(InternalState.ClientEscrow), InternalState.ClientEscrowKey, redeemDestination);
            InternalState.Status = TumblerClientSessionStates.WaitingSolvedVoucher;
            return(solver);
        }
        public SolverClientSession SetClientSignedTransaction(uint160 channelId, Transaction transaction,
                                                              Script redeemDestination)
        {
            AssertState(TumblerClientSessionStates.WaitingClientTransaction);
            var expectedTxout = BuildClientEscrowTxOut();
            var output        = transaction.Outputs.AsIndexedOutputs().Single(o =>
                                                                              o.TxOut.ScriptPubKey == expectedTxout.ScriptPubKey && o.TxOut.Value == expectedTxout.Value);
            var solver = new SolverClientSession(Parameters.CreateSolverParamaters());

            var escrowedCoin = new Coin(output).ToScriptCoin(InternalState.ClientEscrow);

            Logs.Tumbler.LogDebug(
                $"ClientChannelNegotiation.SetClientSignedTransaction() - channelId : {channelId}, escrowedCoin.Outpoint.Hash : {escrowedCoin.Outpoint.Hash}, escrowedCoin.Outpoint.N : {escrowedCoin.Outpoint.N}, escrowKey.GetWif() : {InternalState.ClientEscrowKey.GetWif(this.Parameters.Network)}, redeemDestination : {redeemDestination.ToString()}");
            solver.ConfigureEscrowedCoin(channelId, escrowedCoin, InternalState.ClientEscrowKey, redeemDestination);

            InternalState.Status = TumblerClientSessionStates.WaitingSolvedVoucher;
            return(solver);
        }
        public void TestPuzzleSolver()
        {
            RsaKey         key = TestKeys.Default;
            PuzzleSolution expectedSolution = null;
            Puzzle         puzzle           = key.PubKey.GeneratePuzzle(ref expectedSolution);

            var parameters = new SolverParameters
            {
                FakePuzzleCount = 50,
                RealPuzzleCount = 10,
                ServerKey       = key.PubKey
            };
            SolverClientSession client = new SolverClientSession(parameters);
            SolverServerSession server = new SolverServerSession(key, parameters);

            var clientEscrow = new Key();
            var serverEscrow = new Key();

            var escrow            = CreateEscrowCoin(clientEscrow.PubKey, serverEscrow.PubKey);
            var redeemDestination = new Key().ScriptPubKey;

            client.ConfigureEscrowedCoin(escrow, clientEscrow, redeemDestination);
            client.AcceptPuzzle(puzzle.PuzzleValue);
            RoundTrip(ref client, parameters);
            Assert.True(client.GetInternalState().RedeemDestination == redeemDestination);
            PuzzleValue[] puzzles = client.GeneratePuzzles();
            RoundTrip(ref client, parameters);
            RoundTrip(ref puzzles);

            server.ConfigureEscrowedCoin(escrow, serverEscrow);
            var commitments = server.SolvePuzzles(puzzles);

            RoundTrip(ref server, parameters, key);
            RoundTrip(ref commitments);

            var revelation = client.Reveal(commitments);

            RoundTrip(ref client, parameters);
            RoundTrip(ref revelation);

            SolutionKey[] fakePuzzleKeys = server.CheckRevelation(revelation);
            RoundTrip(ref server, parameters, key);
            RoundTrip(ref fakePuzzleKeys);


            BlindFactor[] blindFactors = client.GetBlindFactors(fakePuzzleKeys);
            RoundTrip(ref client, parameters);
            RoundTrip(ref blindFactors);

            var offerInformation = server.CheckBlindedFactors(blindFactors, FeeRate);

            RoundTrip(ref server, parameters, key);

            var clientOfferSig = client.SignOffer(offerInformation);


            //Verify if the scripts are correctly created
            var fulfill     = server.FulfillOffer(clientOfferSig, new Key().ScriptPubKey, FeeRate);
            var offerRedeem = client.CreateOfferRedeemTransaction(FeeRate);

            var offerTransaction = server.GetSignedOfferTransaction();
            var offerCoin        = offerTransaction.Transaction.Outputs.AsCoins().First();
            var resigned         = offerTransaction.ReSign(client.EscrowedCoin);

            TransactionBuilder txBuilder = new TransactionBuilder();

            txBuilder.AddCoins(client.EscrowedCoin);
            Assert.True(txBuilder.Verify(resigned));

            resigned  = fulfill.ReSign(offerCoin);
            txBuilder = new TransactionBuilder();
            txBuilder.AddCoins(offerCoin);
            Assert.True(txBuilder.Verify(resigned));

            var offerRedeemTx = offerRedeem.ReSign(offerCoin);

            txBuilder = new TransactionBuilder();
            txBuilder.AddCoins(offerCoin);
            Assert.True(txBuilder.Verify(offerRedeemTx));


            client.CheckSolutions(fulfill.Transaction);
            RoundTrip(ref client, parameters);

            var clientEscapeSignature = client.SignEscape();
            var escapeTransaction     = server.GetSignedEscapeTransaction(clientEscapeSignature, FeeRate, new Key().ScriptPubKey);

            txBuilder = new TransactionBuilder();
            txBuilder.AddCoins(client.EscrowedCoin);
            Assert.True(txBuilder.Verify(escapeTransaction));

            var solution = client.GetSolution();

            RoundTrip(ref client, parameters);
            Assert.True(solution == expectedSolution);
        }
Beispiel #4
0
        public void TestPuzzleSolver()
        {
            RsaKey         key = TestKeys.Default;
            PuzzleSolution expectedSolution = null;
            Puzzle         puzzle           = key.PubKey.GeneratePuzzle(ref expectedSolution);

            var parameters = new SolverParameters
            {
                FakePuzzleCount = 50,
                RealPuzzleCount = 10,
                ServerKey       = key.PubKey
            };
            SolverClientSession client = new SolverClientSession(parameters);
            SolverServerSession server = new SolverServerSession(key, parameters);

            var clientEscrow = new Key();
            var serverEscrow = new Key();
            var clientRedeem = new Key();

            var escrow = CreateEscrowCoin(clientEscrow.PubKey, serverEscrow.PubKey, clientRedeem.PubKey);

            client.ConfigureEscrowedCoin(escrow, clientEscrow, clientRedeem);
            client.AcceptPuzzle(puzzle.PuzzleValue);
            RoundTrip(ref client, parameters);
            PuzzleValue[] puzzles = client.GeneratePuzzles();
            RoundTrip(ref client, parameters);
            RoundTrip(ref puzzles);

            server.ConfigureEscrowedCoin(escrow, serverEscrow);
            var commitments = server.SolvePuzzles(puzzles);

            RoundTrip(ref server, parameters, key);
            RoundTrip(ref commitments);

            var revelation = client.Reveal(commitments);

            RoundTrip(ref client, parameters);
            RoundTrip(ref revelation);

            SolutionKey[] fakePuzzleKeys = server.CheckRevelation(revelation);
            RoundTrip(ref server, parameters, key);
            RoundTrip(ref fakePuzzleKeys);


            BlindFactor[] blindFactors = client.GetBlindFactors(fakePuzzleKeys);
            RoundTrip(ref client, parameters);
            RoundTrip(ref blindFactors);

            var offerInformation = server.CheckBlindedFactors(blindFactors, FeeRate);

            RoundTrip(ref server, parameters, key);

            var clientOfferSig = client.SignOffer(offerInformation);

            //Verify if the scripts are correctly created
            var fulfill = server.FulfillOffer(clientOfferSig, new Key().ScriptPubKey, FeeRate);

            var offerTransaction         = server.GetSignedOfferTransaction();
            TransactionBuilder txBuilder = new TransactionBuilder();

            txBuilder.AddCoins(client.EscrowedCoin);
            Assert.True(txBuilder.Verify(offerTransaction.Transaction));

            txBuilder = new TransactionBuilder();
            txBuilder.AddCoins(offerTransaction.Transaction.Outputs.AsCoins().ToArray());
            Assert.True(txBuilder.Verify(fulfill.Transaction));

            //Check if can resign fulfill in case offer get malleated
            offerTransaction.Transaction.LockTime = new LockTime(1);
            fulfill.Transaction.Inputs[0].PrevOut = offerTransaction.Transaction.Outputs.AsCoins().First().Outpoint;
            txBuilder = new TransactionBuilder();
            txBuilder.Extensions.Add(new OfferBuilderExtension());
            txBuilder.AddKeys(server.GetInternalState().FulfillKey);
            txBuilder.AddCoins(offerTransaction.Transaction.Outputs.AsCoins().ToArray());
            txBuilder.SignTransactionInPlace(fulfill.Transaction);
            Assert.True(txBuilder.Verify(fulfill.Transaction));
            ////////////////////////////////////////////////

            client.CheckSolutions(fulfill.Transaction);
            RoundTrip(ref client, parameters);
            var solution = client.GetSolution();

            RoundTrip(ref client, parameters);
            Assert.True(solution == expectedSolution);
        }