Bitcoin Forum
December 08, 2022, 02:59:44 AM *
News: Reminder: do not keep your money in online accounts
 
   Home   Help Search Login Register More  
Pages: [1] 2 3 4 »  All
  Print  
Author Topic: Blockchain security tracking/colored coins/smart contracts - short python script  (Read 8795 times)
bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 10, 2012, 03:26:56 PM
Last edit: October 18, 2012, 01:12:38 PM by bitfair
 #1

There are now many threads about "colored coins", "smart contracts" and otherwise tracking specific coins through the blockchain.

I have created a simple command-line Python program to handle tracking of "colored coins", "smart contracts" or "blockchain securities" in the blockchain.

There are other approaches, and the one I have taken is very simple, but it is a working solution (primitively duct-taped and glued together). I also have not read gone into the details of the other approaches, so it is probably safe to say that the method I have created is completely incompatible with the other methods (which are probably much smarter too).

But it is a live and implemented solution, which gives it an advantage over others. It is short, easy to modify and fun to play with. It might be useful for some adventurous security issuers who are looking for a solution now that GLBSE is shutting down.

Full source: http://pastebin.com/hbUz0vS9

Update Oct 15:

The source code has been updated, the new version includes functions to pay dividends to holders of the colored coins.

Also, it has been tested more thoroughly, and I have created a 12-step usage example that shows the current capabilities of the script.

Update Oct 18:

The source code has been updated, the new version includes the functions to include a transaction fee from an (uncolored!) address in your standard/satoshi-client wallet when transferring assets so that the transaction is more likely to confirm more quickly (command-line option -w <fee>, where <fee> is the fee you want to pay in BTC, e.g. -w 0.005 for half a bitcent).

Also, the tracking of colored coins is now also believed to be compatible with other approaches for "order-based" tracking, such as the one used in killerstorm's modified Satoshi client (however, I still want to build up a bunch of test-cases to see if the clients interpret all the cases in the same way).

Next, I think it is time to create ample test-cases, and tidy up the code - it has turned into a bit of a spaghetti-ball.

Usage example:
Code:

1. First run config setup:
$ python bitpaint.py
Configuration file bitpaint.conf not found. Creating one...
bitcoind rpc host (default: 127.0.0.1):
bitcoind rpc port (default: 8332):
bitcoind rpc username (default: <blank>): ********
bitcoind rpc password (default: <blank>): ********

2. Create address:
$ python bitpaint.py -n
Address added: 1AEn3U5MhedXYRbpxLKmkL4r5Kz1XkdZA

3. Transfer the colored coins to the address (sent 0.01 BTC to the address above using Satoshi client). Make a note of (1) the Transaction ID: 630bccacd6f4401fde53822af3f011daa47ec1bedd1702bf7d0be4391948f49b, (2) the output that was sent to the address above: 1.

4. "Paint" the output of the transaction, call the painted coins "BITFAIR-SHARES":
$ python bitpaint.py -p BITFAIR-SHARES:630bccacd6f4401fde53822af3f011daa47ec1bedd1702bf7d0be4391948f49b:1

5. Show the ownership info of the painted coins "BITFAIR-SHARES", the output is the owner address, the amount the address owns, and the transaction output which assigned ownership to that address, then a line with a total at the bottom:
$ python bitpaint.py -o BITFAIR-SHARES
*** BITFAIR-SHARES ***
1AEn3U5MhedXYRbpxLKmkL4r5Kz1XkdZA 0.01 630bccacd6f4401fde53822af3f011daa47ec1bedd1702bf7d0be4391948f49b:1
** Total BITFAIR-SHARES: 0.010000 **

