Skip to main content

Python library for communicating with Trezor Hardware Wallet

Project description

python-trezor

image repology image

Python library and commandline client for communicating with Trezor Hardware Wallet

See https://trezor.io for more information

Install

Python-trezor requires Python 3.5 or higher, and libusb 1.0. The easiest way to install it is with pip. The rest of this guide assumes you have a working pip; if not, you can refer to this guide.

Quick installation

On a typical Linux / Mac / BSD system, you already have all you need. Install trezor with:

pip3 install --upgrade setuptools
pip3 install trezor

On Windows, you also need to install libusb and the appropriate drivers. This is, unfortunately, a topic bigger than this README.

Older Trezor One support

If your Trezor One is on firmware 1.6.3 or older, you will need HIDAPI support for it to be recognized. That requires additional packages.

Debian / Ubuntu

On a Debian or Ubuntu based system, you can install these:

sudo apt-get install python3-dev python3-pip cython3 libusb-1.0-0-dev libudev-dev

Windows

On a Windows based system, you can install these (for more info on choco, refer to this):

choco install vcbuildtools python3 protoc
refreshenv
pip3 install protobuf

When installing the trezor library, you need to specify that you want hidapi:

pip3 install --upgrade setuptools
pip3 install trezor[hidapi]

Ethereum support

Ethereum requires additional python packages. Instead of pip3 install trezor, specify pip3 install trezor[ethereum].

You can combine it with the above, to get both HIDAPI and Ethereum support:

pip3 install trezor[ethereum,hidapi]

FreeBSD

On FreeBSD you can install the packages:

pkg install security/py-trezor

or build via ports:

cd /usr/ports/security/py-trezor
make install clean

Building from source

Sometimes you might need to install the latest-and-greatest unreleased version straight from GitHub. You will need some prerequisites first:

sudo apt-get install protobuf-compiler protobuf-dev
pip3 install protobuf

If you just need to install the package, you can use pip again:

pip3 install git+https://github.com/trezor/python-trezor

If you want to work on the sources, make a local clone:

git clone https://github.com/trezor/python-trezor
cd python-trezor
python3 setup.py prebuild
python3 setup.py develop

Command line client (trezorctl)

The included trezorctl python script can perform various tasks such as changing setting in the Trezor, signing transactions, retrieving account info and addresses. See the docs/ sub folder for detailed examples and options.

NOTE: An older version of the trezorctl command is available for Debian Stretch (and comes pre-installed on Tails OS).

Python Library

You can use this python library to interact with a Bitcoin Trezor and use its capabilities in your application. See examples here in the tools/ sub folder.

PIN Entering

When you are asked for PIN, you have to enter scrambled PIN. Follow the numbers shown on Trezor display and enter the their positions using the numeric keyboard mapping:

7 8 9
4 5 6
1 2 3

Example: your PIN is 1234 and Trezor is displaying the following:

2 8 3
5 4 6
7 9 1

You have to enter: 3795

Contributing

Python-trezor pulls coins info and protobuf messages from trezor-common repository. If you are developing new features for Trezor, you will want to start there. Once your changes are accepted to trezor-common, you can make a PR against this repository. Don't forget to update the submodule with:

git submodule update --init --remote

Then, rebuild the protobuf messages and get coins.json by running:

python3 setup.py prebuild

To get support for BTC-like coins, these steps are enough and no further changes to the library are necessary.

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog.

At the moment, the project does not adhere to Semantic Versioning. That is expected to change with version 1.0.

0.11.6 - 2019-12-30

Added

  • support for get-and-increase FIDO counter operation
  • support for setting wipe code
  • trezorctl device recover supports --u2f-counter option to set the FIDO counter to a custom value

Changed

  • trezorctl command was reworked for ease of use and maintenance. See trezorctl --help and OPTIONS.rst for details. f#510
  • updated EOS transaction parser to match cleos in delegatebw and undelegatebw actions f#680 f#681
  • RecoveryDevice does not set fields when doing dry-run recovery f#666

Fixed

  • fixed "expand words" functionality in trezorctl device recover f#778

Removed

  • trezorctl no longer interactively signs Bitcoin-like transactions, the only allowed input format is JSON. See docs/transaction-format.md for details.
  • support for "load device by xprv" was removed from firmware and trezorlib

0.11.5 - 2019-09-26

