github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/usbwallet/trezor/trezor.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // This file contains the implementation for interacting with the Trezor hardware
    19  // wallets. The wire protocol spec can be found on the SatoshiLabs website:
    20  // https://wiki.trezor.io/Developers_guide-Message_Workflows
    21  
    22  // !!! STAHP !!!
    23  //
    24  // Before you touch the protocol files, you need to be aware of a breaking change
    25  // that occurred between firmware versions 1.7.3->1.8.0 (Model One) and 2.0.10->
    26  // 2.1.0 (Model T). The Ethereum address representation was changed from the 20
    27  // byte binary blob to a 42 byte hex string. The upstream protocol buffer files
    28  // only support the new format, so blindly pulling in a new spec will break old
    29  // devices!
    30  //
    31  // The Trezor devs had the foresight to add the string version as a new message
    32  // code instead of replacing the binary one. This means that the proto file can
    33  // actually define both the old and the new versions as optional. Please ensure
    34  // that you add back the old addresses everywhere (to avoid name clash. use the
    35  // addressBin and addressHex names).
    36  //
    37  // If in doubt, reach out to @karalabe.
    38  
    39  // To regenerate the protocol files in this package:
    40  //   - Download the latest protoc https://github.com/protocolbuffers/protobuf/releases
    41  //   - Build with the usual `./configure && make` and ensure it's on your $PATH
    42  //   - Delete all the .proto and .pb.go files, pull in fresh ones from Trezor
    43  //   - Grab the latest Go plugin `go get -u github.com/golang/protobuf/protoc-gen-go`
    44  //   - Vendor in the latest Go plugin `govendor fetch github.com/golang/protobuf/...`
    45  
    46  //go:generate protoc -I/usr/local/include:. --go_out=import_path=trezor:. messages.proto messages-common.proto messages-management.proto messages-ethereum.proto
    47  
    48  // Package trezor contains the wire protocol.
    49  package trezor
    50  
    51  import (
    52  	"reflect"
    53  
    54  	"github.com/golang/protobuf/proto"
    55  )
    56  
    57  // Type returns the protocol buffer type number of a specific message. If the
    58  // message is nil, this method panics!
    59  func Type(msg proto.Message) uint16 {
    60  	return uint16(MessageType_value["MessageType_"+reflect.TypeOf(msg).Elem().Name()])
    61  }
    62  
    63  // Name returns the friendly message type name of a specific protocol buffer
    64  // type number.
    65  func Name(kind uint16) string {
    66  	name := MessageType_name[int32(kind)]
    67  	if len(name) < 12 {
    68  		return name
    69  	}
    70  	return name[12:]
    71  }