Exemplo n.º 1
0
        public void FundRawTransactionRegtestAddressTest()
        {
            UtxoData[] utxos    = GetBitcoinBnbUtxoList(CfdNetworkType.Regtest);
            ExtPubkey  key      = new ExtPubkey("xpub661MyMwAqRbcGB88KaFbLGiYAat55APKhtWg4uYMkXAmfuSTbq2QYsn9sKJCj1YqZPafsboef4h4YbXXhNhPwMbkHTpkf3zLhx7HvFw1NDy");
            Address    setAddr1 = new Address(key.DerivePubkey(11).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            Address    setAddr2 = new Address(key.DerivePubkey(12).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);

            Transaction tx = new Transaction(2, 0, null, new[] {
                new TxOut(10000000, setAddr1.GetLockingScript()),
                new TxOut(4000000, setAddr2.GetLockingScript()),
            });

            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("02000000000280969800000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00093d00000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000",
                         tx.ToHexString());

            Address addr1    = new Address(key.DerivePubkey(1).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest);
            string  usedAddr = tx.FundRawTransaction(null, utxos, addr1.ToAddressString(), 20.0);

            output.WriteLine("tx: " + tx.ToHexString());

            Assert.Equal("02000000010af4768e14f820cb9063f55833b5999119e53390ecf4bf181842909b11d0974d0000000000ffffffff0380969800000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00093d00000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470ce47f19000000000016001478eb9fc2c9e1cdf633ecb646858ba862b21384ab00000000",
                         tx.ToHexString());
            output.WriteLine(Transaction.DecodeRawTransaction(tx));
            Assert.Equal(addr1.ToAddressString(), usedAddr);
            Assert.Equal(3860, tx.GetLastTxFee());
        }
Exemplo n.º 2
0
        public void DerivePubkeyTest()
        {
            ExtPubkey parent = new ExtPubkey("tpubDBwZbsX7C1m4tfHxHSFBvvuasqMxzMvSNM5yuAWz6kAfCATAgegvrtGdnxkqfr8wwRZi5d9fJHXqE8EFTSogTXd3xVx3GUFy9Xcg8dufREz");
            ExtPubkey key    = parent.DerivePubkey("0/44");

            Assert.Equal("tpubDF7yNiHQHdfns9Mc3XM7PYcS2dqrPqcit3FLkebvHxS4atZxifANou2KTvpQQQP82ANDCkPc5MPQZ28pjYGgmDXGy1iyzaiX6MTBv8i4cua",
                         key.ToString());
            Assert.Equal("03f1e767c0555ce0105b2a76d0f8b19b6d33a147f82f75a05c4c09580c39694fd3",
                         key.GetPubkey().ToHexString());
            Assert.Equal("839fb0d66f1887db167cdc530ab98e871d8b017ebcb198568874b6c98516364e",
                         key.GetChainCode().ToHexString());
            Assert.Equal("043587cf", key.GetVersion().ToHexString());
            Assert.Equal("a53a8ff3", key.GetFingerprint().ToHexString());
            Assert.Equal((uint)44, key.GetChildNumber());
            Assert.Equal((uint)4, key.GetDepth());
            Assert.Equal(CfdNetworkType.Testnet, key.GetNetworkType());
            Assert.True(key.IsValid());

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

            Assert.Equal(key.ToString(), key1.ToString());
            Assert.Equal(key.ToString(), key2.ToString());
            Assert.True(key.Equals(key2));
        }
Exemplo n.º 3
0
        public void EstimateFeeTest()
        {
            // p2sh-p2wpkh
            UtxoData[] utxos    = GetBitcoinBnbUtxoList(CfdNetworkType.Mainnet);
            ExtPubkey  key      = new ExtPubkey("xpub661MyMwAqRbcGB88KaFbLGiYAat55APKhtWg4uYMkXAmfuSTbq2QYsn9sKJCj1YqZPafsboef4h4YbXXhNhPwMbkHTpkf3zLhx7HvFw1NDy");
            Address    setAddr1 = new Address(key.DerivePubkey(11).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Mainnet);
            Address    setAddr2 = new Address(key.DerivePubkey(12).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Mainnet);

            // amount = 85062500 + 39062500 = 124125000
            long        amount    = utxos[1].GetAmount() + utxos[2].GetAmount();
            long        feeAmount = 5000;
            long        amount1   = 100000000;
            long        amount2   = amount - amount1 - feeAmount;
            Transaction tx        = new Transaction(2, 0, new[] {
                new TxIn(utxos[1].GetOutPoint()),
                new TxIn(utxos[2].GetOutPoint()),
            }, new[] {
                new TxOut(amount1, setAddr1.GetLockingScript()),
                new TxOut(amount2, setAddr2.GetLockingScript()),
            });

            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("02000000020a9a33750a810cd384ca5d93b09513f1eb5d93c669091b29eef710d2391ff7300000000000ffffffff0a9bf51e0ac499391efd9426e2c909901edd74a97d2378b49c8832c491ad1e9e0000000000ffffffff0200e1f505000000001600144352a1a6e86311f22274f7ebb2746de21b09b15dc00a7001000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000",
                         tx.ToHexString());

            FeeData feeData = tx.EstimateFee(new[] { utxos[1], utxos[2] }, 10.0);

            Assert.Equal(720, feeData.TxOutFee);
            Assert.Equal(1800, feeData.UtxoFee);
        }
Exemplo n.º 4
0
        public void FundRawTransactionExistTxInTest()
        {
            // p2sh-p2wpkh
            UtxoData[] utxos    = GetBitcoinBnbUtxoList(CfdNetworkType.Mainnet);
            ExtPubkey  key      = new ExtPubkey("xpub661MyMwAqRbcGB88KaFbLGiYAat55APKhtWg4uYMkXAmfuSTbq2QYsn9sKJCj1YqZPafsboef4h4YbXXhNhPwMbkHTpkf3zLhx7HvFw1NDy");
            Address    setAddr1 = new Address(key.DerivePubkey(11).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Mainnet);
            Address    setAddr2 = new Address(key.DerivePubkey(12).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Mainnet);

            // amount = 85062500 + 39062500 = 124125000
            long        amount1 = 100000000;
            long        amount2 = 100000000;
            Transaction tx      = new Transaction(2, 0, new[] {
                new TxIn(utxos[1].GetOutPoint()),
                new TxIn(utxos[2].GetOutPoint()),
            }, new[] {
                new TxOut(amount1, setAddr1.GetLockingScript()),
                new TxOut(amount2, setAddr2.GetLockingScript()),
            });

            output.WriteLine("tx:\n" + tx.ToHexString());
            Assert.Equal("02000000020a9a33750a810cd384ca5d93b09513f1eb5d93c669091b29eef710d2391ff7300000000000ffffffff0a9bf51e0ac499391efd9426e2c909901edd74a97d2378b49c8832c491ad1e9e0000000000ffffffff0200e1f505000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00e1f505000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000",
                         tx.ToHexString());

            UtxoData[] inputUtxos = new[] {
                utxos[1],
                utxos[2],
            };
            Address addr1    = new Address(key.DerivePubkey(1).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Mainnet);
            double  feeRate  = 20.0;
            string  usedAddr = tx.FundRawTransaction(inputUtxos, utxos, addr1.ToAddressString(), feeRate);

            output.WriteLine("tx: " + tx.ToHexString());

            Assert.Equal("02000000030a9a33750a810cd384ca5d93b09513f1eb5d93c669091b29eef710d2391ff7300000000000ffffffff0a9bf51e0ac499391efd9426e2c909901edd74a97d2378b49c8832c491ad1e9e0000000000ffffffff0a503dbd4f8f2b064c70e048b21f93fe4584174478abf5f44747932cd21da87c0000000000ffffffff0300e1f505000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00e1f505000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c0831b8040000000016001478eb9fc2c9e1cdf633ecb646858ba862b21384ab00000000",
                         tx.ToHexString());
            output.WriteLine(Transaction.DecodeRawTransaction(tx));
            Assert.Equal(addr1.ToAddressString(), usedAddr);
            Assert.Equal(7460, tx.GetLastTxFee());

            UtxoData[] feeUtxos = new[] {
                utxos[1],
                utxos[2],
                utxos[0],
            };
            FeeData feeData = tx.EstimateFee(feeUtxos, feeRate);

            Assert.Equal(7460, feeData.TxOutFee + feeData.UtxoFee);
            Assert.Equal(2060, feeData.TxOutFee);
            Assert.Equal(5400, feeData.UtxoFee);
        }
Exemplo n.º 5
0
        public void GeneratePubkeyTest()
        {
            string[] mnemonicEn = { "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "abandon", "about" };
            HDWallet hdWallet   = new HDWallet(mnemonicEn, "TREZOR");

            ExtPubkey pubkey = hdWallet.GeneratePubkey(CfdNetworkType.Mainnet);

            Assert.Equal("xpub661MyMwAqRbcGB88KaFbLGiYAat55APKhtWg4uYMkXAmfuSTbq2QYsn9sKJCj1YqZPafsboef4h4YbXXhNhPwMbkHTpkf3zLhx7HvFw1NDy",
                         pubkey.ToString());

            pubkey = hdWallet.GeneratePubkey(CfdNetworkType.Testnet);
            Assert.Equal("tpubD6NzVbkrYhZ4XyJymmEgYC3uVhyj4YtPFX6yRTbW6RvfRC7Ag3sVhKSz7MNzFWW5MJ7aVBKXCAX7En296EYdpo43M4a4LaeaHuhhgHToSJF",
                         pubkey.ToString());

            ExtPubkey child = hdWallet.GeneratePubkey(CfdNetworkType.Mainnet, "0/44");

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

            ExtPubkey child11 = hdWallet.GeneratePubkey(CfdNetworkType.Mainnet, 0);
            ExtPubkey child12 = child11.DerivePubkey(44);

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

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

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

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

            child2 = hdWallet.GeneratePubkey(CfdNetworkType.Mainnet, new[] { 0x80000000, 44 + 0x80000000 });
            Assert.Equal(child.ToString(), child2.ToString());
        }