Stellar are giving away some of their cryptocurrency, Lumens, to anyone who owns Bitcoin.

To get the Stellar, you need to prove you own the Bitcoin by providing your Bitcoin address(es) and then signing a message to prove you control that address.

I’ve used a lot of addresses, over time, so figuring out which of these addresses I need to provide can be time-consuming.

Bash to the rescue;

#!/bin/bash

# Script to find out the current BTC balance of all addresses to/from which you've sent Bitcoin
#
# Pre-requisites
#
# * curl - https://curl.haxx.se/
# * jq   - https://stedolan.github.io/jq/
#
# Usage
#
#   ./balances.sh [transaction data CSV filename]
#
# Expected output
#
# [BTC address] [final balance in satoshis]
# ...

set -euo pipefail

ADDRESS_FILE=$1  # Filename of transaction data CSV file, exported from your bitcoin wallet

main() {
  local readonly unique_address_file=unique_addresses.csv  # Use any name you like, for this

  get_unique_addresses ${ADDRESS_FILE} > ${unique_address_file}

  for addr in $(bare_addresses ${unique_address_file}); do
    echo $addr $(get_balance_for_address ${addr})
  done
}

get_unique_addresses() {
  local readonly tx_csv=$1
  # tx_csv is the filename of an export from your bitcoin wallet software
  grep -v Address ${tx_csv} | cut -d, -f3,4,5 | sort | uniq
}

bare_addresses() {
  local readonly file=$1
  cat ${file} | cut -d, -f3 | sed 's/"//g'
}

get_balance_for_address() {
  local readonly addr=$1
  fetch_json_for_address ${addr} | jq .addresses[0].final_balance
}


fetch_json_for_address() {
  local readonly addr=$1
  curl -s "https://blockchain.info/multiaddr?cors=true&active=${addr}"
}

main

To use this, I first export a CSV of all the bitcoin transactions I’ve ever done. Then I run the script like this;

./balances.sh tx.csv

…where tx.csv is the filename of my exported transaction data.

The script works through all the addresses I’ve ever interacted with, and looks up the details for that address on blockchain.info

NB: I included both sent and received transactions, because sometimes I’ve sent bitcoin to another wallet address, so it’s useful to find out the balance of addresses I’ve sent bitcoin to as well as bitcoin I’ve received. Be aware that if you’ve sent BTC to someone else, this script will show you their current bitcoin balance, if they haven’t sent the funds on to another address.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s