This blog has moved to Medium

Subscribe via email

Archive for October 2011

WhateverOrigin – Combat the Same Origin Policy with Heroku and Play! Framework

A little while ago, while coding Bitcoin Pie, I found the need to overcome the notorious Same Origin Policy that limits the domains javascript running on a client’s browser can access. Via Stack Overflow I found a site called Any Origin, that’s basically the easiest way to defeat Same Origin Policy without setting up a dedicated server.

All was well, until about a week ago,  Any Origin stopped working for some (but not all) https requests. It just so happened that in that time I had gained some experience with Play! and Heroku, which enabled me to quickly build an open source clone of Any Origin called Whatever Origin (.org!) (on github). For those unfamiliar with Play! and Heroku, let me give a short introduction:

Heroku is one of the leading PaaS providers. PaaS is just a fancy way of saying “Let us manage your servers, scalability, and security … you just focus on writing the appliaction.” Heroku started as a Ruby shop, but they now support a variety of programming languages and platforms including python, java, scala, javascript/Node.Js. What’s extra cool about them is that they offer a huge set of addons ranging from simple stuff like Custom Domains and Logging through scheduling, email, SMS, and up to more powerful addons like Redis, Neo4j and Memcached.

Now for the application part, I had recently found Play! Framework. Play is a Java/Scala framework for writing web applications that borrows from the Ruby on Rails / Django ideas of providing you with a complete pre-built solution, letting you focus on writing your actual business logic, while allowing you to customize everything later if needed. I encourage you to watch the 12 minute video on Play!’s homepage, it shows how to achieve powerful capabilities from literally scratch. Play! is natively supported at Heroku, so really all you need to do to get a production app running is:

  • play new
  • Write some business logic (Controllers/Views/whatnot)
  • git init … git commit
  • “heroku apps add” to create a new app (don’t forget to add “–stack cedar” to use the latest generation Cedar stack)
  • “git push heroku master” to upload a new version of your app … it’s automatically built and deployed.

Armed with these tools (which really took me only a few days to learn), I set out to build Whatever Origin. Handling JSONP requests is an IO-bound task – your server basically does an HTTP request, and when it completes, it sends the response to your client wrapped in some javascript/JSON magic. Luckily Play!’s support for Async IO is really sweet and simple. Just look at my single get method:

