Beispiel #1
0
        public FundingCreatedMessage Handle(AcceptChannelMessage acceptMessage, PendingChannel pendingChannel)
        {
            var oldState    = LocalChannelState.AcceptChannel;
            var openMessage = pendingChannel.OpenMessage;

            _channelLoggingService.LogPendingChannelInfo(openMessage.TemporaryChannelId.ToHex(), oldState, "Remote accepted channel open");
            FundingTransaction fundingTx = _fundingService.CreateFundingTransaction(openMessage.FundingSatoshis, openMessage.FeeratePerKw, openMessage.FundingPubKey, acceptMessage.FundingPubKey);

            var channel = CreateChannel(pendingChannel.ChannelIndex, fundingTx, openMessage, acceptMessage);

            channel.IsFunder = true;

            _commitmentService.CreateInitialCommitmentTransactions(openMessage, acceptMessage, channel, pendingChannel.RevocationKey);
            channel.State = LocalChannelState.FundingCreated;

            var signatureOfRemoteCommitmentTx           = channel.RemoteCommitmentTxParameters.LocalSignature;
            FundingCreatedMessage fundingCreatedMessage = new FundingCreatedMessage
            {
                TemporaryChannelId   = openMessage.TemporaryChannelId,
                FundingTransactionId = fundingTx.Transaction.GetHash().ToBytes(),
                FundingOutputIndex   = fundingTx.FundingOutputIndex,
                Signature            = signatureOfRemoteCommitmentTx.ToRawSignature()
            };

            pendingChannel.Channel = channel;
            _channelLoggingService.LogStateUpdate(channel, oldState, additionalData: fundingTx.Transaction.ToString());

            return(fundingCreatedMessage);
        }
Beispiel #2
0
        public void HandleInvalidRemoteSignatureTest()
        {
            var mocks         = new ChannelEstablishmentMocks();
            var revocationKey = new ECKeyPair("DD06232AE9A50384A72D85CED6351DCB35C798231D4985615C77D6847F83FC65", true);
            var handler       = new FundingMessageSignedHandler(mocks.ChannelLoggingService.Object, mocks.FundingService.Object,
                                                                mocks.CommTxService.Object, mocks.ChannelService.Object, mocks.BlockchainMonitorService.Object);

            OpenChannelMessage openChannelMessage = new OpenChannelMessage()
            {
                ChainHash                = NetworkParameters.BitcoinTestnet.ChainHash,
                TemporaryChannelId       = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                FundingSatoshis          = 420000,
                PushMSat                 = 1000,
                DustLimitSatoshis        = 500,
                MaxHtlcValueInFlightMSat = 50000,
                ChannelReserveSatoshis   = 5000,
                HtlcMinimumMSat          = 1100,
                FeeratePerKw             = 150,
                ToSelfDelay              = 133,
                MaxAcceptedHtlcs         = 156,
                FundingPubKey            = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false),
                RevocationBasepoint      = new ECKeyPair("022ecc432552ff86d053514ffb133d3025fb14c39aa5ae2a5169b0367174cabfa4", false),
                PaymentBasepoint         = new ECKeyPair("029d100efe40aa3f58985fa12bd0f5c75711449ff4d30adca6f1968a2200bbbf1a", false),
                DelayedPaymentBasepoint  = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false),
                HtlcBasepoint            = new ECKeyPair("03d029229db8f594adcd545b4a42acbb1013286908d2905fa05c9a4e2083fe3fe2", false),
                FirstPerCommitmentPoint  = new ECKeyPair("02846726efa57378ad8370acf094f26902a7f1e21903791ef4ab6f989da86679f2", false),
                ChannelFlags             = 1
            };

            var signature = "2f26e967305b4d422116a7c876d338bc4298263f329a54c0d1655f55d594de4955356386addd0bc15ced36dacece0af439694195654c838769583409eeac5d3f".HexToByteArray();

            FundingSignedMessage fundingSignedMessage = new FundingSignedMessage()
            {
                ChannelId = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                Signature = signature
            };

            PendingChannel pendingChannel = new PendingChannel(mocks.Peer.Object, openChannelMessage, revocationKey, 0);

            pendingChannel.Channel = mocks.CreateChannelMock();
            pendingChannel.Channel.LocalCommitmentTxParameters = new CommitmentTransactionParameters();

            mocks.CommTxService.Setup(c => c.IsValidRemoteCommitmentSignature(pendingChannel.Channel, It.IsAny <TransactionSignature>())).Returns(false);
            mocks.Peer.Setup(m => m.Messaging).Returns(new Mock <IMessagingClient>().Object);

            Assert.Throws <ChannelException>(() =>
                                             handler.Handle(mocks.Peer.Object, fundingSignedMessage, pendingChannel));
        }
