Skip to content

Instantly share code, notes, and snippets.

@jhoenicke
Last active July 18, 2017 19:32
Show Gist options
  • Save jhoenicke/1ad2f99a275a5004f1d0 to your computer and use it in GitHub Desktop.
Save jhoenicke/1ad2f99a275a5004f1d0 to your computer and use it in GitHub Desktop.
Signing Example for python_trezor
#!/usr/bin/python
import binascii
import trezorlib.types_pb2 as proto_types
from trezorlib.client import TrezorClient
from trezorlib.tx_api import TxApiBitcoin, TxApiTestnet
from trezorlib.transport_hid import HidTransport
def main():
# List all connected TREZORs on USB
devices = HidTransport.enumerate()
# Check whether we found any
if len(devices) == 0:
print 'No TREZOR found'
return
# Use first connected device
transport = HidTransport(devices[0])
# Creates object for manipulating TREZOR
client = TrezorClient(transport)
# client.set_tx_api(TxApiTestnet)
client.set_tx_api(TxApiBitcoin)
amount = 100000000 ### amount in satoshi, (bitcoin * 100,000,000)
srcpath = "49'/2'/0'/0/0" ### bip32 path of the sender address (look it up in the receive tab under the barcode).
txid = "89e4b9883acda3325b2381ee4e9f42a12a4a3afbcad08151423b539b92c685fc"
### Source Transaction ID (txid)
outidx = 0 ### Index in source transaction of the output to spend.
destpath = "44'/0'/0'/0/0" ### bip32 path of destination address
desttype = proto_types.PAYTOADDRESS ### address type: PAYTOADDRESS or PAYTOP2SHWITNESS
fee = 20000; ### fee in satoshi
# The list of inputs.
inputs = [
proto_types.TxInputType(
script_type=proto_types.SPENDP2SHWITNESS,
address_n=client.expand_path(srcpath),
prev_hash=binascii.unhexlify(txid),
prev_index=outidx,
amount=amount,
),
]
# The list of outputs.
# An output script_type can be
# PAYTOADDRESS with address_n = client.expand_path("44'/...") (change)
# PAYTOADDRESS with address = '1...'
# PAYTOSCRIPTHASH with address = '3...'
# PAYTOMULTISIG with multisig.
# PAYTOOPRETURN with op_return_data.
outputs = [
proto_types.TxOutputType(
amount= amount - fee,
address_n=client.expand_path(destpath),
script_type=desttype,
),
]
# (signatures, serialized_tx) = client.sign_tx('Testnet', inputs, outputs)
(signatures, serialized_tx) = client.sign_tx('Bitcoin', inputs, outputs)
print 'Transaction:', binascii.hexlify(serialized_tx)
client.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment