Skip to content

Commit

Permalink
chore(common): separate definitions of protobuf options
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
M1nd3r committed Oct 16, 2024
1 parent a2a189c commit 7041dcd
Show file tree
Hide file tree
Showing 25 changed files with 935 additions and 843 deletions.
6 changes: 3 additions & 3 deletions common/protob/messages-bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package hw.trezor.messages.bitcoin;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageBitcoin";

option (include_in_bitcoin_only) = true;

import "messages.proto";
import "messages-common.proto";
import "options.proto";

option (include_in_bitcoin_only) = true;

/**
* Type of script which will be used for transaction input
Expand Down
4 changes: 2 additions & 2 deletions common/protob/messages-common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package hw.trezor.messages.common;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageCommon";

option (include_in_bitcoin_only) = true;
import "options.proto";

import "messages.proto";
option (include_in_bitcoin_only) = true;

/**
* Response: Success of the previous request
Expand Down
4 changes: 2 additions & 2 deletions common/protob/messages-crypto.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package hw.trezor.messages.crypto;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageCrypto";

option (include_in_bitcoin_only) = true;
import "options.proto";

import "messages.proto";
option (include_in_bitcoin_only) = true;

/**
* Request: Ask device to encrypt or decrypt value of given key
Expand Down
6 changes: 3 additions & 3 deletions common/protob/messages-debug.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package hw.trezor.messages.debug;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageDebug";

option (include_in_bitcoin_only) = true;

import "messages.proto";
import "messages-common.proto";
import "messages-management.proto";
import "options.proto";

option (include_in_bitcoin_only) = true;

/**
* Request: "Press" the button on the device
Expand Down
4 changes: 2 additions & 2 deletions common/protob/messages-management.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package hw.trezor.messages.management;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageManagement";

option (include_in_bitcoin_only) = true;
import "options.proto";

import "messages.proto";
option (include_in_bitcoin_only) = true;

/**
* Type of the mnemonic backup given/received by the device during reset/recovery.
Expand Down
4 changes: 2 additions & 2 deletions common/protob/messages-thp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package hw.trezor.messages.thp;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageThp";

option (include_in_bitcoin_only) = true;
import "options.proto";

import "messages.proto";
option (include_in_bitcoin_only) = true;

/**
* Only for internal use.
Expand Down
62 changes: 2 additions & 60 deletions common/protob/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,9 @@ package hw.trezor.messages;
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessage";

option (include_in_bitcoin_only) = true;

import "google/protobuf/descriptor.proto";

/************************* WARNING ***********************
Due to the way extensions are accessed in pb2py, there needs to be a globally unique
name-ID mapping for extensions. That means that two different extensions, e.g. for
EnumValueOptions and FieldOptions, MUST NOT have the same ID.
Using the same ID indicates the same purpose (protobuf does not allow multiple
extensions with the same name), such as EnumValueOptions.bitcoin_only and
FileOptions.include_in_bitcoin_only. pb2py can then find the extension under
either name.
The convention to achieve this is as follows:
- extensions specific to a type have the same prefix:
* 50xxx for EnumValueOptions
* 51xxx for EnumOptions
* 52xxx for MessageOptions
* 53xxx for FieldOptions
- extensions that might be used across types have the same "global" prefix 60xxx
*/
/**
* Options for specifying message direction and type of wire (normal/debug)
*/
extend google.protobuf.EnumValueOptions {
optional bool wire_in = 50002; // message can be transmitted via wire from PC to Trezor
optional bool wire_out = 50003; // message can be transmitted via wire from Trezor to PC
optional bool wire_debug_in = 50004; // message can be transmitted via debug wire from PC to Trezor
optional bool wire_debug_out = 50005; // message can be transmitted via debug wire from Trezor to PC
optional bool wire_tiny = 50006; // message is handled by Trezor when the USB stack is in tiny mode
optional bool wire_bootloader = 50007; // message is only handled by Trezor Bootloader
optional bool wire_no_fsm = 50008; // message is not handled by Trezor unless the USB stack is in tiny mode

optional bool bitcoin_only = 60000; // enum value is available on BITCOIN_ONLY build
// (messages not marked bitcoin_only will be EXCLUDED)
}

/** Options for tagging enum types */
extend google.protobuf.EnumOptions {
optional bool has_bitcoin_only_values = 51001; // indicate that some values should be excluded on BITCOIN_ONLY builds
}