6. Create three new addresses (could be done by another user, for example the buyer of the security - the buyers should also run the command in step 4 to initiate the coin tracking), and create a transaction that transfers 0.006 to the first address (represents 60% of "outstanding shares"), 0.003 to the second (represents 30% of "outstanding shares) and 0.001 to the third (represents 10% of "outstanding shares"):
$ python bitpaint.py -n
Address added: 1KbDEaZ99YJYaT62emhFwabFoqQ6LYdJg9
$ python bitpaint.py -n
Address added: 1DAHyXPNJjqLGFNqHhavrukWZ6XkY8Kj21
$ python bitpaint.py -n
Address added: 1Mhx4LMjbmN6KJ9B5BkdZ5zVWQP64F5TJC
$ python bitpaint.py -f 1AEn3U5MhedXYRbpxLKmkL4r5Kz1XkdZA:630bccacd6f4401fde53822af3f011daa47ec1bedd1702bf7d0be4391948f49b:1 -t 1KbDEaZ99YJYaT62emhFwabFoqQ6LYdJg9:0.006,1DAHyXPNJjqLGFNqHhavrukWZ6XkY8Kj21:0.003,1Mhx4LMjbmN6KJ9B5BkdZ5zVWQP64F5TJC:0.001 -w 0.005
01000000019bf4481939e40b7dbf0217ddbec17ea4da11f0f32a8253de1f40f4d6accc0b63010000008b483045022100dd23f8d8cc3c92314127badcdd50c22414fe1bdf50215c3edce89f0aa64836bd02200c940f4a5ca59c133f14269c3cc2568e8340ec09d90f65c3264f869e93f28e010141047002cabfc1da529ab13d035a60b11db585be5709ddd7d8ab5649ce5d71a279bcb681007bb929f8adf7d811726cd82da5aa1fa96cd736cca986a79c89186a1e29ffffffff03e0930400000000001976a91485636c3633be3a41ce4893dc84ab31fd2d96ea4f88aca0860100000000001976a914e3211d2305301fe6421dbf85685a2744ca51817e88acc0270900000000001976a914cbea9e8c8392abc07417f95bfcb2f21406c6599088ac00000000

7. The result of the former step is a hex-encoded raw transaction. Because it contains no transaction fees, the standard satoshi client will refuse to send it. However, the resulting hex-encoded raw transaction can be broadcast on the network using http://blockchain.info/pushtx. PS: At the moment, you cannot be sure that the transaction will be included in a block, since it contains no transaction fee (i.e. compensation to the miners). I am currently working on a solution to this.

8. Update list of BITFAIR-SHARES holders, and show it:
$ python bitpaint.py -u BITFAIR-SHARES
$ python bitpaint.py -o BITFAIR-SHARES
*** BITFAIR-SHARES ***
1DAHyXPNJjqLGFNqHhavrukWZ6XkY8Kj21 0.003 d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:0
1Mhx4LMjbmN6KJ9B5BkdZ5zVWQP64F5TJC 0.001 d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:1
1KbDEaZ99YJYaT62emhFwabFoqQ6LYdJg9 0.006 d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:2
** Total BITFAIR-SHARES: 0.010000 **

9. Pay dividends of 0.1 BTC in total from my wallet account named "bitpaint_donations" to the holders of the colored coins ("shareholders of BITFAIR-SHARES") (not sure if this works with an encrypted wallet, should be easy enough to fix for those who have it):
$ python bitpaint.py -d BITFAIR-SHARES:bitpaint_donations:.1

10. List what shares/colored coins are owned by me, their dividends (and address that the shares/dividends belong to, along with the transaction output that assigned the shares to my address):
$ python bitpaint.py -m
BITFAIR-SHARES 0.003 ( div: 0.03 ) 1DAHyXPNJjqLGFNqHhavrukWZ6XkY8Kj21 d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:0
BITFAIR-SHARES 0.001 ( div: 0.01 ) 1Mhx4LMjbmN6KJ9B5BkdZ5zVWQP64F5TJC d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:1
BITFAIR-SHARES 0.006 ( div: 0.06 ) 1KbDEaZ99YJYaT62emhFwabFoqQ6LYdJg9 d8e8dc799be1b552f29f8cbd2df24a1fcb9c2a9f4a43c9b4444b9dc329bbd17d:2

11. Forward dividends from my asset-addresses to one of my wallet addresses:
$ python bitpaint.py -x 1DAHyXPNJjqLGFNqHhavrukWZ6XkY8Kj21 -y 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr
0100000001cf707398596a9f8bf45e22e3f736b819f27ba6d10cb9c12f1ca23a9daf0c6f1d000000008c4930460221009eb892e079fd143fd5b1919c0c07073d1d45a671aabb05d2565721ae3f83aee6022100a60706b226ef968038ef654f8b782103170932201b7e9fc9320ec24027c56e14014104f7e514dad77c0dbf157c1cc88d610a512f9809785fda426ca7ee3c70df59afeee521ccb02f610536e093b954c4718a4b9d2c028aaec62ad2f138d0735a0d918dffffffff01a0252600000000001976a9143a572d6755022d568f7a78d0060234920486cd0f88ac00000000
Paid 0.025 to 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr
$ python bitpaint.py -x 1Mhx4LMjbmN6KJ9B5BkdZ5zVWQP64F5TJC -y 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr
0100000001cf707398596a9f8bf45e22e3f736b819f27ba6d10cb9c12f1ca23a9daf0c6f1d020000008a47304402203ff6f44811c8d772f16535a3482fcb88e0b992cf3f8574b78b32489146212a28022045e2fff119b1124035849f3d54b5f5fa3cf8ebf88463df38df1ee7f4ab4cf358014104dc22a7249282ba829c4351cd9255690aaaa9a9896bd9b95e6075d5a3ec34b305a6eab56f9524f7a4308f418bd4a6eabdaf887a4622874b32cb4f4f55c28e1092ffffffff0120a10700000000001976a9143a572d6755022d568f7a78d0060234920486cd0f88ac00000000
Paid 0.005 to 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr
$ python bitpaint.py -x 1KbDEaZ99YJYaT62emhFwabFoqQ6LYdJg9 -y 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr
0100000001cf707398596a9f8bf45e22e3f736b819f27ba6d10cb9c12f1ca23a9daf0c6f1d030000008c493046022100b440c8997b43067a2a96ece768487e6f219391491d83b384fe7408f106adea5b022100b7d47531358afdeb75cd4e98753309c175ce71e1d722ab851c363c0b9f80d2b50141043d0c63308cb4dcc98b966922a7f78435d44c89b63bf94485f0920c3434a5077c4004adb067ac319f3ee1b551b451a11154e0e1a8ba1c686d15bde93f89963044ffffffff0160ec5300000000001976a9143a572d6755022d568f7a78d0060234920486cd0f88ac00000000
Paid 0.055 to 16KUdKHHZ1AUVb4ZH6Q2VY55L3V3gCCSmr

12. The result of each command in the former step is a hex-encoded raw transaction. It contains a transaction fee of 0.005, to help it get mined into a block. The hex-encoded raw transaction can be broadcast on the network using http://blockchain.info/pushtx. Your client should see the transaction within seconds.


Planned features:
- GUI

Please be cautious when using it, but feel free to criticise, modify and contribute. And if you like it, support it: 1GsnaaAWYMb7yPwBQ19bSLLMTtsfSyjqg3.
1670468384
Hero Member
*
Offline Offline

Posts: 1670468384

View Profile Personal Message (Offline)

Ignore
1670468384
Reply with quote  #2

1670468384
Report to moderator
You get merit points when someone likes your post enough to give you some. And for every 2 merit points you receive, you can send 1 merit point to someone else!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1670468384
Hero Member
*
Offline Offline

Posts: 1670468384

View Profile Personal Message (Offline)

Ignore
1670468384
Reply with quote  #2

1670468384
Report to moderator
jl2012
Legendary
*
Offline Offline

Activity: 1792
Merit: 1017


View Profile
October 10, 2012, 04:19:41 PM
 #2

There are now many threads about "colored coins", "smart contracts" and otherwise tracking specific coins through the blockchain.



I think it is because the fail of GLBSE. The next generation of bitcoin security exchange will be colored-coin-based p2p system. The role of a centralized exchange (if any) is to facilitate bid/ask matching, to advertise IPO, and to rate the trustworthiness of security issuers.

Donation address: 374iXxS4BuqFHsEwwxUuH3nvJ69Y7Hqur3 (Bitcoin ONLY)
LRDGENPLYrcTRssGoZrsCT1hngaH3BVkM4 (LTC)
PGP: D3CC 1772 8600 5BB8 FF67 3294 C524 2A1A B393 6517
matthewh3
Legendary
*
Offline Offline

Activity: 1372
Merit: 1000



View Profile WWW
October 10, 2012, 07:25:40 PM
 #3

Can you automatically pay dividends to coloured coins?

bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 10, 2012, 07:32:48 PM
 #4

Can you automatically pay dividends to coloured coins?

Not yet, but it will be relatively trivial to implement. The script already produces a list of "shareholders" (as represented by their bitcoin addresses) and the share that they own.

From that, it's just a question of massaging the data to the right format and feeding it to the bitcoin json API.

I will see if I have the time to implement it this week, and pastebin a newer version.
matthewh3
Legendary
*
Offline Offline

Activity: 1372
Merit: 1000



View Profile WWW
October 10, 2012, 07:37:00 PM
 #5

Can you automatically pay dividends to coloured coins?

Not yet, but it will be relatively trivial to implement. The script already produces a list of "shareholders" (as represented by their bitcoin addresses) and the share that they own.

From that, it's just a question of massaging the data to the right format and feeding it to the bitcoin json API.

I will see if I have the time to implement it this week, and pastebin a newer version.

Cool, if you do I may use it for my asset.  I'll see if I can organise you a donation for your work from shareholders if we use your client.


bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 10, 2012, 07:50:32 PM
 #6

I am planning to implement it such that dividends/coupons/whatever can be paid to the owners (proportional to their share, of course) from the wallet. It will be paid to the same address which is registered as the owner of the colored coins - but the receiver will need to be careful when spending the dividends, so it doesn't accidentally spend the transaction that represents the share. For that reason, I also need to implement routines to safely transfer the dividends from the address holding the colored coins and to another address.

Thanks for considering using it. It's more fun when someone finds it useful. But beware that it should be considered experimental at this stage (although as long as no private keys are lost, it shouldn't really be "dangerous" to try it).

