Skip to content

Commit de7f554

Browse files
fix: Parse create account instruction
1 parent 0116448 commit de7f554

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

apps/solana_app/solana_sign_txn.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,9 @@ static bool verify_solana_transfer_token_transaction() {
595595
const uint8_t *token_mint = solana_txn_context->transaction_info
596596
.instruction[transfer_instruction_index]
597597
.program.transfer_checked.token_mint;
598-
const solana_token_program_t *contract = NULL;
599-
if (!is_token_whitelisted(token_mint, &contract)) {
598+
solana_token_program_t contract = {0};
599+
const solana_token_program_t* contract_pointer = &contract;
600+
if (!is_token_whitelisted(token_mint, &contract_pointer)) {
600601
// Contract Unverifed, Display warning
601602
delay_scr_init(ui_text_unverified_token, DELAY_TIME);
602603

@@ -625,13 +626,16 @@ static bool verify_solana_transfer_token_transaction() {
625626
.decimal = token_decimals,
626627
};
627628

628-
contract = &empty_contract;
629+
memcpy(&contract, &empty_contract, sizeof(empty_contract));
630+
629631
} else {
632+
memcpy(&contract, contract_pointer, sizeof(contract));
633+
630634
char msg[100] = "";
631635
snprintf(msg,
632636
sizeof(msg),
633637
UI_TEXT_SEND_TOKEN_PROMPT,
634-
contract->symbol,
638+
contract.symbol,
635639
SOLANA_NAME);
636640
if (!core_confirmation(msg, solana_send_error)) {
637641
return false;
@@ -687,7 +691,7 @@ static bool verify_solana_transfer_token_transaction() {
687691

688692
byte_array_to_hex_string(be_units, 8, amount_string, sizeof(amount_string));
689693
if (!convert_byte_array_to_decimal_string(16,
690-
contract->decimal,
694+
contract.decimal,
691695
amount_string,
692696
amount_decimal_string,
693697
sizeof(amount_decimal_string))) {
@@ -699,7 +703,7 @@ static bool verify_solana_transfer_token_transaction() {
699703
sizeof(display),
700704
UI_TEXT_VERIFY_AMOUNT,
701705
amount_decimal_string,
702-
contract->symbol);
706+
contract.symbol);
703707
if (!core_confirmation(display, solana_send_error)) {
704708
return false;
705709
}

apps/solana_app/solana_txn_helpers.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
148148
utxn->blockhash = byte_array + offset;
149149
offset += SOLANA_BLOCKHASH_LENGTH;
150150

151-
// Instructions: Currently expecting count to be 3 or 4. TODO: Handle batch
152-
// instructions
151+
// Instructions: Currently expecting count to be 1 to 4, with only 1 transfer instruction.
152+
// TODO: Handle batch instructions
153153
offset += get_compact_array_size(
154154
byte_array + offset, &(utxn->instructions_count), &error);
155155
if (error != SOL_OK)
@@ -167,6 +167,10 @@ int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
167167
hex_string_to_byte_array(SOLANA_TOKEN_PROGRAM_ADDRESS,
168168
SOLANA_ACCOUNT_ADDRESS_LENGTH * 2,
169169
system_program_id[SOLANA_TOKEN_PROGRAM_ID_INDEX]);
170+
// Set Associated Token Program address
171+
hex_string_to_byte_array(SOLANA_ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
172+
SOLANA_ACCOUNT_ADDRESS_LENGTH * 2,
173+
system_program_id[SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID_INDEX]);
170174
// Set Compute Budget Program address
171175
hex_string_to_byte_array(
172176
SOLANA_COMPUTE_BUDGET_PROGRAM_ADDRESS,
@@ -200,10 +204,8 @@ int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
200204
SOLANA_ACCOUNT_ADDRESS_LENGTH,
201205
system_program_id[SOLANA_SOL_TRANSFER_PROGRAM_ID_INDEX],
202206
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
203-
if (utxn->instruction[i].account_addresses_index_count == 0)
204-
return SOL_D_MIN_LENGTH;
205-
206-
if (utxn->instruction[i].opaque_data_length == 0)
207+
if (utxn->instruction[i].account_addresses_index_count == 0
208+
|| utxn->instruction[i].opaque_data_length == 0)
207209
return SOL_D_MIN_LENGTH;
208210

209211
utxn->transfer_instruction_index = i;
@@ -233,7 +235,8 @@ int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
233235
SOLANA_ACCOUNT_ADDRESS_LENGTH,
234236
system_program_id[SOLANA_TOKEN_PROGRAM_ID_INDEX],
235237
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
236-
if (utxn->instruction[i].account_addresses_index_count == 0)
238+
if (utxn->instruction[i].account_addresses_index_count == 0
239+
|| utxn->instruction[i].opaque_data_length == 0)
237240
return SOL_D_MIN_LENGTH;
238241

239242
utxn->transfer_instruction_index = i;
@@ -268,6 +271,14 @@ int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
268271
default:
269272
break;
270273
}
274+
} else if (memcmp(utxn->account_addresses +
275+
utxn->instruction[i].program_id_index *
276+
SOLANA_ACCOUNT_ADDRESS_LENGTH,
277+
system_program_id[SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID_INDEX],
278+
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
279+
if (utxn->instruction[i].account_addresses_index_count == 0)
280+
return SOL_D_MIN_LENGTH;
281+
271282
} else if (memcmp(utxn->account_addresses +
272283
utxn->instruction[i].program_id_index *
273284
SOLANA_ACCOUNT_ADDRESS_LENGTH,
@@ -316,6 +327,10 @@ int solana_validate_unsigned_txn(const solana_unsigned_txn *utxn) {
316327
hex_string_to_byte_array(SOLANA_TOKEN_PROGRAM_ADDRESS,
317328
SOLANA_ACCOUNT_ADDRESS_LENGTH * 2,
318329
system_program_id[SOLANA_TOKEN_PROGRAM_ID_INDEX]);
330+
// Set Associated Token Program address
331+
hex_string_to_byte_array(SOLANA_ASSOCIATED_TOKEN_PROGRAM_ADDRESS,
332+
SOLANA_ACCOUNT_ADDRESS_LENGTH * 2,
333+
system_program_id[SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID_INDEX]);
319334
// Set Compute Budget Program address
320335
hex_string_to_byte_array(
321336
SOLANA_COMPUTE_BUDGET_PROGRAM_ADDRESS,
@@ -364,6 +379,14 @@ int solana_validate_unsigned_txn(const solana_unsigned_txn *utxn) {
364379
return SOL_V_UNSUPPORTED_INSTRUCTION;
365380
break;
366381
}
382+
} else if (memcmp(utxn->account_addresses +
383+
utxn->instruction[i].program_id_index *
384+
SOLANA_ACCOUNT_ADDRESS_LENGTH,
385+
system_program_id[SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID_INDEX],
386+
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
387+
// no opaque data or instruction enum to validate
388+
// do nothing
389+
367390
} else if (memcmp(utxn->account_addresses +
368391
utxn->instruction[i].program_id_index *
369392
SOLANA_ACCOUNT_ADDRESS_LENGTH,

apps/solana_app/solana_txn_helpers.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
#define SOLANA_ACCOUNT_ADDRESS_LENGTH 32
2929
#define SOLANA_BLOCKHASH_LENGTH 32
3030

31-
#define SOLANA_PROGRAM_ID_COUNT 3 ///< Number of supported program ids
31+
#define SOLANA_PROGRAM_ID_COUNT 4 ///< Number of supported program ids
3232
#define SOLANA_SOL_TRANSFER_PROGRAM_ID_INDEX 0
3333
#define SOLANA_TOKEN_PROGRAM_ID_INDEX 1
34-
#define SOLANA_COMPUTE_BUDGET_PROGRAM_ID_INDEX 2
34+
#define SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID_INDEX 2
35+
#define SOLANA_COMPUTE_BUDGET_PROGRAM_ID_INDEX 3
3536

3637
#define SOLANA_TOKEN_PROGRAM_ADDRESS \
3738
"06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9" ///< "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
@@ -91,6 +92,8 @@ enum SOLANA_TOKEN_PROGRAM_INSTRUCTION {
9192
STPI_UI_AMOUNT_TO_AMOUNT
9293
};
9394

95+
/// Ref :
96+
/// https://docs.rs/solana-sdk/latest/solana_sdk/compute_budget/enum.ComputeBudgetInstruction.html
9497
enum SOLANA_COMPUTE_BUDGET_INSTRUCTION {
9598
SCBI_UNUSED = 0,
9699
SCBI_REQUEST_HEAP_FRAME,
@@ -172,8 +175,8 @@ typedef struct solana_unsigned_txn {
172175
uint8_t *blockhash;
173176

174177
uint16_t instructions_count; // deserialization only supports max 4
175-
// instructions: create account and transfer
176-
solana_instruction instruction[4]; ///< Expects max 4 instructions
178+
// instructions: compute unit limit, compute unit price, create account and transfer
179+
solana_instruction instruction[4]; ///< Expects max 4 instructions: TODO: HANDLE ANY NUMBER/TYPE OF INSTRUCTIONS
177180
uint8_t transfer_instruction_index; // Expects only 1 transfer instruction
178181
uint32_t compute_unit_limit; // To calculate priority fee
179182
uint64_t compute_unit_price_micro_lamports;

0 commit comments

Comments
 (0)