/** Options for tagging message types */
extend google.protobuf.MessageOptions {
optional bool experimental_message = 52001; // indicate that a message is intended for development and beta testing only and its definition may change at any time
optional uint32 wire_type = 52002; // override wire type specified in the MessageType enum
optional bool internal_only = 52003; // indicate that a message is intended for internal use only and should not be transmitted via the wire
}

/** Options for tagging field types */
extend google.protobuf.FieldOptions {
optional bool experimental_field = 53001; // indicate that a field is intended for development and beta testing only
}

/** Options for tagging files with protobuf definitions */
extend google.protobuf.FileOptions {
optional bool include_in_bitcoin_only = 60000; // definitions are available on BITCOIN_ONLY build
// intentionally identical to `bitcoin_only` from enum
}
import "options.proto";

option (include_in_bitcoin_only) = true;

/**
* Mapping between Trezor wire identifier (uint) and a protobuf message
Expand Down
70 changes: 70 additions & 0 deletions common/protob/options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
syntax = "proto2";
package hw.trezor.messages;

// Sugar for easier handling in Java
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorOptions";

import "google/protobuf/descriptor.proto";

/************************* WARNING ***********************
Due to the way extensions are accessed in pb2py, there needs to be a globally unique
name-ID mapping for extensions. That means that two different extensions, e.g. for
EnumValueOptions and FieldOptions, MUST NOT have the same ID.
Using the same ID indicates the same purpose (protobuf does not allow multiple
extensions with the same name), such as EnumValueOptions.bitcoin_only and
FileOptions.include_in_bitcoin_only. pb2py can then find the extension under
either name.
The convention to achieve this is as follows:
- extensions specific to a type have the same prefix:
* 50xxx for EnumValueOptions
* 51xxx for EnumOptions
* 52xxx for MessageOptions
* 53xxx for FieldOptions
- extensions that might be used across types have the same "global" prefix 60xxx
*/

/**
* Options for specifying message direction and type of wire (normal/debug)
*/
extend google.protobuf.EnumValueOptions {
optional bool wire_in = 50002; // message can be transmitted via wire from PC to Trezor
optional bool wire_out = 50003; // message can be transmitted via wire from Trezor to PC
optional bool wire_debug_in = 50004; // message can be transmitted via debug wire from PC to Trezor
optional bool wire_debug_out = 50005; // message can be transmitted via debug wire from Trezor to PC
optional bool wire_tiny = 50006; // message is handled by Trezor when the USB stack is in tiny mode
optional bool wire_bootloader = 50007; // message is only handled by Trezor Bootloader
optional bool wire_no_fsm = 50008; // message is not handled by Trezor unless the USB stack is in tiny mode
optional bool channel_in = 50009;
optional bool channel_out = 50010;
optional bool pairing_in = 50011;
optional bool pairing_out = 50012;

optional bool bitcoin_only = 60000; // enum value is available on BITCOIN_ONLY build
// (messages not marked bitcoin_only will be EXCLUDED)
}

/** Options for tagging enum types */
extend google.protobuf.EnumOptions {
optional bool has_bitcoin_only_values = 51001; // indicate that some values should be excluded on BITCOIN_ONLY builds
}

/** Options for tagging message types */
extend google.protobuf.MessageOptions {
optional bool experimental_message = 52001; // indicate that a message is intended for development and beta testing only and its definition may change at any time
optional uint32 wire_type = 52002; // override wire type specified in the MessageType enum
optional bool internal_only = 52003; // indicate that a message is intended for internal use only and should not be transmitted via the wire
}

/** Options for tagging field types */
extend google.protobuf.FieldOptions {
optional bool experimental_field = 53001; // indicate that a field is intended for development and beta testing only
}

/** Options for tagging files with protobuf definitions */
extend google.protobuf.FileOptions {
optional bool include_in_bitcoin_only = 60000; // definitions are available on BITCOIN_ONLY build
// intentionally identical to `bitcoin_only` from enum
}
4 changes: 3 additions & 1 deletion common/protob/pb2py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,9 @@ def main(
if verbose:
logging.basicConfig(level=logging.DEBUG)

descriptor_proto = protoc(proto)
list_proto = list(proto)
list_proto.sort()
descriptor_proto = protoc(list_proto)
descriptor = Descriptor(
descriptor_proto,
include_deprecated=include_deprecated,
Expand Down
Loading

0 comments on commit 7041dcd

Please sign in to comment.