Exemplo n.º 1
0
        private CommitmentTransactionParameters CreateRemoteCommitmentTxParameters(OpenChannelMessage openMessage, AcceptChannelMessage acceptMessage)
        {
            PublicKeyDerivation           publicKeyDerivation           = new PublicKeyDerivation(acceptMessage.FirstPerCommitmentPoint);
            RevocationPublicKeyDerivation revocationPublicKeyDerivation = new RevocationPublicKeyDerivation(acceptMessage.FirstPerCommitmentPoint);
            PublicKeyDerivation           remotePublicKeyDerivation     = new PublicKeyDerivation(openMessage.FirstPerCommitmentPoint);

            CommitmentTransactionParameters parameters = new CommitmentTransactionParameters
            {
                TransactionNumber       = 0,
                HtlcBasepoint           = acceptMessage.HtlcBasepoint,
                HtlcPublicKey           = remotePublicKeyDerivation.Derive(acceptMessage.HtlcBasepoint),
                PaymentBasepoint        = acceptMessage.PaymentBasepoint,
                PaymentPublicKey        = remotePublicKeyDerivation.Derive(acceptMessage.PaymentBasepoint),
                DelayedPaymentBasepoint = acceptMessage.DelayedPaymentBasepoint,
                DelayedPaymentPublicKey = publicKeyDerivation.Derive(acceptMessage.DelayedPaymentBasepoint),
                RevocationBasepoint     = acceptMessage.RevocationBasepoint,
                RevocationPublicKey     = revocationPublicKeyDerivation.DerivePublicKey(openMessage.RevocationBasepoint),
                PerCommitmentKey        = acceptMessage.FirstPerCommitmentPoint,
                FundingKey   = acceptMessage.FundingPubKey,
                ToRemoteMsat = openMessage.FundingSatoshis * 1000 - openMessage.PushMSat,
                ToLocalMsat  = openMessage.PushMSat
            };

            return(parameters);
        }
        public void UpdatePerCommitmentPoint(ECKeyPair commitmentPoint)
        {
            PublicKeyDerivation publicKeyDerivation = new PublicKeyDerivation(commitmentPoint);

            HtlcPublicKey           = publicKeyDerivation.Derive(HtlcBasepoint);
            PaymentPublicKey        = publicKeyDerivation.Derive(PaymentBasepoint);
            DelayedPaymentPublicKey = publicKeyDerivation.Derive(DelayedPaymentBasepoint);
            PerCommitmentKey        = commitmentPoint;
        }
Exemplo n.º 3
0
        public void DeriveTest5()
        {
            var basepoint           = new ECKeyPair("0303a12202e9020ebc9995391a286b2c364bfbe1a107d4fac772d148e932d994e2", false);
            var perCommitmentPoint  = new ECKeyPair("02b0c45d17a7d72b02ace2fd4fda33f8a9d0274560ccbec6f9c43d6af17e309b47", false);
            var publicKeyDerivation = new PublicKeyDerivation(perCommitmentPoint);

            var resultKey = publicKeyDerivation.Derive(basepoint);

            Assert.Equal("02884DF47298A3B545E0DD278755C09A8D1D18A9C30E71F09CE80EC75939415068".ToLower(), resultKey.PublicKeyCompressed.ToHex());
        }
Exemplo n.º 4
0
        public void DeriveTest4()
        {
            var basepoint           = new ECKeyPair("03f4b6c6ada5f409c1a3de16edec3c84d93576c5c206d4e646f96e03ba9fa636e0", false);
            var perCommitmentPoint  = new ECKeyPair("02b0c45d17a7d72b02ace2fd4fda33f8a9d0274560ccbec6f9c43d6af17e309b47", false);
            var publicKeyDerivation = new PublicKeyDerivation(perCommitmentPoint);

            var resultKey = publicKeyDerivation.Derive(basepoint);

            Assert.Equal("02201305100A05C0408392869DFF1105702437BD8FB07B3AA49DA84C2FC2383427".ToLower(), resultKey.PublicKeyCompressed.ToHex());
        }
Exemplo n.º 5
0
        public void DeriveTest3()
        {
            var basepoint           = new ECKeyPair("03a5d8c82ddbb1c42379abb9f09b7cafb96148351212a101a4f3ff1d113735be5e", false);
            var perCommitmentPoint  = new ECKeyPair("031929dce9f9d1953000afab33d03517880ab37b8a15d0613401cf7a805737d537", false);
            var publicKeyDerivation = new PublicKeyDerivation(perCommitmentPoint);

            var resultKey = publicKeyDerivation.Derive(basepoint);

            Assert.Equal("0247f4bedc1d76bd3f46812a61c5ed235e2f76493b3ea1aa7872e7ca4ac582f8b1".ToLower(), resultKey.PublicKeyCompressed.ToHex());
        }
