DEV Community

loading...
Cover image for Trezor hardware wallet with Python

Trezor hardware wallet with Python

codesharedot
・2 min read

Trezor is a "hardware wallet". It's an electronic device you can plugin to your computer for various purposes:

  • store crypto-currencies (cold storage)
  • U2F authentication
  • use as password manager (as opposed to keepass, pass etc which store passwords on your internet-connected computer)
  • SSH
  • Encryption via GPG

And many more cool stuff.

This is a lot better than leaving all your sensitive data on an internet connected computer (many people have lost bitcoins or passwords that way).

We will interact with the device. Lets code some Python!

Trezor interaction

Before you get started, make sure you know the basics of Python programming. We will use a module named trezorlib.

pip3 install trezor

Once installed, you can use the code below to turn on the device. It will show the home screen.

#!/usr/bin/python3
from trezorlib.tools import parse_path                                                                                                         
from trezorlib import tezos, ui, device                                                                                                        
from trezorlib import messages as proto                                                                                                        
from trezorlib.transport import TransportException                                                                                             
from trezorlib.exceptions import TrezorFailure                                                                                                 
from trezorlib import btc, coins, messages as proto, tools, ui                                                                                 
from trezorlib.client import TrezorClient                                                                                                      
from trezorlib.transport import get_transport                                                                                                  

device = get_transport()                                                                                                                       
client = TrezorClient(transport=device, ui=ui.ClickUI())                                                                                       

# check PIN protection                                                                                                                         
features = client.call(proto.Initialize())                                                                                                     
print('Has PIN protection? ', features.pin_protection)     

Pretty basic, on lights up the screen. Something like this:

It also returns if the trezor device has pin protection configured.

Once you got those basics working, you can do many fancy stuff. Like GPG signing with the Trezor.

Related links:

Discussion (0)