Also considering a simple GUI, that might lower the barriers to using it.
socrates1024
Full Member
***
Offline Offline

Activity: 126
Merit: 108


Andrew Miller


View Profile
October 10, 2012, 08:05:01 PM
Last edit: October 10, 2012, 08:28:07 PM by socrates1024
 #7

Any idea what the asymptotic complexity is of this implementation? I'd be interested in the a) time, b) storage space, and c) transfer requirements, in terms of quantities such as 1) the number of tracked coins, 2) the number of transactions for each coin.

Also, it looks like your config file is not just a config file, but also is a stateful database that stores the current owner of each coin.

amiller on freenode / 19G6VFcV1qZJxe3Swn28xz3F8gDKTznwEM
[my twitter] [research@umd]
I study Merkle trees, credit networks, and Byzantine Consensus algorithms.
matthewh3
Legendary
*
Offline Offline

Activity: 1372
Merit: 1000



View Profile WWW
October 10, 2012, 08:13:46 PM
 #8

Well we will probably wait until it is a bit more mature but I will put forward the idea that you get a donation from our shareholders.

Another question is does the whole blockchain need to be saved and how can I distribute more than one copy of this new coloured blockchain.

bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 10, 2012, 08:29:05 PM
 #9

Any idea what the asymptotic complexity is of this implementation? I'd be interested in the a) time, b) storage space, and c) transfer requirements, in terms of quantities such as 1) the number of tracked coins, 2) the number of transactions for each coin.

