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) # Print out TREZOR's features and settings print(client.features) # Get the first address of first BIP44 account # (should be the same address as shown in wallet.trezor.io) bip32_path = client.expand_path("44'/42'/0'/0/0") address = client.get_address('Decred', bip32_path) print('Decred address:', address) address_n = client.expand_path("0") xpub = client.get_public_node('Decred', address_n) print('xpub: ', xpub) client.close()
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) # Print out TREZOR's features and settings print client.features # Get the first address of first BIP44 account # (should be the same address as shown in mytrezor.com) bip32_path = client.expand_path("44'/0'/0'/0/0") address = client.get_address('Bitcoin', bip32_path) print 'Bitcoin address:', address client.close()
def main(): try: # List all connected TREZORs on USB devices = HidTransport.enumerate() # Check whether we found any if len(devices) == 0: input('No TREZOR found. Press any key...') return # Use first connected device transport = HidTransport(devices[0]) # Creates object for manipulating TREZOR client = TrezorClient(transport) # Print out TREZOR's features and settings print(client.features) # # Get the first address of first BIP44 account # # (should be the same address as shown in wallet.trezor.io) bip32_path = client.expand_path("m/44'/88888888'/0'/0/0") address = client.get_address('Hatch', bip32_path) print('Hatch address:', address) client.close() except Exception as e: print(str(e)) traceback.print_exc(file=sys.stdout) input('Press any key...')
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) # Print out TREZOR's features and settings print client.features # Get the first address of first BIP44 account # (should be the same address as shown in mytrezor.com) bip32_path = client.expand_path("44'/0'/0'/0/0") address = client.get_address('Bitcoin', bip32_path) print 'Bitcoin address:', address client.close()
def main(): # Use first connected device transport = get_transport() # Creates object for manipulating TREZOR client = TrezorClient(transport) # Print out TREZOR's features and settings print(client.features) # Get the first address of first BIP44 account # (should be the same address as shown in wallet.trezor.io) bip32_path = client.expand_path("44'/0'/0'/0/0") address = client.get_address('Bitcoin', bip32_path) print('Bitcoin address:', address) client.close()
ADRS_TOTAL = 5000 devices = HidTransport.enumerate() # Check whether we found any if len(devices) == 0: print('No TREZOR found') sys.exit() # Use first connected device transport = devices[0] # Creates object for manipulating TREZOR client = TrezorClient(transport) adrs = [] for i in tqdm(range(ADRS_TOTAL)): bip32_path = client.expand_path("49'/2'/0'/0/{0}".format(i)) adrs.append( client.get_address('Litecoin', bip32_path, script_type=proto.InputScriptType.SPENDP2SHWITNESS)) client.close() #print(adrs) pickle.dump(adrs, open('LTC.pickle', 'wb')) # SPENDWITNESS # SPENDP2SHWITNESS # SAMPLE ADR: MKVW8NewajMWijvKg7td2e7aUq8cyFYtPB # (trezorfeb) sbassi@Us-IT00354:~/projects/takocoin/takomisc/trezorfeb$ trezorctl get_address --coin Bitcoin --script-type segwit --address "m/49'/0'/0'/0/0" # bc1qrwl055767s26920eq7tfpaa4m97m302cnkh6w4 #
from trezorlib.client import TrezorClient from trezorlib.transport import get_transport client = TrezorClient(get_transport()) bip32_path = client.expand_path("44'/0'/0'/0/0") print(client.get_address('Bitcoin', bip32_path)) bip32_path = client.expand_path("44'/0'/0'/0/1") print(client.get_address('Bitcoin', bip32_path))
""" Generate btc address using Trezor and store them into a list and save it into a pickle. """ from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport from trezorlib import messages as proto from tqdm import tqdm import pickle ADRS_TOTAL = 5 devices = HidTransport.enumerate() # Check whether we found any if len(devices) == 0: print('No TREZOR found') sys.exit() # Use first connected device transport = devices[0] # Creates object for manipulating TREZOR client = TrezorClient(transport) adrs = [] for i in tqdm(range(1, ADRS_TOTAL+1)): bip32_path = client.expand_path("44'/145'/0'/0/{0}".format(i)) adrs.append(client.get_address('Bcash', bip32_path)) client.close() print(adrs) #pickle.dump(adrs, open('BCH.pickle', 'wb'))
def main(): numinputs = 100 sizeinputtx = 10 # Use first connected device try: transport = get_transport() except Exception as e: print(e) return print(transport) txstore = MyTxApiBitcoin() # Creates object for manipulating TREZOR client = TrezorClient(transport) # client.set_tx_api(TxApiTestnet) txstore.set_client(client) txstore.set_publickey( client.get_public_node(client.expand_path("44'/0'/0'"))) print("creating input txs") txstore.create_inputs(numinputs, sizeinputtx) print("go") client.set_tx_api(txstore) # client.set_tx_api(MyTxApiBitcoin()) # Print out TREZOR's features and settings print(client.features) # Get the first address of first BIP44 account # (should be the same address as shown in wallet.trezor.io) outputs = [ proto_types.TxOutputType( amount=0, script_type=proto_types.PAYTOADDRESS, address='p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm' # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') # address_n=client.expand_path("44'/0'/0'/0/35"), # address='3PUxV6Cc4udQZQsJhArVUzvvVoKC8ohkAj', ), # proto_types.TxOutputType( # amount=0, # script_type=proto_types.PAYTOOPRETURN, # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') # ), # proto_types.TxOutputType( # amount= 8120, # script_type=proto_types.PAYTOADDRESS, # address_n=client.expand_path("44'/1'/0'/1/0"), # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # address='14KRxYgFc7Se8j7MDdrK5PTNv8meq4GivK', # ), # proto_types.TxOutputType( # amount= 18684 - 2000, # script_type=proto_types.PAYTOADDRESS, # address_n=client.expand_path("44'/0'/0'/0/7"), # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # # address='1s9TSqr3PHZdXGrYws59Uaf5SPqavH43z', # ), # proto_types.TxOutputType( # amount= 1000, # script_type=proto_types.PAYTOADDRESS, # # address_n=client.expand_path("44'/0'/0'/0/18"), # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # # address='1NcMqUvyWv1K3Zxwmx5sqfj7ZEmPCSdJFM', # ), ] # (signatures, serialized_tx) = client.sign_tx('Testnet', inputs, outputs) (signatures, serialized_tx) = client.sign_tx('Bitcoin', txstore.get_inputs(), txstore.get_outputs()) print('Transaction:', binascii.hexlify(serialized_tx)) client.close()
#!/usr/bin/python3 import binascii from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport devices = HidTransport.enumerate() if len(devices) > 0: t = TrezorClient(devices[0]) else: raise Exception('No TREZOR found') for i in [0, 1, 2]: path = "m/10018'/%d'" % i pk = t.get_public_node(t.expand_path(path), ecdsa_curve_name='ed25519', show_display=True) print(path, '=>', binascii.hexlify(pk.node.public_key).decode())
#!/usr/bin/python3 import binascii from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport devices = HidTransport.enumerate() if len(devices) > 0: t = TrezorClient(devices[0]) else: raise Exception("No TREZOR found") for i in [0, 1, 2]: path = "m/10018'/%d'" % i pk = t.get_public_node(t.expand_path(path), ecdsa_curve_name="ed25519", show_display=True) print(path, "=>", binascii.hexlify(pk.node.public_key).decode())
def main(): numinputs = 600 sizeinputtx = 10 # 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 print(devices[0][0]) # transport = BridgeTransport(devices[0][0]) transport = HidTransport(devices[0]) txstore = MyTXAPIBitcoin() # Creates object for manipulating TREZOR client = TrezorClient(transport) # client.set_tx_api(TXAPITestnet()) txstore.set_client(client) txstore.set_publickey(client.get_public_node(client.expand_path("44'/0'/0'"))) print("creating input txs") txstore.create_inputs(numinputs, sizeinputtx) print("go") client.set_tx_api(txstore) # client.set_tx_api(MyTXAPIBitcoin()) # Print out TREZOR's features and settings print(client.features) # Get the first address of first BIP44 account # (should be the same address as shown in mytrezor.com) outputs = [ proto_types.TxOutputType( amount=0, script_type=proto_types.PAYTOADDRESS, address='p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm' # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') # address_n=client.expand_path("44'/0'/0'/0/35"), # address='3PUxV6Cc4udQZQsJhArVUzvvVoKC8ohkAj', ), # proto_types.TxOutputType( # amount=0, # script_type=proto_types.PAYTOOPRETURN, # op_return_data=binascii.unhexlify('2890770995194662774cd192ee383b805e9a066e6a456be037727649228fb7f6') # ), # proto_types.TxOutputType( # amount= 8120, # script_type=proto_types.PAYTOADDRESS, # address_n=client.expand_path("44'/1'/0'/1/0"), # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # address='14KRxYgFc7Se8j7MDdrK5PTNv8meq4GivK', # ), # proto_types.TxOutputType( # amount= 18684 - 2000, # script_type=proto_types.PAYTOADDRESS, # address_n=client.expand_path("44'/0'/0'/0/7"), # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # # address='1s9TSqr3PHZdXGrYws59Uaf5SPqavH43z', # ), # proto_types.TxOutputType( # amount= 1000, # script_type=proto_types.PAYTOADDRESS, # # address_n=client.expand_path("44'/0'/0'/0/18"), # # address='1PtCkQgyN6xHmXWzLmFFrDNA5vYhYLeNFZ', # address='1NcMqUvyWv1K3Zxwmx5sqfj7ZEmPCSdJFM', # ), ] # (signatures, serialized_tx) = client.sign_tx('Testnet', inputs, outputs) (signatures, serialized_tx) = client.sign_tx('Bitcoin', txstore.get_inputs(), txstore.get_outputs()) print('Transaction:', binascii.hexlify(serialized_tx)) client.close()
class TrezorClient(HardwareWalletClient): # device is an HID device that has already been opened. def __init__(self, device, path): super(TrezorClient, self).__init__(device) device.close() devices = HidTransport.enumerate() self.client = None for d in devices: if d[0] == path: transport = HidTransport(d) self.client = Trezor(transport) break # if it wasn't able to find a client, throw an error if not self.client: raise IOError("no Device") # Must return a dict with the xpub # Retrieves the public key at the specified BIP 32 derivation path def get_pubkey_at_path(self, path): expanded_path = self.client.expand_path(path) output = self.client.get_public_node(expanded_path) return json.dumps({'xpub':output.xpub}) # Must return a hex string with the signed transaction # The tx must be in the psbt format def sign_tx(self, tx): # Get this devices master key fingerprint master_key = self.client.get_public_node([]) master_fp = get_xpub_fingerprint(master_key.xpub) # Prepare inputs inputs = [] for psbt_in, txin in zip(tx.inputs, tx.tx.vin): txinputtype = TxInputType() # Check for 1 key if len(psbt_in.hd_keypaths) == 1: # Is this key ours if psbt_in.hd_keypaths.keys()[0] == master_fp: # Set the keypath txinputtype.address_n.extend(psbt_in.hd_keypaths.values()[0]) else: txinputtype.script_type = external # Set the input stuff txinputtype.prev_hash = txin.prevout.hash txinputtype.prev_index = txin.prevout.n txinputtype.sequence = txin.nSequence # Set spend type if txinputtype.script_type != EXTERNAL and psbt_in.non_witness_utxo: txinputtype.script_type = SPENDADDRESS # Check for multisig (more than 1 key) elif len(psbt_in.hd_keypaths) > 1: # Find our keypath for fp, keypath in psbt_in.hd_keypaths.items(): if fp == master_fp: # Set the keypaths txinputtype.address_n.extend(keypath) # Set the input stuff txinputtype.prev_hash = txin.prevout.hash txinputtype.prev_index = txin.prevout.n txinputtype.sequence = txin.nSequence # Set multisig multisig = proto_types.MultisigRedeemScriptType( pubkeys=[ proto_types.HDNodePathType(node=master_key, address_n=keypath), ], signatures=[b''], ) txinputtype.multisig = multisig break # Set spend type if psbt_in.non_witness_utxo: txinputtype.script_type = SPENDMULTISIG if psbt_in.witness_utxo: # Check if the output is p2sh if psbt_in.witness_utxo.is_p2sh(): txinputtype.script_type = SPENDP2SHWITNESS else: txinputtype.script_type = SPENDWITNESS # Set the amount if psbt_in.non_witness_utxo: txinputtype.amount = psbt_in.non_witness_utxo.vout[txin.prevout.n].nValue elif psbt_in.witness_utxo: txinputtype.amount = psbt_in.witness_utxo.nValue # append to inputs inputs.append(txinputtype) # prepare outputs outputs = [] for out in tx.tx.vout: txoutput = TxOutputType() if out.is_p2pkh: txoutput.address = to_address(out.scriptPubKey[2:22], b"\x00") txoutput.amount = out.nValue txoutput.script_type = PAYTOADDRESS elif out.is_p2sh: txoutput.address = to_address(out.scriptPubKey[3:23], b"\x05") else: # TODO: Figure out what to do here. for now, just break break # append to outputs outputs.append(txoutput) # Sign the transaction self.client.set_tx_api() signed_tx = self.client.sign_tx("Bitcoin", inputs, outputs, tx.tx.nVersion, tx.tx.nLockTime) print(signed_tx) return # Must return a base64 encoded string with the signed message # The message can be any string def sign_message(self, message): raise NotImplementedError('The HardwareWalletClient base class does not ' 'implement this method') # Setup a new device def setup_device(self): raise NotImplementedError('The HardwareWalletClient base class does not ' 'implement this method') # Wipe this device def wipe_device(self): raise NotImplementedError('The HardwareWalletClient base class does not ' 'implement this method')
#!/usr/bin/env python2 from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport from binascii import hexlify, unhexlify # for more details on this, see python-trezor client = TrezorClient(HidTransport(HidTransport.enumerate()[0])) bip32_path = client.expand_path("10015'/0'") masterkey = client.encrypt_keyvalue( bip32_path, "Enable labeling?", unhexlify( "fedcba98765432100123456789abcdeffedcba98765432100123456789abcdef"), True, True) print 'Key:', hexlify(masterkey)
#!/usr/bin/env python2 from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport from binascii import hexlify, unhexlify # for more details on this, see python-trezor client = TrezorClient(HidTransport(HidTransport.enumerate()[0])) bip32_path = client.expand_path("10015'/0'") masterkey = client.encrypt_keyvalue( bip32_path, "Enable labeling?", unhexlify("fedcba98765432100123456789abcdeffedcba98765432100123456789abcdef"), True, True ) print 'Key:', hexlify(masterkey)
#!/usr/bin/env python3 import binascii from trezorlib.client import TrezorClient from trezorlib.transport_hid import HidTransport devices = HidTransport.enumerate() if len(devices) > 0: t = TrezorClient(devices[0]) else: raise Exception("No Trezor found") for i in [0, 1, 2]: path = "m/10018'/%d'" % i pk = t.get_public_node( t.expand_path(path), ecdsa_curve_name="ed25519", show_display=True ) print(path, "=>", binascii.hexlify(pk.node.public_key).decode())