|
28 | 28 | #include "ethereum_networks_onekey.h" |
29 | 29 | #include "ethereum_onekey.h" |
30 | 30 | #include "ethereum_tokens_onekey.h" |
| 31 | +#include "ethereum_typed_data.h" |
31 | 32 | #include "fsm.h" |
32 | 33 | #include "gettext.h" |
33 | 34 | #include "layout2.h" |
@@ -1650,6 +1651,141 @@ void ethereum_typed_hash_sign_onekey(const EthereumSignTypedHashOneKey *msg, |
1650 | 1651 | msg_write(MessageType_MessageType_EthereumTypedDataSignatureOneKey, resp); |
1651 | 1652 | } |
1652 | 1653 |
|
| 1654 | +static bool is_string_in_list(const char *str, const char *const *list, |
| 1655 | + size_t list_size) { |
| 1656 | + for (size_t i = 0; i < list_size; i++) { |
| 1657 | + if (strcmp(str, list[i]) == 0) { |
| 1658 | + return true; |
| 1659 | + } |
| 1660 | + } |
| 1661 | + return false; |
| 1662 | +} |
| 1663 | +static bool typed_data_confirm_final(void) { |
| 1664 | + oledClear(); |
| 1665 | + layoutHeader(_(T_CONFIRM_TYPED_DATA)); |
| 1666 | + char confirm_text[128] = {0}; |
| 1667 | + snprintf(confirm_text, 128, "%s", |
| 1668 | + _(C__DO_YOU_WANT_TO_SIGN_THIS_CHAIN_STR_MESSAGE_QUES)); |
| 1669 | + bracket_replace(confirm_text, "EIP712"); |
| 1670 | + oledDrawStringAdapter(0, 13, confirm_text, FONT_STANDARD); |
| 1671 | + layoutButtonNoAdapter(NULL, &bmp_bottom_left_close); |
| 1672 | + layoutButtonYesAdapter(NULL, &bmp_bottom_right_arrow); |
| 1673 | + oledRefresh(); |
| 1674 | + return protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false); |
| 1675 | +} |
| 1676 | +void ethereum_typed_data_sign_onekey(const EthereumSignTypedDataOneKey *msg, |
| 1677 | + const HDNode *node, |
| 1678 | + EthereumTypedDataSignatureOneKey *resp) { |
| 1679 | + TypedDataEnvelope envelope = {0}; |
| 1680 | + TypedDataEnvelope_init(&envelope, msg->primary_type, |
| 1681 | + strlen(msg->primary_type), msg->metamask_v4_compat); |
| 1682 | + if (!collect_types(&envelope)) { |
| 1683 | + return; |
| 1684 | + } |
| 1685 | + bool is_permit = |
| 1686 | + is_string_in_list(envelope.primary_type, HIGH_RISK_PRIMARY_TYPES_PERMIT, |
| 1687 | + sizeof(HIGH_RISK_PRIMARY_TYPES_PERMIT) / |
| 1688 | + sizeof(HIGH_RISK_PRIMARY_TYPES_PERMIT[0])); |
| 1689 | + bool is_order = |
| 1690 | + is_string_in_list(envelope.primary_type, HIGH_RISK_PRIMARY_TYPES_ORDER, |
| 1691 | + sizeof(HIGH_RISK_PRIMARY_TYPES_ORDER) / |
| 1692 | + sizeof(HIGH_RISK_PRIMARY_TYPES_ORDER[0])); |
| 1693 | + char warning_text[128] = {0}; |
| 1694 | + snprintf(warning_text, 128, "%s", _(I_TYPED_DATA_AUTHORIZATION_WARNING)); |
| 1695 | + char *warning_type = NULL; |
| 1696 | + if (is_permit) { |
| 1697 | + warning_type = "Permit"; |
| 1698 | + } else if (is_order) { |
| 1699 | + warning_type = "Order"; |
| 1700 | + } else { |
| 1701 | + warning_type = "signTypedData"; |
| 1702 | + } |
| 1703 | + bracket_replace(warning_text, warning_type); |
| 1704 | + // show warning |
| 1705 | + layoutDialogCenterAdapterV2(NULL, &bmp_icon_warning, &bmp_bottom_left_close, |
| 1706 | + &bmp_bottom_right_arrow, NULL, NULL, NULL, NULL, |
| 1707 | + NULL, NULL, warning_text); |
| 1708 | + if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) { |
| 1709 | + fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL); |
| 1710 | + return; |
| 1711 | + } |
| 1712 | + uint32_t member_path[] = {0}; |
| 1713 | + uint8_t member_path_len = 1; |
| 1714 | + char parent_objects[1][64] = {TYPE_NAME_DOMAIN}; |
| 1715 | + uint8_t parent_objects_len = 1; |
| 1716 | + uint8_t domain_separator[32] = {0}; |
| 1717 | + display_info_init(&display_info, 16); |
| 1718 | + |
| 1719 | + if (!hash_struct(&envelope, TYPE_NAME_DOMAIN, strlen(TYPE_NAME_DOMAIN), |
| 1720 | + member_path, member_path_len, 0, parent_objects, |
| 1721 | + parent_objects_len, domain_separator)) { |
| 1722 | + display_info_cleanup(&display_info); |
| 1723 | + return; |
| 1724 | + } |
| 1725 | + if (!layoutTypedData(&display_info, TYPE_NAME_DOMAIN)) { |
| 1726 | + display_info_cleanup(&display_info); |
| 1727 | + fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL); |
| 1728 | + return; |
| 1729 | + } |
| 1730 | + display_info_cleanup(&display_info); |
| 1731 | + bool has_message_hash = true; |
| 1732 | + if (strncmp(envelope.primary_type, TYPE_NAME_DOMAIN, |
| 1733 | + strlen(TYPE_NAME_DOMAIN)) == 0) { |
| 1734 | + has_message_hash = false; |
| 1735 | + } |
| 1736 | + uint8_t message_hash[32] = {0}; |
| 1737 | + |
| 1738 | + if (has_message_hash) { |
| 1739 | + member_path[0] = 1; |
| 1740 | + memzero(parent_objects, sizeof(parent_objects)); |
| 1741 | + strncpy(parent_objects[0], envelope.primary_type, |
| 1742 | + strlen(envelope.primary_type)); |
| 1743 | + display_info_init(&display_info, 16); |
| 1744 | + if (!hash_struct(&envelope, envelope.primary_type, |
| 1745 | + strlen(envelope.primary_type), member_path, |
| 1746 | + member_path_len, 0, parent_objects, parent_objects_len, |
| 1747 | + message_hash)) { |
| 1748 | + display_info_cleanup(&display_info); |
| 1749 | + return; |
| 1750 | + } |
| 1751 | + if (!layoutTypedData(&display_info, envelope.primary_type)) { |
| 1752 | + display_info_cleanup(&display_info); |
| 1753 | + fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL); |
| 1754 | + return; |
| 1755 | + } |
| 1756 | + display_info_cleanup(&display_info); |
| 1757 | + } |
| 1758 | + |
| 1759 | + // confirm final |
| 1760 | + if (!typed_data_confirm_final()) { |
| 1761 | + fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL); |
| 1762 | + return; |
| 1763 | + } |
| 1764 | + uint8_t hash[32] = {0}; |
| 1765 | + SHA3_CTX ctx = {0}; |
| 1766 | + sha3_256_Init(&ctx); |
| 1767 | + sha3_Update(&ctx, (const uint8_t *)"\x19\x01", 2); |
| 1768 | + sha3_Update(&ctx, domain_separator, 32); |
| 1769 | + if (has_message_hash) { |
| 1770 | + sha3_Update(&ctx, message_hash, 32); |
| 1771 | + } |
| 1772 | + keccak_Final(&ctx, hash); |
| 1773 | + uint8_t v = 0; |
| 1774 | +#if EMULATOR |
| 1775 | + if (ecdsa_sign_digest(&secp256k1, node->private_key, hash, |
| 1776 | + resp->signature.bytes, &v, ethereum_is_canonic) != 0) { |
| 1777 | +#else |
| 1778 | + if (hdnode_sign_digest(node, hash, resp->signature.bytes, &v, |
| 1779 | + ethereum_is_canonic) != 0) { |
| 1780 | +#endif |
| 1781 | + fsm_sendFailure(FailureType_Failure_ProcessError, "Signing failed"); |
| 1782 | + return; |
| 1783 | + } |
| 1784 | + resp->signature.bytes[64] = 27 + v; |
| 1785 | + resp->signature.size = 65; |
| 1786 | + msg_write(MessageType_MessageType_EthereumTypedDataSignatureOneKey, resp); |
| 1787 | +} |
| 1788 | + |
1653 | 1789 | bool ethereum_parse_onekey(const char *address, uint8_t pubkeyhash[20]) { |
1654 | 1790 | memzero(pubkeyhash, 20); |
1655 | 1791 | size_t len = strlen(address); |
|
0 commit comments