Added

  • trezorctl can dump raw protobuf bytes in debug output f#117
  • trezorctl shows a warning when activating Shamir Backup if the device does not support it f#445
  • warnings are emitted when encountering unknown value for a protobuf enum f#363
  • debug messages show enum value names instead of raw numbers
  • support for packed repeated encoding in the protobuf decoder
  • in trezorctl firmware-update, the new --beta switch enables downloading beta firmwares. By default, only stable firmware is used. f#411, f#420
  • in trezorctl firmware-update, the new --bitcoin-only switch enables downloading Bitcoin-only firmware
  • support for FIDO2 resident credential management
  • support for SD-protect features

Changed

  • package directory structure was changed: src subdirectory contains sources and tests subdirectory contains tests, so that cwd is not cluttered
  • trezorctl script was moved into a module trezorlib.cli.trezorctl and is launched through the entry_points mechanism. This makes it usable on Windows
  • pyblake2 is no longer required on Python 3.6 and up
  • input flows can only be used in with-block (only relevant for unit tests)
  • if not specified, trezorctl will set label to "SLIP-0014" in SLIP-0014 mode
  • in clear_session the client also forgets the passphrase state for TT f#525

Fixed

  • trezorctl will properly check if a firmware is present on a new T1 f#224

Removed

  • device test suite was moved out of trezor package

0.11.4 - 2019-07-31

Added

  • trezorctl support for SLIP-39 Shamir Backup
  • support for Binance Chain

0.11.3 - 2019-05-29

Added

  • trezorctl can now send ERC20 tokens
  • trezorctl usb-reset will perform USB reset on devices in inconsistent state
  • set-display-rotation command added for TT firmware 2.1.1
  • EOS support f#87
  • Tezos: add voting support f#41
  • dict_to_proto now allows enum values as strings

Changed

  • Minimum firmware versions bumped to 1.8.0 and 2.1.0
  • Cleaner errors when UI object is not supplied
  • Generated files are now part of the source tarball again. That means that protoc is no longer required.

Fixed

  • Ethereum commands in trezorctl now work
  • Memory debugging tools now work again

Removed

  • Tron and Ontology support removed until implementations exist in Trezor firmware

0.11.2 - 2019-02-27

Added

  • full support for bootloader 1.8.0 and relevant firmware upgrade functionality
  • trezorctl: support fully offline signing JSON-encoded transaction data
  • trezorctl: dry-run for firmware upgrade command
  • client: new convenience function get_default_client for simple script usage
  • Dash: support DIP-2 special inputs #351
  • Ethereum: add get_public_key methods

Changed

  • coins with BIP-143 fork id (BCH, BTG) won't require prev_tx #352
  • device recovery will restore U2F counter
  • Cardano: change network to protocol_magic
  • tests can run interactively when INTERACT=1 environment variable is set
  • protobuf: improved to_dict function

Deprecated

  • trezorctl: interactive signing with sign-tx is considered deprecated

0.11.1 - 2018-12-28

Fixed

  • crash when entering passphrase on device with Trezor T
  • Qt widgets should only import QtCore #349

0.11.0 - 2018-12-06

Incompatible changes

  • removed support for Python 3.3 and 3.4
  • major refactor of TrezorClient and UI handling. Implementers must now provide a "UI" object instead of overriding callbacks #307, #314
  • protobuf classes now use a get_fields() method instead of FIELDS field #312
  • all methods on TrezorClient class are now in separate modules and take a TrezorClient instance as argument #276
  • mixin classes are also removed, you are not supposed to extend TrezorClient anymore
  • TrezorClientDebugLink was moved to debuglink module
  • changed signature of trezorlib.btc.sign_tx
  • @field decorator was replaced by an argument to @expect

Added

  • trezorlib now has a hardcoded check preventing use of outdated firmware versions #283
  • Ripple support #286
  • Zencash support #287
  • Cardano support #300
  • Ontology support #301
  • Tezos support #302
  • Capricoin support #325
  • limited Monero support (can only get address/watch key, monerowallet is required for signing)
  • support for input flow in tests makes it easier to control complex UI workflows #314
  • protobuf.dict_to_proto can create a protobuf instance from a plain dict
  • support for smarter methods in trezord 2.0.25 and up
  • support for seedless setup
  • trezorctl: firmware handling is greatly improved #304, #308
  • trezorctl: Bitcoin-like signing flow is more user-friendly
  • tx_api now supports Blockbook backend servers

Changed

  • better reporting for debuglink expected messages
  • replaced Ed25519 module with a cleaner, optimized version
  • further reorganization of transports makes them more robust when dependencies are missing
  • codebase now follows Black code style
  • in Qt modules, Qt5 is imported first #315
  • TxApiInsight is just TxApi
  • device.reset and device.recover now have reasonable defaults for all arguments
  • protobuf classes are no longer part of the source distribution and must be compiled locally #284
  • Stellar: addresses are always strings