Beispiel #3
0
        public void Handle(IPeer peer, FundingSignedMessage message, PendingChannel pendingChannel)
        {
            var channel   = pendingChannel.Channel;
            var oldState  = channel.State;
            var signature = SignatureConverter.RawToTransactionSignature(message.Signature);

            if (!_commitmentService.IsValidRemoteCommitmentSignature(channel, signature))
            {
                _channelLoggingService.LogError(channel, LocalChannelError.InvalidCommitmentSignature, $"Remote {peer.NodeAddress} sent us an invalid commitment signature");
                string error = "Invalid commitment transaction signature.";
                channel.State = LocalChannelState.FundingFailed;
                peer.Messaging.Send(new ErrorMessage(message.ChannelId, error));
                throw new ChannelException(error, channel);
            }

            channel.LocalCommitmentTxParameters.RemoteSignature = signature;
            channel.State = LocalChannelState.FundingSigned;
            _channelService.AddChannel(peer, channel);
            _fundingService.BroadcastFundingTransaction(channel);
            _blockchainMonitorService.WatchForTransactionId(channel.FundingTransactionId, (ushort)channel.MinimumDepth);
            _channelService.RemovePendingChannel(pendingChannel);
            _channelLoggingService.LogStateUpdate(channel, oldState);
        }
