Exemplo n.º 1
0
        public void ScriptSignTest()
        {
            ExtPrivkey  privkey   = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            Transaction tx        = new Transaction("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000");
            OutPoint    outpoint2 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 3);
            long        amount    = 25000;

            Privkey privkey21      = privkey.DerivePrivkey(21).GetPrivkey();
            Privkey privkey22      = privkey.DerivePrivkey(22).GetPrivkey();
            Privkey privkey23      = privkey.DerivePrivkey(23).GetPrivkey();
            Pubkey  pubkey21       = privkey21.GetPubkey();
            Pubkey  pubkey22       = privkey22.GetPubkey();
            Pubkey  pubkey23       = privkey23.GetPubkey();
            Script  multisigScript = Script.CreateMultisigScript(2, new[] {
                pubkey21, pubkey22, pubkey23,
            });
            SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false);
            ByteData          sighash     = tx.GetSignatureHash(outpoint2, CfdHashType.P2shP2wsh, multisigScript, amount, sighashType);
            SignParameter     sig22       = privkey22.CalculateEcSignature(sighash);
            SignParameter     sig23       = privkey23.CalculateEcSignature(sighash);

            sig22.SetDerEncode(sighashType);
            sig22.SetRelatedPubkey(pubkey22);
            sig23.SetDerEncode(sighashType);
            sig23.SetRelatedPubkey(pubkey23);
            tx.AddScriptSign(outpoint2, CfdHashType.P2shP2wsh, new[] { new SignParameter(), sig22, sig23 }, multisigScript);
            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff0100000000000000000000000000000000000000000000000000000000000000030000002322002064a0e02e723ce71d8f18441a39bedd5cefc9c5411c3045614c34bba1a8fbd94fffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c0004004730440220749cbe5080a3ce49c2a89f897be537b2b5449b75c64b57030dea1859b22c183f02200573f5be5170bfe4ca617edec0eb021638dd78b90209bbd8eede8a9e8138a32c01473044022019105df75884ff34111282f32c22986db295596983a87bf0df1d16905b4f9a50022075f8a2c8e3335a4265265b428df185fb045d9614ed1b08929bfa9f3f9d294a72016952210334bd4f1bab7f3e6f6bfc4a4aeaa890b858a9a146c6bd6bc5a3fbc00a12524ca72103ff743075c59596729d74b79694ca99b2c57bed6a77a06871b123b6e0d729823021036759d0dc7623e781de940a9bc9162f69c6ad68cc5be1c748e960ae4613e658e053ae00000000",
                         tx.ToHexString());
        }
Exemplo n.º 2
0
        public void CreateRawTransactionTest()
        {
            ExtPrivkey privkey = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            Address    addr1   = new Address(privkey.DerivePubkey(1).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            Address    addr2   = new Address(privkey.DerivePubkey(2).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            Address    addr3   = new Address(privkey.DerivePubkey(3).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);

            OutPoint    outpoint1 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 2);
            OutPoint    outpoint2 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 3);
            Transaction tx        = new Transaction("02000000000000000000", new[] {
                new TxIn(outpoint1),
                new TxIn(outpoint2),
            }, new[] {
                new TxOut(10000, addr1.GetLockingScript()),
                new TxOut(10000, addr2.GetLockingScript()),
            });

            tx.AddTxOut(50000, addr3);
            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000",
                         tx.ToHexString());

            Privkey           privkey1    = privkey.DerivePrivkey(11).GetPrivkey();
            Pubkey            pubkey1     = privkey1.GetPubkey();
            SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false);
            ByteData          sighash     = tx.GetSignatureHash(outpoint1, CfdHashType.P2wpkh, pubkey1, 50000, sighashType);
            SignParameter     signature   = privkey1.CalculateEcSignature(sighash);

            signature.SetDerEncode(sighashType);
            tx.AddSign(outpoint1, CfdHashType.P2wpkh, signature, true);
            tx.AddSign(outpoint1, CfdHashType.P2wpkh, new SignParameter(pubkey1.ToHexString()), false);
            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000",
                         tx.ToHexString());

            Address addr11 = new Address(pubkey1, CfdAddressType.P2wpkh, CfdNetworkType.Regtest);

            try
            {
                tx.VerifySign(outpoint1, addr11, addr11.GetAddressType(), 50000);
            }
            catch (Exception e)
            {
                Assert.Null(e);
            }

            string json = Transaction.DecodeRawTransaction(tx);

            output.WriteLine(json);
        }
