Trezor hardware wallet with Python
Tags: blockchain, cryptocurrency, python, security
Published: 2019-09-20 20:56:44

<a href="https://trezor.io/">Trezor</a> 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 <a href="https://pythonbasics.org/">basics of Python programming</a>. We will use a module named <a href="https://github.com/trezor/trezor-firmware/tree/master/python">trezorlib</a>.

    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:

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fthegadgetflow.com%2Fwp-content%2Fuploads%2F2018%2F04%2FTrezor-One-Hardware-Bitcoin-Wallet-03.jpg&f=1&nofb=1">

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


Once you got those basics working, you can do many fancy stuff. Like <a href="https://github.com/romanz/trezor-sq/blob/f9689a55ae1c0127c792fb8d1b0edeb3830a2431/src/trezor-gpg-sign.py">GPG signing with the Trezor</a>.

Related links:
* <a href="https://trezor.io/">Trezor hardware wallet</a>
* <a href="https://pythonbasics.org">Learn Python programming</a>
* <a href="https://github.com/trezor/trezor-firmware/tree/master/python">Trezorlib</a>