diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index e329fbd593..dc74f361af 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -74,21 +74,21 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size mp_get_buffer_raise(vals[5].u_obj, &public_key, MP_BUFFER_READ); mp_get_buffer_raise(vals[6].u_obj, &curve_name, MP_BUFFER_READ); - if (NULL == chain_code.buf || 32 != chain_code.len) { + if (32 != chain_code.len) { mp_raise_ValueError("chain_code is invalid"); } - if (NULL == public_key.buf && NULL == private_key.buf) { + if (0 == public_key.len && 0 == private_key.len) { mp_raise_ValueError("either public_key or private_key is required"); } - if (NULL != private_key.buf && 32 != private_key.len) { + if (0 != private_key.len && 32 != private_key.len) { mp_raise_ValueError("private_key is invalid"); } - if (NULL != public_key.buf && 33 != public_key.len) { + if (0 != public_key.len && 33 != public_key.len) { mp_raise_ValueError("public_key is invalid"); } const curve_info *curve = NULL; - if (NULL == curve_name.buf) { + if (0 == curve_name.len) { curve = get_curve_by_name(SECP256K1_NAME); } else { curve = get_curve_by_name(curve_name.buf); @@ -103,17 +103,17 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size o->fingerprint = fingerprint; o->hdnode.depth = depth; o->hdnode.child_num = child_num; - if (NULL != chain_code.buf && 32 == chain_code.len) { + if (32 == chain_code.len) { memcpy(o->hdnode.chain_code, chain_code.buf, 32); } else { memzero(o->hdnode.chain_code, 32); } - if (NULL != private_key.buf && 32 == private_key.len) { + if (32 == private_key.len) { memcpy(o->hdnode.private_key, private_key.buf, 32); } else { memzero(o->hdnode.private_key, 32); } - if (NULL != public_key.buf && 33 == public_key.len) { + if (33 == public_key.len) { memcpy(o->hdnode.public_key, public_key.buf, 33); } else { memzero(o->hdnode.public_key, 33); @@ -123,16 +123,27 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size return MP_OBJ_FROM_PTR(o); } -/// def derive(self, index: int) -> None: +/// def derive(self, index: int, public: bool=False) -> None: /// ''' /// Derive a BIP0032 child node in place. /// ''' -STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(mp_obj_t self, mp_obj_t index) { - mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); - uint32_t i = mp_obj_get_int_truncated(index); +STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(size_t n_args, const mp_obj_t *args) { + mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(args[0]); + uint32_t i = mp_obj_get_int_truncated(args[1]); uint32_t fp = hdnode_fingerprint(&o->hdnode); + bool public = n_args > 2 && args[2] == mp_const_true; - if (!hdnode_private_ckd(&o->hdnode, i)) { + int res; + if (public) { + res = hdnode_public_ckd(&o->hdnode, i); + } else { + if (0 == memcmp(o->hdnode.private_key, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) { + memzero(&o->hdnode, sizeof(o->hdnode)); + mp_raise_ValueError("Failed to derive, private key not set"); + } + res = hdnode_private_ckd(&o->hdnode, i); + } + if (!res) { memzero(&o->hdnode, sizeof(o->hdnode)); mp_raise_ValueError("Failed to derive"); } @@ -140,7 +151,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(mp_obj_t self, mp_obj_t index) { return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_obj, mod_trezorcrypto_HDNode_derive); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_derive_obj, 2, 3, mod_trezorcrypto_HDNode_derive); /// def derive_path(self, path: List[int]) -> None: /// ''' diff --git a/mocks/generated/trezorconfig.py b/mocks/generated/trezorconfig.py index 0881917d88..ab1462b942 100644 --- a/mocks/generated/trezorconfig.py +++ b/mocks/generated/trezorconfig.py @@ -1,25 +1,45 @@ from typing import * # extmod/modtrezorconfig/modtrezorconfig.c -def init(self) -> None: +def init() -> None: ''' - Initializes the storage. Must be called before any other method is called from this module! + Initializes the storage. Must be called before any other method is + called from this module! ''' # extmod/modtrezorconfig/modtrezorconfig.c -def get(app: int, key: int) -> bytes: +def unlock(pin: int, waitcallback: (int, int -> None)) -> bool: + ''' + Attempts to unlock the storage with given PIN. Returns True on + success, False on failure. + ''' + +# extmod/modtrezorconfig/modtrezorconfig.c +def has_pin() -> bool: + ''' + Returns True if storage has a configured PIN, False otherwise. + ''' + +# extmod/modtrezorconfig/modtrezorconfig.c +def change_pin(pin: int, newpin: int, waitcallback: (int, int -> None)) -> bool: + ''' + Change PIN. Returns True on success, False on failure. + ''' + +# extmod/modtrezorconfig/modtrezorconfig.c +def get(app: int, key: int, public: bool=False) -> bytes: ''' Gets a value of given key for given app (or empty bytes if not set). ''' # extmod/modtrezorconfig/modtrezorconfig.c -def set(app: int, key: int, value: bytes) -> None: +def set(app: int, key: int, value: bytes, public: bool=False) -> None: ''' Sets a value of given key for given app. ''' # extmod/modtrezorconfig/modtrezorconfig.c -def wipe(self) -> None: +def wipe() -> None: ''' Erases the whole config. Use with caution! ''' diff --git a/mocks/generated/trezorcrypto.py b/mocks/generated/trezorcrypto.py index 1102ab7c16..af87455a75 100644 --- a/mocks/generated/trezorcrypto.py +++ b/mocks/generated/trezorcrypto.py @@ -22,7 +22,18 @@ class HDNode: BIP0032 HD node structure. ''' - def derive(self, index: int) -> None: + def __init__(self, + depth: int, + fingerprint: int, + child_num: int, + chain_code: bytes, + private_key: bytes = None, + public_key: bytes = None, + curve_name: str = None) -> None: + ''' + ''' + + def derive(self, index: int, public: bool=False) -> None: ''' Derive a BIP0032 child node in place. ''' @@ -82,13 +93,9 @@ class HDNode: Compute a base58-encoded address string from the HD node. ''' -# extmod/modtrezorcrypto/modtrezorcrypto-bip32.h -class Bip32: - ''' - ''' - - def __init__(self): + def ethereum_pubkeyhash(self) -> bytes: ''' + Compute an Ethereum pubkeyhash (aka address) from the HD node. ''' def deserialize(self, value: str, version_public: int, version_private: int) -> HDNode: @@ -96,49 +103,67 @@ class Bip32: Construct a BIP0032 HD node from a base58-serialized value. ''' - def from_seed(self, seed: bytes, curve_name: str) -> HDNode: + def from_seed(seed: bytes, curve_name: str) -> HDNode: ''' Construct a BIP0032 HD node from a BIP0039 seed value. ''' # extmod/modtrezorcrypto/modtrezorcrypto-bip39.h -class Bip39: +def find_word(prefix: str) -> Optional[str]: ''' + Return the first word from the wordlist starting with prefix. ''' - def __init__(self): +# extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def complete_word(prefix: str) -> int: + ''' + Return possible 1-letter suffixes for given word prefix. + Result is a bitmask, with 'a' on the lowest bit, 'b' on the second lowest, etc. + ''' + +# extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def generate(strength: int) -> str: + ''' + Generate a mnemonic of given strength (128, 160, 192, 224 and 256 bits). + ''' + +# extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def from_data(data: bytes) -> str: + ''' + Generate a mnemonic from given data (of 16, 20, 24, 28 and 32 bytes). + ''' + +# extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def check(mnemonic: str) -> bool: + ''' + Check whether given mnemonic is valid. + ''' + +# extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +def seed(mnemonic: str, passphrase: str) -> bytes: + ''' + Generate seed from mnemonic and passphrase. + ''' + +# extmod/modtrezorcrypto/modtrezorcrypto-blake256.h +class Blake256: + ''' + Blake256 context. + ''' + + def __init__(self, data: bytes = None) -> None: ''' + Creates a hash context object. ''' - def find_word(self, prefix: str) -> Optional[str]: + def update(self, data: bytes) -> None: ''' - Return the first word from the wordlist starting with prefix. + Update the hash context with hashed data. ''' - def complete_word(self, prefix: str) -> int: + def digest(self) -> bytes: ''' - Return possible 1-letter suffixes for given word prefix. - Result is a bitmask, with 'a' on the lowest bit, 'b' on the second lowest, etc. - ''' - - def generate(self, strength: int) -> str: - ''' - Generate a mnemonic of given strength (128, 160, 192, 224 and 256 bits). - ''' - - def from_data(self, data: bytes) -> str: - ''' - Generate a mnemonic from given data (of 16, 20, 24, 28 and 32 bytes). - ''' - - def check(self, mnemonic: str) -> bool: - ''' - Check whether given mnemonic is valid. - ''' - - def seed(self, mnemonic: str, passphrase: str) -> bytes: - ''' - Generate seed from mnemonic and passphrase. + Returns the digest of hashed data. ''' # extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h @@ -183,117 +208,140 @@ class Blake2s: Returns the digest of hashed data. ''' +# extmod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h +class ChaCha20Poly1305: + ''' + ChaCha20Poly1305 context. + ''' + + def __init__(self, key: bytes, nonce: bytes) -> None: + ''' + Initialize the ChaCha20 + Poly1305 context for encryption or decryption + using a 32 byte key and 12 byte nonce as in the RFC 7539 style. + ''' + + def encrypt(self, data: bytes) -> bytes: + ''' + Encrypt data (length of data must be divisible by 64 except for the final value). + ''' + + def decrypt(self, data: bytes) -> bytes: + ''' + Decrypt data (length of data must be divisible by 64 except for the final value). + ''' + + def auth(self, data: bytes) -> None: + ''' + Include authenticated data in the Poly1305 MAC using the RFC 7539 + style with 16 byte padding. This must only be called once and prior + to encryption or decryption. + ''' + + def finish(self) -> bytes: + ''' + Compute RFC 7539-style Poly1305 MAC. + ''' + # extmod/modtrezorcrypto/modtrezorcrypto-curve25519.h -class Curve25519: +def generate_secret() -> bytes: ''' + Generate secret key. ''' - def __init__(self) -> None: - ''' - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-curve25519.h +def publickey(secret_key: bytes) -> bytes: + ''' + Computes public key from secret key. + ''' - def generate_secret(self) -> bytes: - ''' - Generate secret key. - ''' - - def publickey(self, secret_key: bytes) -> bytes: - ''' - Computes public key from secret key. - ''' - - def multiply(self, secret_key: bytes, public_key: bytes) -> bytes: - ''' - Multiplies point defined by public_key with scalar defined by secret_key. - Useful for ECDH. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-curve25519.h +def multiply(secret_key: bytes, public_key: bytes) -> bytes: + ''' + Multiplies point defined by public_key with scalar defined by secret_key. + Useful for ECDH. + ''' # extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h -class Ed25519: +def generate_secret() -> bytes: ''' + Generate secret key. ''' - def __init__(self) -> None: - ''' - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def publickey(secret_key: bytes) -> bytes: + ''' + Computes public key from secret key. + ''' - def generate_secret(self) -> bytes: - ''' - Generate secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def sign(secret_key: bytes, message: bytes) -> bytes: + ''' + Uses secret key to produce the signature of message. + ''' - def publickey(self, secret_key: bytes) -> bytes: - ''' - Computes public key from secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def verify(public_key: bytes, signature: bytes, message: bytes) -> bool: + ''' + Uses public key to verify the signature of the message. + Returns True on success. + ''' - def sign(self, secret_key: bytes, message: bytes) -> bytes: - ''' - Uses secret key to produce the signature of message. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def cosi_combine_publickeys(public_keys: List[bytes]) -> bytes: + ''' + Combines a list of public keys used in COSI cosigning scheme. + ''' - def verify(self, public_key: bytes, signature: bytes, message: bytes) -> bool: - ''' - Uses public key to verify the signature of the message. - Returns True on success. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def cosi_combine_signatures(R: bytes, signatures: List[bytes]) -> bytes: + ''' + Combines a list of signatures used in COSI cosigning scheme. + ''' - def cosi_combine_publickeys(self, public_keys: List[bytes]) -> bytes: - ''' - Combines a list of public keys used in COSI cosigning scheme. - ''' - - def cosi_combine_signatures(self, R: bytes, signatures: List[bytes]) -> bytes: - ''' - Combines a list of signatures used in COSI cosigning scheme. - ''' - - def cosi_sign(self, secret_key: bytes, message: bytes, nonce: bytes, sigR: bytes, combined_pubkey: bytes) -> bytes: - ''' - Produce signature of message using COSI cosigning scheme. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-ed25519.h +def cosi_sign(secret_key: bytes, message: bytes, nonce: bytes, sigR: bytes, combined_pubkey: bytes) -> bytes: + ''' + Produce signature of message using COSI cosigning scheme. + ''' # extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h -class Nist256p1: +def generate_secret() -> bytes: ''' + Generate secret key. ''' - def __init__(self) -> None: - ''' - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def publickey(secret_key: bytes, compressed: bool = True) -> bytes: + ''' + Computes public key from secret key. + ''' - def generate_secret(self) -> bytes: - ''' - Generate secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes: + ''' + Uses secret key to produce the signature of the digest. + ''' - def publickey(self, secret_key: bytes, compressed: bool = True) -> bytes: - ''' - Computes public key from secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool: + ''' + Uses public key to verify the signature of the digest. + Returns True on success. + ''' - def sign(self, secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes: - ''' - Uses secret key to produce the signature of the digest. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def verify_recover(signature: bytes, digest: bytes) -> bytes: + ''' + Uses signature of the digest to verify the digest and recover the public key. + Returns public key on success, None on failure. + ''' - def verify(self, public_key: bytes, signature: bytes, digest: bytes) -> bool: - ''' - Uses public key to verify the signature of the digest. - Returns True on success. - ''' - - def verify_recover(self, signature: bytes, digest: bytes) -> bytes: - ''' - Uses signature of the digest to verify the digest and recover the public key. - Returns public key on success, None on failure. - ''' - - def multiply(self, secret_key: bytes, public_key: bytes) -> bytes: - ''' - Multiplies point defined by public_key with scalar defined by secret_key - Useful for ECDH - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +def multiply(secret_key: bytes, public_key: bytes) -> bytes: + ''' + Multiplies point defined by public_key with scalar defined by secret_key. + Useful for ECDH. + ''' # extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h class Pbkdf2: @@ -317,28 +365,22 @@ class Pbkdf2: ''' # extmod/modtrezorcrypto/modtrezorcrypto-random.h -class Random: +def uniform(n: int) -> int: ''' + Compute uniform random number from interval 0 ... n - 1. ''' - def __init__(self) -> None: - ''' - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-random.h +def bytes(len: int) -> bytes: + ''' + Generate random bytes sequence of length len. + ''' - def uniform(self, n: int) -> int: - ''' - Compute uniform random number from interval 0 ... n - 1 - ''' - - def bytes(self, len: int) -> bytes: - ''' - Generate random bytes sequence of length len - ''' - - def shuffle(self, data: list) -> None: - ''' - Shuffles items of given list (in-place) - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-random.h +def shuffle(data: list) -> None: + ''' + Shuffles items of given list (in-place). + ''' # extmod/modtrezorcrypto/modtrezorcrypto-rfc6979.h class Rfc6979: @@ -378,46 +420,43 @@ class Ripemd160: ''' # extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h -class Secp256k1: +def generate_secret() -> bytes: ''' + Generate secret key. ''' - def __init__(self) -> None: - ''' - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def publickey(secret_key: bytes, compressed: bool = True) -> bytes: + ''' + Computes public key from secret key. + ''' - def generate_secret(self, ) -> bytes: - ''' - Generate secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def sign(secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes: + ''' + Uses secret key to produce the signature of the digest. + ''' - def publickey(self, secret_key: bytes, compressed: bool = True) -> bytes: - ''' - Computes public key from secret key. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def verify(public_key: bytes, signature: bytes, digest: bytes) -> bool: + ''' + Uses public key to verify the signature of the digest. + Returns True on success. + ''' - def sign(self, secret_key: bytes, digest: bytes, compressed: bool = True) -> bytes: - ''' - Uses secret key to produce the signature of the digest. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def verify_recover(signature: bytes, digest: bytes) -> bytes: + ''' + Uses signature of the digest to verify the digest and recover the public key. + Returns public key on success, None on failure. + ''' - def verify(self, public_key: bytes, signature: bytes, digest: bytes) -> bool: - ''' - Uses public key to verify the signature of the digest. - Returns True on success. - ''' - - def verify_recover(self, signature: bytes, digest: bytes) -> bytes: - ''' - Uses signature of the digest to verify the digest and recover the public key. - Returns public key on success, None on failure. - ''' - - def multiply(self, secret_key: bytes, public_key: bytes) -> bytes: - ''' - Multiplies point defined by public_key with scalar defined by secret_key. - Useful for ECDH. - ''' +# extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +def multiply(secret_key: bytes, public_key: bytes) -> bytes: + ''' + Multiplies point defined by public_key with scalar defined by secret_key. + Useful for ECDH. + ''' # extmod/modtrezorcrypto/modtrezorcrypto-sha1.h class Sha1: @@ -523,22 +562,3 @@ class Sha512: ''' Returns the digest of hashed data. ''' - -# extmod/modtrezorcrypto/modtrezorcrypto-ssss.h -class SSSS: - ''' - ''' - - def __init__(self) -> None: - ''' - ''' - - def split(self, m: int, n: int, secret: bytes) -> tuple: - ''' - Split secret to (M of N) shares using Shamir's Secret Sharing Scheme. - ''' - - def combine(self, shares: tuple) -> bytes: - ''' - Combine M shares of Shamir's Secret Sharing Scheme into secret. - ''' diff --git a/mocks/generated/trezorio.py b/mocks/generated/trezorio.py index a79f2d1014..0c79c832c8 100644 --- a/mocks/generated/trezorio.py +++ b/mocks/generated/trezorio.py @@ -1,5 +1,87 @@ from typing import * +# extmod/modtrezorio/modtrezorio-flash.h +class FlashOTP: + ''' + ''' + + def __init__(self) -> None: + ''' + ''' + + def FlashOTP.write(self, block: int, offset: int, data: bytes) -> None: + ''' + Writes data to OTP flash + ''' + + def FlashOTP.read(self, block: int, offset: int, data: bytearray) -> None: + ''' + Reads data from OTP flash + ''' + + def FlashOTP.lock(self, block: int) -> None: + ''' + Lock OTP flash block + ''' + + def FlashOTP.is_locked(self, block: int) -> bool: + ''' + Is OTP flash block locked? + ''' + +# extmod/modtrezorio/modtrezorio-hid.h +class HID: + ''' + USB HID interface configuration. + ''' + + def __init__(self, + iface_num: int, + ep_in: int, + ep_out: int, + report_desc: bytes, + subclass: int = 0, + protocol: int = 0, + polling_interval: int = 1, + max_packet_len: int = 64) -> None: + ''' + ''' + + def iface_num(self) -> int: + ''' + Returns the configured number of this interface. + ''' + + def write(self, msg: bytes) -> int: + ''' + Sends message using USB HID (device) or UDP (emulator). + ''' + +# extmod/modtrezorio/modtrezorio-poll.h +def poll(ifaces: Iterable[int], list_ref: List, timeout_us: int) -> bool: + ''' + Wait until one of `ifaces` is ready to read or write (using masks + `list_ref`: + `list_ref[0]` - the interface number, including the mask + `list_ref[1]` - for touch event, tuple of (event_type, x_position, y_position) + - for USB read event, received bytes + If timeout occurs, False is returned, True otherwise. + ''' + +# extmod/modtrezorio/modtrezorio-sbu.h +class SBU: + ''' + ''' + + def __init__(self) -> None: + ''' + ''' + + def set(self, sbu1: bool, sbu2: bool) -> None: + ''' + Sets SBU wires to sbu1 and sbu2 values respectively + ''' + # extmod/modtrezorio/modtrezorio-sdcard.h class SDCard: ''' @@ -27,12 +109,94 @@ class SDCard: def read(self, block_num: int, buf: bytearray) -> bool: ''' - Reads block_num block from the SD card into buf. + Reads blocks starting with block_num from the SD card into buf. + Number of bytes read is length of buf rounded down to multiply of SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise. ''' def write(self, block_num: int, buf: bytes) -> bool: ''' - Writes block_num block from buf to the SD card. + Writes blocks starting with block_num from buf to the SD card. + Number of bytes written is length of buf rounded down to multiply of SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise. ''' + +# extmod/modtrezorio/modtrezorio-usb.h +class USB: + ''' + USB device configuration. + ''' + + def __init__(self, + vendor_id: int, + product_id: int, + release_num: int, + manufacturer: str='', + product: str='', + serial_number: str='', + configuration: str='', + interface: str='') -> None: + ''' + ''' + + def add(self, iface: Union[HID, VCP, WebUSB]) -> None: + ''' + Registers passed interface into the USB stack. + ''' + + def open(self) -> None: + ''' + Initializes the USB stack. + ''' + + def close(self) -> None: + ''' + Cleans up the USB stack. + ''' + +# extmod/modtrezorio/modtrezorio-vcp.h +class VCP: + ''' + USB VCP interface configuration. + ''' + + def __init__(self, + iface_num: int, + data_iface_num: int, + ep_in: int, + ep_out: int, + ep_cmd: int) -> None: + ''' + ''' + + def iface_num(self) -> int: + ''' + Returns the configured number of this interface. + ''' + +# extmod/modtrezorio/modtrezorio-webusb.h +class WebUSB: + ''' + USB WebUSB interface configuration. + ''' + + def __init__(self, + iface_num: int, + ep_in: int, + ep_out: int, + subclass: int = 0, + protocol: int = 0, + polling_interval: int = 1, + max_packet_len: int = 64) -> None: + ''' + ''' + + def iface_num(self) -> int: + ''' + Returns the configured number of this interface. + ''' + + def write(self, msg: bytes) -> int: + ''' + Sends message using USB WebUSB (device) or UDP (emulator). + ''' diff --git a/mocks/generated/trezormsg.py b/mocks/generated/trezormsg.py deleted file mode 100644 index ff54ab9b20..0000000000 --- a/mocks/generated/trezormsg.py +++ /dev/null @@ -1,83 +0,0 @@ -from typing import * - -# extmod/modtrezormsg/modtrezormsg.c -class HID: - ''' - USB HID interface configuration. - ''' - - def __init__(self, - iface_num: int, - ep_in: int, - ep_out: int, - report_desc: bytes, - subclass: int = 0, - protocol: int = 0, - polling_interval: int = 1, - max_packet_len: int = 64) -> None: - ''' - ''' - -# extmod/modtrezormsg/modtrezormsg.c -class VCP: - ''' - USB VCP interface configuration. - ''' - - def __init__(self, - iface_num: int, - data_iface_num: int, - ep_in: int, - ep_out: int, - ep_cmd: int) -> None: - ''' - ''' - -# extmod/modtrezormsg/modtrezormsg.c -class USB: - ''' - USB device configuration. - ''' - - def __init__(self, - vendor_id: int, - product_id: int, - release_num: int, - manufacturer_str: str, - product_str: str, - serial_number_str: str, - configuration_str: str = '', - interface_str: str = '') -> None: - ''' - ''' - -# extmod/modtrezormsg/modtrezormsg.c -class Msg: - ''' - Interface with USB and touch events. - ''' - - def __init__(self) -> None: - ''' - ''' - - def init_usb(self, usb_info: USB, usb_ifaces: List[Union[HID, VCP]]) -> None: - ''' - Registers passed interfaces and initializes the USB stack. - ''' - - def deinit_usb(self) -> None: - ''' - Cleans up the USB stack - ''' - - def send(self, iface: int, message: bytes) -> int: - ''' - Sends message using USB HID (device) or UDP (emulator). - ''' - - def select(self, timeout_us: int) -> tuple: - ''' - Polls the event queue and returns the event object. - Function returns None if timeout specified in microseconds is reached. - ''' diff --git a/mocks/generated/trezorui.py b/mocks/generated/trezorui.py index e014cf7c6d..acec414926 100644 --- a/mocks/generated/trezorui.py +++ b/mocks/generated/trezorui.py @@ -38,10 +38,17 @@ class Display: The image needs to be in TREZOR Optimized Image Format (TOIF) - full-color mode. ''' + def avatar(self, x: int, y: int, image: bytes, fgcolor: int, bgcolor: int) -> None: + ''' + Renders an avatar at position (x,y). + The image needs to be in TREZOR Optimized Image Format (TOIF) - full-color mode. + Image needs to be of exactly AVATAR_IMAGE_SIZE x AVATAR_IMAGE_SIZE pixels size. + ''' + def icon(self, x: int, y: int, icon: bytes, fgcolor: int, bgcolor: int) -> None: ''' Renders an icon at position (x,y), fgcolor is used as foreground color, bgcolor as background. - The image needs to be in TREZOR Optimized Image Format (TOIF) - gray-scale mode. + The icon needs to be in TREZOR Optimized Image Format (TOIF) - gray-scale mode. ''' def print(self, text: str) -> None: @@ -49,19 +56,19 @@ class Display: Renders text using 5x8 bitmap font (using special text mode). ''' - def text(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None: + def text(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None: ''' Renders left-aligned text at position (x,y) where x is left position and y is baseline. Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. ''' - def text_center(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None: + def text_center(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None: ''' Renders text centered at position (x,y) where x is text center and y is baseline. Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. ''' - def text_right(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int) -> None: + def text_right(self, x: int, y: int, text: str, font: int, fgcolor: int, bgcolor: int, minwidth: int=None) -> None: ''' Renders right-aligned text at position (x,y) where x is right position and y is baseline. Font font is used for rendering, fgcolor is used as foreground color, bgcolor as background. diff --git a/mocks/generated/trezorutils.py b/mocks/generated/trezorutils.py index 2e09f53984..fefe5d3ff0 100644 --- a/mocks/generated/trezorutils.py +++ b/mocks/generated/trezorutils.py @@ -1,5 +1,14 @@ from typing import * +# extmod/modtrezorutils/modtrezorutils.c +def consteq(sec: bytes, pub: bytes) -> bool: + ''' + Compares the private information in `sec` with public, user-provided + information in `pub`. Runs in constant time, corresponding to a length + of `pub`. Can access memory behind valid length of `sec`, caller is + expected to avoid any invalid memory access. + ''' + # extmod/modtrezorutils/modtrezorutils.c def memcpy(dst: bytearray, dst_ofs: int, src: bytearray, src_ofs: int, diff --git a/src/apps/debug/__init__.py b/src/apps/debug/__init__.py index 936e285988..432f39ef16 100644 --- a/src/apps/debug/__init__.py +++ b/src/apps/debug/__init__.py @@ -1,3 +1,7 @@ +import micropython +import gc +from uctypes import bytes_at, bytearray_at + from trezor import log from trezor import loop from trezor.utils import unimport @@ -5,62 +9,50 @@ from trezor.wire import register, protobuf_workflow from trezor.messages.wire_types import \ DebugLinkDecision, DebugLinkGetState, DebugLinkStop, \ DebugLinkMemoryRead, DebugLinkMemoryWrite, DebugLinkFlashErase +from trezor.messages.DebugLinkMemory import DebugLinkMemory +from trezor.messages.DebugLinkState import DebugLinkState +from trezor.ui.confirm import CONFIRMED, CANCELLED + +from apps.common.confirm import signal +from apps.common import storage +from apps.management import reset_device -@unimport async def dispatch_DebugLinkDecision(ctx, msg): - from trezor.ui.confirm import CONFIRMED, CANCELLED - from apps.common.confirm import signal signal.send(CONFIRMED if msg.yes_no else CANCELLED) -@unimport async def dispatch_DebugLinkGetState(ctx, msg): - from trezor.messages.DebugLinkState import DebugLinkState - from apps.common import storage - from apps.management import reset_device - m = DebugLinkState() m.mnemonic = storage.get_mnemonic() m.passphrase_protection = storage.has_passphrase() m.reset_entropy = reset_device.internal_entropy m.reset_word = reset_device.current_word - return m -@unimport async def dispatch_DebugLinkStop(ctx, msg): pass -@unimport async def dispatch_DebugLinkMemoryRead(ctx, msg): - from trezor.messages.DebugLinkMemory import DebugLinkMemory - from uctypes import bytes_at m = DebugLinkMemory() m.memory = bytes_at(msg.address, msg.length) return m -@unimport async def dispatch_DebugLinkMemoryWrite(ctx, msg): - from uctypes import bytearray_at l = len(msg.memory) data = bytearray_at(msg.address, l) data[0:l] = msg.memory -@unimport async def dispatch_DebugLinkFlashErase(ctx, msg): # TODO: erase(msg.sector) pass async def memory_stats(interval): - import micropython - import gc - sleep = loop.sleep(interval * 1000 * 1000) while True: micropython.mem_info() diff --git a/src/apps/wallet/get_address.py b/src/apps/wallet/get_address.py index 6433c97d85..aef0b5db8c 100644 --- a/src/apps/wallet/get_address.py +++ b/src/apps/wallet/get_address.py @@ -1,24 +1,20 @@ from micropython import const -from trezor import wire, ui +from trezor import ui async def layout_get_address(ctx, msg): from trezor.messages.Address import Address from trezor.messages.InputScriptType import SPENDWITNESS - from trezor.messages.FailureType import ProcessError from ..common import coins from ..common import seed from ..wallet.sign_tx import addresses - if msg.multisig: - raise wire.FailureError(ProcessError, 'GetAddress.multisig is unsupported') - address_n = msg.address_n or () coin_name = msg.coin_name or 'Bitcoin' coin = coins.by_name(coin_name) node = await seed.derive_node(ctx, address_n) - address = addresses.get_address(msg.script_type, coin, node) + address = addresses.get_address(msg.script_type, coin, node, msg.multisig) if msg.show_display: while True: diff --git a/src/apps/wallet/sign_tx/__init__.py b/src/apps/wallet/sign_tx/__init__.py index 31407610fd..2502f2fd8d 100644 --- a/src/apps/wallet/sign_tx/__init__.py +++ b/src/apps/wallet/sign_tx/__init__.py @@ -21,6 +21,10 @@ async def sign_tx(ctx, msg): raise wire.FailureError(*e.args) except signing.AddressError as e: raise wire.FailureError(*e.args) + except signing.ScriptsError as e: + raise wire.FailureError(*e.args) + except signing.Bip143Error as e: + raise wire.FailureError(*e.args) if req.__qualname__ == 'TxRequest': if req.request_type == TXFINISHED: break diff --git a/src/apps/wallet/sign_tx/addresses.py b/src/apps/wallet/sign_tx/addresses.py index afa8963c1c..3ae0069319 100644 --- a/src/apps/wallet/sign_tx/addresses.py +++ b/src/apps/wallet/sign_tx/addresses.py @@ -8,6 +8,9 @@ from trezor.messages.CoinType import CoinType from trezor.messages import FailureType from trezor.messages import InputScriptType +from apps.wallet.sign_tx.scripts import * +from apps.wallet.sign_tx.multisig import * + # supported witness version for bech32 addresses _BECH32_WITVER = const(0x00) @@ -16,21 +19,50 @@ class AddressError(Exception): pass -def get_address(script_type: InputScriptType, coin: CoinType, node) -> str: +def get_address(script_type: InputScriptType, coin: CoinType, node, multisig=None) -> str: - if script_type == InputScriptType.SPENDADDRESS: # p2pkh + if script_type == InputScriptType.SPENDADDRESS or script_type == InputScriptType.SPENDMULTISIG: + if multisig: # p2sh multisig + pubkey = node.public_key() + index = multisig_pubkey_index(multisig, pubkey) + if index is None: + raise AddressError(FailureType.ProcessError, + 'Public key not found') + if coin.address_type_p2sh is None: + raise AddressError(FailureType.ProcessError, + 'Multisig not enabled on this coin') + + pubkeys = multisig_get_pubkeys(multisig) + return address_multisig_p2sh(pubkeys, multisig.m, coin.address_type_p2sh) + if script_type == InputScriptType.SPENDMULTISIG: + raise AddressError(FailureType.ProcessError, + 'Multisig details required') + + # p2pkh return node.address(coin.address_type) - elif script_type == InputScriptType.SPENDWITNESS: # native p2wpkh + elif script_type == InputScriptType.SPENDWITNESS: # native p2wpkh or native p2wsh if not coin.segwit or not coin.bech32_prefix: raise AddressError(FailureType.ProcessError, 'Segwit not enabled on this coin') + # native p2wsh multisig + if multisig is not None: + pubkeys = multisig_get_pubkeys(multisig) + return address_multisig_p2wsh(pubkeys, multisig.m, coin.bech32_prefix) + + # native p2wpkh return address_p2wpkh(node.public_key(), coin.bech32_prefix) - elif script_type == InputScriptType.SPENDP2SHWITNESS: # p2wpkh using p2sh - if not coin.segwit or not coin.address_type_p2sh: + elif script_type == InputScriptType.SPENDP2SHWITNESS: # p2wpkh or p2wsh nested in p2sh + if not coin.segwit or coin.address_type_p2sh is None: raise AddressError(FailureType.ProcessError, 'Segwit not enabled on this coin') + # p2wsh multisig nested in p2sh + if multisig is not None: + pubkeys = multisig_get_pubkeys(multisig) + return address_multisig_p2wsh_in_p2sh(pubkeys, multisig.m, coin.address_type_p2sh) + + # p2wpkh nested in p2sh return address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) else: @@ -38,21 +70,51 @@ def get_address(script_type: InputScriptType, coin: CoinType, node) -> str: 'Invalid script type') -def address_p2wpkh_in_p2sh(pubkey: bytes, addrtype: int) -> str: +def address_multisig_p2sh(pubkeys: bytes, m: int, addrtype: int): + if addrtype is None: + raise AddressError(FailureType.ProcessError, + 'Multisig not enabled on this coin') + redeem_script = output_script_multisig(pubkeys, m) + redeem_script_hash = sha256_ripemd160_digest(redeem_script) + return address_p2sh(redeem_script_hash, addrtype) + + +def address_multisig_p2wsh_in_p2sh(pubkeys: bytes, m: int, addrtype: int): + if addrtype is None: + raise AddressError(FailureType.ProcessError, + 'Multisig not enabled on this coin') + witness_script = output_script_multisig(pubkeys, m) + witness_script_hash = sha256(witness_script).digest() + return address_p2wsh_in_p2sh(witness_script_hash, addrtype) + + +def address_multisig_p2wsh(pubkeys: bytes, m: int, hrp: str): + if not hrp: + raise AddressError(FailureType.ProcessError, + 'Multisig not enabled on this coin') + witness_script = output_script_multisig(pubkeys, m) + witness_script_hash = sha256(witness_script).digest() + return address_p2wsh(witness_script_hash, hrp) + + +def address_p2sh(redeem_script_hash: bytes, addrtype: int) -> str: s = bytearray(21) s[0] = addrtype - s[1:21] = address_p2wpkh_in_p2sh_raw(pubkey) + s[1:21] = redeem_script_hash return base58.encode_check(bytes(s)) -def address_p2wpkh_in_p2sh_raw(pubkey: bytes) -> bytes: - s = bytearray(22) - s[0] = 0x00 # OP_0 - s[1] = 0x14 # pushing 20 bytes - s[2:22] = ecdsa_hash_pubkey(pubkey) - h = sha256(s).digest() - h = ripemd160(h).digest() - return h +def address_p2wpkh_in_p2sh(pubkey: bytes, addrtype: int) -> str: + pubkey_hash = ecdsa_hash_pubkey(pubkey) + redeem_script = output_script_native_p2wpkh_or_p2wsh(pubkey_hash) + redeem_script_hash = sha256_ripemd160_digest(redeem_script) + return address_p2sh(redeem_script_hash, addrtype) + + +def address_p2wsh_in_p2sh(witness_script_hash: bytes, addrtype: int) -> str: + redeem_script = output_script_native_p2wpkh_or_p2wsh(witness_script_hash) + redeem_script_hash = sha256_ripemd160_digest(redeem_script) + return address_p2sh(redeem_script_hash, addrtype) def address_p2wpkh(pubkey: bytes, hrp: str) -> str: @@ -64,6 +126,14 @@ def address_p2wpkh(pubkey: bytes, hrp: str) -> str: return address +def address_p2wsh(witness_script_hash: bytes, hrp: str) -> str: + address = bech32.encode(hrp, _BECH32_WITVER, witness_script_hash) + if address is None: + raise AddressError(FailureType.ProcessError, + 'Invalid address') + return address + + def decode_bech32_address(prefix: str, address: str) -> bytes: witver, raw = bech32.decode(prefix, address) if witver != _BECH32_WITVER: diff --git a/src/apps/wallet/sign_tx/multisig.py b/src/apps/wallet/sign_tx/multisig.py new file mode 100644 index 0000000000..baa8c19247 --- /dev/null +++ b/src/apps/wallet/sign_tx/multisig.py @@ -0,0 +1,88 @@ +from trezor.crypto.hashlib import sha256 +from trezor.crypto import bip32 + +from trezor.messages.MultisigRedeemScriptType import MultisigRedeemScriptType +from trezor.messages.HDNodePathType import HDNodePathType + +from apps.wallet.sign_tx.writers import * +from apps.common.hash_writer import * + + +class MultisigFingerprint: + def __init__(self): + self.fingerprint = None # multisig fingerprint bytes + self.mismatch = False # flag if multisig input fingerprints are equal + + def add(self, multisig: MultisigRedeemScriptType): + fp = multisig_fingerprint(multisig) + assert fp is not None + if self.fingerprint is None: + self.fingerprint = fp + elif self.fingerprint != fp: + self.mismatch = True + + def matches(self, multisig: MultisigRedeemScriptType): + fp = multisig_fingerprint(multisig) + assert fp is not None + if self.mismatch is False and self.fingerprint == fp: + return True + else: + return False + + +def multisig_fingerprint(multisig: MultisigRedeemScriptType) -> bytes: + pubkeys = multisig.pubkeys + m = multisig.m + n = len(pubkeys) + + if n < 1 or n > 15 or m < 1 or m > 15: + raise SigningError(FailureType.DataError, + 'Invalid multisig parameters') + + for hd in pubkeys: + d = hd.node + if len(d.public_key) != 33 or len(d.chain_code) != 32: + raise SigningError(FailureType.DataError, + 'Invalid multisig parameters') + + # casting to bytes(), sorting on bytearray() is not supported in MicroPython + pubkeys = sorted(pubkeys, key=lambda hd: bytes(hd.node.public_key)) + + h = HashWriter(sha256) + write_uint32(h, m) + write_uint32(h, n) + for hd in pubkeys: + d = hd.node + write_uint32(h, d.depth) + write_uint32(h, d.fingerprint) + write_uint32(h, d.child_num) + write_bytes(h, d.chain_code) + write_bytes(h, d.public_key) + + return h.get_digest() + + +def multisig_pubkey_index(multisig: MultisigRedeemScriptType, pubkey: bytes) -> int: + for i, hd in enumerate(multisig.pubkeys): + if multisig_get_pubkey(hd) == pubkey: + return i + raise SigningError(FailureType.DataError, + 'Pubkey not found in multisig script') + + +def multisig_get_pubkey(hd: HDNodePathType) -> bytes: + p = hd.address_n + n = hd.node + node = bip32.HDNode( + depth=n.depth, + fingerprint=n.fingerprint, + child_num=n.child_num, + chain_code=n.chain_code, + public_key=n.public_key) + for i in p: + node.derive(i, True) + return node.public_key() + + +def multisig_get_pubkeys(multisig: MultisigRedeemScriptType): + return [multisig_get_pubkey(hd) for hd in multisig.pubkeys] diff --git a/src/apps/wallet/sign_tx/scripts.py b/src/apps/wallet/sign_tx/scripts.py index d0e71a4207..1839ef12f7 100644 --- a/src/apps/wallet/sign_tx/scripts.py +++ b/src/apps/wallet/sign_tx/scripts.py @@ -1,20 +1,21 @@ +from trezor.crypto.hashlib import ripemd160, sha256 +from apps.wallet.sign_tx.multisig import multisig_get_pubkeys from apps.wallet.sign_tx.writers import * -# TX Scripts +class ScriptsError(ValueError): + pass + + +# P2PKH, P2SH # === +# https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki -# -------------------------- First gen -------------------------- - -# =============== P2PK =============== -# obsolete - - -# =============== P2PKH =============== def input_script_p2pkh_or_p2sh(pubkey: bytes, signature: bytes, sighash: int) -> bytearray: w = bytearray_with_cap(5 + len(signature) + 1 + 5 + len(pubkey)) - append_signature_and_pubkey(w, pubkey, signature, sighash) + append_signature(w, signature, sighash) + append_pubkey(w, pubkey) return w @@ -29,13 +30,9 @@ def output_script_p2pkh(pubkeyhash: bytes) -> bytearray: return s -# =============== P2SH =============== -# see https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki - -# input script (scriptSig) is the same as input_script_p2pkh_or_p2sh - -# output script (scriptPubKey) is A9 14 87 def output_script_p2sh(scripthash: bytes) -> bytearray: + # A9 14 87 + s = bytearray(23) s[0] = 0xA9 # OP_HASH_160 s[1] = 0x14 # pushing 20 bytes @@ -44,22 +41,28 @@ def output_script_p2sh(scripthash: bytes) -> bytearray: return s -# -------------------------- SegWit -------------------------- +# SegWit: Native P2WPKH or P2WSH +# === +# https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh +# https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wsh +# +# P2WPKH (Pay-to-Witness-Public-Key-Hash) is the segwit native P2PKH. +# Not backwards compatible. +# +# P2WSH (Pay-to-Witness-Script-Hash) is segwit native P2SH. +# Not backwards compatible. -# =============== Native P2WPKH =============== -# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh -# P2WPKH (Pay-to-Witness-Public-Key-Hash) is the segwit native P2PKH -# not backwards compatible -# input script is completely replaced by the witness and therefore empty def input_script_native_p2wpkh_or_p2wsh() -> bytearray: + # Completely replaced by the witness and therefore empty. return bytearray(0) -# output script is either: -# 00 14 <20-byte-key-hash> -# 00 20 <32-byte-script-hash> def output_script_native_p2wpkh_or_p2wsh(witprog: bytes) -> bytearray: + # Either: + # 00 14 <20-byte-key-hash> + # 00 20 <32-byte-script-hash> + w = bytearray_with_cap(3 + len(witprog)) w.append(0x00) # witness version byte w.append(len(witprog)) # pub key hash length is 20 (P2WPKH) or 32 (P2WSH) bytes @@ -67,39 +70,139 @@ def output_script_native_p2wpkh_or_p2wsh(witprog: bytes) -> bytearray: return w -# =============== Native P2WPKH nested in P2SH =============== -# P2WPKH is nested in P2SH to be backwards compatible -# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program +# SegWit: P2WPKH nested in P2SH +# === +# https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program +# +# P2WPKH is nested in P2SH to be backwards compatible. +# Uses normal P2SH output scripts. + -# input script (scriptSig) is 16 00 14 -# signature is moved to the witness def input_script_p2wpkh_in_p2sh(pubkeyhash: bytes) -> bytearray: + # 16 00 14 + # Signature is moved to the witness. + w = bytearray_with_cap(3 + len(pubkeyhash)) - w.append(0x16) # 0x16 - length of the redeemScript + w.append(0x16) # length of the data w.append(0x00) # witness version byte w.append(0x14) # P2WPKH witness program (pub key hash length) write_bytes(w, pubkeyhash) # pub key hash return w -# output script (scriptPubKey) is A9 14 87 -# which is same as the output_script_p2sh + +# SegWit: P2WSH nested in P2SH +# === +# https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wsh-nested-in-bip16-p2sh +# +# P2WSH is nested in P2SH to be backwards compatible. +# Uses normal P2SH output scripts. -# =============== Native P2WSH =============== -# see https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wsh -# P2WSH (Pay-to-Witness-Script-Hash) is segwit native P2SH -# not backwards compatible +def input_script_p2wsh_in_p2sh(script_hash: bytes) -> bytearray: + # 22 00 20 + # Signature is moved to the witness. -# input script is completely replaced by the witness and therefore empty -# same as input_script_native_p2wpkh_or_p2wsh + if len(script_hash) != 32: + raise ScriptsError('Redeem script hash should be 32 bytes long') -# output script consists of 00 20 <32-byte-key-hash> -# same as output_script_native_p2wpkh_or_p2wsh (only different length) + w = bytearray_with_cap(3 + len(script_hash)) + w.append(0x22) # length of the data + w.append(0x00) # witness version byte + w.append(0x20) # P2WSH witness program (redeem script hash length) + write_bytes(w, script_hash) + return w -# -------------------------- Others -------------------------- +# SegWit: Witness getters +# === + + +def witness_p2wpkh(signature: bytes, pubkey: bytes, sighash: int): + w = bytearray_with_cap(1 + 5 + len(signature) + 1 + 5 + len(pubkey)) + write_varint(w, 0x02) # num of segwit items, in P2WPKH it's always 2 + append_signature(w, signature, sighash) + append_pubkey(w, pubkey) + return w + + +def witness_p2wsh(multisig: MultisigRedeemScriptType, signature: bytes, signature_index: int, sighash: int): + signatures = multisig.signatures # other signatures + if len(signatures[signature_index]) > 0: + raise ScriptsError('Invalid multisig parameters') + signatures[signature_index] = signature # our signature + + # filter empty + signatures = [s for s in multisig.signatures if len(s) > 0] + + # witness program + signatures + redeem script + num_of_witness_items = 1 + len(signatures) + 1 + + w = bytearray() + write_varint(w, num_of_witness_items) + write_varint(w, 0) # version 0 witness program + + for s in signatures: + append_signature(w, s, sighash) # size of the witness included + + # redeem script + pubkeys = multisig_get_pubkeys(multisig) + redeem_script = output_script_multisig(pubkeys, multisig.m) + write_varint(w, len(redeem_script)) + write_bytes(w, redeem_script) + return w + + +# Multisig +# === +# +# Used either as P2SH, P2WSH, or P2WSH nested in P2SH. + + +def input_script_multisig(multisig: MultisigRedeemScriptType, signature: bytes, signature_index: int, sighash: int): + signatures = multisig.signatures # other signatures + if len(signatures[signature_index]) > 0: + raise ScriptsError('Invalid multisig parameters') + signatures[signature_index] = signature # our signature + + w = bytearray() + # Starts with OP_FALSE because of an old OP_CHECKMULTISIG bug, which + # consumes one additional item on the stack: + # https://bitcoin.org/en/developer-guide#standard-transactions + w.append(0x00) + + for s in signatures: + if len(s): + append_signature(w, s, sighash) + + # redeem script + pubkeys = multisig_get_pubkeys(multisig) + redeem_script = output_script_multisig(pubkeys, multisig.m) + write_op_push(w, len(redeem_script)) + write_bytes(w, redeem_script) + + return w + + +def output_script_multisig(pubkeys, m: int) -> bytearray: + n = len(pubkeys) + if n < 1 or n > 15 or m < 1 or m > 15: + raise ScriptsError('Invalid multisig parameters') + for pubkey in pubkeys: + if len(pubkey) != 33: + raise ScriptsError('Invalid multisig parameters') + + w = bytearray() + w.append(0x50 + m) # numbers 1 to 16 are pushed as 0x50 + value + for p in pubkeys: + append_pubkey(w, p) + w.append(0x50 + n) + w.append(0xAE) # OP_CHECKMULTISIG + return w + + +# OP_RETURN +# === -# === OP_RETURN script def output_script_paytoopreturn(data: bytes) -> bytearray: w = bytearray_with_cap(1 + 5 + len(data)) @@ -109,12 +212,24 @@ def output_script_paytoopreturn(data: bytes) -> bytearray: return w -# === helpers +# Helpers +# === -def append_signature_and_pubkey(w: bytearray, pubkey: bytes, signature: bytes, sighash: int) -> bytearray: + +def append_signature(w: bytearray, signature: bytes, sighash: int) -> bytearray: write_op_push(w, len(signature) + 1) write_bytes(w, signature) w.append(sighash) + return w + + +def append_pubkey(w: bytearray, pubkey: bytes) -> bytearray: write_op_push(w, len(pubkey)) write_bytes(w, pubkey) return w + + +def sha256_ripemd160_digest(b: bytes) -> bytes: + h = sha256(b).digest() + h = ripemd160(h).digest() + return h diff --git a/src/apps/wallet/sign_tx/segwit_bip143.py b/src/apps/wallet/sign_tx/segwit_bip143.py index 863e0020fb..0423a46317 100644 --- a/src/apps/wallet/sign_tx/segwit_bip143.py +++ b/src/apps/wallet/sign_tx/segwit_bip143.py @@ -3,6 +3,8 @@ from trezor.messages.SignTx import SignTx from trezor.messages import InputScriptType, FailureType from apps.wallet.sign_tx.writers import * +from apps.wallet.sign_tx.scripts import output_script_p2pkh, output_script_multisig +from apps.wallet.sign_tx.multisig import multisig_get_pubkeys from apps.common.hash_writer import HashWriter @@ -58,22 +60,21 @@ class Bip143: return get_tx_hash(h_preimage, True) - # this not redeemScript nor scriptPubKey - # for P2WPKH this is always 0x1976a914{20-byte-pubkey-hash}88ac + # see https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification + # item 5 for details def derive_script_code(self, txi: TxInputType, pubkeyhash: bytes) -> bytearray: - # p2wpkh in p2sh or native p2wpkh + + if txi.multisig: + return output_script_multisig(multisig_get_pubkeys(txi.multisig), txi.multisig.m) + p2pkh = (txi.script_type == InputScriptType.SPENDWITNESS or txi.script_type == InputScriptType.SPENDP2SHWITNESS or txi.script_type == InputScriptType.SPENDADDRESS) if p2pkh: - s = bytearray(25) - s[0] = 0x76 # OP_DUP - s[1] = 0xA9 # OP_HASH_160 - s[2] = 0x14 # pushing 20 bytes - s[3:23] = pubkeyhash - s[23] = 0x88 # OP_EQUALVERIFY - s[24] = 0xAC # OP_CHECKSIG - return s + # for p2wpkh in p2sh or native p2wpkh + # the scriptCode is a classic p2pkh + return output_script_p2pkh(pubkeyhash) + else: raise Bip143Error(FailureType.DataError, 'Unknown input script type for bip143 script code') diff --git a/src/apps/wallet/sign_tx/signing.py b/src/apps/wallet/sign_tx/signing.py index b51c0113ce..0599cf9a21 100644 --- a/src/apps/wallet/sign_tx/signing.py +++ b/src/apps/wallet/sign_tx/signing.py @@ -1,22 +1,21 @@ from micropython import const -from trezor.crypto.hashlib import sha256 +from trezor.crypto import base58, bip32, der from trezor.crypto.curve import secp256k1 -from trezor.crypto import base58, der +from trezor.crypto.hashlib import sha256 -from trezor.messages.TxRequestSerializedType import TxRequestSerializedType -from trezor.messages.TxRequestDetailsType import TxRequestDetailsType from trezor.messages import OutputScriptType +from trezor.messages.TxRequestDetailsType import TxRequestDetailsType +from trezor.messages.TxRequestSerializedType import TxRequestSerializedType -from apps.common import address_type -from apps.common import coins +from apps.common import address_type, coins +from apps.common.hash_writer import HashWriter from apps.wallet.sign_tx.addresses import * from apps.wallet.sign_tx.helpers import * -from apps.wallet.sign_tx.segwit_bip143 import * from apps.wallet.sign_tx.scripts import * -from apps.wallet.sign_tx.writers import * +from apps.wallet.sign_tx.segwit_bip143 import * from apps.wallet.sign_tx.tx_weight_calculator import * -from apps.common.hash_writer import HashWriter +from apps.wallet.sign_tx.writers import * # the number of bip32 levels used in a wallet (chain and address) _BIP32_WALLET_DEPTH = const(2) @@ -44,20 +43,17 @@ class SigningError(ValueError): # - check inputs, previous transactions, and outputs # - ask for confirmations # - check fee -async def check_tx_fee(tx: SignTx, root): - +async def check_tx_fee(tx: SignTx, root: bip32.HDNode): coin = coins.by_name(tx.coin_name) # h_first is used to make sure the inputs and outputs streamed in Phase 1 # are the same as in Phase 2. it is thus not required to fully hash the # tx, as the SignTx info is streamed only once h_first = HashWriter(sha256) # not a real tx hash - bip143 = Bip143() - weight = TxWeightCalculator(tx.inputs_count, tx.outputs_count) - txo_bin = TxOutputBinType() - tx_req = TxRequest() - tx_req.details = TxRequestDetailsType() + bip143 = Bip143() # bip143 transaction hashing + multifp = MultisigFingerprint() # control checksum of multisig inputs + weight = TxWeightCalculator(tx.inputs_count, tx.outputs_count) total_in = 0 # sum of input amounts segwit_in = 0 # sum of segwit input amounts @@ -66,28 +62,25 @@ async def check_tx_fee(tx: SignTx, root): wallet_path = [] # common prefix of input paths segwit = {} # dict of booleans stating if input is segwit + # output structures + txo_bin = TxOutputBinType() + tx_req = TxRequest() + tx_req.details = TxRequestDetailsType() + for i in range(tx.inputs_count): # STAGE_REQUEST_1_INPUT txi = await request_tx_input(tx_req, i) wallet_path = input_extract_wallet_path(txi, wallet_path) write_tx_input_check(h_first, txi) weight.add_input(txi) - bip143.add_prevouts(txi) + bip143.add_prevouts(txi) # all inputs are included (non-segwit as well) bip143.add_sequence(txi) - is_segwit = (txi.script_type == InputScriptType.SPENDWITNESS or - txi.script_type == InputScriptType.SPENDP2SHWITNESS) - if coin.force_bip143: - is_bip143 = (txi.script_type == InputScriptType.SPENDADDRESS) - if not is_bip143: - raise SigningError(FailureType.DataError, - 'Wrong input script type') - if not txi.amount: - raise SigningError(FailureType.DataError, - 'BIP 143 input without amount') - segwit[i] = False - segwit_in += txi.amount - total_in += txi.amount - elif is_segwit: + + if txi.multisig: + multifp.add(txi.multisig) + + if txi.script_type in (InputScriptType.SPENDWITNESS, + InputScriptType.SPENDP2SHWITNESS): if not coin.segwit: raise SigningError(FailureType.DataError, 'Segwit not enabled on this coin') @@ -97,10 +90,21 @@ async def check_tx_fee(tx: SignTx, root): segwit[i] = True segwit_in += txi.amount total_in += txi.amount - elif txi.script_type == InputScriptType.SPENDADDRESS: - segwit[i] = False - total_in += await get_prevtx_output_value( - tx_req, txi.prev_hash, txi.prev_index) + + elif txi.script_type in (InputScriptType.SPENDADDRESS, + InputScriptType.SPENDMULTISIG): + if coin.force_bip143: + if not txi.amount: + raise SigningError(FailureType.DataError, + 'BIP 143 input without amount') + segwit[i] = False + segwit_in += txi.amount + total_in += txi.amount + else: + segwit[i] = False + total_in += await get_prevtx_output_value( + tx_req, txi.prev_hash, txi.prev_index) + else: raise SigningError(FailureType.DataError, 'Wrong input script type') @@ -111,14 +115,14 @@ async def check_tx_fee(tx: SignTx, root): txo_bin.amount = txo.amount txo_bin.script_pubkey = output_derive_script(txo, coin, root) weight.add_output(txo_bin.script_pubkey) - if output_is_change(txo, wallet_path, segwit_in): - if change_out != 0: - raise SigningError(FailureType.ProcessError, - 'Only one change output is valid') + + if change_out == 0 and is_change(txo, wallet_path, segwit_in, multifp): + # output is change and does not need confirmation change_out = txo.amount elif not await confirm_output(txo, coin): raise SigningError(FailureType.ActionCancelled, 'Output cancelled') + write_tx_output(h_first, txo_bin) bip143.add_output(txo_bin) total_out += txo_bin.amount @@ -141,8 +145,7 @@ async def check_tx_fee(tx: SignTx, root): return h_first, bip143, segwit, total_in, wallet_path -async def sign_tx(tx: SignTx, root): - +async def sign_tx(tx: SignTx, root: bip32.HDNode): tx = sanitize_sign_tx(tx) # Phase 1 @@ -171,7 +174,8 @@ async def sign_tx(tx: SignTx, root): txi_sign = await request_tx_input(tx_req, i_sign) input_check_wallet_path(txi_sign, wallet_path) - is_bip143 = (txi_sign.script_type == InputScriptType.SPENDADDRESS) + is_bip143 = (txi_sign.script_type == InputScriptType.SPENDADDRESS or + txi_sign.script_type == InputScriptType.SPENDMULTISIG) if not is_bip143 or txi_sign.amount > authorized_in: raise SigningError(FailureType.ProcessError, 'Transaction has changed during signing') @@ -182,6 +186,10 @@ async def sign_tx(tx: SignTx, root): bip143_hash = bip143.preimage_hash( tx, txi_sign, ecdsa_hash_pubkey(key_sign_pub), get_hash_type(coin)) + # if multisig, check if singing with a key that is included in multisig + if txi_sign.multisig: + multisig_pubkey_index(txi_sign.multisig, key_sign_pub) + signature = ecdsa_sign(key_sign, bip143_hash) tx_ser.signature_index = i_sign tx_ser.signature = signature @@ -239,8 +247,18 @@ async def sign_tx(tx: SignTx, root): txi_sign = txi key_sign = node_derive(root, txi.address_n) key_sign_pub = key_sign.public_key() - txi_sign.script_sig = output_script_p2pkh( - ecdsa_hash_pubkey(key_sign_pub)) + # for the signing process the script_sig is equal + # to the previous tx's scriptPubKey (P2PKH) or a redeem script (P2SH) + if txi_sign.script_type == InputScriptType.SPENDMULTISIG: + txi_sign.script_sig = output_script_multisig( + multisig_get_pubkeys(txi_sign.multisig), + txi_sign.multisig.m) + elif txi_sign.script_type == InputScriptType.SPENDADDRESS: + txi_sign.script_sig = output_script_p2pkh( + ecdsa_hash_pubkey(key_sign_pub)) + else: + raise SigningError(FailureType.ProcessError, + 'Unknown transaction type') else: txi.script_sig = bytes() write_tx_input(h_sign, txi) @@ -264,6 +282,10 @@ async def sign_tx(tx: SignTx, root): raise SigningError(FailureType.ProcessError, 'Transaction has changed during signing') + # if multisig, check if singing with a key that is included in multisig + if txi_sign.multisig: + multisig_pubkey_index(txi_sign.multisig, key_sign_pub) + # compute the signature from the tx digest signature = ecdsa_sign(key_sign, get_tx_hash(h_sign, True)) tx_ser.signature_index = i_sign @@ -321,7 +343,12 @@ async def sign_tx(tx: SignTx, root): tx, txi, ecdsa_hash_pubkey(key_sign_pub), get_hash_type(coin)) signature = ecdsa_sign(key_sign, bip143_hash) - witness = get_p2wpkh_witness(coin, signature, key_sign_pub) + if txi.multisig: + # find out place of our signature based on the pubkey + signature_index = multisig_pubkey_index(txi.multisig, key_sign_pub) + witness = witness_p2wsh(txi.multisig, signature, signature_index, get_hash_type(coin)) + else: + witness = witness_p2wpkh(signature, key_sign_pub, get_hash_type(coin)) tx_ser.serialized_tx = witness tx_ser.signature_index = i @@ -392,7 +419,7 @@ def get_hash_type(coin: CoinType) -> int: return hashtype -def get_tx_header(tx: SignTx, segwit=False): +def get_tx_header(tx: SignTx, segwit: bool = False): w_txi = bytearray() write_uint32(w_txi, tx.version) if segwit: @@ -402,26 +429,21 @@ def get_tx_header(tx: SignTx, segwit=False): return w_txi -def get_p2wpkh_witness(coin: CoinType, signature: bytes, pubkey: bytes): - w = bytearray_with_cap(1 + 5 + len(signature) + 1 + 5 + len(pubkey)) - write_varint(w, 0x02) # num of segwit items, in P2WPKH it's always 2 - append_signature_and_pubkey(w, pubkey, signature, get_hash_type(coin)) - return w - - # TX Outputs # === -def output_derive_script(o: TxOutputType, coin: CoinType, root) -> bytes: +def output_derive_script(o: TxOutputType, coin: CoinType, root: bip32.HDNode) -> bytes: if o.script_type == OutputScriptType.PAYTOOPRETURN: + # op_return output if o.amount != 0: raise SigningError(FailureType.DataError, 'OP_RETURN output with non-zero amount') return output_script_paytoopreturn(o.op_return_data) - if o.address_n: # change output + if o.address_n: + # change output if o.address: raise SigningError(FailureType.DataError, 'Address in change output') o.address = get_address_for_change(o, coin, root) @@ -429,24 +451,27 @@ def output_derive_script(o: TxOutputType, coin: CoinType, root) -> bytes: if not o.address: raise SigningError(FailureType.DataError, 'Missing address') - if coin.bech32_prefix and o.address.startswith(coin.bech32_prefix): # p2wpkh or p2wsh + if coin.bech32_prefix and o.address.startswith(coin.bech32_prefix): + # p2wpkh or p2wsh witprog = decode_bech32_address(coin.bech32_prefix, o.address) return output_script_native_p2wpkh_or_p2wsh(witprog) raw_address = base58.decode_check(o.address) - if address_type.check(coin.address_type, raw_address): # p2pkh + if address_type.check(coin.address_type, raw_address): + # p2pkh pubkeyhash = address_type.strip(coin.address_type, raw_address) return output_script_p2pkh(pubkeyhash) - elif address_type.check(coin.address_type_p2sh, raw_address): # p2sh + elif address_type.check(coin.address_type_p2sh, raw_address): + # p2sh scripthash = address_type.strip(coin.address_type_p2sh, raw_address) return output_script_p2sh(scripthash) raise SigningError(FailureType.DataError, 'Invalid address type') -def get_address_for_change(o: TxOutputType, coin: CoinType, root): +def get_address_for_change(o: TxOutputType, coin: CoinType, root: bip32.HDNode): if o.script_type == OutputScriptType.PAYTOADDRESS: input_script_type = InputScriptType.SPENDADDRESS elif o.script_type == OutputScriptType.PAYTOMULTISIG: @@ -457,11 +482,10 @@ def get_address_for_change(o: TxOutputType, coin: CoinType, root): input_script_type = InputScriptType.SPENDP2SHWITNESS else: raise SigningError(FailureType.DataError, 'Invalid script type') - return get_address(input_script_type, coin, node_derive(root, o.address_n)) + return get_address(input_script_type, coin, node_derive(root, o.address_n), o.multisig) def output_is_change(o: TxOutputType, wallet_path: list, segwit_in: int) -> bool: - address_n = o.address_n is_segwit = (o.script_type == OutputScriptType.PAYTOWITNESS or o.script_type == OutputScriptType.PAYTOP2SHWITNESS) if is_segwit and o.amount > segwit_in: @@ -469,10 +493,10 @@ def output_is_change(o: TxOutputType, wallet_path: list, segwit_in: int) -> bool # segwit inputs paid. this is to prevent user being tricked into # creating ANYONECANSPEND outputs before full segwit activation. return False - return (address_n is not None and wallet_path is not None and - wallet_path == address_n[:-_BIP32_WALLET_DEPTH] and - address_n[-2] == _BIP32_CHANGE_CHAIN and - address_n[-1] <= _BIP32_MAX_LAST_ELEMENT) + return (wallet_path is not None and + wallet_path == o.address_n[:-_BIP32_WALLET_DEPTH] and + o.address_n[-2] <= _BIP32_CHANGE_CHAIN and + o.address_n[-1] <= _BIP32_MAX_LAST_ELEMENT) # Tx Inputs @@ -481,11 +505,33 @@ def output_is_change(o: TxOutputType, wallet_path: list, segwit_in: int) -> bool def input_derive_script(coin: CoinType, i: TxInputType, pubkey: bytes, signature: bytes=None) -> bytes: if i.script_type == InputScriptType.SPENDADDRESS: - return input_script_p2pkh_or_p2sh(pubkey, signature, get_hash_type(coin)) # p2pkh or p2sh - if i.script_type == InputScriptType.SPENDP2SHWITNESS: # p2wpkh using p2sh + # p2pkh or p2sh + return input_script_p2pkh_or_p2sh( + pubkey, signature, get_hash_type(coin)) + + if i.script_type == InputScriptType.SPENDP2SHWITNESS: + # p2wpkh or p2wsh using p2sh + + if i.multisig: + # p2wsh in p2sh + pubkeys = multisig_get_pubkeys(i.multisig) + witness_script = output_script_multisig(pubkeys, i.multisig.m) + witness_script_hash = sha256(witness_script).digest() + return input_script_p2wsh_in_p2sh(witness_script_hash) + + # p2wpkh in p2sh return input_script_p2wpkh_in_p2sh(ecdsa_hash_pubkey(pubkey)) - elif i.script_type == InputScriptType.SPENDWITNESS: # native p2wpkh or p2wsh + + elif i.script_type == InputScriptType.SPENDWITNESS: + # native p2wpkh or p2wsh return input_script_native_p2wpkh_or_p2wsh() + + elif i.script_type == InputScriptType.SPENDMULTISIG: + # p2sh multisig + signature_index = multisig_pubkey_index(i.multisig, pubkey) + return input_script_multisig( + i.multisig, signature, signature_index, get_hash_type(coin)) + else: raise SigningError(FailureType.ProcessError, 'Invalid script type') @@ -512,13 +558,24 @@ def input_check_wallet_path(txi: TxInputType, wallet_path: list) -> list: 'Transaction has changed during signing') -def node_derive(root, address_n: list): +def node_derive(root: bip32.HDNode, address_n: list): node = root.clone() node.derive_path(address_n) return node -def ecdsa_sign(node, digest: bytes) -> bytes: +def ecdsa_sign(node: bip32.HDNode, digest: bytes) -> bytes: sig = secp256k1.sign(node.private_key(), digest) sigder = der.encode_seq((sig[1:33], sig[33:65])) return sigder + + +def is_change( + txo: TxOutputType, + wallet_path: list, + segwit_in: int, + multifp: MultisigFingerprint) -> bool: + if txo.multisig: + if not multifp.matches(txo.multisig): + return False + return output_is_change(txo, wallet_path, segwit_in) diff --git a/src/apps/wallet/sign_tx/tx_weight_calculator.py b/src/apps/wallet/sign_tx/tx_weight_calculator.py index 88655838f5..e368614967 100644 --- a/src/apps/wallet/sign_tx/tx_weight_calculator.py +++ b/src/apps/wallet/sign_tx/tx_weight_calculator.py @@ -55,7 +55,7 @@ class TxWeightCalculator: if i.multisig: multisig_script_size = ( _TXSIZE_MULTISIGSCRIPT + - i.multisig.pubkeys_count * (1 + _TXSIZE_PUBKEY)) + len(i.multisig.pubkeys) * (1 + _TXSIZE_PUBKEY)) input_script_size = ( 1 + # the OP_FALSE bug in multisig i.multisig.m * (1 + _TXSIZE_SIGNATURE) + diff --git a/src/apps/wallet/sign_tx/writers.py b/src/apps/wallet/sign_tx/writers.py index 37c5821add..ac181934f2 100644 --- a/src/apps/wallet/sign_tx/writers.py +++ b/src/apps/wallet/sign_tx/writers.py @@ -1,8 +1,8 @@ - from trezor.crypto.hashlib import sha256 from apps.wallet.sign_tx.writers import * + # TX Serialization # === diff --git a/src/trezor/messages/Address.py b/src/trezor/messages/Address.py index 36c5232955..c780dc320a 100644 --- a/src/trezor/messages/Address.py +++ b/src/trezor/messages/Address.py @@ -7,3 +7,11 @@ class Address(p.MessageType): 1: ('address', p.UnicodeType, 0), # required } MESSAGE_WIRE_TYPE = 30 + + def __init__( + self, + address: str = None, + **kwargs, + ): + self.address = address + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ApplyFlags.py b/src/trezor/messages/ApplyFlags.py index 4f3c2b94c2..9fd1151d8a 100644 --- a/src/trezor/messages/ApplyFlags.py +++ b/src/trezor/messages/ApplyFlags.py @@ -7,3 +7,11 @@ class ApplyFlags(p.MessageType): 1: ('flags', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 28 + + def __init__( + self, + flags: int = None, + **kwargs, + ): + self.flags = flags + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ApplySettings.py b/src/trezor/messages/ApplySettings.py index fbc6848ea0..b9c888e3b6 100644 --- a/src/trezor/messages/ApplySettings.py +++ b/src/trezor/messages/ApplySettings.py @@ -10,3 +10,17 @@ class ApplySettings(p.MessageType): 4: ('homescreen', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 25 + + def __init__( + self, + language: str = None, + label: str = None, + use_passphrase: bool = None, + homescreen: bytes = None, + **kwargs, + ): + self.language = language + self.label = label + self.use_passphrase = use_passphrase + self.homescreen = homescreen + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/BackupDevice.py b/src/trezor/messages/BackupDevice.py index f44788ff8d..9b17da6ace 100644 --- a/src/trezor/messages/BackupDevice.py +++ b/src/trezor/messages/BackupDevice.py @@ -4,3 +4,9 @@ import protobuf as p class BackupDevice(p.MessageType): MESSAGE_WIRE_TYPE = 34 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ButtonAck.py b/src/trezor/messages/ButtonAck.py index 08b1691836..018274913f 100644 --- a/src/trezor/messages/ButtonAck.py +++ b/src/trezor/messages/ButtonAck.py @@ -4,3 +4,9 @@ import protobuf as p class ButtonAck(p.MessageType): MESSAGE_WIRE_TYPE = 27 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ButtonRequest.py b/src/trezor/messages/ButtonRequest.py index cc5f3dd8f5..fed1c92110 100644 --- a/src/trezor/messages/ButtonRequest.py +++ b/src/trezor/messages/ButtonRequest.py @@ -8,3 +8,13 @@ class ButtonRequest(p.MessageType): 2: ('data', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 26 + + def __init__( + self, + code: int = None, + data: str = None, + **kwargs, + ): + self.code = code + self.data = data + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Cancel.py b/src/trezor/messages/Cancel.py index 4f8a68df71..151af97ce8 100644 --- a/src/trezor/messages/Cancel.py +++ b/src/trezor/messages/Cancel.py @@ -4,3 +4,9 @@ import protobuf as p class Cancel(p.MessageType): MESSAGE_WIRE_TYPE = 20 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ChangePin.py b/src/trezor/messages/ChangePin.py index 167b9fef8a..4289c8010d 100644 --- a/src/trezor/messages/ChangePin.py +++ b/src/trezor/messages/ChangePin.py @@ -7,3 +7,11 @@ class ChangePin(p.MessageType): 1: ('remove', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 4 + + def __init__( + self, + remove: bool = None, + **kwargs, + ): + self.remove = remove + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CipherKeyValue.py b/src/trezor/messages/CipherKeyValue.py index d3d157d2fd..25d7f050b1 100644 --- a/src/trezor/messages/CipherKeyValue.py +++ b/src/trezor/messages/CipherKeyValue.py @@ -13,3 +13,23 @@ class CipherKeyValue(p.MessageType): 7: ('iv', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 23 + + def __init__( + self, + address_n: list = [], + key: str = None, + value: bytes = None, + encrypt: bool = None, + ask_on_encrypt: bool = None, + ask_on_decrypt: bool = None, + iv: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.key = key + self.value = value + self.encrypt = encrypt + self.ask_on_encrypt = ask_on_encrypt + self.ask_on_decrypt = ask_on_decrypt + self.iv = iv + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CipheredKeyValue.py b/src/trezor/messages/CipheredKeyValue.py index aa7c54e627..7fe8dc9d4f 100644 --- a/src/trezor/messages/CipheredKeyValue.py +++ b/src/trezor/messages/CipheredKeyValue.py @@ -7,3 +7,11 @@ class CipheredKeyValue(p.MessageType): 1: ('value', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 48 + + def __init__( + self, + value: bytes = None, + **kwargs, + ): + self.value = value + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ClearSession.py b/src/trezor/messages/ClearSession.py index 808d5b76ee..9ab99e9ebe 100644 --- a/src/trezor/messages/ClearSession.py +++ b/src/trezor/messages/ClearSession.py @@ -4,3 +4,9 @@ import protobuf as p class ClearSession(p.MessageType): MESSAGE_WIRE_TYPE = 24 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CoinType.py b/src/trezor/messages/CoinType.py index 626784651f..5ed7fa140c 100644 --- a/src/trezor/messages/CoinType.py +++ b/src/trezor/messages/CoinType.py @@ -14,5 +14,30 @@ class CoinType(p.MessageType): 10: ('xprv_magic', p.UVarintType, 0), # default=76066276 11: ('segwit', p.BoolType, 0), 12: ('forkid', p.UVarintType, 0), - 13: ('force_bip143', p.BoolType, 0), } + + def __init__( + self, + coin_name: str = None, + coin_shortcut: str = None, + address_type: int = None, + maxfee_kb: int = None, + address_type_p2sh: int = None, + signed_message_header: str = None, + xpub_magic: int = None, + xprv_magic: int = None, + segwit: bool = None, + forkid: int = None, + **kwargs, + ): + self.coin_name = coin_name + self.coin_shortcut = coin_shortcut + self.address_type = address_type + self.maxfee_kb = maxfee_kb + self.address_type_p2sh = address_type_p2sh + self.signed_message_header = signed_message_header + self.xpub_magic = xpub_magic + self.xprv_magic = xprv_magic + self.segwit = segwit + self.forkid = forkid + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CosiCommit.py b/src/trezor/messages/CosiCommit.py index ed51be29a0..a39c8cfc47 100644 --- a/src/trezor/messages/CosiCommit.py +++ b/src/trezor/messages/CosiCommit.py @@ -8,3 +8,13 @@ class CosiCommit(p.MessageType): 2: ('data', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 71 + + def __init__( + self, + address_n: list = [], + data: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.data = data + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CosiCommitment.py b/src/trezor/messages/CosiCommitment.py index 526d54dca2..468ac88408 100644 --- a/src/trezor/messages/CosiCommitment.py +++ b/src/trezor/messages/CosiCommitment.py @@ -8,3 +8,13 @@ class CosiCommitment(p.MessageType): 2: ('pubkey', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 72 + + def __init__( + self, + commitment: bytes = None, + pubkey: bytes = None, + **kwargs, + ): + self.commitment = commitment + self.pubkey = pubkey + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CosiSign.py b/src/trezor/messages/CosiSign.py index b46d1fe702..d79596c33a 100644 --- a/src/trezor/messages/CosiSign.py +++ b/src/trezor/messages/CosiSign.py @@ -10,3 +10,17 @@ class CosiSign(p.MessageType): 4: ('global_pubkey', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 73 + + def __init__( + self, + address_n: list = [], + data: bytes = None, + global_commitment: bytes = None, + global_pubkey: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.data = data + self.global_commitment = global_commitment + self.global_pubkey = global_pubkey + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/CosiSignature.py b/src/trezor/messages/CosiSignature.py index 1ea3a92a7f..19bf191928 100644 --- a/src/trezor/messages/CosiSignature.py +++ b/src/trezor/messages/CosiSignature.py @@ -7,3 +7,11 @@ class CosiSignature(p.MessageType): 1: ('signature', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 74 + + def __init__( + self, + signature: bytes = None, + **kwargs, + ): + self.signature = signature + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkDecision.py b/src/trezor/messages/DebugLinkDecision.py index 445f3129e4..9bdcbcec11 100644 --- a/src/trezor/messages/DebugLinkDecision.py +++ b/src/trezor/messages/DebugLinkDecision.py @@ -7,3 +7,11 @@ class DebugLinkDecision(p.MessageType): 1: ('yes_no', p.BoolType, 0), # required } MESSAGE_WIRE_TYPE = 100 + + def __init__( + self, + yes_no: bool = None, + **kwargs, + ): + self.yes_no = yes_no + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkFlashErase.py b/src/trezor/messages/DebugLinkFlashErase.py index 884674bca5..45d4ce0146 100644 --- a/src/trezor/messages/DebugLinkFlashErase.py +++ b/src/trezor/messages/DebugLinkFlashErase.py @@ -7,3 +7,11 @@ class DebugLinkFlashErase(p.MessageType): 1: ('sector', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 113 + + def __init__( + self, + sector: int = None, + **kwargs, + ): + self.sector = sector + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkGetState.py b/src/trezor/messages/DebugLinkGetState.py index 35cbbf9afe..7332103306 100644 --- a/src/trezor/messages/DebugLinkGetState.py +++ b/src/trezor/messages/DebugLinkGetState.py @@ -4,3 +4,9 @@ import protobuf as p class DebugLinkGetState(p.MessageType): MESSAGE_WIRE_TYPE = 101 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkLog.py b/src/trezor/messages/DebugLinkLog.py index d36982404c..61d8ff3b82 100644 --- a/src/trezor/messages/DebugLinkLog.py +++ b/src/trezor/messages/DebugLinkLog.py @@ -9,3 +9,15 @@ class DebugLinkLog(p.MessageType): 3: ('text', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 104 + + def __init__( + self, + level: int = None, + bucket: str = None, + text: str = None, + **kwargs, + ): + self.level = level + self.bucket = bucket + self.text = text + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkMemory.py b/src/trezor/messages/DebugLinkMemory.py index 331497ee75..6cdbd3fc44 100644 --- a/src/trezor/messages/DebugLinkMemory.py +++ b/src/trezor/messages/DebugLinkMemory.py @@ -7,3 +7,11 @@ class DebugLinkMemory(p.MessageType): 1: ('memory', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 111 + + def __init__( + self, + memory: bytes = None, + **kwargs, + ): + self.memory = memory + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkMemoryRead.py b/src/trezor/messages/DebugLinkMemoryRead.py index 819a146dbb..9adc1cdf3e 100644 --- a/src/trezor/messages/DebugLinkMemoryRead.py +++ b/src/trezor/messages/DebugLinkMemoryRead.py @@ -8,3 +8,13 @@ class DebugLinkMemoryRead(p.MessageType): 2: ('length', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 110 + + def __init__( + self, + address: int = None, + length: int = None, + **kwargs, + ): + self.address = address + self.length = length + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkMemoryWrite.py b/src/trezor/messages/DebugLinkMemoryWrite.py index abd9478c1f..a6a83bc78f 100644 --- a/src/trezor/messages/DebugLinkMemoryWrite.py +++ b/src/trezor/messages/DebugLinkMemoryWrite.py @@ -9,3 +9,15 @@ class DebugLinkMemoryWrite(p.MessageType): 3: ('flash', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 112 + + def __init__( + self, + address: int = None, + memory: bytes = None, + flash: bool = None, + **kwargs, + ): + self.address = address + self.memory = memory + self.flash = flash + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkState.py b/src/trezor/messages/DebugLinkState.py index 4b8e102b1f..e6a22defe2 100644 --- a/src/trezor/messages/DebugLinkState.py +++ b/src/trezor/messages/DebugLinkState.py @@ -17,3 +17,29 @@ class DebugLinkState(p.MessageType): 10: ('recovery_word_pos', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 102 + + def __init__( + self, + layout: bytes = None, + pin: str = None, + matrix: str = None, + mnemonic: str = None, + node: HDNodeType = None, + passphrase_protection: bool = None, + reset_word: str = None, + reset_entropy: bytes = None, + recovery_fake_word: str = None, + recovery_word_pos: int = None, + **kwargs, + ): + self.layout = layout + self.pin = pin + self.matrix = matrix + self.mnemonic = mnemonic + self.node = node + self.passphrase_protection = passphrase_protection + self.reset_word = reset_word + self.reset_entropy = reset_entropy + self.recovery_fake_word = recovery_fake_word + self.recovery_word_pos = recovery_word_pos + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DebugLinkStop.py b/src/trezor/messages/DebugLinkStop.py index 5025da0b96..c643fba0d6 100644 --- a/src/trezor/messages/DebugLinkStop.py +++ b/src/trezor/messages/DebugLinkStop.py @@ -4,3 +4,9 @@ import protobuf as p class DebugLinkStop(p.MessageType): MESSAGE_WIRE_TYPE = 103 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DecryptMessage.py b/src/trezor/messages/DecryptMessage.py index afe50b9776..3636ab9878 100644 --- a/src/trezor/messages/DecryptMessage.py +++ b/src/trezor/messages/DecryptMessage.py @@ -10,3 +10,17 @@ class DecryptMessage(p.MessageType): 4: ('hmac', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 51 + + def __init__( + self, + address_n: list = [], + nonce: bytes = None, + message: bytes = None, + hmac: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.nonce = nonce + self.message = message + self.hmac = hmac + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/DecryptedMessage.py b/src/trezor/messages/DecryptedMessage.py index c294c5998c..1a4a6039d6 100644 --- a/src/trezor/messages/DecryptedMessage.py +++ b/src/trezor/messages/DecryptedMessage.py @@ -8,3 +8,13 @@ class DecryptedMessage(p.MessageType): 2: ('address', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 52 + + def __init__( + self, + message: bytes = None, + address: str = None, + **kwargs, + ): + self.message = message + self.address = address + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ECDHSessionKey.py b/src/trezor/messages/ECDHSessionKey.py index df84b553f4..6c16d7b1a3 100644 --- a/src/trezor/messages/ECDHSessionKey.py +++ b/src/trezor/messages/ECDHSessionKey.py @@ -7,3 +7,11 @@ class ECDHSessionKey(p.MessageType): 1: ('session_key', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 62 + + def __init__( + self, + session_key: bytes = None, + **kwargs, + ): + self.session_key = session_key + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EncryptMessage.py b/src/trezor/messages/EncryptMessage.py index b0177f5c33..dcd51e91f8 100644 --- a/src/trezor/messages/EncryptMessage.py +++ b/src/trezor/messages/EncryptMessage.py @@ -11,3 +11,19 @@ class EncryptMessage(p.MessageType): 5: ('coin_name', p.UnicodeType, 0), # default='Bitcoin' } MESSAGE_WIRE_TYPE = 49 + + def __init__( + self, + pubkey: bytes = None, + message: bytes = None, + display_only: bool = None, + address_n: list = [], + coin_name: str = None, + **kwargs, + ): + self.pubkey = pubkey + self.message = message + self.display_only = display_only + self.address_n = address_n + self.coin_name = coin_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EncryptedMessage.py b/src/trezor/messages/EncryptedMessage.py index d98cc1d0f1..1126176b82 100644 --- a/src/trezor/messages/EncryptedMessage.py +++ b/src/trezor/messages/EncryptedMessage.py @@ -9,3 +9,15 @@ class EncryptedMessage(p.MessageType): 3: ('hmac', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 50 + + def __init__( + self, + nonce: bytes = None, + message: bytes = None, + hmac: bytes = None, + **kwargs, + ): + self.nonce = nonce + self.message = message + self.hmac = hmac + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Entropy.py b/src/trezor/messages/Entropy.py index 21d97384f9..801ba0bdf3 100644 --- a/src/trezor/messages/Entropy.py +++ b/src/trezor/messages/Entropy.py @@ -7,3 +7,11 @@ class Entropy(p.MessageType): 1: ('entropy', p.BytesType, 0), # required } MESSAGE_WIRE_TYPE = 10 + + def __init__( + self, + entropy: bytes = None, + **kwargs, + ): + self.entropy = entropy + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EntropyAck.py b/src/trezor/messages/EntropyAck.py index 594e102cda..bcfe63b1ff 100644 --- a/src/trezor/messages/EntropyAck.py +++ b/src/trezor/messages/EntropyAck.py @@ -7,3 +7,11 @@ class EntropyAck(p.MessageType): 1: ('entropy', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 36 + + def __init__( + self, + entropy: bytes = None, + **kwargs, + ): + self.entropy = entropy + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EntropyRequest.py b/src/trezor/messages/EntropyRequest.py index fa732dacc6..aefdf109fe 100644 --- a/src/trezor/messages/EntropyRequest.py +++ b/src/trezor/messages/EntropyRequest.py @@ -4,3 +4,9 @@ import protobuf as p class EntropyRequest(p.MessageType): MESSAGE_WIRE_TYPE = 35 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EstimateTxSize.py b/src/trezor/messages/EstimateTxSize.py index 9a9342b34d..db0bf8c4db 100644 --- a/src/trezor/messages/EstimateTxSize.py +++ b/src/trezor/messages/EstimateTxSize.py @@ -9,3 +9,15 @@ class EstimateTxSize(p.MessageType): 3: ('coin_name', p.UnicodeType, 0), # default='Bitcoin' } MESSAGE_WIRE_TYPE = 43 + + def __init__( + self, + outputs_count: int = None, + inputs_count: int = None, + coin_name: str = None, + **kwargs, + ): + self.outputs_count = outputs_count + self.inputs_count = inputs_count + self.coin_name = coin_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumAddress.py b/src/trezor/messages/EthereumAddress.py index 69da373e3d..e48518f356 100644 --- a/src/trezor/messages/EthereumAddress.py +++ b/src/trezor/messages/EthereumAddress.py @@ -7,3 +7,11 @@ class EthereumAddress(p.MessageType): 1: ('address', p.BytesType, 0), # required } MESSAGE_WIRE_TYPE = 57 + + def __init__( + self, + address: bytes = None, + **kwargs, + ): + self.address = address + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumGetAddress.py b/src/trezor/messages/EthereumGetAddress.py index b4b594f171..ce4030f809 100644 --- a/src/trezor/messages/EthereumGetAddress.py +++ b/src/trezor/messages/EthereumGetAddress.py @@ -8,3 +8,13 @@ class EthereumGetAddress(p.MessageType): 2: ('show_display', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 56 + + def __init__( + self, + address_n: list = [], + show_display: bool = None, + **kwargs, + ): + self.address_n = address_n + self.show_display = show_display + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumMessageSignature.py b/src/trezor/messages/EthereumMessageSignature.py index 42c95f98dc..49f454a2a3 100644 --- a/src/trezor/messages/EthereumMessageSignature.py +++ b/src/trezor/messages/EthereumMessageSignature.py @@ -8,3 +8,13 @@ class EthereumMessageSignature(p.MessageType): 2: ('signature', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 66 + + def __init__( + self, + address: bytes = None, + signature: bytes = None, + **kwargs, + ): + self.address = address + self.signature = signature + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumSignMessage.py b/src/trezor/messages/EthereumSignMessage.py index 6bd211c034..fe5eecf66e 100644 --- a/src/trezor/messages/EthereumSignMessage.py +++ b/src/trezor/messages/EthereumSignMessage.py @@ -8,3 +8,13 @@ class EthereumSignMessage(p.MessageType): 2: ('message', p.BytesType, 0), # required } MESSAGE_WIRE_TYPE = 64 + + def __init__( + self, + address_n: list = [], + message: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.message = message + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumSignTx.py b/src/trezor/messages/EthereumSignTx.py index ddac9034f7..97377e6b09 100644 --- a/src/trezor/messages/EthereumSignTx.py +++ b/src/trezor/messages/EthereumSignTx.py @@ -15,3 +15,27 @@ class EthereumSignTx(p.MessageType): 9: ('chain_id', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 58 + + def __init__( + self, + address_n: list = [], + nonce: bytes = None, + gas_price: bytes = None, + gas_limit: bytes = None, + to: bytes = None, + value: bytes = None, + data_initial_chunk: bytes = None, + data_length: int = None, + chain_id: int = None, + **kwargs, + ): + self.address_n = address_n + self.nonce = nonce + self.gas_price = gas_price + self.gas_limit = gas_limit + self.to = to + self.value = value + self.data_initial_chunk = data_initial_chunk + self.data_length = data_length + self.chain_id = chain_id + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumTxAck.py b/src/trezor/messages/EthereumTxAck.py index a0226364e4..c874093adc 100644 --- a/src/trezor/messages/EthereumTxAck.py +++ b/src/trezor/messages/EthereumTxAck.py @@ -7,3 +7,11 @@ class EthereumTxAck(p.MessageType): 1: ('data_chunk', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 60 + + def __init__( + self, + data_chunk: bytes = None, + **kwargs, + ): + self.data_chunk = data_chunk + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumTxRequest.py b/src/trezor/messages/EthereumTxRequest.py index a837c8df89..840613ed38 100644 --- a/src/trezor/messages/EthereumTxRequest.py +++ b/src/trezor/messages/EthereumTxRequest.py @@ -10,3 +10,17 @@ class EthereumTxRequest(p.MessageType): 4: ('signature_s', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 59 + + def __init__( + self, + data_length: int = None, + signature_v: int = None, + signature_r: bytes = None, + signature_s: bytes = None, + **kwargs, + ): + self.data_length = data_length + self.signature_v = signature_v + self.signature_r = signature_r + self.signature_s = signature_s + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/EthereumVerifyMessage.py b/src/trezor/messages/EthereumVerifyMessage.py index 58bb818470..692f40faad 100644 --- a/src/trezor/messages/EthereumVerifyMessage.py +++ b/src/trezor/messages/EthereumVerifyMessage.py @@ -9,3 +9,15 @@ class EthereumVerifyMessage(p.MessageType): 3: ('message', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 65 + + def __init__( + self, + address: bytes = None, + signature: bytes = None, + message: bytes = None, + **kwargs, + ): + self.address = address + self.signature = signature + self.message = message + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Failure.py b/src/trezor/messages/Failure.py index 50d86f5901..f0e91c6740 100644 --- a/src/trezor/messages/Failure.py +++ b/src/trezor/messages/Failure.py @@ -8,3 +8,13 @@ class Failure(p.MessageType): 2: ('message', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 3 + + def __init__( + self, + code: int = None, + message: str = None, + **kwargs, + ): + self.code = code + self.message = message + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Features.py b/src/trezor/messages/Features.py index b6c5014a71..598931fd71 100644 --- a/src/trezor/messages/Features.py +++ b/src/trezor/messages/Features.py @@ -25,12 +25,51 @@ class Features(p.MessageType): 18: ('firmware_present', p.BoolType, 0), 19: ('needs_backup', p.BoolType, 0), 20: ('flags', p.UVarintType, 0), - 21: ('model', p.UnicodeType, 0), - 22: ('fw_major', p.UVarintType, 0), - 23: ('fw_minor', p.UVarintType, 0), - 24: ('fw_patch', p.UVarintType, 0), - 25: ('fw_vendor', p.UnicodeType, 0), - 26: ('fw_vendor_keys', p.BytesType, 0), - 27: ('state', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 17 + + def __init__( + self, + vendor: str = None, + major_version: int = None, + minor_version: int = None, + patch_version: int = None, + bootloader_mode: bool = None, + device_id: str = None, + pin_protection: bool = None, + passphrase_protection: bool = None, + language: str = None, + label: str = None, + coins: list = [], + initialized: bool = None, + revision: bytes = None, + bootloader_hash: bytes = None, + imported: bool = None, + pin_cached: bool = None, + passphrase_cached: bool = None, + firmware_present: bool = None, + needs_backup: bool = None, + flags: int = None, + **kwargs, + ): + self.vendor = vendor + self.major_version = major_version + self.minor_version = minor_version + self.patch_version = patch_version + self.bootloader_mode = bootloader_mode + self.device_id = device_id + self.pin_protection = pin_protection + self.passphrase_protection = passphrase_protection + self.language = language + self.label = label + self.coins = coins + self.initialized = initialized + self.revision = revision + self.bootloader_hash = bootloader_hash + self.imported = imported + self.pin_cached = pin_cached + self.passphrase_cached = passphrase_cached + self.firmware_present = firmware_present + self.needs_backup = needs_backup + self.flags = flags + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/FirmwareErase.py b/src/trezor/messages/FirmwareErase.py index 7e59cff16b..06ac715722 100644 --- a/src/trezor/messages/FirmwareErase.py +++ b/src/trezor/messages/FirmwareErase.py @@ -7,3 +7,11 @@ class FirmwareErase(p.MessageType): 1: ('length', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 6 + + def __init__( + self, + length: int = None, + **kwargs, + ): + self.length = length + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/FirmwareRequest.py b/src/trezor/messages/FirmwareRequest.py index a91f9a8db1..77dca3dfc3 100644 --- a/src/trezor/messages/FirmwareRequest.py +++ b/src/trezor/messages/FirmwareRequest.py @@ -8,3 +8,13 @@ class FirmwareRequest(p.MessageType): 2: ('length', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 8 + + def __init__( + self, + offset: int = None, + length: int = None, + **kwargs, + ): + self.offset = offset + self.length = length + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/FirmwareUpload.py b/src/trezor/messages/FirmwareUpload.py index 489e45d1e1..2d66ea5165 100644 --- a/src/trezor/messages/FirmwareUpload.py +++ b/src/trezor/messages/FirmwareUpload.py @@ -8,3 +8,13 @@ class FirmwareUpload(p.MessageType): 2: ('hash', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 7 + + def __init__( + self, + payload: bytes = None, + hash: bytes = None, + **kwargs, + ): + self.payload = payload + self.hash = hash + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/GetAddress.py b/src/trezor/messages/GetAddress.py index ad08d43145..528b46811b 100644 --- a/src/trezor/messages/GetAddress.py +++ b/src/trezor/messages/GetAddress.py @@ -12,3 +12,19 @@ class GetAddress(p.MessageType): 5: ('script_type', p.UVarintType, 0), # default=0 } MESSAGE_WIRE_TYPE = 29 + + def __init__( + self, + address_n: list = [], + coin_name: str = None, + show_display: bool = None, + multisig: MultisigRedeemScriptType = None, + script_type: int = None, + **kwargs, + ): + self.address_n = address_n + self.coin_name = coin_name + self.show_display = show_display + self.multisig = multisig + self.script_type = script_type + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/GetECDHSessionKey.py b/src/trezor/messages/GetECDHSessionKey.py index d6cf310a24..23ac6cc916 100644 --- a/src/trezor/messages/GetECDHSessionKey.py +++ b/src/trezor/messages/GetECDHSessionKey.py @@ -10,3 +10,15 @@ class GetECDHSessionKey(p.MessageType): 3: ('ecdsa_curve_name', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 61 + + def __init__( + self, + identity: IdentityType = None, + peer_public_key: bytes = None, + ecdsa_curve_name: str = None, + **kwargs, + ): + self.identity = identity + self.peer_public_key = peer_public_key + self.ecdsa_curve_name = ecdsa_curve_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/GetEntropy.py b/src/trezor/messages/GetEntropy.py index ded2c81d15..824499588e 100644 --- a/src/trezor/messages/GetEntropy.py +++ b/src/trezor/messages/GetEntropy.py @@ -7,3 +7,11 @@ class GetEntropy(p.MessageType): 1: ('size', p.UVarintType, 0), # required } MESSAGE_WIRE_TYPE = 9 + + def __init__( + self, + size: int = None, + **kwargs, + ): + self.size = size + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/GetFeatures.py b/src/trezor/messages/GetFeatures.py index 85862a6217..44358c2561 100644 --- a/src/trezor/messages/GetFeatures.py +++ b/src/trezor/messages/GetFeatures.py @@ -4,3 +4,9 @@ import protobuf as p class GetFeatures(p.MessageType): MESSAGE_WIRE_TYPE = 55 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/GetPublicKey.py b/src/trezor/messages/GetPublicKey.py index b16ac25a34..a90021afb5 100644 --- a/src/trezor/messages/GetPublicKey.py +++ b/src/trezor/messages/GetPublicKey.py @@ -10,3 +10,17 @@ class GetPublicKey(p.MessageType): 4: ('coin_name', p.UnicodeType, 0), # default='Bitcoin' } MESSAGE_WIRE_TYPE = 11 + + def __init__( + self, + address_n: list = [], + ecdsa_curve_name: str = None, + show_display: bool = None, + coin_name: str = None, + **kwargs, + ): + self.address_n = address_n + self.ecdsa_curve_name = ecdsa_curve_name + self.show_display = show_display + self.coin_name = coin_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/HDNodePathType.py b/src/trezor/messages/HDNodePathType.py index f3284a1591..49971d5ef9 100644 --- a/src/trezor/messages/HDNodePathType.py +++ b/src/trezor/messages/HDNodePathType.py @@ -8,3 +8,13 @@ class HDNodePathType(p.MessageType): 1: ('node', HDNodeType, 0), # required 2: ('address_n', p.UVarintType, p.FLAG_REPEATED), } + + def __init__( + self, + node: HDNodeType = None, + address_n: list = [], + **kwargs, + ): + self.node = node + self.address_n = address_n + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/HDNodeType.py b/src/trezor/messages/HDNodeType.py index a1995795dc..7786d94f57 100644 --- a/src/trezor/messages/HDNodeType.py +++ b/src/trezor/messages/HDNodeType.py @@ -11,3 +11,21 @@ class HDNodeType(p.MessageType): 5: ('private_key', p.BytesType, 0), 6: ('public_key', p.BytesType, 0), } + + def __init__( + self, + depth: int = None, + fingerprint: int = None, + child_num: int = None, + chain_code: bytes = None, + private_key: bytes = None, + public_key: bytes = None, + **kwargs, + ): + self.depth = depth + self.fingerprint = fingerprint + self.child_num = child_num + self.chain_code = chain_code + self.private_key = private_key + self.public_key = public_key + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/IdentityType.py b/src/trezor/messages/IdentityType.py index 8cf6505796..6aa532a0ea 100644 --- a/src/trezor/messages/IdentityType.py +++ b/src/trezor/messages/IdentityType.py @@ -11,3 +11,21 @@ class IdentityType(p.MessageType): 5: ('path', p.UnicodeType, 0), 6: ('index', p.UVarintType, 0), # default=0 } + + def __init__( + self, + proto: str = None, + user: str = None, + host: str = None, + port: str = None, + path: str = None, + index: int = None, + **kwargs, + ): + self.proto = proto + self.user = user + self.host = host + self.port = port + self.path = path + self.index = index + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Initialize.py b/src/trezor/messages/Initialize.py index 3aaa845672..9562713e8e 100644 --- a/src/trezor/messages/Initialize.py +++ b/src/trezor/messages/Initialize.py @@ -3,7 +3,10 @@ import protobuf as p class Initialize(p.MessageType): - FIELDS = { - 1: ('state', p.BytesType, 0), - } MESSAGE_WIRE_TYPE = 0 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/LoadDevice.py b/src/trezor/messages/LoadDevice.py index 262488414e..d97a1722b4 100644 --- a/src/trezor/messages/LoadDevice.py +++ b/src/trezor/messages/LoadDevice.py @@ -15,3 +15,25 @@ class LoadDevice(p.MessageType): 8: ('u2f_counter', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 13 + + def __init__( + self, + mnemonic: str = None, + node: HDNodeType = None, + pin: str = None, + passphrase_protection: bool = None, + language: str = None, + label: str = None, + skip_checksum: bool = None, + u2f_counter: int = None, + **kwargs, + ): + self.mnemonic = mnemonic + self.node = node + self.pin = pin + self.passphrase_protection = passphrase_protection + self.language = language + self.label = label + self.skip_checksum = skip_checksum + self.u2f_counter = u2f_counter + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/MessageSignature.py b/src/trezor/messages/MessageSignature.py index 337c628420..d5a62a2885 100644 --- a/src/trezor/messages/MessageSignature.py +++ b/src/trezor/messages/MessageSignature.py @@ -8,3 +8,13 @@ class MessageSignature(p.MessageType): 2: ('signature', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 40 + + def __init__( + self, + address: str = None, + signature: bytes = None, + **kwargs, + ): + self.address = address + self.signature = signature + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/MessageType.py b/src/trezor/messages/MessageType.py index ca870dd54d..93c2a0125d 100644 --- a/src/trezor/messages/MessageType.py +++ b/src/trezor/messages/MessageType.py @@ -73,8 +73,6 @@ CosiCommit = const(71) CosiCommitment = const(72) CosiSign = const(73) CosiSignature = const(74) -NEMDecryptMessage = const(75) -NEMDecryptedMessage = const(76) DebugLinkDecision = const(100) DebugLinkGetState = const(101) DebugLinkState = const(102) diff --git a/src/trezor/messages/MultisigRedeemScriptType.py b/src/trezor/messages/MultisigRedeemScriptType.py index 6f4e05b0b8..eab03693a8 100644 --- a/src/trezor/messages/MultisigRedeemScriptType.py +++ b/src/trezor/messages/MultisigRedeemScriptType.py @@ -9,3 +9,15 @@ class MultisigRedeemScriptType(p.MessageType): 2: ('signatures', p.BytesType, p.FLAG_REPEATED), 3: ('m', p.UVarintType, 0), } + + def __init__( + self, + pubkeys: list = [], + signatures: list = [], + m: int = None, + **kwargs, + ): + self.pubkeys = pubkeys + self.signatures = signatures + self.m = m + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMAddress.py b/src/trezor/messages/NEMAddress.py index 72a1f00780..79a81004b3 100644 --- a/src/trezor/messages/NEMAddress.py +++ b/src/trezor/messages/NEMAddress.py @@ -7,3 +7,11 @@ class NEMAddress(p.MessageType): 1: ('address', p.UnicodeType, 0), # required } MESSAGE_WIRE_TYPE = 68 + + def __init__( + self, + address: str = None, + **kwargs, + ): + self.address = address + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMAggregateModification.py b/src/trezor/messages/NEMAggregateModification.py index 53347f9209..6aaff97f34 100644 --- a/src/trezor/messages/NEMAggregateModification.py +++ b/src/trezor/messages/NEMAggregateModification.py @@ -8,3 +8,13 @@ class NEMAggregateModification(p.MessageType): 1: ('modifications', NEMCosignatoryModification, p.FLAG_REPEATED), 2: ('relative_change', p.Sint32Type, 0), } + + def __init__( + self, + modifications: list = [], + relative_change: int = None, + **kwargs, + ): + self.modifications = modifications + self.relative_change = relative_change + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMCosignatoryModification.py b/src/trezor/messages/NEMCosignatoryModification.py index e54a26ec21..3c2f18b73e 100644 --- a/src/trezor/messages/NEMCosignatoryModification.py +++ b/src/trezor/messages/NEMCosignatoryModification.py @@ -7,3 +7,13 @@ class NEMCosignatoryModification(p.MessageType): 1: ('type', p.UVarintType, 0), 2: ('public_key', p.BytesType, 0), } + + def __init__( + self, + type: int = None, + public_key: bytes = None, + **kwargs, + ): + self.type = type + self.public_key = public_key + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMDecryptMessage.py b/src/trezor/messages/NEMDecryptMessage.py deleted file mode 100644 index 447b337565..0000000000 --- a/src/trezor/messages/NEMDecryptMessage.py +++ /dev/null @@ -1,12 +0,0 @@ -# Automatically generated by pb2py -import protobuf as p - - -class NEMDecryptMessage(p.MessageType): - FIELDS = { - 1: ('address_n', p.UVarintType, p.FLAG_REPEATED), - 2: ('network', p.UVarintType, 0), - 3: ('public_key', p.BytesType, 0), - 4: ('payload', p.BytesType, 0), - } - MESSAGE_WIRE_TYPE = 75 diff --git a/src/trezor/messages/NEMDecryptedMessage.py b/src/trezor/messages/NEMDecryptedMessage.py deleted file mode 100644 index 1ee4e3e084..0000000000 --- a/src/trezor/messages/NEMDecryptedMessage.py +++ /dev/null @@ -1,9 +0,0 @@ -# Automatically generated by pb2py -import protobuf as p - - -class NEMDecryptedMessage(p.MessageType): - FIELDS = { - 1: ('payload', p.BytesType, 0), - } - MESSAGE_WIRE_TYPE = 76 diff --git a/src/trezor/messages/NEMGetAddress.py b/src/trezor/messages/NEMGetAddress.py index 86c7931af0..612423a692 100644 --- a/src/trezor/messages/NEMGetAddress.py +++ b/src/trezor/messages/NEMGetAddress.py @@ -9,3 +9,15 @@ class NEMGetAddress(p.MessageType): 3: ('show_display', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 67 + + def __init__( + self, + address_n: list = [], + network: int = None, + show_display: bool = None, + **kwargs, + ): + self.address_n = address_n + self.network = network + self.show_display = show_display + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMImportanceTransfer.py b/src/trezor/messages/NEMImportanceTransfer.py index 85455abfc0..31d92e9925 100644 --- a/src/trezor/messages/NEMImportanceTransfer.py +++ b/src/trezor/messages/NEMImportanceTransfer.py @@ -7,3 +7,13 @@ class NEMImportanceTransfer(p.MessageType): 1: ('mode', p.UVarintType, 0), 2: ('public_key', p.BytesType, 0), } + + def __init__( + self, + mode: int = None, + public_key: bytes = None, + **kwargs, + ): + self.mode = mode + self.public_key = public_key + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMMosaic.py b/src/trezor/messages/NEMMosaic.py index b6f7b46ce2..5e793d9dfd 100644 --- a/src/trezor/messages/NEMMosaic.py +++ b/src/trezor/messages/NEMMosaic.py @@ -8,3 +8,15 @@ class NEMMosaic(p.MessageType): 2: ('mosaic', p.UnicodeType, 0), 3: ('quantity', p.UVarintType, 0), } + + def __init__( + self, + namespace: str = None, + mosaic: str = None, + quantity: int = None, + **kwargs, + ): + self.namespace = namespace + self.mosaic = mosaic + self.quantity = quantity + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMMosaicCreation.py b/src/trezor/messages/NEMMosaicCreation.py index edabded737..79e4cecaae 100644 --- a/src/trezor/messages/NEMMosaicCreation.py +++ b/src/trezor/messages/NEMMosaicCreation.py @@ -9,3 +9,15 @@ class NEMMosaicCreation(p.MessageType): 2: ('sink', p.UnicodeType, 0), 3: ('fee', p.UVarintType, 0), } + + def __init__( + self, + definition: NEMMosaicDefinition = None, + sink: str = None, + fee: int = None, + **kwargs, + ): + self.definition = definition + self.sink = sink + self.fee = fee + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMMosaicDefinition.py b/src/trezor/messages/NEMMosaicDefinition.py index 934dc71a39..a02499e52c 100644 --- a/src/trezor/messages/NEMMosaicDefinition.py +++ b/src/trezor/messages/NEMMosaicDefinition.py @@ -20,3 +20,39 @@ class NEMMosaicDefinition(p.MessageType): 14: ('description', p.UnicodeType, 0), 15: ('networks', p.UVarintType, p.FLAG_REPEATED), } + + def __init__( + self, + name: str = None, + ticker: str = None, + namespace: str = None, + mosaic: str = None, + divisibility: int = None, + levy: int = None, + fee: int = None, + levy_address: str = None, + levy_namespace: str = None, + levy_mosaic: str = None, + supply: int = None, + mutable_supply: bool = None, + transferable: bool = None, + description: str = None, + networks: list = [], + **kwargs, + ): + self.name = name + self.ticker = ticker + self.namespace = namespace + self.mosaic = mosaic + self.divisibility = divisibility + self.levy = levy + self.fee = fee + self.levy_address = levy_address + self.levy_namespace = levy_namespace + self.levy_mosaic = levy_mosaic + self.supply = supply + self.mutable_supply = mutable_supply + self.transferable = transferable + self.description = description + self.networks = networks + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMMosaicSupplyChange.py b/src/trezor/messages/NEMMosaicSupplyChange.py index 1546bf66d4..e228161173 100644 --- a/src/trezor/messages/NEMMosaicSupplyChange.py +++ b/src/trezor/messages/NEMMosaicSupplyChange.py @@ -9,3 +9,17 @@ class NEMMosaicSupplyChange(p.MessageType): 3: ('type', p.UVarintType, 0), 4: ('delta', p.UVarintType, 0), } + + def __init__( + self, + namespace: str = None, + mosaic: str = None, + type: int = None, + delta: int = None, + **kwargs, + ): + self.namespace = namespace + self.mosaic = mosaic + self.type = type + self.delta = delta + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMProvisionNamespace.py b/src/trezor/messages/NEMProvisionNamespace.py index c8c7a1b043..d18bb60b30 100644 --- a/src/trezor/messages/NEMProvisionNamespace.py +++ b/src/trezor/messages/NEMProvisionNamespace.py @@ -9,3 +9,17 @@ class NEMProvisionNamespace(p.MessageType): 3: ('sink', p.UnicodeType, 0), 4: ('fee', p.UVarintType, 0), } + + def __init__( + self, + namespace: str = None, + parent: str = None, + sink: str = None, + fee: int = None, + **kwargs, + ): + self.namespace = namespace + self.parent = parent + self.sink = sink + self.fee = fee + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMSignTx.py b/src/trezor/messages/NEMSignTx.py index 0164a7a6f1..23e833e186 100644 --- a/src/trezor/messages/NEMSignTx.py +++ b/src/trezor/messages/NEMSignTx.py @@ -22,3 +22,27 @@ class NEMSignTx(p.MessageType): 9: ('importance_transfer', NEMImportanceTransfer, 0), } MESSAGE_WIRE_TYPE = 69 + + def __init__( + self, + transaction: NEMTransactionCommon = None, + multisig: NEMTransactionCommon = None, + transfer: NEMTransfer = None, + cosigning: bool = None, + provision_namespace: NEMProvisionNamespace = None, + mosaic_creation: NEMMosaicCreation = None, + supply_change: NEMMosaicSupplyChange = None, + aggregate_modification: NEMAggregateModification = None, + importance_transfer: NEMImportanceTransfer = None, + **kwargs, + ): + self.transaction = transaction + self.multisig = multisig + self.transfer = transfer + self.cosigning = cosigning + self.provision_namespace = provision_namespace + self.mosaic_creation = mosaic_creation + self.supply_change = supply_change + self.aggregate_modification = aggregate_modification + self.importance_transfer = importance_transfer + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMSignedTx.py b/src/trezor/messages/NEMSignedTx.py index 7423dd756f..489b4657b6 100644 --- a/src/trezor/messages/NEMSignedTx.py +++ b/src/trezor/messages/NEMSignedTx.py @@ -8,3 +8,13 @@ class NEMSignedTx(p.MessageType): 2: ('signature', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 70 + + def __init__( + self, + data: bytes = None, + signature: bytes = None, + **kwargs, + ): + self.data = data + self.signature = signature + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMTransactionCommon.py b/src/trezor/messages/NEMTransactionCommon.py index fb3217a87d..1d6622adfe 100644 --- a/src/trezor/messages/NEMTransactionCommon.py +++ b/src/trezor/messages/NEMTransactionCommon.py @@ -11,3 +11,21 @@ class NEMTransactionCommon(p.MessageType): 5: ('deadline', p.UVarintType, 0), 6: ('signer', p.BytesType, 0), } + + def __init__( + self, + address_n: list = [], + network: int = None, + timestamp: int = None, + fee: int = None, + deadline: int = None, + signer: bytes = None, + **kwargs, + ): + self.address_n = address_n + self.network = network + self.timestamp = timestamp + self.fee = fee + self.deadline = deadline + self.signer = signer + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/NEMTransfer.py b/src/trezor/messages/NEMTransfer.py index eba2b9dba3..d019f9aecb 100644 --- a/src/trezor/messages/NEMTransfer.py +++ b/src/trezor/messages/NEMTransfer.py @@ -11,3 +11,19 @@ class NEMTransfer(p.MessageType): 4: ('public_key', p.BytesType, 0), 5: ('mosaics', NEMMosaic, p.FLAG_REPEATED), } + + def __init__( + self, + recipient: str = None, + amount: int = None, + payload: bytes = None, + public_key: bytes = None, + mosaics: list = [], + **kwargs, + ): + self.recipient = recipient + self.amount = amount + self.payload = payload + self.public_key = public_key + self.mosaics = mosaics + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/PassphraseAck.py b/src/trezor/messages/PassphraseAck.py index 3e865a0463..d8adf5f20c 100644 --- a/src/trezor/messages/PassphraseAck.py +++ b/src/trezor/messages/PassphraseAck.py @@ -5,6 +5,13 @@ import protobuf as p class PassphraseAck(p.MessageType): FIELDS = { 1: ('passphrase', p.UnicodeType, 0), # required - 2: ('state', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 42 + + def __init__( + self, + passphrase: str = None, + **kwargs, + ): + self.passphrase = passphrase + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/PassphraseRequest.py b/src/trezor/messages/PassphraseRequest.py index 63e4b52e9a..7e89a4676a 100644 --- a/src/trezor/messages/PassphraseRequest.py +++ b/src/trezor/messages/PassphraseRequest.py @@ -3,7 +3,10 @@ import protobuf as p class PassphraseRequest(p.MessageType): - FIELDS = { - 1: ('on_device', p.BoolType, 0), - } MESSAGE_WIRE_TYPE = 41 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/PinMatrixAck.py b/src/trezor/messages/PinMatrixAck.py index 294ed5a1bf..29043c53a3 100644 --- a/src/trezor/messages/PinMatrixAck.py +++ b/src/trezor/messages/PinMatrixAck.py @@ -7,3 +7,11 @@ class PinMatrixAck(p.MessageType): 1: ('pin', p.UnicodeType, 0), # required } MESSAGE_WIRE_TYPE = 19 + + def __init__( + self, + pin: str = None, + **kwargs, + ): + self.pin = pin + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/PinMatrixRequest.py b/src/trezor/messages/PinMatrixRequest.py index 9f9c68b27a..4775cb87f9 100644 --- a/src/trezor/messages/PinMatrixRequest.py +++ b/src/trezor/messages/PinMatrixRequest.py @@ -7,3 +7,11 @@ class PinMatrixRequest(p.MessageType): 1: ('type', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 18 + + def __init__( + self, + type: int = None, + **kwargs, + ): + self.type = type + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Ping.py b/src/trezor/messages/Ping.py index ab40301d33..51a594a1ea 100644 --- a/src/trezor/messages/Ping.py +++ b/src/trezor/messages/Ping.py @@ -10,3 +10,17 @@ class Ping(p.MessageType): 4: ('passphrase_protection', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 1 + + def __init__( + self, + message: str = None, + button_protection: bool = None, + pin_protection: bool = None, + passphrase_protection: bool = None, + **kwargs, + ): + self.message = message + self.button_protection = button_protection + self.pin_protection = pin_protection + self.passphrase_protection = passphrase_protection + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/PublicKey.py b/src/trezor/messages/PublicKey.py index ebcc7d42e3..19842f22a2 100644 --- a/src/trezor/messages/PublicKey.py +++ b/src/trezor/messages/PublicKey.py @@ -9,3 +9,13 @@ class PublicKey(p.MessageType): 2: ('xpub', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 12 + + def __init__( + self, + node: HDNodeType = None, + xpub: str = None, + **kwargs, + ): + self.node = node + self.xpub = xpub + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/RecoveryDevice.py b/src/trezor/messages/RecoveryDevice.py index d6c21aa58d..2dfb4dd512 100644 --- a/src/trezor/messages/RecoveryDevice.py +++ b/src/trezor/messages/RecoveryDevice.py @@ -15,3 +15,27 @@ class RecoveryDevice(p.MessageType): 10: ('dry_run', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 45 + + def __init__( + self, + word_count: int = None, + passphrase_protection: bool = None, + pin_protection: bool = None, + language: str = None, + label: str = None, + enforce_wordlist: bool = None, + type: int = None, + u2f_counter: int = None, + dry_run: bool = None, + **kwargs, + ): + self.word_count = word_count + self.passphrase_protection = passphrase_protection + self.pin_protection = pin_protection + self.language = language + self.label = label + self.enforce_wordlist = enforce_wordlist + self.type = type + self.u2f_counter = u2f_counter + self.dry_run = dry_run + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/ResetDevice.py b/src/trezor/messages/ResetDevice.py index 6ea628abd7..e5c8cf7db0 100644 --- a/src/trezor/messages/ResetDevice.py +++ b/src/trezor/messages/ResetDevice.py @@ -14,3 +14,25 @@ class ResetDevice(p.MessageType): 8: ('skip_backup', p.BoolType, 0), } MESSAGE_WIRE_TYPE = 14 + + def __init__( + self, + display_random: bool = None, + strength: int = None, + passphrase_protection: bool = None, + pin_protection: bool = None, + language: str = None, + label: str = None, + u2f_counter: int = None, + skip_backup: bool = None, + **kwargs, + ): + self.display_random = display_random + self.strength = strength + self.passphrase_protection = passphrase_protection + self.pin_protection = pin_protection + self.language = language + self.label = label + self.u2f_counter = u2f_counter + self.skip_backup = skip_backup + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SelfTest.py b/src/trezor/messages/SelfTest.py index fbd3623480..cc9d67f1a3 100644 --- a/src/trezor/messages/SelfTest.py +++ b/src/trezor/messages/SelfTest.py @@ -7,3 +7,11 @@ class SelfTest(p.MessageType): 1: ('payload', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 32 + + def __init__( + self, + payload: bytes = None, + **kwargs, + ): + self.payload = payload + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SetU2FCounter.py b/src/trezor/messages/SetU2FCounter.py index c2f487c66e..26a5c97d87 100644 --- a/src/trezor/messages/SetU2FCounter.py +++ b/src/trezor/messages/SetU2FCounter.py @@ -7,3 +7,11 @@ class SetU2FCounter(p.MessageType): 1: ('u2f_counter', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 63 + + def __init__( + self, + u2f_counter: int = None, + **kwargs, + ): + self.u2f_counter = u2f_counter + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SignIdentity.py b/src/trezor/messages/SignIdentity.py index dbd5fafedc..46b9be5750 100644 --- a/src/trezor/messages/SignIdentity.py +++ b/src/trezor/messages/SignIdentity.py @@ -11,3 +11,17 @@ class SignIdentity(p.MessageType): 4: ('ecdsa_curve_name', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 53 + + def __init__( + self, + identity: IdentityType = None, + challenge_hidden: bytes = None, + challenge_visual: str = None, + ecdsa_curve_name: str = None, + **kwargs, + ): + self.identity = identity + self.challenge_hidden = challenge_hidden + self.challenge_visual = challenge_visual + self.ecdsa_curve_name = ecdsa_curve_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SignMessage.py b/src/trezor/messages/SignMessage.py index 9f238882de..3856912587 100644 --- a/src/trezor/messages/SignMessage.py +++ b/src/trezor/messages/SignMessage.py @@ -10,3 +10,17 @@ class SignMessage(p.MessageType): 4: ('script_type', p.UVarintType, 0), # default=0 } MESSAGE_WIRE_TYPE = 38 + + def __init__( + self, + address_n: list = [], + message: bytes = None, + coin_name: str = None, + script_type: int = None, + **kwargs, + ): + self.address_n = address_n + self.message = message + self.coin_name = coin_name + self.script_type = script_type + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SignTx.py b/src/trezor/messages/SignTx.py index e66fe713ca..3ef8609efb 100644 --- a/src/trezor/messages/SignTx.py +++ b/src/trezor/messages/SignTx.py @@ -9,6 +9,21 @@ class SignTx(p.MessageType): 3: ('coin_name', p.UnicodeType, 0), # default='Bitcoin' 4: ('version', p.UVarintType, 0), # default=1 5: ('lock_time', p.UVarintType, 0), # default=0 - 6: ('decred_expiry', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 15 + + def __init__( + self, + outputs_count: int = None, + inputs_count: int = None, + coin_name: str = None, + version: int = None, + lock_time: int = None, + **kwargs, + ): + self.outputs_count = outputs_count + self.inputs_count = inputs_count + self.coin_name = coin_name + self.version = version + self.lock_time = lock_time + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SignedIdentity.py b/src/trezor/messages/SignedIdentity.py index 2c7e25c643..effc4d4490 100644 --- a/src/trezor/messages/SignedIdentity.py +++ b/src/trezor/messages/SignedIdentity.py @@ -9,3 +9,15 @@ class SignedIdentity(p.MessageType): 3: ('signature', p.BytesType, 0), } MESSAGE_WIRE_TYPE = 54 + + def __init__( + self, + address: str = None, + public_key: bytes = None, + signature: bytes = None, + **kwargs, + ): + self.address = address + self.public_key = public_key + self.signature = signature + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/SimpleSignTx.py b/src/trezor/messages/SimpleSignTx.py index 62c03c8b85..2bf7e6e3ff 100644 --- a/src/trezor/messages/SimpleSignTx.py +++ b/src/trezor/messages/SimpleSignTx.py @@ -15,3 +15,21 @@ class SimpleSignTx(p.MessageType): 6: ('lock_time', p.UVarintType, 0), # default=0 } MESSAGE_WIRE_TYPE = 16 + + def __init__( + self, + inputs: list = [], + outputs: list = [], + transactions: list = [], + coin_name: str = None, + version: int = None, + lock_time: int = None, + **kwargs, + ): + self.inputs = inputs + self.outputs = outputs + self.transactions = transactions + self.coin_name = coin_name + self.version = version + self.lock_time = lock_time + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/Success.py b/src/trezor/messages/Success.py index 9dd722ca00..b81b1f6da0 100644 --- a/src/trezor/messages/Success.py +++ b/src/trezor/messages/Success.py @@ -7,3 +7,11 @@ class Success(p.MessageType): 1: ('message', p.UnicodeType, 0), } MESSAGE_WIRE_TYPE = 2 + + def __init__( + self, + message: str = None, + **kwargs, + ): + self.message = message + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TransactionType.py b/src/trezor/messages/TransactionType.py index 5167c195ae..2a9f398cbf 100644 --- a/src/trezor/messages/TransactionType.py +++ b/src/trezor/messages/TransactionType.py @@ -16,5 +16,28 @@ class TransactionType(p.MessageType): 7: ('outputs_cnt', p.UVarintType, 0), 8: ('extra_data', p.BytesType, 0), 9: ('extra_data_len', p.UVarintType, 0), - 10: ('decred_expiry', p.UVarintType, 0), } + + def __init__( + self, + version: int = None, + inputs: list = [], + bin_outputs: list = [], + lock_time: int = None, + outputs: list = [], + inputs_cnt: int = None, + outputs_cnt: int = None, + extra_data: bytes = None, + extra_data_len: int = None, + **kwargs, + ): + self.version = version + self.inputs = inputs + self.bin_outputs = bin_outputs + self.lock_time = lock_time + self.outputs = outputs + self.inputs_cnt = inputs_cnt + self.outputs_cnt = outputs_cnt + self.extra_data = extra_data + self.extra_data_len = extra_data_len + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxAck.py b/src/trezor/messages/TxAck.py index 9e0b7cc11f..3ea27bf9e6 100644 --- a/src/trezor/messages/TxAck.py +++ b/src/trezor/messages/TxAck.py @@ -8,3 +8,11 @@ class TxAck(p.MessageType): 1: ('tx', TransactionType, 0), } MESSAGE_WIRE_TYPE = 22 + + def __init__( + self, + tx: TransactionType = None, + **kwargs, + ): + self.tx = tx + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxInputType.py b/src/trezor/messages/TxInputType.py index b3b5671583..f5a0333cb3 100644 --- a/src/trezor/messages/TxInputType.py +++ b/src/trezor/messages/TxInputType.py @@ -13,6 +13,26 @@ class TxInputType(p.MessageType): 6: ('script_type', p.UVarintType, 0), # default=0 7: ('multisig', MultisigRedeemScriptType, 0), 8: ('amount', p.UVarintType, 0), - 9: ('decred_tree', p.UVarintType, 0), - 10: ('decred_script_version', p.UVarintType, 0), } + + def __init__( + self, + address_n: list = [], + prev_hash: bytes = None, + prev_index: int = None, + script_sig: bytes = None, + sequence: int = None, + script_type: int = None, + multisig: MultisigRedeemScriptType = None, + amount: int = None, + **kwargs, + ): + self.address_n = address_n + self.prev_hash = prev_hash + self.prev_index = prev_index + self.script_sig = script_sig + self.sequence = sequence + self.script_type = script_type + self.multisig = multisig + self.amount = amount + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxOutputBinType.py b/src/trezor/messages/TxOutputBinType.py index 12c63a9120..4dd3cddcc9 100644 --- a/src/trezor/messages/TxOutputBinType.py +++ b/src/trezor/messages/TxOutputBinType.py @@ -6,5 +6,14 @@ class TxOutputBinType(p.MessageType): FIELDS = { 1: ('amount', p.UVarintType, 0), # required 2: ('script_pubkey', p.BytesType, 0), # required - 3: ('decred_script_version', p.UVarintType, 0), } + + def __init__( + self, + amount: int = None, + script_pubkey: bytes = None, + **kwargs, + ): + self.amount = amount + self.script_pubkey = script_pubkey + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxOutputType.py b/src/trezor/messages/TxOutputType.py index bfd3fa470a..837b98ca96 100644 --- a/src/trezor/messages/TxOutputType.py +++ b/src/trezor/messages/TxOutputType.py @@ -11,5 +11,22 @@ class TxOutputType(p.MessageType): 4: ('script_type', p.UVarintType, 0), # required 5: ('multisig', MultisigRedeemScriptType, 0), 6: ('op_return_data', p.BytesType, 0), - 7: ('decred_script_version', p.UVarintType, 0), } + + def __init__( + self, + address: str = None, + address_n: list = [], + amount: int = None, + script_type: int = None, + multisig: MultisigRedeemScriptType = None, + op_return_data: bytes = None, + **kwargs, + ): + self.address = address + self.address_n = address_n + self.amount = amount + self.script_type = script_type + self.multisig = multisig + self.op_return_data = op_return_data + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxRequest.py b/src/trezor/messages/TxRequest.py index ac9fd784cf..8409d1bf15 100644 --- a/src/trezor/messages/TxRequest.py +++ b/src/trezor/messages/TxRequest.py @@ -11,3 +11,15 @@ class TxRequest(p.MessageType): 3: ('serialized', TxRequestSerializedType, 0), } MESSAGE_WIRE_TYPE = 21 + + def __init__( + self, + request_type: int = None, + details: TxRequestDetailsType = None, + serialized: TxRequestSerializedType = None, + **kwargs, + ): + self.request_type = request_type + self.details = details + self.serialized = serialized + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxRequestDetailsType.py b/src/trezor/messages/TxRequestDetailsType.py index 7057e466aa..1755dff311 100644 --- a/src/trezor/messages/TxRequestDetailsType.py +++ b/src/trezor/messages/TxRequestDetailsType.py @@ -9,3 +9,17 @@ class TxRequestDetailsType(p.MessageType): 3: ('extra_data_len', p.UVarintType, 0), 4: ('extra_data_offset', p.UVarintType, 0), } + + def __init__( + self, + request_index: int = None, + tx_hash: bytes = None, + extra_data_len: int = None, + extra_data_offset: int = None, + **kwargs, + ): + self.request_index = request_index + self.tx_hash = tx_hash + self.extra_data_len = extra_data_len + self.extra_data_offset = extra_data_offset + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxRequestSerializedType.py b/src/trezor/messages/TxRequestSerializedType.py index 32f7b78c37..61bf936a1a 100644 --- a/src/trezor/messages/TxRequestSerializedType.py +++ b/src/trezor/messages/TxRequestSerializedType.py @@ -8,3 +8,15 @@ class TxRequestSerializedType(p.MessageType): 2: ('signature', p.BytesType, 0), 3: ('serialized_tx', p.BytesType, 0), } + + def __init__( + self, + signature_index: int = None, + signature: bytes = None, + serialized_tx: bytes = None, + **kwargs, + ): + self.signature_index = signature_index + self.signature = signature + self.serialized_tx = serialized_tx + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/TxSize.py b/src/trezor/messages/TxSize.py index 272954b160..b2fd88b48c 100644 --- a/src/trezor/messages/TxSize.py +++ b/src/trezor/messages/TxSize.py @@ -7,3 +7,11 @@ class TxSize(p.MessageType): 1: ('tx_size', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 44 + + def __init__( + self, + tx_size: int = None, + **kwargs, + ): + self.tx_size = tx_size + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/VerifyMessage.py b/src/trezor/messages/VerifyMessage.py index 88428aeebc..2a13075a76 100644 --- a/src/trezor/messages/VerifyMessage.py +++ b/src/trezor/messages/VerifyMessage.py @@ -10,3 +10,17 @@ class VerifyMessage(p.MessageType): 4: ('coin_name', p.UnicodeType, 0), # default='Bitcoin' } MESSAGE_WIRE_TYPE = 39 + + def __init__( + self, + address: str = None, + signature: bytes = None, + message: bytes = None, + coin_name: str = None, + **kwargs, + ): + self.address = address + self.signature = signature + self.message = message + self.coin_name = coin_name + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/WipeDevice.py b/src/trezor/messages/WipeDevice.py index 483fd6d259..830dcac400 100644 --- a/src/trezor/messages/WipeDevice.py +++ b/src/trezor/messages/WipeDevice.py @@ -4,3 +4,9 @@ import protobuf as p class WipeDevice(p.MessageType): MESSAGE_WIRE_TYPE = 5 + + def __init__( + self, + **kwargs, + ): + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/WordAck.py b/src/trezor/messages/WordAck.py index 8f9b8ac9df..3e364a5bdf 100644 --- a/src/trezor/messages/WordAck.py +++ b/src/trezor/messages/WordAck.py @@ -7,3 +7,11 @@ class WordAck(p.MessageType): 1: ('word', p.UnicodeType, 0), # required } MESSAGE_WIRE_TYPE = 47 + + def __init__( + self, + word: str = None, + **kwargs, + ): + self.word = word + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/WordRequest.py b/src/trezor/messages/WordRequest.py index 7154732588..12c671259a 100644 --- a/src/trezor/messages/WordRequest.py +++ b/src/trezor/messages/WordRequest.py @@ -7,3 +7,11 @@ class WordRequest(p.MessageType): 1: ('type', p.UVarintType, 0), } MESSAGE_WIRE_TYPE = 46 + + def __init__( + self, + type: int = None, + **kwargs, + ): + self.type = type + p.MessageType.__init__(self, **kwargs) diff --git a/src/trezor/messages/wire_types.py b/src/trezor/messages/wire_types.py index 11ae6e11d9..ceec1509b9 100644 --- a/src/trezor/messages/wire_types.py +++ b/src/trezor/messages/wire_types.py @@ -56,8 +56,6 @@ Initialize = const(0) LoadDevice = const(13) MessageSignature = const(40) NEMAddress = const(68) -NEMDecryptMessage = const(75) -NEMDecryptedMessage = const(76) NEMGetAddress = const(67) NEMSignTx = const(69) NEMSignedTx = const(70) diff --git a/tests/test_apps.wallet.address.py b/tests/test_apps.wallet.address.py new file mode 100644 index 0000000000..465738267a --- /dev/null +++ b/tests/test_apps.wallet.address.py @@ -0,0 +1,119 @@ +from common import * + +from apps.wallet.sign_tx.signing import * +from apps.common import coins +from trezor.crypto import bip32, bip39 + + +class TestAddress(unittest.TestCase): + # pylint: disable=C0301 + + def test_p2wpkh_in_p2sh_address(self): + coin = coins.by_name('Testnet') + address = address_p2wpkh_in_p2sh( + unhexlify('03a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f'), + coin.address_type_p2sh + ) + self.assertEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2') + + def test_p2wpkh_in_p2sh_node_derive_address(self): + coin = coins.by_name('Testnet') + seed = bip39.seed(' '.join(['all'] * 12), '') + root = bip32.from_seed(seed, 'secp256k1') + + node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0]) + address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) + + self.assertEqual(address, '2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX') + + node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 1]) + address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) + + self.assertEqual(address, '2NFWLCJQBSpz1oUJwwLpX8ECifFWGznBVqs') + + node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0]) + address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) + + self.assertEqual(address, '2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp') + + def test_p2wpkh_address(self): + # test data from https://bc-2.jp/tools/bech32demo/index.html + coin = coins.by_name('Testnet') + address = address_p2wpkh( + unhexlify('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'), + coin.bech32_prefix + ) + self.assertEqual(address, 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx') + + def test_p2sh_address(self): + coin = coins.by_name('Testnet') + + address = address_p2sh( + unhexlify('7a55d61848e77ca266e79a39bfc85c580a6426c9'), + coin.address_type_p2sh + ) + self.assertEqual(address, '2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp') + + def test_p2wsh_address(self): + coin = coins.by_name('Testnet') + + # pubkey OP_CHECKSIG + script = unhexlify('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac') + h = HashWriter(sha256) + write_bytes(h, script) + + address = address_p2wsh( + h.get_digest(), + coin.bech32_prefix + ) + self.assertEqual(address, 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7') + + def test_p2wsh_in_p2sh_address(self): + coin = coins.by_name('Bitcoin') + + # test data from Mastering Bitcoin + address = address_p2wsh_in_p2sh( + unhexlify('9592d601848d04b172905e0ddb0adde59f1590f1e553ffc81ddc4b0ed927dd73'), + coin.address_type_p2sh + ) + self.assertEqual(address, '3Dwz1MXhM6EfFoJChHCxh1jWHb8GQqRenG') + + def test_multisig_address_p2sh(self): + # # test data from + # # http://www.soroushjp.com/2014/12/20/bitcoin-multisig-the-hard-way-understanding-raw-multisignature-bitcoin-transactions/ + # # commented out because uncompressed public keys are not supported + # coin = coins.by_name('Bitcoin') + # pubkeys = [ + # unhexlify('04a882d414e478039cd5b52a92ffb13dd5e6bd4515497439dffd691a0f12af9575fa349b5694ed3155b136f09e63975a1700c9f4d4df849323dac06cf3bd6458cd'), + # unhexlify('046ce31db9bdd543e72fe3039a1f1c047dab87037c36a669ff90e28da1848f640de68c2fe913d363a51154a0c62d7adea1b822d05035077418267b1a1379790187'), + # unhexlify('0411ffd36c70776538d079fbae117dc38effafb33304af83ce4894589747aee1ef992f63280567f52f5ba870678b4ab4ff6c8ea600bd217870a8b4f1f09f3a8e83'), + # ] + # address = address_multisig_p2sh(pubkeys, 2, coin.address_type_p2sh) + # self.assertEqual(address, '347N1Thc213QqfYCz3PZkjoJpNv5b14kBd') + + coin = coins.by_name('Bitcoin') + pubkeys = [ + unhexlify('02fe6f0a5a297eb38c391581c4413e084773ea23954d93f7753db7dc0adc188b2f'), + unhexlify('02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8'), + ] + address = address_multisig_p2sh(pubkeys, 2, coin.address_type_p2sh) + self.assertEqual(address, '39bgKC7RFbpoCRbtD5KEdkYKtNyhpsNa3Z') + + def test_multisig_address_p2wsh_in_p2sh(self): + # test data from + # https://bitcoin.stackexchange.com/questions/62656/generate-a-p2sh-p2wsh-address-and-spend-output-sent-to-it + coin = coins.by_name('Testnet') + pubkeys = [ + unhexlify('020b020e27e49f049eac10010506499a84e1d59a500cd3680e9ded580df9a107b0'), + unhexlify('0320ce424c6d61f352ccfea60d209651672cfb03b2dc77d1d64d3ba519aec756ae'), + ] + + address = address_multisig_p2wsh_in_p2sh(pubkeys, 2, coin.address_type_p2sh) + self.assertEqual(address, '2MsZ2fpGKUydzY62v6trPHR8eCx5JTy1Dpa') + + # def test_multisig_address_p2wsh(self): + # todo couldn't find test data + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_apps.wallet.segwit.address.py b/tests/test_apps.wallet.segwit.address.py deleted file mode 100644 index 8bfb45e2ab..0000000000 --- a/tests/test_apps.wallet.segwit.address.py +++ /dev/null @@ -1,48 +0,0 @@ -from common import * - -from apps.wallet.sign_tx.signing import * -from apps.common import coins -from trezor.crypto import bip32, bip39 - - -class TestSegwitAddress(unittest.TestCase): - # pylint: disable=C0301 - - def test_p2wpkh_in_p2sh_address(self): - coin = coins.by_name('Testnet') - - address = address_p2wpkh_in_p2sh( - unhexlify('03a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f'), - coin.address_type_p2sh - ) - self.assertEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2') - - def test_p2wpkh_in_p2sh_raw_address(self): - raw = address_p2wpkh_in_p2sh_raw( - unhexlify('03a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f') - ) - self.assertEqual(raw, unhexlify('336caa13e08b96080a32b5d818d59b4ab3b36742')) - - def test_p2wpkh_in_p2sh_node_derive_address(self): - coin = coins.by_name('Testnet') - seed = bip39.seed(' '.join(['all'] * 12), '') - root = bip32.from_seed(seed, 'secp256k1') - - node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0]) - address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) - - self.assertEqual(address, '2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX') - - node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 1]) - address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) - - self.assertEqual(address, '2NFWLCJQBSpz1oUJwwLpX8ECifFWGznBVqs') - - node = node_derive(root, [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 0, 0]) - address = address_p2wpkh_in_p2sh(node.public_key(), coin.address_type_p2sh) - - self.assertEqual(address, '2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp') - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py b/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py index 163124e3e2..bc877b93d8 100644 --- a/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py +++ b/tests/test_apps.wallet.segwit.bip143.native_p2wpkh.py @@ -19,22 +19,26 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase): prev_index=0, amount=625000000, # 6.25 btc script_type=InputScriptType.SPENDWITNESS, + multisig=None, sequence=0xffffffee) inp2 = TxInputType(address_n=[1], # Trezor expects hash in reversed format prev_hash=unhexlify('8ac60eb9575db5b2d987e29f301b5b819ea83a5c6579d282d189cc04b8e151ef'), prev_index=1, + multisig=None, amount=600000000, # 6 btc script_type=InputScriptType.SPENDWITNESS, sequence=0xffffffff) out1 = TxOutputType(address='1Cu32FVupVCgHkMMRJdYJugxwo2Aprgk7H', # derived amount=0x0000000006b22c20, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + multisig=None, + address_n=[]) out2 = TxOutputType(address='16TZ8J6Q5iZKBWizWzFAYnrsaox5Z5aBRV', # derived amount=0x000000000d519390, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + multisig=None, + address_n=[]) def test_prevouts(self): diff --git a/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py b/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py index 738f987f52..b2bf861bf4 100644 --- a/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py +++ b/tests/test_apps.wallet.segwit.bip143.p2wpkh_in_p2sh.py @@ -17,17 +17,20 @@ class TestSegwitBip143(unittest.TestCase): # Trezor expects hash in reversed format prev_hash=unhexlify('77541aeb3c4dac9260b68f74f44c973081a9d4cb2ebe8038b2d70faa201b6bdb'), prev_index=1, + multisig=None, amount=1000000000, # 10 btc script_type=InputScriptType.SPENDP2SHWITNESS, # TODO: is this correct? sequence=0xfffffffe) out1 = TxOutputType(address='1Fyxts6r24DpEieygQiNnWxUdb18ANa5p7', amount=0x000000000bebb4b8, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + multisig=None, + address_n=[]) out2 = TxOutputType(address='1Q5YjKVj5yQWHBBsyEBamkfph3cA6G9KK8', amount=0x000000002faf0800, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + multisig=None, + address_n=[]) def test_bip143_prevouts(self): diff --git a/tests/test_apps.wallet.segwit.signtx.native_p2wpkh.py b/tests/test_apps.wallet.segwit.signtx.native_p2wpkh.py index 7e79fe2bce..8f97c0c28d 100644 --- a/tests/test_apps.wallet.segwit.signtx.native_p2wpkh.py +++ b/tests/test_apps.wallet.segwit.signtx.native_p2wpkh.py @@ -42,13 +42,15 @@ class TestSignSegwitTxNativeP2WPKH(unittest.TestCase): address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp', amount=5000000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], + multisig=None, ) out2 = TxOutputType( address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu', script_type=OutputScriptType.PAYTOADDRESS, amount=12300000 - 11000 - 5000000, - address_n=None, + address_n=[], + multisig=None, ) tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2) @@ -135,13 +137,15 @@ class TestSignSegwitTxNativeP2WPKH(unittest.TestCase): address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp', amount=5000000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, # TODO: ask honza about sanitizing + address_n=[], + multisig=None, ) out2 = TxOutputType( address=None, address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0], script_type=OutputScriptType.PAYTOWITNESS, amount=12300000 - 11000 - 5000000, + multisig=None, ) tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2) diff --git a/tests/test_apps.wallet.segwit.signtx.p2wpkh_in_p2sh.py b/tests/test_apps.wallet.segwit.signtx.p2wpkh_in_p2sh.py index 86985c179e..66e48a6e67 100644 --- a/tests/test_apps.wallet.segwit.signtx.p2wpkh_in_p2sh.py +++ b/tests/test_apps.wallet.segwit.signtx.p2wpkh_in_p2sh.py @@ -42,13 +42,15 @@ class TestSignSegwitTxP2WPKHInP2SH(unittest.TestCase): address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC', amount=12300000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], + multisig=None, ) out2 = TxOutputType( address='2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX', script_type=OutputScriptType.PAYTOADDRESS, amount=123456789 - 11000 - 12300000, - address_n=None, + address_n=[], + multisig=None, ) tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2) @@ -135,13 +137,15 @@ class TestSignSegwitTxP2WPKHInP2SH(unittest.TestCase): address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC', amount=12300000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], + multisig=None, ) out2 = TxOutputType( address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0], script_type=OutputScriptType.PAYTOP2SHWITNESS, amount=123456789 - 11000 - 12300000, - address=None, # TODO: ask about sanitizing + address=None, + multisig=None, ) tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2) @@ -248,13 +252,15 @@ class TestSignSegwitTxP2WPKHInP2SH(unittest.TestCase): address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC', amount=8, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], + multisig=None, ) out2 = TxOutputType( address_n=[49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0], script_type=OutputScriptType.PAYTOP2SHWITNESS, amount=1, address=None, + multisig=None, ) tx = SignTx(coin_name='Testnet', version=None, lock_time=None, inputs_count=1, outputs_count=2) diff --git a/tests/test_apps.wallet.signtx.fee_threshold.py b/tests/test_apps.wallet.signtx.fee_threshold.py index 1e2ef8ddec..0147aec366 100644 --- a/tests/test_apps.wallet.signtx.fee_threshold.py +++ b/tests/test_apps.wallet.signtx.fee_threshold.py @@ -28,16 +28,19 @@ class TestSignTxFeeThreshold(unittest.TestCase): pinp1 = TxInputType(script_sig=unhexlify('483045022072ba61305fe7cb542d142b8f3299a7b10f9ea61f6ffaab5dca8142601869d53c0221009a8027ed79eb3b9bc13577ac2853269323434558528c6b6a7e542be46e7e9a820141047a2d177c0f3626fc68c53610b0270fa6156181f46586c679ba6a88b34c6f4874686390b4d92e5769fbb89c8050b984f4ec0b257a0e5c4ff8bd3b035a51709503'), prev_hash=unhexlify('c16a03f1cf8f99f6b5297ab614586cacec784c2d259af245909dedb0e39eddcf'), prev_index=1, + multisig=None, script_type=None, sequence=None) pinp2 = TxInputType(script_sig=unhexlify('48304502200fd63adc8f6cb34359dc6cca9e5458d7ea50376cbd0a74514880735e6d1b8a4c0221008b6ead7fe5fbdab7319d6dfede3a0bc8e2a7c5b5a9301636d1de4aa31a3ee9b101410486ad608470d796236b003635718dfc07c0cac0cfc3bfc3079e4f491b0426f0676e6643a39198e8e7bdaffb94f4b49ea21baa107ec2e237368872836073668214'), prev_hash=unhexlify('1ae39a2f8d59670c8fc61179148a8e61e039d0d9e8ab08610cb69b4a19453eaf'), prev_index=1, + multisig=None, script_type=None, sequence=None) pout1 = TxOutputBinType(script_pubkey=unhexlify('76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'), amount=390000, - address_n=None) + multisig=None, + address_n=[]) inp1 = TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e # amount=390000, @@ -49,8 +52,9 @@ class TestSignTxFeeThreshold(unittest.TestCase): sequence=None) out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 100000, # fee increased to 100000 => too high + multisig=None, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + address_n=[]) tx = SignTx(coin_name=None, version=None, lock_time=None, inputs_count=1, outputs_count=1) messages = [ @@ -91,15 +95,18 @@ class TestSignTxFeeThreshold(unittest.TestCase): prev_hash=unhexlify('c16a03f1cf8f99f6b5297ab614586cacec784c2d259af245909dedb0e39eddcf'), prev_index=1, script_type=None, + multisig=None, sequence=None) pinp2 = TxInputType(script_sig=unhexlify('48304502200fd63adc8f6cb34359dc6cca9e5458d7ea50376cbd0a74514880735e6d1b8a4c0221008b6ead7fe5fbdab7319d6dfede3a0bc8e2a7c5b5a9301636d1de4aa31a3ee9b101410486ad608470d796236b003635718dfc07c0cac0cfc3bfc3079e4f491b0426f0676e6643a39198e8e7bdaffb94f4b49ea21baa107ec2e237368872836073668214'), prev_hash=unhexlify('1ae39a2f8d59670c8fc61179148a8e61e039d0d9e8ab08610cb69b4a19453eaf'), prev_index=1, + multisig=None, script_type=None, sequence=None) pout1 = TxOutputBinType(script_pubkey=unhexlify('76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'), amount=390000, - address_n=None) + multisig=None, + address_n=[]) inp1 = TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e # amount=390000, @@ -112,7 +119,8 @@ class TestSignTxFeeThreshold(unittest.TestCase): out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 90000, # fee increased to 90000, slightly less than the threshold script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + multisig=None, + address_n=[]) tx = SignTx(coin_name=None, version=None, lock_time=None, inputs_count=1, outputs_count=1) messages = [ diff --git a/tests/test_apps.wallet.signtx.py b/tests/test_apps.wallet.signtx.py index 15dd326b1b..ebba5deb7d 100644 --- a/tests/test_apps.wallet.signtx.py +++ b/tests/test_apps.wallet.signtx.py @@ -40,7 +40,7 @@ class TestSignTx(unittest.TestCase): sequence=None) pout1 = TxOutputBinType(script_pubkey=unhexlify('76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'), amount=390000, - address_n=None) + address_n=[]) inp1 = TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e # amount=390000, @@ -53,7 +53,8 @@ class TestSignTx(unittest.TestCase): out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 10000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None) + address_n=[], + multisig=None) tx = SignTx(coin_name=None, version=None, lock_time=None, inputs_count=1, outputs_count=1) messages = [ diff --git a/tests/test_apps.wallet.txweight.py b/tests/test_apps.wallet.txweight.py index a99d5cdc8e..84e92018dd 100644 --- a/tests/test_apps.wallet.txweight.py +++ b/tests/test_apps.wallet.txweight.py @@ -30,7 +30,7 @@ class TestCalculateTxWeight(unittest.TestCase): out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 10000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], multisig=None) calculator = TxWeightCalculator(1, 1) @@ -63,14 +63,14 @@ class TestCalculateTxWeight(unittest.TestCase): address='mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC', amount=12300000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], multisig=None, ) out2 = TxOutputType( address='2N1LGaGg836mqSQqiuUBLfcyGBhyZbremDX', script_type=OutputScriptType.PAYTOADDRESS, amount=123456789 - 11000 - 12300000, - address_n=None, + address_n=[], multisig=None, ) @@ -105,14 +105,14 @@ class TestCalculateTxWeight(unittest.TestCase): address='2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp', amount=5000000, script_type=OutputScriptType.PAYTOADDRESS, - address_n=None, + address_n=[], multisig=None, ) out2 = TxOutputType( address='tb1q694ccp5qcc0udmfwgp692u2s2hjpq5h407urtu', script_type=OutputScriptType.PAYTOADDRESS, amount=12300000 - 11000 - 5000000, - address_n=None, + address_n=[], multisig=None, ) diff --git a/tools/build_mocks b/tools/build_mocks index 1ae3f3b0e6..6e8e8991e0 100755 --- a/tools/build_mocks +++ b/tools/build_mocks @@ -84,8 +84,8 @@ def build_module(mod_file, dest): def build_directory(dir, dest): print("Building mocks for", dir, "to", dest) - for pkg in os.listdir(dir): - for mod in os.listdir(os.path.join(dir, pkg)): + for pkg in sorted(os.listdir(dir)): + for mod in sorted(os.listdir(os.path.join(dir, pkg))): build_module(os.path.join(dir, pkg, mod), dest) diff --git a/tools/build_protobuf b/tools/build_protobuf index e375ad819f..9d0cd60309 100755 --- a/tools/build_protobuf +++ b/tools/build_protobuf @@ -4,6 +4,8 @@ PB2DIR=$CURDIR/pb2 OUTDIR=../src/trezor/messages INDEX=$OUTDIR/wire_types.py +set -x + rm -f $OUTDIR/[A-Z]*.py mkdir -p $OUTDIR mkdir -p $PB2DIR @@ -21,7 +23,7 @@ for i in types messages ; do done # hack to make output python 3 compatible -sed -i 's/^import types_pb2/from . import types_pb2/g' $CURDIR/pb2/messages_pb2.py +sed -i '' 's/^import types_pb2/from . import types_pb2/g' $CURDIR/pb2/messages_pb2.py for i in types messages ; do # Convert google protobuf library to trezor's internal format diff --git a/tools/pb2py b/tools/pb2py index 50d7cebc7c..7825d9e2db 100755 --- a/tools/pb2py +++ b/tools/pb2py @@ -14,6 +14,8 @@ def process_type(t, cls, msg_id, indexfile, is_upy): imports = [] out = ["", "", "class %s(p.MessageType):" % t, ] + args = [] + assigns = [] if cls.DESCRIPTOR.fields_by_name: out.append(" FIELDS = {") @@ -34,30 +36,37 @@ def process_type(t, cls, msg_id, indexfile, is_upy): # TYPE_UINT32 = 13 # TYPE_ENUM = 14 type = 'p.UVarintType' + pytype = 'int' elif v.type in (17,): # TYPE_SINT32 = 17 type = 'p.Sint32Type' + pytype = 'int' elif v.type in (18,): # TYPE_SINT64 = 18 type = 'p.Sint64Type' + pytype = 'int' elif v.type == 9: # TYPE_STRING = 9 type = 'p.UnicodeType' + pytype = 'str' elif v.type == 8: # TYPE_BOOL = 8 type = 'p.BoolType' + pytype = 'bool' elif v.type == 12: # TYPE_BYTES = 12 type = 'p.BytesType' + pytype = 'bytes' elif v.type == 11: # TYPE_MESSAGE = 1 type = v.message_type.name + pytype = v.message_type.name imports.append("from .%s import %s" % (v.message_type.name, v.message_type.name)) @@ -74,12 +83,19 @@ def process_type(t, cls, msg_id, indexfile, is_upy): if repeated: flags = 'p.FLAG_REPEATED' + pytype = "list" + value = [] else: flags = '0' + value = None out.append(" %d: ('%s', %s, %s),%s" % (number, fieldname, type, flags, comment)) + args.append(" %s: %s = %s," % (fieldname, pytype, value)) + + assigns.append(" self.%s = %s" % (fieldname, fieldname)) + # print fieldname, number, type, repeated, comment # print v.__dict__ # print v.CPPTYPE_STRING @@ -109,7 +125,12 @@ def process_type(t, cls, msg_id, indexfile, is_upy): imports = ['from __future__ import absolute_import', 'from .. import protobuf as p'] + imports - return imports + out + args.append(" **kwargs,") + assigns.append(" p.MessageType.__init__(self, **kwargs)") + + init = ["", " def __init__(", " self,"] + args + [" ):"] + assigns + + return imports + out + init def process_enum(t, cls, is_upy):