Exemplo n.º 3
0
        public void PrivkeySignTest()
        {
            ExtPrivkey  privkey   = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            Transaction tx        = new Transaction("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000");
            OutPoint    outpoint1 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 2);

            Privkey           privkey1    = privkey.DerivePrivkey(11).GetPrivkey();
            SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false);

            tx.AddSignWithPrivkeySimple(outpoint1, CfdHashType.P2wpkh, privkey1, 50000, sighashType);
            // output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000",
                         tx.ToHexString());
        }
Exemplo n.º 4
0
        public void ExtPrivkeyTestnetTest()
        {
            ExtPrivkey key = new ExtPrivkey("tprv8ZgxMBicQKsPeWHBt7a68nPnvgTnuDhUgDWC8wZCgA8GahrQ3f3uWpq7wE7Uc1dLBnCe1hhCZ886K6ND37memRDWqsA9HgSKDXtwh2Qxo6J");

            Assert.Equal("tprv8ZgxMBicQKsPeWHBt7a68nPnvgTnuDhUgDWC8wZCgA8GahrQ3f3uWpq7wE7Uc1dLBnCe1hhCZ886K6ND37memRDWqsA9HgSKDXtwh2Qxo6J",
                         key.ToString());
            Assert.Equal("cbedc75b0d6412c85c79bc13875112ef912fd1e756631b5a00330866f22ff184",
                         key.GetPrivkey().ToHexString());
            Assert.Equal("a3fa8c983223306de0f0f65e74ebb1e98aba751633bf91d5fb56529aa5c132c1",
                         key.GetChainCode().ToHexString());
            Assert.Equal("04358394", key.GetVersion().ToHexString());
            Assert.Equal("00000000", key.GetFingerprint().ToHexString());
            Assert.Equal((uint)0, key.GetChildNumber());
            Assert.Equal((uint)0, key.GetDepth());
            Assert.Equal(CfdNetworkType.Testnet, key.GetNetworkType());
            Assert.True(key.IsValid());
        }
Exemplo n.º 5
0
        public void DerivePrivkeyTest()
        {
            ExtPrivkey parent = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            ExtPrivkey key    = parent.DerivePrivkey("0/44");

            Assert.Equal("xprvA5P4YtgFjzqM4QpXJZ8Zr7Wkhng7ugTybA3KWMAqDfAamqu5nqJ3zKRhB29cxuqCc8hPagZcN5BsuoXx4Xn7iYHnQvEdyMwZRFgoJXs8CDN",
                         key.ToString());
            Assert.Equal((uint)44, key.GetChildNumber());
            Assert.Equal((uint)6, key.GetDepth());
            Assert.Equal(CfdNetworkType.Mainnet, key.GetNetworkType());
            Assert.True(key.IsValid());

            ExtPrivkey key1 = parent.DerivePrivkey(0).DerivePrivkey(44);
            ExtPrivkey key2 = parent.DerivePrivkey(new uint[] { 0, 44 });

            Assert.Equal(key.ToString(), key1.ToString());
            Assert.Equal(key.ToString(), key2.ToString());
            Assert.True(key.Equals(key2));
        }
