Skip to content

Trezor Message Format

Bitcoin Solutions Ltd edited this page Jan 30, 2014 · 1 revision

The Trezor uses a binary message format consisting of a header and detail section defined as:

Bytes=##cclllldddddd...

## = Literal '##' for message alignment
cc = Unsigned short (2 bytes) for message type code (e.g. 0 = Initialize)
llll = Unsigned long (4 bytes) for detail message length
ddddd... = Detail (protocol buffer message)

Numerical values are Big-endian following the Python Byte Ordering and Alignment specification of ">HL".

In Java, you would use code similar to this to read a message:

private AbstractMessage readMessage(DataInputStream in) throws IOException {

  // Read and throw away the magic header markers
  in.readByte();
  in.readByte();

  // Read the header code and select a suitable parser
  Short headerCode = in.readShort();
  Parser parser = MessageType.getParserByHeaderCode(headerCode);

  // Read the detail length
  int detailLength = in.readInt();

  // Read the remaining bytes
  byte[] detail = new byte[detailLength];
  int actualLength = in.read(detail, 0, detailLength);

  // Verify the read
  Preconditions.checkState(actualLength == detailLength,"Detail not read fully. Expected="+detailLength+" actual="+actualLength);

  // Parse the detail into a message
  return (AbstractMessage) parser.parseFrom(detail);
}
Clone this wiki locally