I've tested coloring via bfs traversal on the real blockchain, and it doesn't work so well: sometimes it needs to scan like 20000 transactions to get to coinbase one. (And thus prove that output in uncolored.)
DFS seems to be a little better, but it blew up Python stack on at least one output, which means that depth is about 1000, which isn't cool either.
So I think we need a solution which finds the shortest path.
So to start with this we'll allocate two bounties:
1. Port dfs backward-scan coloring to JavaScript: 5 BTC.
As I've already mentioned, it is already implemented in Python by Vitalik Buterin.
BFS version:
https://github.com/vbuterin/BitcoinArmory/blob/color/gettxcolor.pyDFS version:
https://github.com/vbuterin/BitcoinArmory/blob/92f021b0770cf7504bbe5df17d89044b97fc901b/gettxcolor.pyHowever, DFS should be implemented in such way that it doesn't blow up the stack, also it needs asynchronous queries, so it's going to be considerable different from Python version in structure. But still you need something like get_matching_inputs to do the coloring.
Theory:
https://github.com/bitcoinx/colored-coin-tools/blob/master/colors.mdTo access transaction information you can use blockchain.info API:
http://blockchain.info/ru/api/blockchain_api (and async queries, of course!), but please abstract transaction-data query function so we will be able to use it with a different API.
You can take this code for an inspiration:
https://github.com/domtancredi/bitcoinjs-color/blob/master/async.js it kinda tries to implement this via DFS, but is broken. Still you can borrow general structure etc.
2. Build shortest path database based on bitcoinjs server: 7 BTC.
We need to know distance from a transaction output to nearest coinbase. We will store these distances in database, and then query this database to find shortest path.
Algorithm is fairly simple:
Scan all transactions in blockchain sequentially starting with the first block:
1. If transaction is coinbase its outputs have zero distance to coinbase.
2. Otherwise, for each tx output, we find all matching tx inputs (same function is used for coloring, see get_matching_inputs here:
https://github.com/vbuterin/BitcoinArmory/blob/color/gettxcolor.py)
Fetch distance of each input from database (they are already there because we're scanning sequentially).
Find minimum of input distances, then output's distance is minimum+1.
Write this number to database.
I recommend using this as a starting point:
https://github.com/0i0/bitcoin-tx-spent-dbIt is a thing you need to run together with bitcoinjs server, basically it installs its query code into bitcoinjs, then it provides REST API to database it have built.
You can either fork it and replace spent-db code with distance-to-coinbase code. Or you can extend this code and write to two separate collections. It's up to you.
The starting point is
https://github.com/0i0/bitcoin-tx-spent-db/blob/master/app.js -> everParseBlock -> getCollection, it is a function which goes through transactions and adds the to database, but in our case it should find shortest distance for each output and write it to database.
Please note that working with main net is very resource intensive, you'll need 20+ GB of disk space and 20+ hours for processing. Use testnet, there is a command line option for bitcoinjs. (Don't forget that genesis tx hash is different, for some reason it is used in app.js)
Both tasks share the need for get_matching_inputs. It would be cool to implement it only once in JS, but it's not critical, I guess.
That's all... Whoever wants to work on this, please let me know. Questions are welcome not matter whether you're going to work or not.
If you think that bounty is too low (e.g. you would do it for 10 BTC), please let me know.