Exemplo n.º 6
0
        public void DerivePubkeyTest()
        {
            ExtPrivkey parent      = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            ExtPubkey  key         = parent.DerivePrivkey("0/44'").GetExtPubkey();
            uint       childNumber = 0x8000002c;

            Assert.Equal("xpub6JNQxQDHv2vcUQiXjggbaGYZg3nmxX6ojMcJPSs4KfLSLnMBCg8VbJUh5n4to2SwLWXdSXnHBkUQx1fVnJ9oKYjPPYAQehjWRpx6ErQyykX",
                         key.ToString());
            Assert.Equal(childNumber, key.GetChildNumber());
            Assert.Equal((uint)6, key.GetDepth());
            Assert.Equal(CfdNetworkType.Mainnet, key.GetNetworkType());
            Assert.True(key.IsValid());

            ExtPubkey key1 = parent.DerivePrivkey(0).DerivePrivkey(childNumber).GetExtPubkey();
            ExtPubkey key2 = parent.DerivePrivkey(new uint[] { 0, childNumber }).GetExtPubkey();

            Assert.Equal(key.ToString(), key1.ToString());
            Assert.Equal(key.ToString(), key2.ToString());
            Assert.True(key.Equals(key2));
        }
Exemplo n.º 7
0
        public void GeneratePrivkeyTest()
        {
            string[] mnemonicEn = { "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "about" };
            HDWallet hdWallet   = new HDWallet(mnemonicEn, "TREZOR");

            ExtPrivkey privkey = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet);

            Assert.Equal("xprv9s21ZrQH143K3h3fDYiay8mocZ3afhfULfb5GX8kCBdno77K4HiA15Tg23wpbeF1pLfs1c5SPmYHrEpTuuRhxMwvKDwqdKiGJS9XFKzUsAF",
                         privkey.ToString());

            privkey = hdWallet.GeneratePrivkey(CfdNetworkType.Testnet);
            Assert.Equal("tprv8ZgxMBicQKsPeWHBt7a68nPnvgTnuDhUgDWC8wZCgA8GahrQ3f3uWpq7wE7Uc1dLBnCe1hhCZ886K6ND37memRDWqsA9HgSKDXtwh2Qxo6J",
                         privkey.ToString());

            ExtPrivkey child = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, "0/44");

            Assert.Equal("xprv9wiYQ21HNxnQ8FxBjbYjJy5ckuEZ6CAFsKdHEnfkRcw5pZbXAFSturoZugNE6ZpVSu6kdrYw752chFPAbPMXZ62ZLfYwLMHdzMVXqwnfRFn",
                         child.ToString());

            ExtPrivkey child11 = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, 0);
            ExtPrivkey child12 = child11.DerivePrivkey(44);

            Assert.Equal(child.ToString(), child12.ToString());

            ExtPrivkey child2 = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, new uint[] { 0, 44 });

            Assert.Equal(child.ToString(), child2.ToString());

            child = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, "0'/44'");
            Assert.Equal("xprv9xcgxExFiq8qWLdxFHXpEZF8VH7Qr9YDZb8c7vMsqygWk2YGTBgSnDtm1LESskfAJqGMWkWWGagNCSbHdVgA8EFxSbfAQTKSD1z4iJ8qHtq",
                         child.ToString());

            child11 = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, 0x80000000);
            child12 = child11.DerivePrivkey(44 + 0x80000000);
            Assert.Equal(child.ToString(), child12.ToString());

            child2 = hdWallet.GeneratePrivkey(CfdNetworkType.Mainnet, new[] { 0x80000000, 44 + 0x80000000 });
            Assert.Equal(child.ToString(), child2.ToString());
        }
