Bitcoin Forum
June 29, 2024, 10:16:28 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Help with NBitcoin (C#) transfering an asset (0.1BTC bounty)  (Read 1182 times)
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 21, 2015, 09:10:09 PM
 #1

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";
        }
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 21, 2015, 09:24:30 PM
Last edit: September 21, 2015, 10:41:10 PM by doof
 #2

I've make a console app and added to github

https://github.com/bitcoinbrisbane/colourcoinpoc
Nicolas Dorier
Hero Member
*****
Offline Offline

Activity: 714
Merit: 661


View Profile
September 22, 2015, 12:44:07 AM
 #3

Check 9932354c8f1d5c55494a0e2a9b4388ddf6018a649d05eb97c91753edbbc2f344

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

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

Bitcoin address 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 12:56:31 AM
 #4

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.
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 12:59:45 AM
 #5

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?
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 01:14:03 AM
 #6

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)
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 03:27:20 AM
 #7

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?
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 03:41:46 AM
 #8

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?
Nicolas Dorier
Hero Member
*****
Offline Offline

Activity: 714
Merit: 661


View Profile
September 22, 2015, 03:04:41 PM
 #9

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)

Bitcoin address 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 10:54:55 PM
 #10

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)
doof (OP)
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
September 22, 2015, 11:00:41 PM
 #11

Thanks mate, love the book too.  Some of the hyperlinks in it don't work Sad
Nicolas Dorier
Hero Member
*****
Offline Offline

Activity: 714
Merit: 661


View Profile
September 23, 2015, 01:47:11 AM
 #12

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.

Bitcoin address 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!