1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00
trezor-firmware/tools/tx_sign_tool.py

115 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
#
# Copyright (C) 2017 mruddy
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import binascii
from trezorlib.client import TrezorClient
from trezorlib.transport_hid import HidTransport
2017-06-23 19:31:42 +00:00
from trezorlib.tx_api import TxApiBitcoin, TxApiTestnet, TxApiLitecoin
from trezorlib import types_pb2 as types
2017-06-23 19:31:42 +00:00
# Python2 vs Python3
try:
input = raw_input
except NameError:
pass
2017-02-26 16:11:31 +00:00
def get_client():
2017-06-23 19:31:42 +00:00
devices = HidTransport.enumerate() # list all connected TREZORs on USB
if len(devices) == 0: # check whether we found any
2017-02-26 16:11:31 +00:00
return None
2017-06-23 19:31:42 +00:00
transport = HidTransport(devices[0]) # use first connected device
return TrezorClient(transport) # creates object for communicating with TREZOR
2017-02-26 16:11:31 +00:00
def get_txapi():
2017-06-23 19:31:42 +00:00
coin = input('Which coin {Bitcoin, Testnet, Litecoin}? ').strip()
2017-02-26 16:11:31 +00:00
if coin not in {'Bitcoin', 'Testnet', 'Litecoin'}:
return None, None
txapi_lookup = {
'Bitcoin': TxApiBitcoin,
'Testnet': TxApiTestnet,
'Litecoin': TxApiLitecoin
}
2017-02-26 16:11:31 +00:00
return coin, txapi_lookup[coin]
2017-02-26 16:11:31 +00:00
def main():
client = get_client()
if not client:
print('No TREZOR connected')
return
print()
print('Welcome to the user-unfriendly transaction signing tool')
print('USE AT YOUR OWN RISK!!!')
print()
coin, txapi = get_txapi()
if not txapi:
print('Coin not supported')
return
client.set_tx_api(txapi)
inputs = []
while True:
2017-02-26 16:11:31 +00:00
print()
2017-06-23 19:31:42 +00:00
prev_in_hash = input('Previous input hash (empty to move on): ').strip()
if prev_in_hash == '':
break
2017-06-23 19:31:42 +00:00
prev_in_vout = input('Previous input index: ').strip()
addrn = input("Node path to sign with (e.g.- %s/0'/0/0): " % coin).strip()
inputs.append(types.TxInputType(
2017-06-23 19:31:42 +00:00
prev_hash=binascii.unhexlify(prev_in_hash),
prev_index=int(prev_in_vout, 10),
address_n=client.expand_path(addrn)
))
outputs = []
while True:
2017-02-26 16:11:31 +00:00
print()
2017-06-23 19:31:42 +00:00
out_addr = input('Pay to address (empty to move on): ').strip()
if out_addr == '':
break
2017-06-23 19:31:42 +00:00
out_amount = input('Amount (in satoshis): ').strip()
outputs.append(types.TxOutputType(
2017-06-23 19:31:42 +00:00
amount=int(out_amount, 10),
script_type=types.PAYTOADDRESS,
address=out_addr
))
(signatures, serialized_tx) = client.sign_tx(coin, inputs, outputs)
2017-02-26 16:11:31 +00:00
client.close()
print()
print('Signed Transaction:', binascii.hexlify(serialized_tx))
# note: these api's are useful for checking and sending the output of this tool:
# https://btc.blockr.io/tx/push -or- https://live.blockcypher.com/btc/pushtx/
# https://tbtc.blockr.io/tx/push -or- https://live.blockcypher.com/btc-testnet/pushtx/
# https://ltc.blockr.io/tx/push -or - https://live.blockcypher.com/ltc/pushtx/
if __name__ == '__main__':
main()