Exemplo n.º 1
1
        public static string Transfer(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey)
        {
            NBitcoin.Network network = NBitcoin.Network.TestNet;
            NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey);

            //Get bitcoin balance, 0.0006 is required
            Decimal fundingBalance = 0;

            const Decimal BITCOIN_TRANSACTION_FEE = 0.00006M;

            NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress);
            NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress);

            //UTXOS
            CoinPrism txRepo = new CoinPrism(true);
            var ccutoxs = txRepo.GetTransactions(fromAddress);
            var utxos = txRepo.Get(fromAddress).Where(ux => ux.value > 10000).ToList();

            //hard coded, worked
            //transaction_hash
            //var coin = new Coin(fromTxHash: new uint256("f131c91b3ce35f83258a3db54f5547daecc2a61142f598a39a8491ed2844eb28"),
            //    fromOutputIndex: 0,
            //    amount: Money.Satoshis(3000), //20000
            //    scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac")));

            //var forfees = new Coin(fromTxHash: new uint256("a86a907f36071754ff4fdc4e27de4ec92272f239de5236af5f4806133ac66d0f"),
            //    fromOutputIndex: 0,
            //    amount: Money.Satoshis(97322000), //9957600
            //    scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac")));

            //Coin from Bob
            var coin = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"),
                fromOutputIndex: 0,
                amount: Money.Satoshis(600), //20000
                scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey);

            //Coin from Alice
            var forfees = new Coin(fromTxHash: new uint256("b4326462d6d3b522d7e2c06d9c904313f546395eb62661b06b57195691f5fe5f"),
                fromOutputIndex: 1,
                amount: Money.Coins(1m), //9957600
                scriptPubKey: BitcoinAddress.Create("muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF").ScriptPubKey);

            //var coin = new Coin(fromTxHash: new uint256(utxos[0].transaction_hash),
            //    fromOutputIndex: utxos[0].output_index,
            //    amount: Money.Satoshis(60000), //20000
            //    scriptPubKey: new Script(Encoders.Hex.DecodeData(utxos[0].script_hex)));

            //var forfees = new Coin(fromTxHash: new uint256(utxos[1].transaction_hash),
            //    fromOutputIndex: utxos[1].output_index,
            //    amount: Money.Satoshis(60000), //20000
            //    scriptPubKey: new Script(Encoders.Hex.DecodeData(utxos[1].script_hex)));

            BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet);
            var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet);

            ulong u = Convert.ToUInt64(amount);
            ColoredCoin colored = coin.ToColoredCoin(assetIdx, u);

            //var satoshi = Key.Parse(, Network.TestNet);
            string aliceWIF = "cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH";
            var satoshi = Key.Parse(aliceWIF, Network.TestNet);
            ;

            var bobKey = new BitcoinSecret("cMdLBsUCQ92VSRmqfEL4TgJCisWpjVBd8GsP2mAmUZxQ9bh5E7CN");
            var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH");
            var txBuilder = new TransactionBuilder();
            var tx = txBuilder
                .AddKeys(bobKey,aliceKey)
                .AddCoins(forfees, colored)
                .SendAsset(alice, new AssetMoney(assetIdx, u))
                //.SendAsset(satoshi.PubKey, new NBitcoin.OpenAsset.AssetMoney(assetIdx, u))
                .SetChange(bitcoinFromAddress)
                .SendFees(Money.Coins(0.001m))
                .BuildTransaction(true);
            var ok = txBuilder.Verify(tx);
            Submit(tx);
            return "ok";
        }
Exemplo n.º 2
0
        /// <summary>
        /// Code as per nic
        /// </summary>
        /// <param name="fromAddress"></param>
        /// <param name="amount"></param>
        /// <param name="toAddress"></param>
        /// <param name="assetId"></param>
        /// <param name="senderWifKey"></param>
        /// <returns></returns>
        public static string TransferAsFromNic(String fromAddress, Int64 amount, String toAddress, String assetId, String senderWifKey)
        {
            NBitcoin.Network network = NBitcoin.Network.TestNet;
            NBitcoin.BitcoinSecret _key = NBitcoin.Network.TestNet.CreateBitcoinSecret(senderWifKey);
            NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress);
            NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress);

            //UTXOS
            CoinPrism txRepo = new CoinPrism(true);

            //Get a UTXO to fund.  Make sure its large enough, order by size.  Maybe order by date?
            var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First();

            var ccutoxs = txRepo.GetTransactions(fromAddress);

            //Find output which contains an incoming asset
            var assetOutput = ccutoxs.FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)).outputs.FirstOrDefault(o => o.asset_id == assetId);

            var coin_nic = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"),
                fromOutputIndex: 0,
                amount: Money.Satoshis(600), //default fee
                scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey);

            if (1 == 2)
            {
                //Find the coin bob sent
                //Coin from Bob

                //Coin from Alice
                var forfees_nic = new Coin(fromTxHash: new uint256("b4326462d6d3b522d7e2c06d9c904313f546395eb62661b06b57195691f5fe5f"),
                    fromOutputIndex: 1,
                    amount: Money.Coins(1m), //9957600
                    scriptPubKey: BitcoinAddress.Create("muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF").ScriptPubKey);
            }

            //Colour coin utxo that was sent
            var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash),
                fromOutputIndex: Convert.ToUInt32(assetOutput.index),
                amount: Money.Satoshis(600), //default fee
                scriptPubKey: BitcoinAddress.Create("mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax").ScriptPubKey);

            //Arbitary coin
            var forfees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash),
                fromOutputIndex: fundingUTXO.output_index,
                amount: Money.Satoshis(fundingUTXO.value), //20000
                scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex)));

            BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet);
            var alice = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet);

            ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity));

            //FROM NIC
            var bobKey = new BitcoinSecret("cMdLBsUCQ92VSRmqfEL4TgJCisWpjVBd8GsP2mAmUZxQ9bh5E7CN");
            var aliceKey = new BitcoinSecret("cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH");
            var txBuilder = new TransactionBuilder();

            var tx = txBuilder
                .AddKeys(bobKey, aliceKey)
                .AddCoins(forfees, colored)
                .SendAsset(alice, new AssetMoney(assetIdx, Convert.ToUInt64(amount)))
                .SetChange(bitcoinFromAddress)
                .SendFees(Money.Coins(0.001m))
                .BuildTransaction(true);

            var ok = txBuilder.Verify(tx);
            Submit(tx);
            return "ok";
        }
