Author

Topic: Help with NBitcoin (C#) transfering an asset (0.1BTC bounty) (Read 1204 times)

hero member
Activity: 714
Merit: 662
Thanks mate, love the book too.  Some of the hyperlinks in it don't work Sad

Can you tell me which one ? I will fix it.

Thanks for the bounty, feel free to ask me.
By the way, I created a block explorer with a C# API called qbit ninja (http://api.qbit.ninja)
My public servers are not ready for production, but with the QBit Ninja client API integrated with NBitcoin, you can get the colored coin of any balance easily.
hero member
Activity: 765
Merit: 503
Thanks mate, love the book too.  Some of the hyperlinks in it don't work Sad
hero member
Activity: 765
Merit: 503
Last question nic (hopefully).  I sent "Silver".  However, if i try resend gold again Bob -> Alice it doesn't work.

Do I use the UTXO (for coin) from the first example, seeing how that has a large balance?

hmm, I see via the first tx https://testnet.coinprism.info/tx/9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344 that the residual 99,995 where destroyed!

Can we make them as change back to the original?


My address is 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe, I'm happy you like NBitcoin, lots of Bitcoin love have been into this project ! Smiley
The residual got destroyed because the quantities of the spent ColoredCoin were incorrect. Your main problem right now is only getting the coins you want to spend correctly.

We can't reverse the residual destoyed, you need to emit again. Just be very careful about how you build the Coins, the way you use TransactionBuilder is correct.


The colored coin as explained in https://blockchainprogramming.azurewebsites.net/ is a normal Coin, except with information concerning the asset and quantity attached.
600 satoshi is used because it correspond to the minimum allowed txout.

If you mess up the normal Coin information => Transaction rejected by nodes.
If you mess up the asset informations of the coins => Asset get destroyed.
If you mess up building the transaction => TransactionBuilder will throw.

The fact that gold got destroyed show that the quantity when I built the ColoredCoin of the asset was incorrect. (I took your value, I did not checked it)

Thanks nic, sorry I PM'd you too.  I couldn't find this thread after it got moved.  

Yep, I've re emitted.  Ok, so we build the coin object with the 10,000, but transfer the 5 in the tx builder.  Ill try tonight.

Bounty paid c504b223e81f27638fa96f68e8ffcb2337507e5b4a30f0ed2dbfbecf3b5f005b-000 (Can sign a message if required)
hero member
Activity: 714
Merit: 662
Last question nic (hopefully).  I sent "Silver".  However, if i try resend gold again Bob -> Alice it doesn't work.

Do I use the UTXO (for coin) from the first example, seeing how that has a large balance?

hmm, I see via the first tx https://testnet.coinprism.info/tx/9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344 that the residual 99,995 where destroyed!

Can we make them as change back to the original?


My address is 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe, I'm happy you like NBitcoin, lots of Bitcoin love have been into this project ! Smiley
The residual got destroyed because the quantities of the spent ColoredCoin were incorrect. Your main problem right now is only getting the coins you want to spend correctly.

We can't reverse the residual destoyed, you need to emit again. Just be very careful about how you build the Coins, the way you use TransactionBuilder is correct.


The colored coin as explained in https://blockchainprogramming.azurewebsites.net/ is a normal Coin, except with information concerning the asset and quantity attached.
600 satoshi is used because it correspond to the minimum allowed txout.

If you mess up the normal Coin information => Transaction rejected by nodes.
If you mess up the asset informations of the coins => Asset get destroyed.
If you mess up building the transaction => TransactionBuilder will throw.

The fact that gold got destroyed show that the quantity when I built the ColoredCoin of the asset was incorrect. (I took your value, I did not checked it)
hero member
Activity: 765
Merit: 503
Last question nic (hopefully).  I sent "Silver".  However, if i try resend gold again Bob -> Alice it doesn't work.

Do I use the UTXO (for coin) from the first example, seeing how that has a large balance?

hmm, I see via the first tx https://testnet.coinprism.info/tx/9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344 that the residual 99,995 where destroyed!

Can we make them as change back to the original?
hero member
Activity: 765
Merit: 503
Last question nic (hopefully).  I sent "Silver".  However, if i try resend gold again Bob -> Alice it doesn't work.

Do I use the UTXO (for coin) from the first example, seeing how that has a large balance?
hero member
Activity: 765
Merit: 503
the coin (colour coin object) is just a UTXO, or does it have to be the UTXO containing the sent asset?  + a fee of 600 satoshi?

Ah, you sent 5 gold.  So thats what is in that coin tx.  (as per your code comment)
hero member
Activity: 765
Merit: 503
Just to confirm:

the coin (colour coin object) is just a UTXO, or does it have to be the UTXO containing the sent asset?  + a fee of 600 satoshi?

the fee coin, is just a simple utxo?  Where money is the amount in that UTXO?
hero member
Activity: 765
Merit: 503
Check 9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344

https://github.com/bitcoinbrisbane/colourcoinpoc/pull/1

You just messed up with the coins (wrong amount / scriptpubkey)

I knew it would be something simple.  I wasn't using "Findcoins" so I'm sure that was where i was going wrong.  Put a BTC address here on the forum and ill send the bounty (after I confirm)

Really love this lib.
hero member
Activity: 714
Merit: 662
Check 9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344

https://github.com/bitcoinbrisbane/colourcoinpoc/pull/1

You just messed up with the coins (wrong amount / scriptpubkey)
hero member
Activity: 765
Merit: 503
I've make a console app and added to github

https://github.com/bitcoinbrisbane/colourcoinpoc
hero member
Activity: 765
Merit: 503
I've been working with Nicolas to help get this done, but I think hes pretty busy.

I've got code to Issue colour coins from Bob to Alice.  However, I cant get a transfer of the assets.

Bob -> 20 Gold -> Alice
Alice -> 10 Gold -> Doof

This is where Im getting stuck.  After the tx builder I get a valid tx hash.  However if I broad cast nothing happens.  Some code snippets, sorry for the commenting, been trying lots of different approaches.

Ill pay once working code is submitted.  Im using blockr or coinprism as a repo to get the txs.   Happy to hard code a tx hash and script sig to pay the bounty, as long as I can retrieve new ones in the future from my repo.

Notes:
INetwork just calls my bitcoin-core client

Some test keys

Code:
           const String BobAddress = "mxSimcis5yCPkBaFZ7ZrJ7fsPqLXatxTax";
            const String BobAssetAddress = "bX8Qc1nYCZT65cRjcbjg2wyaSjSWhY2gB5p";
            const String BobWIFKey = "cMdLBsUCQ92VSRmqfEL4TgJCisWpjVBd8GsP2mAmUZxQ9bh5E7CN";

            const String AliceAddress = "muJjaSHk99LGMnaFduU9b3pWHdT1ZRPASF";
            const String AliceAssetAddress = "bX5Gcpc75cdDxE2jcgXaLEuj5dEdBUdnpDu";
            const String AlicePrivateWIFKey = "cPKW4EsFiPeczwHeSCgo4GTzm4T291Xb6sLGi1HoroXkiqGcGgsH";

assetId = "oLcV5F6R59zbChoTBQ962Har24tAnhbtHo";

Code:
public string Transfer(String fromAddress, Int64 amount, String toAddress, String assetId, INetwork network)
        {
            //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
            EP.Repository.ColourCoinTransaction.CoinPrism txRepo = new Repository.ColourCoinTransaction.CoinPrism(true);
            var ccutoxs = txRepo.GetTransactions(fromAddress);
            var utxos = txRepo.Get(fromAddress).Where(ux => ux.value > 10000).ToList();

            //hard coded, worked but didn't send assets
            //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")));



            var coin = new Coin(fromTxHash: new uint256("dc19133d57bf9013d898bd89198069340d8ca99d71f0d5f6c6e142d724a9ba92"),
                fromOutputIndex: 0,
                amount: Money.Satoshis(199867600), //20000
                scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac")));

            var forfees = new Coin(fromTxHash: new uint256("302290290074826991bb936c157ef1e5be2882e975154d31b985ae8a26dd3161"),
                fromOutputIndex: 2,
                amount: Money.Satoshis(20000), //9957600
                scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914b9ad2f3f358c24ec207abf72125790e67301284488ac")));


            //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 txBuilder = new TransactionBuilder();
            var tx = txBuilder
                .AddKeys(_key.PrivateKey)
                //.AddCoins(forfees, colored)
                .AddCoins(forfees)
                .AddCoins(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);

            network.Submit(tx);
            return "ok";
        }
Jump to: