feat: Add hedera-base support #667
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Technical Breakdown of the
hedera_app
ImplementationThis application enables the Cypherock X1 wallet to securely manage a user's Hedera (HBAR) account by performing two primary cryptographic functions: exporting a public key and signing a transaction.
1. Core Architecture & App Registration
Entry Point (
hedera_main.c
):hedera_app_desc
which includes a unique App ID (e.g., 24) and the version. This descriptor is registered with the main firmware controller insrc/controller_main.c
.hedera_main()
, which acts as the central router for all incoming requests for the Hedera app. It decodes the top-levelhedera_query_t
message to determine the requested action (e.g.,get_public_keys
,sign_txn
) and calls the appropriate handler.Communication Layer (
hedera_api.c
,hedera_api.h
):nanopb
'spb_encode
andpb_decode
functions.decode_hedera_query
safely parses incoming APDU data into C structs, whilehedera_send_result
serializes C structs back into a byte stream to send to the host.hedera_send_error
) that sends acommon_error_t
message back to the host, ensuring the host knows precisely why an operation failed.2. Cryptographic Operations & Key Management
hedera_pub_key.c
,hedera_txn.c
):derive_hdnode_from_path
.m/44'/3030'/0'/0'/i'
inhedera_helpers.c
viahedera_derivation_path_guard()
. This prevents the wallet from signing with keys from non-standard paths, which is a critical security measure. The3030'
component is Hedera's registered coin type.reconstruct_seed()
to temporarily reconstruct the master seed in RAM, uses it, and then immediately erases it by callingmemzero()
.3. Public Key Export (
hedera_pub_key.c
)hedera_get_pub_keys
flow.reconstruct_seed()
to get the master seed.derive_hdnode_from_path()
to derive the corresponding Ed25519 key pair.hedera_format_pubkey
) and displayed on the device screen for the user to verify. This is crucial because Hedera account IDs (0.0.123
) are not cryptographically derived from the key.4. Transaction Signing (
hedera_txn.c
)hedera_sign_transaction
flow, the app's most critical feature.TransactionBody
from the host. This byte array is stored inhedera_txn_context->raw_txn_bytes
.pb_decode
and thenanopb
-generatedHedera_TransactionBody_fields
descriptor to parseraw_txn_bytes
into thehedera_txn_context->txn
C struct. This decoding is only for display and verification purposes.txn
struct.txn.transactionID.accountID
).txn.which_data
. The code specifically handlescryptoTransfer
.accountAmounts
list, displaying each recipient's account ID and the amount.hedera_format_tinybars_to_hbar_string
to convert the integeramount
(in tinybars) into a human-readable HBAR string (e.g., "12.345 HBAR").txn.transactionFee
.txn.memo
if it exists.reconstruct_seed()
andderive_hdnode_from_path()
to get the required Ed25519 private key.ed25519_sign()
from the device's crypto library.raw_txn_bytes
received in step 2. It does NOT sign a hash.5. Helper Utilities (
hedera_helpers.c
,hedera_context.h
)HEDERA_NAME
, coin type, key sizes) in one place for easy maintenance.hedera_derivation_path_guard
to ensure that the app only ever operates on valid, standard derivation paths, preventing many types of attacks.