Esempio n. 1
0
class TrezorIdentityProvider(IdentityProvider):
    def __init__(self, w3, index):
        self.w3 = w3
        self.client = TrezorClient(HidTransport.enumerate()[0])
        self.index = index
        self.address = self.w3.toChecksumAddress("0x" + bytes(
            self.client.ethereum_get_address([
                44 + bip32utils.BIP32_HARDEN, 60 +
                bip32utils.BIP32_HARDEN, bip32utils.BIP32_HARDEN, 0, index
            ])).hex())

    def get_address(self):
        return self.address

    def transact(self, transaction, out_f):
        print("Sending transaction to trezor for signature...\n", file=out_f)
        signature = self.client.ethereum_sign_tx(
            n=[
                44 + bip32utils.BIP32_HARDEN, 60 + bip32utils.BIP32_HARDEN,
                bip32utils.BIP32_HARDEN, 0, self.index
            ],
            nonce=transaction["nonce"],
            gas_price=transaction["gasPrice"],
            gas_limit=transaction["gas"],
            to=bytearray.fromhex(transaction["to"][2:]),
            value=transaction["value"],
            data=bytearray.fromhex(transaction["data"][2:]))

        transaction.pop("from")
        unsigned_transaction = serializable_unsigned_transaction_from_dict(
            transaction)
        raw_transaction = encode_transaction(unsigned_transaction,
                                             vrs=(signature[0],
                                                  int(signature[1].hex(), 16),
                                                  int(signature[2].hex(), 16)))

        print("Submitting transaction...\n", file=out_f)
        txn_hash = self.w3.eth.sendRawTransaction(raw_transaction)

        # Wait for transaction to be mined
        receipt = None
        while receipt is None:
            time.sleep(1)
            receipt = self.w3.eth.getTransactionReceipt(txn_hash)

        return receipt

    def sign_message(self, message, out_f, agent_version=2):
        n = self.client._convert_prime([
            44 + bip32utils.BIP32_HARDEN, 60 + bip32utils.BIP32_HARDEN,
            bip32utils.BIP32_HARDEN, 0, self.index
        ])
        print("Sending message to trezor for signature...\n", file=out_f)
        if agent_version == 1:
            message = self.w3.sha3(hexstr=message)
        else:
            message = message.lower().encode("utf-8")
        return self.client.call(
            proto.EthereumSignMessage(address_n=n, message=message)).signature
Esempio n. 2
0
class TrezorIdentityProvider(IdentityProvider):
    def __init__(self, w3, index):
        self.w3 = w3
        self.client = TrezorClient(HidTransport.enumerate()[0])
        self.index = index
        self.address = self.w3.toChecksumAddress(
            "0x" + bytes(self.client.ethereum_get_address([44 + BIP32_HARDEN,
                                                           60 + BIP32_HARDEN,
                                                           BIP32_HARDEN, 0,
                                                           index])).hex())

    def get_address(self):
        return self.address

    def transact(self, transaction, out_f):
        print("Sending transaction to trezor for signature...\n", file=out_f)
        signature = self.client.ethereum_sign_tx(n=[44 + BIP32_HARDEN, 60 + BIP32_HARDEN,
                                                    BIP32_HARDEN, 0, self.index],
                                                 nonce=transaction["nonce"],
                                                 gas_price=transaction["gasPrice"],
                                                 gas_limit=transaction["gas"],
                                                 to=bytearray.fromhex(
                                                     transaction["to"][2:]),
                                                 value=transaction["value"],
                                                 data=bytearray.fromhex(transaction["data"][2:]))

        transaction.pop("from")
        unsigned_transaction = serializable_unsigned_transaction_from_dict(
            transaction)
        raw_transaction = encode_transaction(unsigned_transaction,
                                             vrs=(signature[0],
                                                  int(signature[1].hex(), 16),
                                                  int(signature[2].hex(), 16)))
        return send_and_wait_for_transaction(raw_transaction, self.w3, out_f)

    def sign_message_after_soliditySha3(self, message):
        n = self.client._convert_prime([44 + BIP32_HARDEN,
                                        60 + BIP32_HARDEN,
                                        BIP32_HARDEN,
                                        0,
                                        self.index])
        return self.client.call(proto.EthereumSignMessage(address_n=n, message=message)).signature
DUMMY_SIZE = 500
SEQ = 0x01234567

SIG_DUMMY = shell
SIG_DUMMY += "\x63\xfd\x01\x20" * ((DUMMY_SIZE - len(shell)) / 4) # 1.2.0


devices = HidTransport.enumerate()
client = TrezorClient(HidTransport(devices[0]))

client.transport.session_begin()
tx = proto.SignTx()
tx.inputs_count = 1 
tx.outputs_count = 1 
tx.coin_name = "Bitcoin" 
res = client.call(tx)

if isinstance(res, proto.Failure):
    raise CallException("Signing failed")

if not isinstance(res, proto.TxRequest):
   raise CallException("Unexpected message")

print res

msg = types.TransactionType()
testInput = types.TxInputType(address_n=[0], 
                             prev_hash=binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'),
                             prev_index=0,
			     script_sig=SIG_DUMMY,
			     sequence=SEQ
from trezorlib import *
from trezorlib.client import TrezorClient
from trezorlib.transport_hid import HidTransport
import trezorlib.messages_pb2 as proto
import trezorlib.types_pb2 as types
import binascii

devices = HidTransport.enumerate()
client = TrezorClient(HidTransport(devices[0]))

client.transport.session_begin()
tx = proto.SignTx()
tx.inputs_count = 1
tx.outputs_count = 2
tx.coin_name = "Bitcoin"
res = client.call(tx)

if isinstance(res, proto.Failure):
    raise CallException("Signing failed")

if not isinstance(res, proto.TxRequest):
   raise CallException("Unexpected message")

dummyhdnode = types.HDNodeType(
	depth=0, 
	fingerprint=0,
	child_num=0,
	chain_code=binascii.unhexlify('2fb77e25cd3e2e034bcbafa1f81ec7e4caf927b06c87db296f1186208315525e'),
	public_key=binascii.unhexlify('03f645ec6544a92f951f3bca16a2bc1c56846d065594db222725d59b9902528385')
)
dummyhdnodepath = types.HDNodePathType(