Beispiel #4
0
        public void HandleTest()
        {
            var mocks         = new ChannelEstablishmentMocks();
            var revocationKey = new ECKeyPair("DD06232AE9A50384A72D85CED6351DCB35C798231D4985615C77D6847F83FC65", true);
            var handler       = new AcceptChannelMessageHandler(mocks.ChannelLoggingService.Object, mocks.FundingService.Object, mocks.CommTxService.Object);

            var localSig = "3045022100a5c01383d3ec646d97e40f44318d49def817fcd61a0ef18008a665b3e151785502203e648efddd5838981ef55ec954be69c4a652d021e6081a100d034de366815e9b".HexToByteArray();

            OpenChannelMessage openChannelMessage = new OpenChannelMessage()
            {
                ChainHash                = NetworkParameters.BitcoinTestnet.ChainHash,
                TemporaryChannelId       = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                FundingSatoshis          = 420000,
                PushMSat                 = 1000,
                DustLimitSatoshis        = 500,
                MaxHtlcValueInFlightMSat = 50000,
                ChannelReserveSatoshis   = 5000,
                HtlcMinimumMSat          = 1100,
                FeeratePerKw             = 150,
                ToSelfDelay              = 133,
                MaxAcceptedHtlcs         = 156,
                FundingPubKey            = new ECKeyPair("0245b02f6672c2342fe3ced57118fcf4a0309327e32c335ce494365eb0d15b7200", false),
                RevocationBasepoint      = new ECKeyPair("02d91224d91760f477df21d24713b713c681b084e508f48dc77ca14db549ba8ceb", false),
                PaymentBasepoint         = new ECKeyPair("022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92", false),
                DelayedPaymentBasepoint  = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false),
                HtlcBasepoint            = new ECKeyPair("029d100efe40aa3f58985fa12bd0f5c75711449ff4d30adca6f1968a2200bbbf1a", false),
                FirstPerCommitmentPoint  = new ECKeyPair("022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92", false),
                ChannelFlags             = 1
            };

            AcceptChannelMessage acceptChannelMessage = new AcceptChannelMessage()
            {
                TemporaryChannelId       = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                DustLimitSatoshis        = 500,
                MaxHtlcValueInFlightMSat = 50000,
                ChannelReserveSatoshis   = 5000,
                HtlcMinimumMSat          = 1100,
                ToSelfDelay             = 133,
                MaxAcceptedHtlcs        = 156,
                FundingPubKey           = new ECKeyPair("0299de4bbf495e5bbeb2456c2beb3f40450a3fa41aaa50819ae201f8ad69226bfe", false),
                RevocationBasepoint     = new ECKeyPair("022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92", false),
                PaymentBasepoint        = new ECKeyPair("02d91224d91760f477df21d24713b713c681b084e508f48dc77ca14db549ba8ceb", false),
                DelayedPaymentBasepoint = new ECKeyPair("0341665cedb568e09f0ab2ab4a28bc2749620deacefb3dce61aac8251c91709d3a", false),
                HtlcBasepoint           = new ECKeyPair("0336439e36e2bc1f264c6d3bc6e12db6256389bef2056c32e6267d6e285c2b2122", false),
                FirstPerCommitmentPoint = new ECKeyPair("039360132ab07e7f56d6782a644233da9c4c24845609fcd302cbedd69f69848358", false)
            };

            PendingChannel pendingChannel = new PendingChannel(mocks.Peer.Object, openChannelMessage, revocationKey, 0);

            FundingTransaction fundingTransactionMock = mocks.CreateFundingTxMock(openChannelMessage.FundingPubKey, acceptChannelMessage.FundingPubKey);

            mocks.FundingService.Setup(f => f.CreateFundingTransaction(openChannelMessage.FundingSatoshis,
                                                                       openChannelMessage.FeeratePerKw, openChannelMessage.FundingPubKey, acceptChannelMessage.FundingPubKey))
            .Returns(fundingTransactionMock);

            mocks.CommTxService.Setup(f => f.CreateInitialCommitmentTransactions(openChannelMessage, acceptChannelMessage, It.IsAny <LocalChannel>(), revocationKey))
            .Callback <OpenChannelMessage, AcceptChannelMessage, LocalChannel, ECKeyPair>((openMessage, acceptMessage, channel, rkey) =>
            {
                channel.RemoteCommitmentTxParameters = new CommitmentTransactionParameters();
                channel.RemoteCommitmentTxParameters.LocalSignature = new TransactionSignature(localSig, SigHash.All);
            });

            FundingCreatedMessage fundingCreatedMessage = handler.Handle(acceptChannelMessage, pendingChannel);

            Assert.Equal("15fadebd5a68d2d9f216a2dc5531c4dbc977eac0c42ab97899e21d549dddbefa", fundingCreatedMessage.FundingTransactionId.ToHex());
            Assert.Equal(new TransactionSignature(localSig, SigHash.All).ToRawSignature().ToHex(), fundingCreatedMessage.Signature.ToHex());
            Assert.Equal(0, fundingCreatedMessage.FundingOutputIndex);
            Assert.Equal("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000", fundingCreatedMessage.TemporaryChannelId.ToHex());
        }
