How to Tip Bitcoin With a Single Command

This article was prepared by a guest contributor to ProgrammableWeb. The opinions expressed in this article are the author's own and do not necessarily reflect the view of ProgrammableWeb or its editorial staff.

If you’re like me, you spend the majority of your time at the command line. A power user can write code, install packages and configure servers, all without leaving the terminal. If I wanted to send a friend a candy’s worth of bitcoin from the command line, here’s how:

$ ./send_a_candy.sh 17astdTmG8zzVmry8mV8A7atAr3XefEgRX

This script sends 200 bits (0.0002 bitcoins, or about 4 cents) to any provided bitcoin address. How does it work? It runs a slightly longer but simple command using BlockCypher’s microtransaction API. Here’s my script to send a candy’s worth of bitcoin for Ubuntu (should be similar for any *nix):

https://gist.github.com/matthieu/e4d139ada2419b4ed5fb#file-send_a_candy-sh

1 $ cat ./send_a_candy.sh
2 #!/bin/bash
3 openssl enc -d -aes-256-cbc -nosalt -base64 -in send_a_candy.json.aes256 2>&1 | \
4 curl -H "Content-Type: application/json" -d @- https://api.blockcypher.com/v1/btc/main/txs/micro?to_address=$1

The third line uses OpenSSL to decrypt a password-protected file that contains a JSON document (you can see how to create the file below). The document describes a bitcoin microtransaction in just three lines. Once decrypted, the document is piped into a curl command that will send it using an HTTP POST on the BlockCypher API. The API then does all the grunt work of building a full bitcoin transaction, minimizing the fees to fractions of a cent and sending the transaction all over the world on the bitcoin network.

To send money with bitcoin, all that’s required is a private key, which is like a very long, random password. The private key is used to digitally sign a transaction, proving that the money belongs to you. With that private key comes a public key and an address. To receive bitcoin, all you need is a person’s address. Because the private key can be used by anyone to spend money, it’s sensitive data, which is why we only store it encrypted.

All the microtransaction API needs is four fields:

  • from_private: the private key for the address you’re sending from (for more advanced scenarios, you can also send a public key and sign yourself)
  • value_satoshis: the amount sent in satoshis; 100 million satoshis make a bitcoin
  • to_address: the bitcoin address to send the money to
  • token: the BlockCypher API token

Each field can be provided either in a JSON document or as a URL property. To make tipping from the command line easy, the to_address field is passed as an argument to the script and then added as a URL property. All the other fields are in a precreated JSON document that doesn’t need to change.

Setting this up for yourself requires only a few steps. We’ll start with send_a_candy.sh, which sends 20,000 satoshis (0.0002 bitcoins, or about 4 cents).

1. Get a new tipping address (do not reuse the one below):
https://gist.github.com/matthieu/f7366ddc820785cce85e#file-new_address_curl

$ curl -X POST https://api.blockcypher.com/v1/btc/main/addrs
{
 "private": "da01587298ff1721a968539e87329abe18f9b20cb46124eb650671662d3acd1a",
 "public": "02d47eaaca7f9b96a977c996396d1668a729155c02a18a37429ca5be9cfda91893",
 "address": "1PjfjXBM4GQs8FaNFESiUZzQwVxMHXdBXG",
 "wif": "L4XV4V5ACQmrtJn7ombma3sdqNrn1T9FFfwjLUaHVmd7Qz5iNHTt"
}

2. Send a small amount of money to the new address using your regular wallet, just enough for tips.

3. Create a JSON document to describe the transaction, replacing the private key. Let’s start with 20,000 satoshis (~ 4 cents).
https://gist.github.com/matthieu/2beaa4ea0cfa9542e33c#file-send_a_candy-...

$ cat send_a_candy.json
{
 "from_private": "da01587298ff1721a968539e87329abe18f9b20cb46124eb650671662d3acd1a",
 "value_satoshis": 2000000,
 "token": "37048cd01f250eca918ab55b658de3ec"
}

4. Encrypt it so the private key is not in clear on your machine.
https://gist.github.com/matthieu/bd4179143d00194bd652#file-encrypt_send_...

$ openssl enc -aes-256-cbc -nosalt -base64 -in send_a_candy.json -out send_a_candy.json.aes256

5. Finally, create send_a_candy.sh and run it (passing an address) to share some bitcoins!

And for friends who are extra nice, I also have a send_a_beer.sh script sending 0.02 bitcoins (2,000,000 satoshis) just by replacing the amount in the JSON document.

For more details, check out the BlockCypher microtransaction API. It works for any type of microtransaction. Also make sure to check all the other BlockCypher bitcoin APIs — maybe one of them will give you an idea for a fun project.

Matthieu Riou Matthieu Riou, CTO and Founder of BlockCypher. BlockCypher’s web services enable block chain applications to be built easily and reliably. Matthieu has architected many software systems at MommaZoo, C3, Intalio, BEA, Reef, BNP, and Societe Generale. He's a member and former VP at the Apache Software Foundation and has founded and mentored many different open source projects, from distributed databases to build tools. Matthieu knows 10+ programming languages and all tools ranging from Lisp to Java and Node.js. He holds a BS and MS in Computer Science and Artificial Intelligence from Ecole des Mines d'Ales.

Comments