TimKnowsBest logotimknowsbestdot com

Building a Crypto Kill Switch

Updated: a month ago

4 MIN READ

Investing in crypto can be overwhelming for the average investor. On Binance, you have to understand trading pairs, endure decimal denominated assets, and enter 2FA. These points of friction are especially troublesome during price spikes - when the market price can swing dramatically during the few minutes it takes you to enter basic order info.

Although investors rarely suggest going all-in or all-out on an investment, it is nice to have a fail-safe for your assets.

I created a hardware IOT switch to act as a fail-safe for exiting the market. You can self host this switch on a Raspberry Pi Zero W so access to your private keys always remains on your local network. The switch has two states: ON and OFF. ON means you wish to stay in the crypto markets. OFF means you wish to move all of your holdings into US dollar backed stable coins. When switching from ON to OFF, the switch will remember your portfolio allocation percentages and automatically rebalance your portfolio when switching from OFF to ON. Switch state & portfolio contents are sent securely to your Telegram account.

My source code for everything listed in this project is available here

🔩 Hardware Required

16gb Micro SD card, Raspberry Pi Zero W, GPIO hammer header, Physical switch, Female to male GPIO jumper wires. Not pictured: 2.5A power supply

This is a simple Raspberry Pi GPIO based project. Parts & where to buy are listed below.

  • 16gb Micro SD card
  • Raspberry Pi Zero W
  • GPIO hammer header
  • Physical switch
  • Female to male GPIO jumper wires
  • 2.5A power supply
  • (optional) Solder iron kit

📘 Notes on Soldering

  • You can save some money & learn a new skill if you solder the GPIO header to the Raspberry Pi Zero W board. This is not recommended for beginners as it is easy to jump connectors by using too much solder.
  • If you decide to solder, please ensure you have a clean, well-ventilated environment.
If you decide to solder, please ensure you have a clean, well-ventilated environment.

🍔 Tech Stack

Python 3

🏃🏻 Getting Started

At its core, this project abstracts the process of transferring from crypto assets and stable coins to one function call. You are free to activate it using a custom controller like Siri Shortcuts or a Chat Bot, but I included instructions on working with a physical switch.

  1. Clone the crypto-kill-switch Github repo to your workspace.
git clone https://github.com/timothymiller/crypto-kill-switch.git
  1. Enter your Binance API Key &  Secret into crypto_algorithm.py
# Binance Specific API credentials
api_key = ""        # TODO Insert your API key here
api_secret = ""     # TODO Insert your secret here

3.    Enter your Telegram Bot token. Instructions on how to get your Telegram Bot token are available here.

bot = telegram.Bot(token='')  # TODO Insert Telegram secret here

🐧 Setting up your secure Linux environment

Setup your secure Linux environment to run the Python script. For this tutorial, I will demonstrate on a Raspberry Pi Zero W running the latest Raspbian.

  • Download the latest version of Raspbian Lite here
  • Flash the image to your SD card using Etcher

🖥 Enable SSH

For security reasons, ssh is no longer enabled by default. To enable it you need to place a blank text file called "ssh" in the root of the boot SD by doing the following:

  1. Open your favorite text editor.
  2. Click File / Save As ...
  3. Save file without a .txt extension
  4. Name the file ssh and save it

📡 Configure access to your wifi network

  1. Create a file called wpa_supplicant.conf and place it in the root of the SD card.
  2. Paste in the contents below. Enter your information for the name of your country code, wifi network name, and wifi network password.
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
   ssid="ENTER_YOUR_WIFI_NETWORK_NAME"
   psk="YOUR_WIFI_NETWORK_PASSWORD"
}

⚙️ Configuring the hardware

  • Attach your GPIO header to your Raspberry Pi.
  • Connect the female end of the GPIO jumper cables to GPIO #1 & #29. Order does not matter. A map of the GPIO layout for a Raspberry Pi board is available below.
A map of the GPIO layout for a Raspberry Pi Zero W board
  • Secure the male end of the jumper cables to the bottom connectors on the switch.
Secure the male end of the jumper cables to the bottom connectors on the switch.
  • Insert the micro SD card you prepared earlier & connect the Raspberry Pi Zero W to a power supply.
  • First boot may take up to ten minutes. Be patient, wait for your the device to appear on your network.
  • You can opt to build a custom case for your kill switch. The possibilities are limitless so I will not cover the build process for the wooden case in this tutorial.

🔨 Installing RPi.GPIO

We need to provide access to the GPIO pins for our Python code.

sudo apt-get update
sudo apt-get install rpi.gpio

🎬 Autostart the Python script

  • SFTP into the raspberry pi. Upload the project folder crypto-kill-switch to /home/pi/
  • SSH into the raspberry pi.
crontab -e
  • Add the following line
@reboot /home/pi/crypto-kill-switch/start.sh
  • Reboot the Raspberry Pi
sudo reboot now

🔬 Testing

You can confirm everything is working by sending commands to your Telegram Bot.

Commands

  • portfolio

༼ つ ◕_◕ ༽つ Returns your portfolio balance in a telegram message with style ༼ つ ◕_◕ ༽つ

🚧 Roadmap

Eventually I will update this switch to operate on the Binance DEX.