Skip to content

Instantly share code, notes, and snippets.

@brianddk
Last active February 24, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianddk/e57a4ef04e2a7fc24da29a4baf91b343 to your computer and use it in GitHub Desktop.
Save brianddk/e57a4ef04e2a7fc24da29a4baf91b343 to your computer and use it in GitHub Desktop.
A very simple one-to-one TXN for python trezor v0.11.0
#!/usr/bin/env python3
# [rights] Copyright Dan B. (brianddk) 2018 https://github.com/brianddk
# [license] Licensed under Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
# [repo] https://gist.github.com/brianddk/e57a4ef04e2a7fc24da29a4baf91b343
# [tipjar] LTC: LQjSwZLigtgqHA3rE14yeRNbNNY2r3tXcA
#
from trezorlib import btc, coins, messages as proto, tools, ui
from trezorlib.client import TrezorClient
from trezorlib.transport import get_transport
# self.setup_mnemonic_allallall() # see trezorlib///test_msg_signtx.py
# User Provided Fields; These are pulled from test scripts
# CHANGE THESE!!!
coin = "Testnet"
in1_prev_hash = "e5040e1bc1ae7667ffb9e5248e90b2fb93cd9150234151ce90e14ab2f5933bcd"
in1_prev_index = 0
in1_addr_path = "44'/1'/0'/0/0"
in1_amount = 31000000
out1_address = "msj42CCGruhRsFrGATiUuh25dtxYtnpbTx"
out1_amount = 30090000
# Defaults
sequence = 4294967293
# Code
prev_hash = bytes.fromhex(in1_prev_hash)
device = get_transport()
client = TrezorClient(transport=device, ui=ui.ClickUI())
in1 = proto.TxInputType(
address_n=tools.parse_path(in1_addr_path),
prev_hash=prev_hash,
prev_index=in1_prev_index,
amount=in1_amount,
script_type=proto.InputScriptType.SPENDADDRESS,
sequence=sequence
)
out1 = proto.TxOutputType(
address=out1_address,
amount=out1_amount,
script_type=proto.OutputScriptType.PAYTOADDRESS
)
_, serialized_tx = btc.sign_tx(client, coin, [in1], [out1], prev_txes=coins.tx_api[coin])
client.close()
print('"txn_hex": {', serialized_tx.hex(), '}')
@matejcik
Copy link

You can actually save a little work by using the tx_api instance directly:

txapi = coins.tx_api[coin]
_, serialized_tx = btc.sign_tx(client, coin, [in1], [out1], details=signtx, prev_txes=txapi)

you also don't have to specify details, only if you want to change the defaults

@brianddk
Copy link
Author

brianddk commented Dec 11, 2018

@matejcik , Very clever! I see now, passing in a TxApi object into prev_txes allows use of the __getitem__ overload avoided the later exception on SPENDADDRESS. Also see the defaulting of details... Thx again!

BTW... really great job on v0.11. I really like the new TxApi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment