Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,26 @@ jobs:
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}

- name: Install clang-format
run: sudo apt-get install -y clang-format-14

- name: Check code formatting
run: |
# Use pre-installed clang-format (ubuntu-latest ships with it)
CF=$(command -v clang-format-14 || command -v clang-format || true)
if [ -z "$CF" ]; then
echo "::warning::clang-format not found, skipping lint"
exit 0
fi
echo "Using: $CF ($($CF --version | head -1))"
FAILED=0
for f in $(find include/keepkey lib/firmware lib/board lib/transport/src \
-name '*.c' -o -name '*.h' 2>/dev/null | grep -v generated | grep -v '.pb.'); do
if ! clang-format-14 --style=file --dry-run --Werror "$f" 2>/dev/null; then
if ! $CF --style=file --dry-run --Werror "$f" 2>/dev/null; then
echo "::warning file=$f::Formatting differs from .clang-format"
FAILED=1
fi
done
if [ "$FAILED" = "1" ]; then
echo ""
echo "Run: clang-format -i <file> to fix formatting"
echo "Run: $CF -i <file> to fix formatting"
echo "Note: treating as warning until codebase is reformatted"
fi
# Warn-only until existing code is reformatted
Expand Down
4 changes: 4 additions & 0 deletions include/keepkey/firmware/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ void fsm_msgMayachainGetAddress(const MayachainGetAddress *msg);
void fsm_msgMayachainSignTx(const MayachainSignTx *msg);
void fsm_msgMayachainMsgAck(const MayachainMsgAck *msg);

void fsm_msgSolanaGetAddress(const SolanaGetAddress *msg);
void fsm_msgSolanaSignTx(const SolanaSignTx *msg);
void fsm_msgSolanaSignMessage(const SolanaSignMessage *msg);

#if DEBUG_LINK
// void fsm_msgDebugLinkDecision(DebugLinkDecision *msg);
void fsm_msgDebugLinkGetState(DebugLinkGetState *msg);
Expand Down
44 changes: 44 additions & 0 deletions include/keepkey/firmware/solana.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2024 KeepKey
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPKEY_FIRMWARE_SOLANA_H
#define KEEPKEY_FIRMWARE_SOLANA_H

#include "trezor/crypto/bip32.h"
#include "messages-solana.pb.h"

#define SOLANA_ADDRESS_SIZE 50 // Base58-encoded Ed25519 public key (typically 44 chars + null)
#define SOLANA_SIGNATURE_SIZE 64 // Ed25519 signature size

// Convert Ed25519 public key to Solana Base58 address
bool solana_publicKeyToAddress(const uint8_t public_key[32], char *address,
size_t address_size);

// Sign Solana transaction (returns false on failure)
bool solana_signTx(const HDNode *node, const SolanaSignTx *msg,
SolanaSignedTx *resp);

// Sign Solana message (off-chain signature)
void solana_signMessage(const HDNode *node, const uint8_t *message,
size_t message_len, uint8_t *signature_out);

// Display message to user for confirmation
bool solana_confirmMessage(const uint8_t *message, size_t message_len);

#endif
104 changes: 104 additions & 0 deletions include/keepkey/firmware/solana_tx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* This file is part of the KeepKey project.
*
* Copyright (C) 2024 KeepKey
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPKEY_FIRMWARE_SOLANA_TX_H
#define KEEPKEY_FIRMWARE_SOLANA_TX_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

// Known Solana program IDs
#define SOLANA_SYSTEM_PROGRAM_ID "\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"
#define SOLANA_TOKEN_PROGRAM_ID "\x06\xdd\xf6\xe1\xd7\x65\xa1\x93\xd9\xcb\xe1\x46\xce\xeb\x79\xac\x1c\xb4\x85\xed\x5f\x5b\x37\x91\x3a\x8c\xf5\x85\x7e\xff\x00\xa9"
#define SOLANA_STAKE_PROGRAM_ID "\x06\xa7\xd5\x17\x18\xc7\x74\xc9\x28\x56\x63\x98\x69\x1d\x5e\xb6\x8b\x5e\xb8\xa3\x9b\x4b\x6d\x5c\x73\x55\x5b\x21\x00\x00\x00\x00"

// Solana instruction types
typedef enum {
SOLANA_INSTRUCTION_UNKNOWN = 0,
SOLANA_INSTRUCTION_SYSTEM_TRANSFER = 1,
SOLANA_INSTRUCTION_SYSTEM_CREATE_ACCOUNT = 2,
SOLANA_INSTRUCTION_TOKEN_TRANSFER = 3,
SOLANA_INSTRUCTION_TOKEN_TRANSFER_CHECKED = 4,
SOLANA_INSTRUCTION_TOKEN_APPROVE = 5,
SOLANA_INSTRUCTION_STAKE_DELEGATE = 6,
SOLANA_INSTRUCTION_STAKE_WITHDRAW = 7
} SolanaInstructionType;

// Parsed instruction data
typedef struct {
SolanaInstructionType type;
uint8_t program_id[32];
uint8_t num_accounts;
uint8_t account_indices[16]; // Max accounts per instruction
const uint8_t *data;
size_t data_len;
} SolanaInstruction;

// Parsed transaction
typedef struct {
uint8_t num_signatures;
uint8_t num_required_signatures;
uint8_t num_readonly_signed;
uint8_t num_readonly_unsigned;
uint16_t num_accounts;
uint8_t account_keys[16][32]; // 16 accounts (512 bytes)
uint8_t recent_blockhash[32];
uint8_t num_instructions;
SolanaInstruction instructions[8]; // 8 instructions
} SolanaParsedTransaction;

// System Transfer instruction data
typedef struct {
uint64_t lamports;
} SolanaSystemTransfer;

// Token Transfer instruction data
typedef struct {
uint64_t amount;
uint8_t decimals; // For TransferChecked
} SolanaTokenTransfer;

// Read Solana compact-u16 varint
bool read_compact_u16(const uint8_t **data, size_t *remaining, uint16_t *out);

// Parse a raw Solana transaction
bool solana_parseTransaction(const uint8_t *raw_tx, size_t tx_size,
SolanaParsedTransaction *parsed);

// Identify instruction type
SolanaInstructionType solana_identifyInstruction(const uint8_t *program_id,
const uint8_t *data,
size_t data_len);

// Parse specific instruction types
bool solana_parseSystemTransfer(const uint8_t *data, size_t len,
SolanaSystemTransfer *transfer);

bool solana_parseTokenTransfer(const uint8_t *data, size_t len,
SolanaTokenTransfer *transfer);

// Display transaction to user
bool solana_confirmTransaction(const SolanaParsedTransaction *tx,
const uint8_t *signer_pubkey);

// Format lamports to SOL string (1 SOL = 1,000,000,000 lamports)
void solana_formatLamports(uint64_t lamports, char *out, size_t out_len);

#endif // KEEPKEY_FIRMWARE_SOLANA_TX_H
1 change: 1 addition & 0 deletions include/keepkey/transport/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "messages-tendermint.pb.h"
#include "messages-thorchain.pb.h"
#include "messages-mayachain.pb.h"
#include "messages-solana.pb.h"

#include "types.pb.h"
#include "trezor_transport.h"
Expand Down
17 changes: 17 additions & 0 deletions include/keepkey/transport/messages-solana.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SolanaGetAddress.address_n max_count:8
SolanaGetAddress.coin_name max_size:21

SolanaAddress.address max_size:64

SolanaSignTx.address_n max_count:8
SolanaSignTx.coin_name max_size:21
SolanaSignTx.raw_tx max_size:2048

SolanaSignedTx.signature max_size:64

SolanaSignMessage.address_n max_count:8
SolanaSignMessage.coin_name max_size:21
SolanaSignMessage.message max_size:1024

SolanaMessageSignature.public_key max_size:32
SolanaMessageSignature.signature max_size:64
12 changes: 12 additions & 0 deletions include/keepkey/transport/messages-ton.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TonGetAddress.address_n max_count:8
TonGetAddress.coin_name max_size:21

TonAddress.address max_size:50
TonAddress.raw_address max_size:70

TonSignTx.address_n max_count:8
TonSignTx.coin_name max_size:21
TonSignTx.raw_tx max_size:1024
TonSignTx.to_address max_size:50

TonSignedTx.signature max_size:64
14 changes: 14 additions & 0 deletions include/keepkey/transport/messages-tron.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TronGetAddress.address_n max_count:8
TronGetAddress.coin_name max_size:21

TronAddress.address max_size:35

TronSignTx.address_n max_count:8
TronSignTx.coin_name max_size:21
TronSignTx.raw_data max_size:2048
TronSignTx.ref_block_bytes max_size:4
TronSignTx.ref_block_hash max_size:32
TronSignTx.contract_type max_size:64
TronSignTx.to_address max_size:35

TronSignedTx.signature max_size:65
2 changes: 2 additions & 0 deletions include/keepkey/transport/messages.options
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ FlashHash.challenge max_size:32
FlashWrite.data max_size:1024

FlashHashResponse.data max_size:32

Bip85Mnemonic.mnemonic max_size:241
3 changes: 3 additions & 0 deletions lib/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ set(sources
ripple_base58.c
signing.c
signtx_tendermint.c
solana.c
solana_msg.c
solana_tx.c
storage.c
tendermint.c
thorchain.c
Expand Down
4 changes: 4 additions & 0 deletions lib/firmware/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#include "keepkey/firmware/signing.h"
#include "keepkey/firmware/signtx_tendermint.h"
#include "keepkey/firmware/storage.h"
#include "keepkey/firmware/solana.h"
#include "keepkey/firmware/solana_tx.h"
#include "keepkey/firmware/tendermint.h"
#include "keepkey/firmware/thorchain.h"
#include "keepkey/firmware/transaction.h"
Expand Down Expand Up @@ -84,6 +86,7 @@
#include "messages-ripple.pb.h"
#include "messages-thorchain.pb.h"
#include "messages-mayachain.pb.h"
#include "messages-solana.pb.h"

#include <stdio.h>

Expand Down Expand Up @@ -284,3 +287,4 @@ void fsm_msgClearSession(ClearSession *msg) {
#include "fsm_msg_tendermint.h"
#include "fsm_msg_thorchain.h"
#include "fsm_msg_mayachain.h"
#include "fsm_msg_solana.h"
Loading
Loading