Also, tt looks like your config file is not just a config file, but also is a stateful database that stores the current owner of each coin.

Excellent questions, sir! It's a very crude script at the moment, following one of the pillars of "open source": release early, release often. (I guess the reason is that it gives others the chance to make and suggest improvements!)

Complexity (answers pulled out of my hat without terribly much thinking, I might need to correct it later):
a) Time: standard tree traversal time, O(n) where n is the number of nodes. In this case that means the number of transactions of the security that have been made since the root/ancestor transaction. In this implementation, the latency from accessing blockchain.info on the web will dominate (which it does to find out which transaction the coin was spent in next) - executed O(n) times.
b) Storage: While traversing the blockchain, a stack depth of O(log(n)) will be kept in memory (many assumptions in this). After traversal, the leaf nodes are stored in the config file, so it is proportional to the amount of "owners" of the colored coins.

And yes, you are quite right: in the spirit of simplicity and transparency over rigid correctness, all information is currently stored in the configuration file. (You may now commence your complaining:)
bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 10, 2012, 08:36:49 PM
 #10

Well we will probably wait until it is a bit more mature but I will put forward the idea that you get a donation from our shareholders.

Another question is does the whole blockchain need to be saved and how can I distribute more than one copy of this new coloured blockchain.

There is no "colored blockchain", it uses only the standard bitcoin blockchain and tracks all the transactions in a chain starting at a particular given transaction. That a coin is "colored" only means that it can be traced (through the blockchain) to belong to a very specific transaction, chosen and named by yourself.

And it simply makes sure that the transactions built by the script are formed in such a way that allows them to be tracked by the script.
jgarzik
Legendary
*
Offline Offline

Activity: 1596
Merit: 1049


View Profile
October 10, 2012, 09:31:31 PM
 #11

There is no "colored blockchain", it uses only the standard bitcoin blockchain and tracks all the transactions in a chain starting at a particular given transaction. That a coin is "colored" only means that it can be traced (through the blockchain) to belong to a very specific transaction, chosen and named by yourself.

True, however:  a chain specifically for smart property could potentially keep bloat out of the main chain.


Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 11, 2012, 11:15:36 AM
Last edit: October 11, 2012, 11:28:11 AM by bitfair
 #12

True, however:  a chain specifically for smart property could potentially keep bloat out of the main chain.

I agree in principle, although I believe bloat from piggybacking on the main chain would be negligible in the bigger picture. In addition, a separate chain for smart property would also require separate mining, and mining requires some kind of compensation paid to the miners - and I haven't been able to think of any sensible "compensation scheme" that would make it worth their while. [But, by all means, don't let the discussion stop at my lack of imagination!]

Edit: Currently testing dividend/coupon-payment function using the 1 BTC that was donated (thanks!), script will be updated when it appears to be working. And shortly after that, expect a "howto"/annotated example.
markm
Legendary
*
Offline Offline

