forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_protobuf
executable file
·119 lines (103 loc) · 3.28 KB
/
build_protobuf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
cd $(dirname $0)/..
PROTOB=common/protob
CORE_PROTOBUF_SOURCES="\
$PROTOB/messages.proto \
$PROTOB/messages-binance.proto \
$PROTOB/messages-bitcoin.proto \
$PROTOB/messages-cardano.proto \
$PROTOB/messages-common.proto \
$PROTOB/messages-crypto.proto \
$PROTOB/messages-debug.proto \
$PROTOB/messages-eos.proto \
$PROTOB/messages-ethereum.proto \
$PROTOB/messages-lisk.proto \
$PROTOB/messages-management.proto \
$PROTOB/messages-monero.proto \
$PROTOB/messages-nem.proto \
$PROTOB/messages-ripple.proto \
$PROTOB/messages-stellar.proto \
$PROTOB/messages-tezos.proto \
$PROTOB/messages-webauthn.proto \
"
PYTHON_PROTOBUF_SOURCES=$PROTOB/*.proto
CORE_MESSAGES_IGNORE="\
CosiCommit \
CosiCommitment \
CosiSign \
CosiSignature \
DebugLinkFlashErase \
DebugLinkLog \
DebugLinkMemory \
DebugLinkMemoryRead \
DebugLinkMemoryWrite \
DebugLinkStop \
NEMDecryptMessage \
NEMDecryptedMessage \
PinMatrixAck \
PinMatrixRequest \
PinMatrixRequestType \
WordAck \
WordRequest \
WordRequestType \
"
PYTHON_MESSAGES_IGNORE=""
RETURN=0
do_rebuild() {
# rebuild protobuf in specified directory
local DESTDIR="$1"
shift
local SOURCES="$1"
shift
local IGNORE="$1"
shift
local APPLY_BITCOIN_ONLY="$1"
shift
mkdir -p "$DESTDIR"
rm -f "$DESTDIR"/[A-Z]*.py
# note $SOURCES is unquoted - we want wildcard expansion and multiple args
$PROTOB/pb2py "$@" -o "$DESTDIR" $SOURCES
# TODO: make this less hackish
# maybe introduce attribute "altcoin" in protobuf?
if [ "$APPLY_BITCOIN_ONLY" == "TRUE" ]; then
sed -i "3ifrom trezor import utils\n" "$DESTDIR"/Capability.py
sed -i "3ifrom trezor import utils\n" "$DESTDIR"/MessageType.py
sed -i "/^EthereumGetPublicKey/iif not utils.BITCOIN_ONLY:" "$DESTDIR"/MessageType.py
for altcoin in Ethereum NEM Lisk Tezos Stellar Cardano Ripple Monero DebugMonero Eos Binance WebAuthn; do
sed -i "s:^$altcoin: $altcoin:" "$DESTDIR"/Capability.py
sed -i "s:^$altcoin: $altcoin:" "$DESTDIR"/MessageType.py
done
sed -i "/^Bitcoin_like/iif not utils.BITCOIN_ONLY:" "$DESTDIR"/Capability.py
sed -i "/^EOS/iif not utils.BITCOIN_ONLY:" "$DESTDIR"/Capability.py
for feature in Bitcoin_like EOS U2F; do
sed -i "s:^$feature: $feature:" "$DESTDIR"/Capability.py
done
fi
# ENDTODO
# delete unused messages
for F in $IGNORE; do
rm -f "$DESTDIR"/"$F".py
done
}
do_check() {
# rebuild protobuf in tmpdir and check result against specified directory
local TMPDIR=$(mktemp -d proto-check.XXXXXX)
local DESTDIR="$1"
shift
cp -rT "$DESTDIR" "$TMPDIR"
do_rebuild "$TMPDIR" "$@"
DIFF=$(diff -ur --exclude __pycache__ "$DESTDIR" "$TMPDIR")
rm -r "$TMPDIR"
if [ -n "$DIFF" ]; then
echo "$DIFF"
RETURN=1
fi
}
if [ "$1" == "--check" ]; then
func=do_check
else
func=do_rebuild
fi
$func core/src/trezor/messages "$CORE_PROTOBUF_SOURCES" "$CORE_MESSAGES_IGNORE" TRUE --no-init-py
$func python/src/trezorlib/messages "$PYTHON_PROTOBUF_SOURCES" "$PYTHON_MESSAGES_IGNORE" FALSE --include-deprecated -P ..protobuf
exit $RETURN