From 4b8f52cd2ff04ccd88a4f6d23fae2a10b4feadbc Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 26 Jul 2021 11:16:43 +0200 Subject: [PATCH] refactor(legacy): update to new Stellar protobuf usage [no changelog] --- .../firmware/protob/messages-stellar.options | 4 +- legacy/firmware/stellar.c | 52 +++++++++---------- legacy/firmware/stellar.h | 4 +- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/legacy/firmware/protob/messages-stellar.options b/legacy/firmware/protob/messages-stellar.options index ce3de65c2..d76de77a5 100644 --- a/legacy/firmware/protob/messages-stellar.options +++ b/legacy/firmware/protob/messages-stellar.options @@ -1,5 +1,5 @@ -StellarAssetType.code max_size:13 -StellarAssetType.issuer max_size:57 +StellarAsset.code max_size:13 +StellarAsset.issuer max_size:57 StellarGetAddress.address_n max_count:10 StellarAddress.address max_size:57 diff --git a/legacy/firmware/stellar.c b/legacy/firmware/stellar.c index 7896c2deb..83e2d14f0 100644 --- a/legacy/firmware/stellar.c +++ b/legacy/firmware/stellar.c @@ -121,20 +121,20 @@ bool stellar_signingInit(const StellarSignTx *msg) { stellar_hashupdate_uint32(msg->memo_type); switch (msg->memo_type) { // None, nothing else to do - case 0: + case StellarMemoType_NONE: break; // Text: 4 bytes (size) + up to 28 bytes - case 1: + case StellarMemoType_TEXT: stellar_hashupdate_string((unsigned char *)&(msg->memo_text), strnlen(msg->memo_text, 28)); break; // ID (8 bytes, uint64) - case 2: + case StellarMemoType_ID: stellar_hashupdate_uint64(msg->memo_id); break; // Hash and return are the same data structure (32 byte tx hash) - case 3: - case 4: + case StellarMemoType_HASH: + case StellarMemoType_RETURN: stellar_hashupdate_bytes(msg->memo_hash.bytes, 32); break; default: @@ -791,7 +791,7 @@ bool stellar_confirmSetOptionsOp(const StellarSetOptionsOp *msg) { char *str_signer_type = NULL; bool needs_hash_confirm = false; switch (msg->signer_type) { - case 0: + case StellarSignerType_ACCOUNT: strlcat(str_title, _("account"), sizeof(str_title)); const char **str_addr_rows = @@ -805,8 +805,8 @@ bool stellar_confirmSetOptionsOp(const StellarSetOptionsOp *msg) { return false; } break; - case 1: - case 2: + case StellarSignerType_PRE_AUTH: + case StellarSignerType_HASH: str_signer_type = (msg->signer_type == 1) ? _("pre-auth hash") : _("hash(x)"); needs_hash_confirm = true; @@ -971,13 +971,13 @@ bool stellar_confirmAllowTrustOp(const StellarAllowTrustOp *msg) { // asset code char padded_code[12 + 1] = {0}; switch (msg->asset_type) { - case 0: // native asset (XLM) + case StellarAssetType_NATIVE: // native asset (XLM) break; - case 1: // alphanum 4 + case StellarAssetType_ALPHANUM4: strlcpy(padded_code, msg->asset_code, 4 + 1); stellar_hashupdate_bytes((uint8_t *)padded_code, 4); break; - case 2: // alphanum 12 + case StellarAssetType_ALPHANUM12: strlcpy(padded_code, msg->asset_code, 12 + 1); stellar_hashupdate_bytes((uint8_t *)padded_code, 12); break; @@ -1286,7 +1286,7 @@ const char **stellar_lineBreakAddress(const uint8_t *addrbytes) { * MOBI (G123456789000) * ALPHA12EXAMP (G0987) */ -void stellar_format_asset(const StellarAssetType *asset, char *str_formatted, +void stellar_format_asset(const StellarAsset *asset, char *str_formatted, size_t len) { char str_asset_code[12 + 1] = {0}; // truncated asset issuer, final length depends on length of asset code @@ -1297,17 +1297,17 @@ void stellar_format_asset(const StellarAssetType *asset, char *str_formatted, memzero(str_asset_issuer_trunc, sizeof(str_asset_issuer_trunc)); // Validate issuer account for non-native assets - if (asset->type != 0 && !stellar_validateAddress(asset->issuer)) { + if (asset->type != StellarAssetType_NATIVE && !stellar_validateAddress(asset->issuer)) { stellar_signingAbort(_("Invalid asset issuer")); return; } // Native asset - if (asset->type == 0) { + if (asset->type == StellarAssetType_NATIVE) { strlcpy(str_formatted, _("XLM (native asset)"), len); } // 4-character custom - if (asset->type == 1) { + if (asset->type == StellarAssetType_ALPHANUM4) { memcpy(str_asset_code, asset->code, 4); strlcpy(str_formatted, str_asset_code, len); @@ -1315,7 +1315,7 @@ void stellar_format_asset(const StellarAssetType *asset, char *str_formatted, memcpy(str_asset_issuer_trunc, asset->issuer, 13); } // 12-character custom - if (asset->type == 2) { + if (asset->type == StellarAssetType_ALPHANUM12) { memcpy(str_asset_code, asset->code, 12); strlcpy(str_formatted, str_asset_code, len); @@ -1323,7 +1323,7 @@ void stellar_format_asset(const StellarAssetType *asset, char *str_formatted, memcpy(str_asset_issuer_trunc, asset->issuer, 5); } // Issuer is read the same way for both types of custom assets - if (asset->type == 1 || asset->type == 2) { + if (asset->type == StellarAssetType_ALPHANUM4 || asset->type == StellarAssetType_ALPHANUM12) { strlcat(str_formatted, _(" ("), len); strlcat(str_formatted, str_asset_issuer_trunc, len); strlcat(str_formatted, _(")"), len); @@ -1544,19 +1544,19 @@ void stellar_hashupdate_address(const uint8_t *address_bytes) { * a typical string, so if "TEST" is the asset code then the hashed value needs * to be 4 bytes and not include the null at the end of the string */ -void stellar_hashupdate_asset(const StellarAssetType *asset) { +void stellar_hashupdate_asset(const StellarAsset *asset) { stellar_hashupdate_uint32(asset->type); // For non-native assets, validate issuer account and convert to bytes uint8_t issuer_bytes[STELLAR_KEY_SIZE] = {0}; - if (asset->type != 0 && + if (asset->type != StellarAssetType_NATIVE && !stellar_getAddressBytes(asset->issuer, issuer_bytes)) { stellar_signingAbort(_("Invalid asset issuer")); return; } // 4-character asset code - if (asset->type == 1) { + if (asset->type == StellarAssetType_ALPHANUM4) { char code4[4 + 1] = {0}; memzero(code4, sizeof(code4)); strlcpy(code4, asset->code, sizeof(code4)); @@ -1566,7 +1566,7 @@ void stellar_hashupdate_asset(const StellarAssetType *asset) { } // 12-character asset code - if (asset->type == 2) { + if (asset->type == StellarAssetType_ALPHANUM12) { char code12[12 + 1] = {0}; memzero(code12, sizeof(code12)); strlcpy(code12, asset->code, sizeof(code12)); @@ -1627,13 +1627,13 @@ void stellar_layoutTransactionSummary(const StellarSignTx *msg) { memzero(str_lines, sizeof(str_lines)); switch (msg->memo_type) { - case 0: // Memo: none + case StellarMemoType_NONE: strlcpy(str_lines[0], _("[No Memo Set]"), sizeof(str_lines[0])); strlcpy(str_lines[1], _("Important:"), sizeof(str_lines[0])); strlcpy(str_lines[2], _("Many exchanges require"), sizeof(str_lines[0])); strlcpy(str_lines[3], _("a memo when depositing."), sizeof(str_lines[0])); break; - case 1: // Memo: text + case StellarMemoType_TEXT: strlcpy(str_lines[0], _("Memo (TEXT)"), sizeof(str_lines[0])); // Split 28-character string into two lines of 19 / 9 @@ -1641,15 +1641,15 @@ void stellar_layoutTransactionSummary(const StellarSignTx *msg) { strlcpy(str_lines[1], (const char *)msg->memo_text, 19 + 1); strlcpy(str_lines[2], (const char *)(msg->memo_text + 19), 9 + 1); break; - case 2: // Memo: ID + case StellarMemoType_ID: strlcpy(str_lines[0], _("Memo (ID)"), sizeof(str_lines[0])); stellar_format_uint64(msg->memo_id, str_lines[1], sizeof(str_lines[1])); break; - case 3: // Memo: hash + case StellarMemoType_HASH: needs_memo_hash_confirm = 1; strlcpy(str_lines[0], _("Memo (HASH)"), sizeof(str_lines[0])); break; - case 4: // Memo: return + case StellarMemoType_RETURN: needs_memo_hash_confirm = 1; strlcpy(str_lines[0], _("Memo (RETURN)"), sizeof(str_lines[0])); break; diff --git a/legacy/firmware/stellar.h b/legacy/firmware/stellar.h index 9de0d5ecc..5f67d5521 100644 --- a/legacy/firmware/stellar.h +++ b/legacy/firmware/stellar.h @@ -94,7 +94,7 @@ void stellar_hashupdate_uint64(uint64_t value); void stellar_hashupdate_bool(bool value); void stellar_hashupdate_string(const uint8_t *data, size_t len); void stellar_hashupdate_address(const uint8_t *address_bytes); -void stellar_hashupdate_asset(const StellarAssetType *asset); +void stellar_hashupdate_asset(const StellarAsset *asset); void stellar_hashupdate_bytes(const uint8_t *data, size_t len); void stellar_fillSignedTx(StellarSignedTx *resp); @@ -104,7 +104,7 @@ void stellar_getSignatureForActiveTx(uint8_t *out_signature); void stellar_format_uint32(uint32_t number, char *out, size_t outlen); void stellar_format_uint64(uint64_t number, char *out, size_t outlen); void stellar_format_stroops(uint64_t number, char *out, size_t outlen); -void stellar_format_asset(const StellarAssetType *asset, char *str_formatted, +void stellar_format_asset(const StellarAsset *asset, char *str_formatted, size_t len); void stellar_format_price(uint32_t numerator, uint32_t denominator, char *out, size_t outlen);