Exemplo n.º 3
0
        public static string Transfer(String fromAddress, Int64 amount, String toAddress, String assetId)
        {
            NBitcoin.Network network = NBitcoin.Network.TestNet;

            NBitcoin.BitcoinAddress bitcoinToAddress = new BitcoinAddress(toAddress, network);
            NBitcoin.BitcoinAddress bitcoinFromAddress = new BitcoinAddress(fromAddress, network);

            //UTXOS
            CoinPrism txRepo = new CoinPrism(true);

            //Get a UTXO to fund.  Make sure its large enough, order by size.  Maybe order by date?
            var fundingUTXO = txRepo.GetUnspent(fromAddress).Where(ux => ux.value > 10000).OrderByDescending(o => o.value).First();

            if (fundingUTXO == null)
            {
                throw new ArgumentNullException("Need more btc");
            }

            //Find unspent utxos with assets
            var assetFundingUtxos = txRepo.GetUnspent(fromAddress).Where(ux => ux.asset_id == assetId).ToList();

            //Note last emmitting tx = 55ef4ea701ee0df5aac55d56a068d2488780da827aca3c08615cfa92dbfc470e
            var ccutoxs = txRepo.GetTransactions(fromAddress);

            //Find new tx first
            var allOutputsForAsset = ccutoxs.OrderBy(a => a.block_time)
                .Where(i => i.outputs.Any(o => o.asset_id == assetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress)))
                //.outputs.OrderByDescending(o => o.asset_quantity)
                //.outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress));
                .ToList();

            //TODO:  CHANGE FOR DIVISIBILITY
            //Find output which contains an incoming asset
            var assetOutput = ccutoxs.OrderByDescending(a => a.block_time)
                .FirstOrDefault(i => i.outputs.Any(o => o.asset_id == assetId)) // && i.outputs.Any(oo => oo.addresses.Contains(fromAddress)))
                //.outputs.OrderByDescending(o => o.asset_quantity)
                //.outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount && o.addresses.Contains(fromAddress));
                .outputs.FirstOrDefault(o => o.asset_id == assetId && o.asset_quantity >= amount);

            if (assetOutput == null)
            {
                throw new ArgumentNullException("Not enough assets");
            }

            ////Colour coin utxo that was sent
            var coin = new Coin(fromTxHash: new uint256(assetOutput.transaction_hash),
                fromOutputIndex: Convert.ToUInt32(assetOutput.index),
                amount: Money.Satoshis(assetOutput.value), //default fee
                scriptPubKey: bitcoinFromAddress.ScriptPubKey);

            //HACKE FOR 1 USD FROM ALICE BACK TO BOB.  TX 47~48
            //ADDRESS WAS ALICE
            //Colour coin utxo that was sent
            //var coin = new Coin(fromTxHash: new uint256("b52454d26d9020a241d3d3173270bf0b0e218fc4cf21d18c8ac34d8d29f0f988"),
            //    fromOutputIndex: 2,
            //    amount: Money.Satoshis(600), //default fee
            //    scriptPubKey: bitcoinFromAddress.ScriptPubKey);

            //Arbitary coin
            var forfees = new Coin(fromTxHash: new uint256(fundingUTXO.transaction_hash),
                fromOutputIndex: fundingUTXO.output_index,
                amount: Money.Satoshis(fundingUTXO.value), //20000
                scriptPubKey: new Script(Encoders.Hex.DecodeData(fundingUTXO.script_hex)));

            BitcoinAssetId assetIdx = new BitcoinAssetId(assetId, Network.TestNet);
            //var recipient = NBitcoin.BitcoinAddress.Create(toAddress, NBitcoin.Network.TestNet);

            //44696
            ColoredCoin colored = coin.ToColoredCoin(assetIdx, Convert.ToUInt64(assetOutput.asset_quantity));
            //ColoredCoin colored = coin.ToColoredCoin(assetIdx, 44696);

            //FROM NIC
            var key1 = new BitcoinSecret(BobWIFKey);
            var key2 = new BitcoinSecret(CarolWIFKey);
            var txBuilder = new TransactionBuilder();

            var tx = txBuilder
                .AddKeys(key1, key2)
                .AddCoins(forfees, colored)
                .SendAsset(bitcoinToAddress, new AssetMoney(assetIdx, Convert.ToUInt64(amount)))
                .SetChange(bitcoinFromAddress)
                .SendFees(Money.Coins(0.001m))
                .BuildTransaction(true);

            var ok = txBuilder.Verify(tx);
            var response = Submit(tx);

            return response;
        }