Bitcoin Linux

Posted by Balaji Srinivasan

Bitcoin Linux

Linux and other operating systems based on Unix (such as OS X) are frequently chosen by power users - in part because they contain powerful commands that can be easily chained together in ad-hoc pipelines to accomplish what could take hundreds or thousands of lines of code in a regular program.

As a separate trend, Software as a Service (SAAS) tools have also become increasingly popular. On the buyer side, they take care of much of the hassle out of setting up and maintaining complex server-side apps for HR, financials, email, and the like. On the seller side, they allow entrepreneurs to sell easily-updated software without giving away the full source code.

Here, we show how to merge these two concepts to build command-line SAAS programs: single commands that can pipe into each other like Unix commands, and that are paid on a per-command basis using Bitcoin. Because they can be easily monetized, they can be easily maintained by teams of developers - vastly expanding the scope of what can be conveniently done at the command line.

Motivation: Command-line monetization

One of the biggest advantages of SaaS compared to traditional software is SaaS's low barrier to entry. In most cases you don't need to install anything---you just browse to a particular domain name and then get started. That's a lot easier than attempting to reimplement their particular software stack on your machine---imagine how much data you'd have download and how many database indices you'd have to build in order to get something like the Google Maps server code working on a personal machine.

On the the other hand, these large services also require large funding, so they can't be given away for free in unlimited quantities the same way open source command-line programs work. This has likely contributed to the reason that full command line interfaces to SaaS apps are infrequent, as there's no obvious way for SaaS companies to get money from command line users.

Bitcoin micropayments change that, making it possible to charge money for each command line invocation. In the previous weeks you've already paid for services such as retweeting on the command line, and it was easy to do. In addition to being easy, there was almost no setup required; you didn't need to register for API access and you only needed to pay for what you used.

Not only can we make paid command line access to remote programs easy, we can also bring these command line programs fully into the Unix paradigm by making them useful in pipelines so that they can easily interoperate.

Bitcoin Linux: Walkthrough

In this lab, you'll do the following steps:

  • Clone a GitHub repository containing two commands

  • Run a pipeline that downloads an image, pipes it to an Optical Character Recognition (OCR) API that outputs text, and that further pipes it to a machine transaction API that converts the English text to Chinese

We’ve made running the Bitcoin Linux example very easy:

## Clone this repository
git clone https://github.com/21dotco/bitcoin-linux.git

Install the following software on Linux:

sudo apt-get install libjpeg62-turbo-dev tesseract-ocr

Enter the newly-created code directory:

cd bitcoin-linux

Run the img2txt21 server and install the img2txt21 command

## Enter the img2txt21 directory from the bitcoin-linux directory
cd img2txt21

## Install the dependencies
sudo pip3 install pytesseract

## Run the server in the background
python3 server.py &

## Install img2txt21 as a command
sudo pip3 install --editable .

Return to the main bitcoin-linux directory, set your Google API key, run the translate21 server, and install the translate21 command:

## Enter the translate21 directory from the bitcoin-linux directory
cd ../translate21

## Set your Google API key
export GOOGLE_TRANSLATE_API_KEY=YOUR_KEY

## Run the server in the background
python3 translate-server.py &

## Install translate21 as a command
sudo pip3 install --editable .

Run the pipeline

curl -L http://bit.ly/bitlinux-img | img2txt21 --server='[::]:5001' | translate21 --server='[::]:5000'

This should print the following line to your console:

这是21比特币计算机教程的测试图像。这是GIMP 2.8生成的.png文件。我们正在测试图像文本的翻译。黑客快乐大家!

If you look at the image, you'll notice that it was in English, which means the the pipeline converted the image to English text and the English into Chinese.

Code review: img2txt21

Here is the code for the OCR command. Notice that it's just a tiny wrapper around the remote API that does the actual work.

import click

# import from the 21 Bitcoin Developer Library
from two1.commands.config import Config
from two1.wallet import Wallet
from two1.bitrequests import BitTransferRequests

username = Config().username
wallet = Wallet()
requests = BitTransferRequests(wallet, username)

@click.command()
@click.argument('file_name', required=False)
@click.option('--server', default='[::1]:5001', help='ip:port to connect to')
def img2txt21(server, file_name):
    """ Call the img to text api hosted on the micropayments server"""

    ## If a file isn't specified, read image from stdin
    if file_name:
      upload = requests.post('http://' + server + '/upload', files={'file': open(file_name, 'rb')})
    else:
      file = click.get_binary_stream('stdin')
      file_name = 'test.jpg'
      upload = requests.post('http://' + server + '/upload', files={'file': (file_name, file)})

    # convert image to text
    # print('Converting image file %s to text...' % file_name)
    ocr_url = 'http://' + server + '/ocr?image={0}&payout_address={1}'
    answer = requests.post(url=ocr_url.format(file_name, wallet.get_payout_address()))
    print(answer.text)

Code review: translate21

Here is the code for the translation command. Again, it's a very simple wrapper.

import sys
import click

#import from the 21 Developer Library
from two1.commands.config import Config
from two1.wallet import Wallet
from two1.bitrequests import BitTransferRequests

#set up bitrequest client for BitTransfer requests
wallet = Wallet()
username = Config().username
requests = BitTransferRequests(wallet, username)

@click.command()
@click.argument('inp_text', required=False)
@click.option('--server', default='[::1]:5000', help='ip:port to connect to')
def cli(server, inp_text):

    """ Call the translate api hosted on the micropayments server"""

    if not inp_text:
        inp_text = click.get_text_stream('stdin').read()

    # Send request to server with user input text and user's wallet address for payment
    sel_url = 'http://' + server + '/translate?text={0}&payout_address={1}'
    response = requests.get(url=sel_url.format(inp_text, wallet.get_payout_address()))

    # Print the translated text out to the terminal
    click.echo(response.text)

Other code

Not shown here, but present in the repository you downloaded, is the server code for the OCR server and translation server. The servers in this case wrap existing code and APIs, so they're fairly simple, but a real API could be powered by a complicated set of routines, exposing only the parts the user needs.

Next steps

See if you can write some new Bitcoin-paid command line programs that call remote APIs to do most or all of the work. If you write a command, we recommend mentioning in the Slack chatroom so that other people can start thinking about how to use it in powerful pipelines.