2019-07-31 15:02:41 +00:00
|
|
|
from micropython import const
|
2021-12-08 09:10:58 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-07-31 15:02:41 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if TYPE_CHECKING:
|
2022-09-15 16:30:05 +00:00
|
|
|
from trezor.messages import BinanceInputOutput, BinanceSignTx
|
2021-12-08 09:10:58 +00:00
|
|
|
from trezor.protobuf import MessageType
|
|
|
|
|
2019-09-03 07:12:04 +00:00
|
|
|
# 1*10^8 Jagers equal 1 BNB https://www.binance.vision/glossary/jager
|
2019-12-09 16:43:30 +00:00
|
|
|
DECIMALS = const(8)
|
2019-07-31 15:02:41 +00:00
|
|
|
|
|
|
|
|
2022-09-16 12:53:44 +00:00
|
|
|
# NOTE: it is 11 bytes smaller to define it on top level than inside a function
|
|
|
|
def _make_input_output(input_output: BinanceInputOutput) -> str:
|
|
|
|
COIN_BLUEPRINT = '{{"amount":{},"denom":"{}"}}'
|
|
|
|
INPUT_OUTPUT_BLUEPRINT = '{{"address":"{}","coins":[{}]}}'
|
|
|
|
|
|
|
|
coins = ",".join(
|
|
|
|
COIN_BLUEPRINT.format(c.amount, c.denom) for c in input_output.coins
|
|
|
|
)
|
|
|
|
return INPUT_OUTPUT_BLUEPRINT.format(input_output.address, coins)
|
|
|
|
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
def produce_json_for_signing(envelope: BinanceSignTx, msg: MessageType) -> str:
|
2022-09-16 12:53:44 +00:00
|
|
|
from trezor import wire
|
2023-08-15 15:56:09 +00:00
|
|
|
from trezor.messages import BinanceCancelMsg, BinanceOrderMsg, BinanceTransferMsg
|
2022-09-16 12:53:44 +00:00
|
|
|
|
|
|
|
# NOTE: not defining kwargs in format string saves 7 bytes per each argument
|
|
|
|
ENVELOPE_BLUEPRINT = '{{"account_number":"{}","chain_id":"{}","data":null,"memo":"{}","msgs":[{}],"sequence":"{}","source":"{}"}}'
|
|
|
|
MSG_TRANSFER_BLUEPRINT = '{{"inputs":[{}],"outputs":[{}]}}'
|
|
|
|
MSG_NEWORDER_BLUEPRINT = '{{"id":"{}","ordertype":{},"price":{},"quantity":{},"sender":"{}","side":{},"symbol":"{}","timeinforce":{}}}'
|
|
|
|
MSG_CANCEL_BLUEPRINT = '{{"refid":"{}","sender":"{}","symbol":"{}"}}'
|
|
|
|
|
2021-03-23 12:35:27 +00:00
|
|
|
if BinanceTransferMsg.is_type_of(msg):
|
2022-09-16 12:53:44 +00:00
|
|
|
inputs = ",".join(_make_input_output(i) for i in msg.inputs)
|
|
|
|
outputs = ",".join(_make_input_output(o) for o in msg.outputs)
|
|
|
|
json_msg = MSG_TRANSFER_BLUEPRINT.format(inputs, outputs)
|
2021-03-23 12:35:27 +00:00
|
|
|
elif BinanceOrderMsg.is_type_of(msg):
|
2022-09-16 12:53:44 +00:00
|
|
|
json_msg = MSG_NEWORDER_BLUEPRINT.format(
|
|
|
|
msg.id,
|
|
|
|
msg.ordertype,
|
|
|
|
msg.price,
|
|
|
|
msg.quantity,
|
|
|
|
msg.sender,
|
|
|
|
msg.side,
|
|
|
|
msg.symbol,
|
|
|
|
msg.timeinforce,
|
|
|
|
)
|
2021-03-23 12:35:27 +00:00
|
|
|
elif BinanceCancelMsg.is_type_of(msg):
|
2022-09-16 12:53:44 +00:00
|
|
|
json_msg = MSG_CANCEL_BLUEPRINT.format(msg.refid, msg.sender, msg.symbol)
|
2019-07-31 15:02:41 +00:00
|
|
|
else:
|
2021-12-08 09:10:58 +00:00
|
|
|
raise wire.ProcessError("input message unrecognized")
|
2019-07-31 15:02:41 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if envelope.source < 0:
|
|
|
|
raise wire.DataError("Source is invalid")
|
2019-07-31 15:02:41 +00:00
|
|
|
|
|
|
|
return ENVELOPE_BLUEPRINT.format(
|
2022-09-16 12:53:44 +00:00
|
|
|
envelope.account_number,
|
|
|
|
envelope.chain_id,
|
|
|
|
envelope.memo,
|
|
|
|
json_msg,
|
|
|
|
envelope.sequence,
|
|
|
|
envelope.source,
|
2019-07-31 15:02:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def address_from_public_key(pubkey: bytes, hrp: str) -> str:
|
|
|
|
"""
|
|
|
|
Address = RIPEMD160(SHA256(compressed public key))
|
|
|
|
Address_Bech32 = HRP + '1' + bech32.encode(convert8BitsTo5Bits(RIPEMD160(SHA256(compressed public key))))
|
|
|
|
HRP - bnb for productions, tbnb for tests
|
|
|
|
"""
|
2022-09-16 12:53:44 +00:00
|
|
|
from trezor.crypto import bech32
|
|
|
|
from trezor.crypto.scripts import sha256_ripemd160
|
2019-07-31 15:02:41 +00:00
|
|
|
|
2021-03-19 20:26:52 +00:00
|
|
|
h = sha256_ripemd160(pubkey).digest()
|
2019-07-31 15:02:41 +00:00
|
|
|
|
2021-11-02 12:54:18 +00:00
|
|
|
assert (len(h) * 8) % 5 == 0 # no padding will be added by convertbits
|
|
|
|
convertedbits = bech32.convertbits(h, 8, 5)
|
2021-07-19 08:30:16 +00:00
|
|
|
return bech32.bech32_encode(hrp, convertedbits, bech32.Encoding.BECH32)
|