Activity: 2884
Merit: 1075



View Profile WWW
October 11, 2012, 03:13:43 PM
 #13

Since coins can now be tracked, and thus need no longer be and maybe will no longer be fungible, we can devalue coins mined into the coinbase transactions of blocks where list of merged mined chains codes does not include our chain.

Maybe only people who try to buy securities will care, if no other chains also devalue those specific coins and their descendants, of course.

But would the actual coins of a securities chain actually be worthless when uncoloured? Or would folk looking to colour them consider them worth buying?

How many securitysatoshis would be mined per block in such a chain?

When bitcoin catches on, how fabulously expensive is it going to be to have to pay for an actual bitcoin chain transaction every time you want to change which family-member controls the garage door opener or other smart-property? Might using one of the many commonly merged chains be more cost-effective when dealing with smart property smaller than, say, a small house or fancy vehicle?

Would this use of the bitcoin chain be a better way of implementing domains, aliases, pseudonymous identities, keyrings and such than the ostensibly designed for the purpose namecoin? (Maybe namecoin was a silly clumsy ill thought out idea that overlooked this much more natural, elegant, effective approach?)

-MarkM-

Browser-launched Crossfire client now online (select CrossCiv server for Galactic  Milieu)
Free website hosting with PHP, MySQL etc: http://hosting.knotwork.com/
Andrew Vorobyov
Hero Member
*****
Offline Offline

Activity: 558
Merit: 500



View Profile
October 11, 2012, 03:59:34 PM
 #14

Post on github.com
bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 11, 2012, 05:15:12 PM
 #15

Post on github.com

It's starting to push the limit for what can go into a single file - I will be considering putting it on github when it becomes necessary to split it into several files. (Probably pretty soon!)
yoniassia
Newbie
*
Offline Offline

Activity: 42
Merit: 0



View Profile WWW
October 11, 2012, 06:35:43 PM
 #16

This is really great ! Can you explain the diff between your implementation and the main colored coins thread ? Would be great to merge coloring so there will be one standard.
bitfair (OP)
Sr. Member
****
Offline Offline

Activity: 362
Merit: 250


View Profile
October 11, 2012, 06:53:07 PM
 #17

This is really great ! Can you explain the diff between your implementation and the main colored coins thread ? Would be great to merge coloring so there will be one standard.

I'm not quite up-to-date on the main colored coins thread, so I'm afraid I can't explain it at the moment. Will read up on it, though, and see if our thoughts are compatible.

In other news, a newer version is currently being tested, which allows paying of dividends/coupons/etc from your wallet to the addresses that hold the colored coins. However, progress has been stumped by a bug in blockchain.info (https://bitcointalk.org/index.php?topic=40264.msg1265405#msg1265405). Either they will fix the bug in reasonable time, or I guess I will need to work around it (which would be tedious).

Stay tuned!
killerstorm
Legendary
*
Offline Offline

Activity: 1022
Merit: 1013



View Profile
October 11, 2012, 09:44:26 PM
 #18

I guess the main difference is that we were focusing on allowing to have coins of multiple colors in same transaction, while this version doesn't allow it, comment in code:

Code:
# This is because it is not possible to follow a colored coin across a transaction
 # with multiple inputs

More fine-grained color tracking is needed to implement secure exchange: coins of different colors will be exchanged in one atomic transaction.

Otherwise this script seems to be roughly equivalent to the prototype I've implemented about a month ago, in terms of coloring.

Chromia: a better dapp platform
yoniassia
Newbie
*
Offline Offline

Activity: 42
Merit: 0



View Profile WWW
October 12, 2012, 09:17:21 PM
 #19

So can both coloring schemes be merged into one standard, can we define that standard ? Anything worth updating here ? ( no need to allow multi colors in one transaction in all scripts, simply means they can read only "pure colors").
killerstorm
Legendary
*
Offline Offline

Activity: 1022
Merit: 1013



View Profile
October 12, 2012, 11:40:37 PM
 #20

Well, it is compatible with order-based coloring: if we have one input, it is always in correct order Smiley

But I don't think that having two versions (let's call them simple and complex) is a good idea: that would mean that if somebody makes a 'complex' transaction, those coins won't be visible in 'simple' version anymore. Forever: there is no way to repair it for 'simple' version.

Also I think simple version should support at least 1 color + uncolored scheme. This would allow payment of transaction fees, buy/sell atomic transactions etc. There is no need to support more than one color in one transaction, but colored + uncolored is sort of necessary.

Chromia: a better dapp platform
Pages: [1] 2 3 4 »  All
  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!