Exemplo n.º 8
0
        public void ExtPrivkeyMainnetTest()
        {
            ExtPrivkey emptyKey = new ExtPrivkey();

            Assert.False(emptyKey.IsValid());

            ExtPrivkey key = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");

            Assert.Equal("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV",
                         key.ToString());
            Assert.Equal("73a2361673d25f998d1e9d94aabdeba8ac1ddd4628bc4f55341397d263bd560c",
                         key.GetPrivkey().ToHexString());
            Assert.Equal("xpub6DsNDJWpxZBXsbWsCy1VeBY8xf6hZBgznDTXSnp3FregxWoWfGsvtQ9j5wBJNPebZXD5YmhpQBV7nVjhUsUgkG9R7yE31mh6sVh2w854a1o",
                         key.GetExtPubkey().ToString());
            Assert.Equal("28009126a24557d32ff2c5da21850dd06529f34faed53b4a3552b5ed4bda35d5",
                         key.GetChainCode().ToHexString());
            Assert.Equal("0488ade4", key.GetVersion().ToHexString());
            Assert.Equal("2da711a5",
                         key.GetFingerprint().ToHexString());
            Assert.Equal((uint)0, key.GetChildNumber());
            Assert.Equal((uint)4, key.GetDepth());
            Assert.Equal(CfdNetworkType.Mainnet, key.GetNetworkType());
            Assert.True(key.IsValid());
        }
Exemplo n.º 9
0
        public void GetTxInfoTest()
        {
            ExtPrivkey privkey = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV");
            Address    addr1   = new Address(privkey.DerivePubkey(1).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            Address    addr2   = new Address(privkey.DerivePubkey(2).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            Address    addr3   = new Address(privkey.DerivePubkey(3).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);

            OutPoint outpoint1 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 2);
            OutPoint outpoint2 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 3);
            var      txins     = new[] {
                new TxIn(outpoint1),
                new TxIn(outpoint2),
            };
            var txouts = new[] {
                new TxOut(10000, addr1.GetLockingScript()),
                new TxOut(10000, addr2.GetLockingScript()),
            };
            Transaction tx = new Transaction("02000000000000000000", txins, txouts);

            tx.AddTxOut(50000, addr3);
            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000",
                         tx.ToHexString());

            Privkey           privkey1    = privkey.DerivePrivkey(11).GetPrivkey();
            SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false);

            tx.AddSignWithPrivkeySimple(outpoint1, CfdHashType.P2wpkh, privkey1, 50000, sighashType);
            // output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000",
                         tx.ToHexString());

            Txid txid = tx.GetTxid();

            output.WriteLine("txid: " + txid.ToHexString());
            Assert.Equal("67e1878d1621e77e166bed9d726bff27b2afcde9eb3dbb1ae3088d0387f40be4",
                         txid.ToHexString());
            Txid wtxid = tx.GetWtxid();

            output.WriteLine("wtxid: " + wtxid.ToHexString());
            Assert.Equal("24c66461b4b38c750fa4528d0cf3aea9a13d3156c0a73cfd6fca6958523b97f7",
                         wtxid.ToHexString());
            Assert.Equal((uint)295, tx.GetSize());
            Assert.Equal((uint)213, tx.GetVsize());
            Assert.Equal((uint)850, tx.GetWeight());
            Assert.Equal((uint)2, tx.GetVersion());
            Assert.Equal((uint)0, tx.GetLockTime());

            Assert.Equal((uint)2, tx.GetTxInCount());
            Assert.Equal((uint)3, tx.GetTxOutCount());
            Assert.Equal((uint)1, tx.GetTxInIndex(outpoint2));
            Assert.Equal((uint)2, tx.GetTxOutIndex(addr3));
            Assert.Equal((uint)1, tx.GetTxOutIndex(addr2.GetLockingScript()));

            Assert.True(outpoint2.Equals(tx.GetTxIn(outpoint2).OutPoint));
            Assert.True(outpoint2.Equals(tx.GetTxIn(1).OutPoint));
            Assert.True(outpoint2.Equals(tx.GetTxInList()[1].OutPoint));

            Assert.True(addr2.GetLockingScript().Equals(tx.GetTxOut(addr2).ScriptPubkey));
            Assert.True(addr2.GetLockingScript().Equals(tx.GetTxOut(addr2.GetLockingScript()).ScriptPubkey));
            Assert.True(addr2.GetLockingScript().Equals(tx.GetTxOut(1).ScriptPubkey));
            Assert.True(addr2.GetLockingScript().Equals(tx.GetTxOutList()[1].ScriptPubkey));
        }