trezor.go 3.0 KB

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