Removed

  • set_tx_api method on TrezorClient is replaced by an argument for sign_tx
  • caching functionality of TxApi was moved to a separate test-support class
  • Stellar: public key methods removed
  • EncryptMessage and DecryptMessage actions are gone

Fixed:

  • TrezorClient can now detect when a HID device is removed and a different one is plugged in on the same path
  • trezorctl now works with Click 7.0 and considers "_" and "-" as same in command names #314
  • bash completion fixed
  • Stellar: several bugs in the XDR parser were fixed

0.10.2 - 2018-06-21

Added

  • stellar_get_address and _public_key functions support show_display parameter
  • trezorctl: stellar_get_address and _public_key commands for the respective functionality

Removed

  • trezorctl: list_coins is removed because we no longer parse the relevant protobuf field (and newer Trezor firmwares don't send it) #277

Fixed

  • test support module was not included in the release, so code relying on the deprecated ckd_public module would fail #280

0.10.1 - 2018-06-11

Fixed

  • previous release fails to build on Windows #274

0.10.0 - 2018-06-08

Added

  • Lisk support #197
  • Stellar support #167, #268
  • Wanchain support #230
  • support for "auto lock delay" feature
  • TrezorClient takes an additional argument state that allows reusing the previously entered passphrase #241
  • USB transports mention udev rules in exception messages #245
  • log.enable_debug_output function turns on wire logging, instead of having to use TrezorClientVerbose
  • BIP32 paths now support 123h in addition to 123' to indicate hardening
  • trezorctl: -p now supports prefix search for device path #226
  • trezorctl: smarter handling of firmware updates #242, #269

Changed

  • reorganized transports and moved into their own transport submodule
  • protobuf messages and coins info is now regenerated at build time from the trezor-common repository #248
  • renamed ed25519raw to _ed25519 to indicate its privateness
  • renamed ed25519cosi to cosi and expanded its API
  • protobuf messages are now logged through Python's logging facility instead of custom printing through VerboseWireMixin
  • client.format_protobuf is moved to protobuf.format_message
  • tools.Hash is renamed to tools.btc_hash
  • coins module coins_txapi is renamed to tx_api. coins_slip44 is renamed to slip44.
  • build: stricter flake8 checks
  • build: split requirements to separate files
  • tests: unified finding test device, while respecting TREZOR_PATH env variable.
  • tests: auto-skip appropriately marked tests based on Trezor device version
  • tests: only show wire output when run with -v
  • tests: allow running xfailed tests selectively based on pytest.ini
  • docs: updated README with clearer install instructions #185
  • docs: switched changelog to Keep a Changelog format #94

Deprecated

  • ckd_public is only maintained in tests.support submodule and considered private
  • TrezorClient.expand_path is moved to plain function tools.parse_path
  • TrezorDevice is deprecated in favor of transport.enumerate_devices and transport.get_transport
  • XPUB-related handling in tools is slated for removal

Removed

  • most Python 2 compatibility constructs are gone #229
  • TrezorClientVerbose and VerboseWireMixin is removed
  • specific tx_api.TxApi* classes removed in favor of coins.tx_api
  • client.PRIME_DERIVATION_FLAG is removed in favor of tools.HARDENED_FLAG and tools.H_()
  • hard dependency on Ethereum libraries and HIDAPI is changed into extras that need to be specified explicitly. Require trezor[hidapi] or trezor[ethereum] to get them.

Fixed

  • WebUSB enumeration returning bad devices on Windows 10 #223
  • sign_tx operation sending empty address string #237
  • Wrongly formatted Ethereum signatures #236
  • protobuf layer would wrongly encode signed integers #249, #250
  • protobuf pretty-printing broken on Python 3.4 #256
  • trezorctl: Matrix recovery on Windows wouldn't allow backspace #207
  • aes_encfs_getpass.py: fixed Python 3 bug #169

0.9.1 - 2018-03-05

Added

  • proper support for Trezor model T
  • support for Monacoin
  • improvements to trezorctl:
    • add pretty-printing of features and protobuf debug dumps (fixes #199)
    • support TREZOR_PATH environment variable to preselect a Trezor device.

Removed

  • gradually dropping Python 2 compatibility (pypi package will now be marked as Python 3 only)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trezor-0.11.6.tar.gz (302.8 kB view hashes)

Uploaded Source

Built Distribution

trezor-0.11.6-py3-none-any.whl (664.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page