public static void get(final String url, final String callback) {
    F.Promise<WS.HttpResponse> remoteCall = WS.url(url).getAsync();
    await(remoteCall, new F.Action<WS.HttpResponse>() {
        public void invoke(WS.HttpResponse result) {
            String responseStr = getResponseStr(result, url);   // code for getResponseStr() not included in this snippet to hide some ugly irrelevant details
            if ( callback != null ) {
                response.contentType = "application/x-javascript";
                responseStr = callback + "(" + responseStr + ")";
            } else {
                response.contentType = "application/json";

The first line initiates an async fetch of the requested URL, followed by registration to the completion event, and releasing the thread. You could almost think this is Node.Js!

What actually took me the longest time to develop and debug was JSONP itself. The information I found about it, and jQuery’s client-side support was a little tricky to find, and I spent a few hours struggling with overly escaped JSON and other fun stuff. After that was done, I simply pushed it to github, registered the domain for a measly $7 a year, and replaced with in Bitcoin Pie’s code, and voila – the site was back online.

I really like developing websites in 2011 – there are entire industries out there that have set out to make it easy for individuals / small startups to build amazing products.

Be an early adopter now – is LiteCoin the new, fair BitCoin?

tl;dr – If you think Bitcoin is an interesting concept, at least in theory, consider a new fork of it, and start mining … this Wednesday/Thursday.

Little over a year ago, I blogged about Bitcoin, a new high-yield investment.
Since then, through ups and downs, its value has increased 5 folds.


Interpret this graph as you wish, Bitcoin has risen X5 in the last 6 months (I still think it will continue to rise, despite the price decline in the last few months).

Now, a new currency is poised to become “The Silver to Bitcoin’s Gold”. I think it has a shot. Introducing … LiteCoin (to be officialy released in a few days).

LiteCoin is extremly similar to Bitcoin. It also has a pre-set limit on the number of coins (84 million). The generation rate halves every four years, just like Bitcoin.
Its major feature, at least IMO, is that it’s fair:

  • By now, a lot of people have at least heard of Bitcoin (certainly the readers of this blog), so they won’t take so much time to digest the idea. If you decide not to buy/mine, that’s fine, but it won’t be because you’re not aware of it.
  • There are no per-mined coins, unlike some other alternative chains.
  • You can actually mine them! When I learned of Bitcoin, I was disappointed to learn that they’re very difficult to mine. They require dedicated GPUs, which the official client did not support. Mining them wasn’t impossible, but it was much too complex, and simply wasn’t worth it for almost everyone that encountered them at this stage. Buying them was much more lucrative (for me, at least).
  • LiteCoin uses scrypt, which cannot be run well on GPUs, and is intended to be “CPU friendly”.
  • Windows binaries are already availble (on testnet, not functional until the release date) – you can prepare in advance, and don’t have to race other people to start mining first.

LiteCoin might succeed, and it might fail – just like Bitcoin. While I might buy some in the future (after an exchange starts trading in them), for now I’ll be content to mine them, starting either this Wednesday or Thursday. It’s dead simple – just download the client, and run it on your spare CPU cores. You can run it on a low priority, so it won’t boggle down your day to day work.

There is one issue – botnets. If this turns out very profitable, it’s possible botnets will use the infected hosts to mine LiteCoin. Still, it remains to be seen whether this will be a major impact or not. Not that even today some botnets mine Bitcoins with the GPUs of their infected host.

I find the very fact I can actually generate money on my computer exciting. It will not be worth a lot at first, but its value might appreciate, like Bitcoin did. I invite you to join the experiment and try it out for yourselves.

Important note – don’t run the miner without making sure your CPU is properly cooled. Even though summer is just ending, the core will run at 100% X how many threads you run. It should be ok as long as your computer case is properly ventilated.

P.S. – to premept any comments in the style of “Bitcoin is a scam”, please read Is Bitcoin a Ponzi Scheme? before posting. The same reasoning applies identically to LiteCoin.

Bitcoin Pie

I just finished the first version of Bitcoin Pie – a service that displays the values and market caps of alternative crypto-currencies.
Besides Bitcoin, it supports Tenebrix, NameCoin and SolidCoin.

It appears that Bitcoin is now only 98.5% of all the crypto-currencies, with Tenebrix and Namecoin taking up 1.4%.

A wiki for mathematical theorems

A dream I’ve had for some time is to build a wiki for mathematical proofs. It would be sort of like Stack Exchange / Quora – people would post conjectures, and the answers would be in the form of a formal proof, automatically verified by the site.

You could link to proven theorems as lemmas in your proof. A conjecture could have several different proofs. The conjectures/theorems and proofs would all be voted on, tagged, and have English text explaining the steps in addition to the logical formulation.

This site would essentially be the Wikipedia of mathematics. If successful, it will become a place where anyone can find important theorems in a specific field, or build new theorems formally from smaller building blocks, validating their correctness.

I won’t have time to build it in the very near future, but I’m juggling the idea in my head nonetheless. A few open questions:

  • Do you think such a site would be useful?
  • Does something like this already exists?
  • Is it even practical?
  • Where would you start?
  • What programming language / framework would you use for this?
  • Any other helpful ideas?
  • And finally … do you want to collaborate with me on this?

Update – Please see also this cross-post I did to Quora. It seems the problem is a lot more complicated than I had first imagined. I wonder if it fits as a doctorate thesis…

In any case, it requires a lot of research, not just coding. Perhaps someday.