Exemplo n.º 6
0
        public void DeriveTest2()
        {
            var basepoint           = new ECKeyPair("0319c56699c3ac4e7f2c3f1475e6374e08dfabafbbac852cd1578c9c45a61ded99", false);
            var perCommitmentPoint  = new ECKeyPair("031929dce9f9d1953000afab33d03517880ab37b8a15d0613401cf7a805737d537", false);
            var publicKeyDerivation = new PublicKeyDerivation(perCommitmentPoint);

            var resultKey = publicKeyDerivation.Derive(basepoint);

            Assert.Equal("034DF2D703AD06243285F07B7CBFF7B309F996961244818EBF4B8BD58CF6862CAE".ToLower(), resultKey.PublicKeyCompressed.ToHex());
        }
Exemplo n.º 7
0
        public void DeriveTest()
        {
            var basePoint           = new ECKeyPair("036d6caac248af96f6afa7f904f550253a0f3ef3f5aa2fe6838a95b216691468e2", false);
            var perCommitmentPoint  = new ECKeyPair("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486", false);
            var publicKeyDerivation = new PublicKeyDerivation(perCommitmentPoint);

            var resultKey = publicKeyDerivation.Derive(basePoint);

            Assert.Equal("0235f2dbfaa89b57ec7b055afe29849ef7ddfeb1cefdb9ebdc43f5494984db29e5", resultKey.PublicKeyCompressed.ToHex());
        }
        public void ValidateTest()
        {
            CommitmentTransactionBuilder builder = new CommitmentTransactionBuilder();

/*
 *          OpenChannelMessage(32)
 *          Chain Hash (Hash (32 Bytes), 32):            43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000
 *          Temporary Channel ID (Channel ID, 32):       9b33024da3d0f725434cbaa91dfbdcd18d8e372d879d2a0c94d5e23836786715
 *          Funding Satoshis (Unsigned Long, 8):         25000 (0x00000000000061a8)
 *          Push mSat (Unsigned Long, 8):                0 (0x0000000000000000)
 *          Dust Limit Satoshis (Unsigned Long, 8):      546 (0x0000000000000222)
 *          Max HTLC Value In Flight mSat (Unsigned Long, 8): 5000000000 (0x000000012a05f200)
 *          Channel Reserve Satoshis (Unsigned Long, 8): 2500 (0x00000000000009c4)
 *          HTLC Minimum mSat (Unsigned Long, 8):        1000 (0x00000000000003e8)
 *          Feerate Per KW (Unsigned Integer, 4):        1011 (0x000003f3)
 *          To Self Delay (Unsigned Short, 2):           144 (0x0090)
 *          Max Accepted HTLCs (Unsigned Short, 2):      483 (0x01e3)
 *          Funding PubKey (Public Key, 33):             0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5
 *          Revocation Basepoint (Public Key, 33):       022ecc432552ff86d053514ffb133d3025fb14c39aa5ae2a5169b0367174cabfa4
 *          Payment Basepoint (Public Key, 33):          029d100efe40aa3f58985fa12bd0f5c75711449ff4d30adca6f1968a2200bbbf1a
 *          Delayed Payment Basepoint (Public Key, 33):  0245b02f6672c2342fe3ced57118fcf4a0309327e32c335ce494365eb0d15b7200
 *          HTLC Basepoint (Public Key, 33):             03d029229db8f594adcd545b4a42acbb1013286908d2905fa05c9a4e2083fe3fe2
 *          First Per Commitment Point (Public Key, 33): 02846726efa57378ad8370acf094f26902a7f1e21903791ef4ab6f989da86679f2
 *          Channel Flags (Byte, 1):                     00
 *          Shutdown ScriptPubKey (Variable Array, 25):  76a9141c60596620b0b9966400cb710b8da6de5a80d68588ac
 *
 *          AcceptChannelMessage(33)
 *          Temporary Channel ID (Channel ID, 32):       9b33024da3d0f725434cbaa91dfbdcd18d8e372d879d2a0c94d5e23836786715
 *          Dust Limit Satoshis (Unsigned Long, 8):      573 (0x000000000000023d)
 *          Max HTLC Value In Flight mSat (Unsigned Long, 8): 24750000 (0x000000000179a7b0)
 *          Channel Reserve Satoshis (Unsigned Long, 8): 546 (0x0000000000000222)
 *          HTLC Minimum mSat (Unsigned Long, 8):        1000 (0x00000000000003e8)
 *          Minimum Depth (Unsigned Integer, 4):         3 (0x00000003)
 *          To Self Delay (Unsigned Short, 2):           144 (0x0090)
 *          Max Accepted HTLCs (Unsigned Short, 2):      483 (0x01e3)
 *          Funding PubKey (Public Key, 33):             0299de4bbf495e5bbeb2456c2beb3f40450a3fa41aaa50819ae201f8ad69226bfe
 *          Revocation Basepoint (Public Key, 33):       022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92
 *          Payment Basepoint (Public Key, 33):          02d91224d91760f477df21d24713b713c681b084e508f48dc77ca14db549ba8ceb
 *          Delayed Payment Basepoint (Public Key, 33):  0341665cedb568e09f0ab2ab4a28bc2749620deacefb3dce61aac8251c91709d3a
 *          HTLC Basepoint (Public Key, 33):             0336439e36e2bc1f264c6d3bc6e12db6256389bef2056c32e6267d6e285c2b2122
 *          First Per Commitment Point (Public Key, 33): 039360132ab07e7f56d6782a644233da9c4c24845609fcd302cbedd69f69848358
 *
 *          FundingCreatedMessage(34)
 *          Temporary Channel ID (Channel ID, 32):       9b33024da3d0f725434cbaa91dfbdcd18d8e372d879d2a0c94d5e23836786715
 *          Funding Transaction ID (Transaction ID, 32): 282ea2263611611169ee505fc83979ecaf1ad99b565f9d64c1b1fb804c427da3
 *          Funding Output Index (Unsigned Short, 2):    1 (0x0001)
 *          Signature (Signature, 64):                   4fed2d370e0166934d63d4b0ca5c523839ca12bc2acdc5c3238c33a1460e409d39a5d267d7132abe3b8c6bf46c2bf456a47b4462931558af371dfd1f7312f0a5
 *
 *          FundingSignedMessage(35)
 *          Channel ID (Channel ID, 32):                 282ea2263611611169ee505fc83979ecaf1ad99b565f9d64c1b1fb804c427da2
 *          Signature (Signature, 64):                   2f26e967305b4d422116a7c876d338bc4298263f329a54c0d1655f55d594de4955356386addd0bc15ced36dacece0af439694195654c838769583409eeac5d3f
 *
 */

            // Local
            ECKeyPair fundingKeyPrivateKey         = new ECKeyPair("DD06232AE9A50384A72D85CED6351DCB35C798231D4985615C77D6847F83FC65", true);
            ECKeyPair localFundingPubKey           = new ECKeyPair("0250d049da6b5832a9f2416df3b0db52da127426c2b70a35ca9c270a72f3f840b5", false);
            ECKeyPair localPaymentBasepoint        = new ECKeyPair("029d100efe40aa3f58985fa12bd0f5c75711449ff4d30adca6f1968a2200bbbf1a", false);
            ECKeyPair localDelayedPaymentBasepoint = new ECKeyPair("0245b02f6672c2342fe3ced57118fcf4a0309327e32c335ce494365eb0d15b7200", false);
            ECKeyPair localPerCommitmentPoint      = new ECKeyPair("02846726efa57378ad8370acf094f26902a7f1e21903791ef4ab6f989da86679f2", false);

            // Remote
            ECKeyPair remoteRevocationBasepoint = new ECKeyPair("022b2aa486f5a8aca1898824ac3b2a8a15c92de813362846b992f94d923b143f92", false);
            ECKeyPair remotePaymentBasepoint    = new ECKeyPair("02d91224d91760f477df21d24713b713c681b084e508f48dc77ca14db549ba8ceb", false);
            ECKeyPair remoteFundingKey          = new ECKeyPair("0299de4bbf495e5bbeb2456c2beb3f40450a3fa41aaa50819ae201f8ad69226bfe", false);

            // Derive Local
            RevocationPublicKeyDerivation revocationPublicKeyDerivation = new RevocationPublicKeyDerivation(localPerCommitmentPoint);
            PublicKeyDerivation           publicKeyDerivation           = new PublicKeyDerivation(localPerCommitmentPoint);

            ECKeyPair localDelayedPaymentPubkey = publicKeyDerivation.Derive(localDelayedPaymentBasepoint);
            ECKeyPair localRevocationPubkey     = revocationPublicKeyDerivation.DerivePublicKey(remoteRevocationBasepoint);

            builder.CommitmentTxParams = new CommitmentTransactionParameters();
            builder.CommitmentTxParams.TransactionNumber       = 0;
            builder.CommitmentTxParams.RevocationPublicKey     = localRevocationPubkey;
            builder.CommitmentTxParams.DelayedPaymentPublicKey = localDelayedPaymentPubkey;
            builder.CommitmentTxParams.PaymentBasepoint        = localPaymentBasepoint;
            builder.CommitmentTxParams.FundingKey = localFundingPubKey;
            builder.FeeratePerKw = 1011;
            builder.CommitmentTxParams.ToLocalMsat  = 25000000;
            builder.CommitmentTxParams.ToRemoteMsat = 0;
            builder.FundingTransactionOutputIndex   = 1;
            builder.FundingAmount          = 25000;
            builder.FundingTransactionHash = "a37d424c80fbb1c1649d5f569bd91aafec7939c85f50ee691161113626a22e28";
            builder.IsFunder          = true;
            builder.ChannelParameters = new ChannelParameters();
            builder.ChannelParameters.DustLimitSatoshis = 546;
            builder.ChannelParameters.ToSelfDelay       = 144;
            builder.RemotePaymentBasePoint = remotePaymentBasepoint;
            builder.RemoteFundingPubKey    = remoteFundingKey;
            builder.Network = NBitcoin.Network.TestNet;

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

            Assert.True(builder.IsValidSignature(signature, fundingKeyPrivateKey));
        }