Beispiel #5
0
        public void HandleTest()
        {
            var mocks         = new ChannelEstablishmentMocks();
            var revocationKey = new ECKeyPair("DD06232AE9A50384A72D85CED6351DCB35C798231D4985615C77D6847F83FC65", true);
            var handler       = new FundingMessageSignedHandler(mocks.ChannelLoggingService.Object, mocks.FundingService.Object,
                                                                mocks.CommTxService.Object, mocks.ChannelService.Object, mocks.BlockchainMonitorService.Object);

            OpenChannelMessage openChannelMessage = new OpenChannelMessage()
            {
                ChainHash                = NetworkParameters.BitcoinTestnet.ChainHash,
                TemporaryChannelId       = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                FundingSatoshis          = 420000,
                PushMSat                 = 1000,
                DustLimitSatoshis        = 500,
                MaxHtlcValueInFlightMSat = 50000,
                ChannelReserveSatoshis   = 5000,
                HtlcMinimumMSat          = 1100,
                FeeratePerKw             = 150,
                ToSelfDelay              = 133,
                MaxAcceptedHtlcs         = 156,
                FundingPubKey            = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false),
                RevocationBasepoint      = new ECKeyPair("022ecc432552ff86d053514ffb133d3025fb14c39aa5ae2a5169b0367174cabfa4", false),
                PaymentBasepoint         = new ECKeyPair("029d100efe40aa3f58985fa12bd0f5c75711449ff4d30adca6f1968a2200bbbf1a", false),
                DelayedPaymentBasepoint  = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false),
                HtlcBasepoint            = new ECKeyPair("03d029229db8f594adcd545b4a42acbb1013286908d2905fa05c9a4e2083fe3fe2", false),
                FirstPerCommitmentPoint  = new ECKeyPair("02846726efa57378ad8370acf094f26902a7f1e21903791ef4ab6f989da86679f2", false),
                ChannelFlags             = 1
            };

//            AcceptChannelMessage acceptChannelMessage = new AcceptChannelMessage()
//            {
//                TemporaryChannelId = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
//                DustLimitSatoshis = 500,
//                MaxHtlcValueInFlightMSat = 50000,
//                ChannelReserveSatoshis = 5000,
//                HtlcMinimumMSat = 1100,
//                ToSelfDelay = 133,
//                MaxAcceptedHtlcs = 156,
//                FundingPubKey = new ECKeyPair("0299de4bbf495e5bbeb2456c2beb3f40450a3fa41aaa50819ae201f8ad69226bfe", false),
//                RevocationBasepoint = new ECKeyPair("022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92", false),
//                PaymentBasepoint = new ECKeyPair("02d91224d91760f477df21d24713b713c681b084e508f48dc77ca14db549ba8ceb", false),
//                DelayedPaymentBasepoint = new ECKeyPair("0341665cedb568e09f0ab2ab4a28bc2749620deacefb3dce61aac8251c91709d3a", false),
//                HtlcBasepoint = new ECKeyPair("0336439e36e2bc1f264c6d3bc6e12db6256389bef2056c32e6267d6e285c2b2122", false),
//                FirstPerCommitmentPoint = new ECKeyPair("039360132ab07e7f56d6782a644233da9c4c24845609fcd302cbedd69f69848358", false)
//            };

            var signature = "2f26e967305b4d422116a7c876d338bc4298263f329a54c0d1655f55d594de4955356386addd0bc15ced36dacece0af439694195654c838769583409eeac5d3f".HexToByteArray();

            FundingSignedMessage fundingSignedMessage = new FundingSignedMessage()
            {
                ChannelId = "43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea3309000000".HexToByteArray(),
                Signature = signature
            };

            PendingChannel pendingChannel = new PendingChannel(mocks.Peer.Object, openChannelMessage, revocationKey, 0);

            pendingChannel.Channel = mocks.CreateChannelMock();
            pendingChannel.Channel.LocalCommitmentTxParameters = new CommitmentTransactionParameters();
            pendingChannel.Channel.FundingTransactionId        = "txId";

            mocks.CommTxService.Setup(c => c.IsValidRemoteCommitmentSignature(pendingChannel.Channel, It.IsAny <TransactionSignature>())).Returns(true);

            handler.Handle(mocks.Peer.Object, fundingSignedMessage, pendingChannel);

            Assert.Equal(LocalChannelState.FundingSigned, pendingChannel.Channel.State);
            Assert.Equal(signature, pendingChannel.Channel.LocalCommitmentTxParameters.RemoteSignature.ToRawSignature());

            mocks.FundingService.Verify(f => f.BroadcastFundingTransaction(pendingChannel.Channel), Times.Once);
            mocks.BlockchainMonitorService.Verify(m => m.WatchForTransactionId(pendingChannel.Channel.FundingTransactionId, 3), Times.Once);
            mocks.ChannelService.Verify(c => c.RemovePendingChannel(pendingChannel), Times.Once);
        }