Skip to content

Commit

Permalink
fix(build): Removed duplicate functions and updated submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
amanCypherock committed Dec 7, 2023
1 parent 39aa2f4 commit db2ebdb
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 271 deletions.
149 changes: 0 additions & 149 deletions common/coin_support/solana.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,151 +72,6 @@ size_t sol_get_derivation_depth(const uint16_t tag) {
}
}

uint16_t get_compact_array_size(const uint8_t *data,
uint16_t *size,
int *error) {
uint16_t offset = 0;
uint32_t value = 0;
*error = 0;

while (offset < 3) {
value |= (*(data + offset) & 0x7F) << offset * 7;
if ((*(data + offset) & 0x80) == 0)
break;
offset++;
}

if (value > UINT16_MAX)
*error = SOL_D_COMPACT_U16_OVERFLOW; // overflow

*size = value;
return offset + 1;
}

int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
uint16_t byte_array_size,
solana_unsigned_txn *utxn) {
if (byte_array == NULL || utxn == NULL)
return SOL_ERROR;
memzero(utxn, sizeof(solana_unsigned_txn));

uint16_t offset = 0;
int error = 0;

// Message header
utxn->required_signatures_count = *(byte_array + offset++);
utxn->read_only_accounts_require_signature_count = *(byte_array + offset++);
utxn->read_only_accounts_not_require_signature_count =
*(byte_array + offset++);

// Account addresses
offset += get_compact_array_size(
byte_array + offset, &(utxn->account_addresses_count), &error);
if (error != SOL_OK)
return error;
if (utxn->account_addresses_count == 0)
return SOL_D_MIN_LENGTH;

utxn->account_addresses = byte_array + offset;
offset += utxn->account_addresses_count * SOLANA_ACCOUNT_ADDRESS_LENGTH;

// Blockhash
utxn->blockhash = byte_array + offset;
offset += SOLANA_BLOCKHASH_LENGTH;

// Instructions: Currently expecting count to be only 1. TODO: Handle batch
// instructions
offset += get_compact_array_size(
byte_array + offset, &(utxn->instructions_count), &error);
if (error != SOL_OK)
return error;
if (utxn->instructions_count == 0)
return SOL_D_MIN_LENGTH;

utxn->instruction.program_id_index = *(byte_array + offset++);

offset +=
get_compact_array_size(byte_array + offset,
&(utxn->instruction.account_addresses_index_count),
&error);
if (error != SOL_OK)
return error;
if (utxn->instruction.account_addresses_index_count == 0)
return SOL_D_MIN_LENGTH;

utxn->instruction.account_addresses_index = byte_array + offset;
offset += utxn->instruction.account_addresses_index_count;
offset += get_compact_array_size(
byte_array + offset, &(utxn->instruction.opaque_data_length), &error);
if (error != SOL_OK)
return error;
if (utxn->instruction.opaque_data_length == 0)
return SOL_D_MIN_LENGTH;

utxn->instruction.opaque_data = byte_array + offset;
offset += utxn->instruction.opaque_data_length;

uint32_t instruction_enum = U32_READ_LE_ARRAY(utxn->instruction.opaque_data);

uint8_t system_program_id[SOLANA_ACCOUNT_ADDRESS_LENGTH] = {
0}; // System instruction address
if (memcmp(utxn->account_addresses + utxn->instruction.program_id_index *
SOLANA_ACCOUNT_ADDRESS_LENGTH,
system_program_id,
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
switch (instruction_enum) {
case SSI_TRANSFER: // transfer instruction
utxn->instruction.program.transfer.funding_account =
utxn->account_addresses +
(*(utxn->instruction.account_addresses_index + 0) *
SOLANA_ACCOUNT_ADDRESS_LENGTH);
utxn->instruction.program.transfer.recipient_account =
utxn->account_addresses +
(*(utxn->instruction.account_addresses_index + 1) *
SOLANA_ACCOUNT_ADDRESS_LENGTH);
utxn->instruction.program.transfer.lamports =
U64_READ_LE_ARRAY(utxn->instruction.opaque_data + 4);
break;

default:
break;
}
}

return ((offset <= byte_array_size) && (offset > 0))
? SOL_OK
: SOL_D_READ_SIZE_MISMATCH;
}

int solana_validate_unsigned_txn(const solana_unsigned_txn *utxn) {
if (utxn->instructions_count != 1)
return SOL_V_UNSUPPORTED_INSTRUCTION_COUNT;

if (!(0 < utxn->instruction.program_id_index &&
utxn->instruction.program_id_index < utxn->account_addresses_count))
return SOL_V_INDEX_OUT_OF_RANGE;

uint32_t instruction_enum = U32_READ_LE_ARRAY(utxn->instruction.opaque_data);

uint8_t system_program_id[SOLANA_ACCOUNT_ADDRESS_LENGTH] = {
0}; // System instruction address
if (memcmp(utxn->account_addresses + utxn->instruction.program_id_index *
SOLANA_ACCOUNT_ADDRESS_LENGTH,
system_program_id,
SOLANA_ACCOUNT_ADDRESS_LENGTH) == 0) {
switch (instruction_enum) {
case SSI_TRANSFER: // transfer instruction
break;
default:
return SOL_V_UNSUPPORTED_INSTRUCTION;
break;
}
} else {
return SOL_V_UNSUPPORTED_PROGRAM;
}
return SOL_OK;
}

void solana_sig_unsigned_byte_array(const uint8_t *unsigned_txn_byte_array,
uint64_t unsigned_txn_len,
const txn_metadata *transaction_metadata,
Expand Down Expand Up @@ -303,7 +158,3 @@ bool sol_verify_derivation_path(const uint32_t *path, uint8_t levels) {

return status;
}

uint8_t solana_get_decimal() {
return SOLANA_DECIMAL;
}
122 changes: 1 addition & 121 deletions common/coin_support/solana.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <stdint.h>

#include "coin_utils.h"
#include "solana_txn_helpers.h"

#define SOL_COIN_VERSION 0x00000000

Expand All @@ -31,76 +32,6 @@
#define SOLANA_ACCOUNT_ADDRESS_LENGTH 32
#define SOLANA_BLOCKHASH_LENGTH 32

// Reference:
// https://docs.rs/solana-program/1.14.3/solana_program/system_instruction/enum.SystemInstruction.html
enum SOLANA_SYSTEM_INSTRUCTION {
SSI_CREATE_ACCOUNT = 0,
SSI_ASSIGN,
SSI_TRANSFER,
SSI_CREATE_ACCOUNT_WITH_SEED,
SSI_ADVANCE_NONCE_ACCOUNT,
SSI_WITHDRAW_NONCE_ACCOUNT,
SSI_INITIALIZE_NONCE_ACCOUNT,
SSI_AUTHORIZE_NONCE_ACCOUNT,
SSI_ALLOCATE,
SSI_ALLOCATE_WITH_SEED,
SSI_ASSIGN_WITH_SEED,
SSI_TRANSFER_WITH_SEED,
SSI_UPGRADE_NONCE_ACCOUNT,
};

enum SOLANA_ERROR_CODES {
SOL_OK = 0,
SOL_ERROR,
SOL_D_MIN_LENGTH,
SOL_D_COMPACT_U16_OVERFLOW,
SOL_D_READ_SIZE_MISMATCH,
SOL_V_UNSUPPORTED_PROGRAM,
SOL_V_UNSUPPORTED_INSTRUCTION,
SOL_V_UNSUPPORTED_INSTRUCTION_COUNT,
SOL_V_INDEX_OUT_OF_RANGE,
SOL_BU_INVALID_BLOCKHASH,
};

// Reference :
// https://docs.rs/solana-program/1.14.3/solana_program/system_instruction/enum.SystemInstruction.html#variant.Transfer
typedef struct solana_transfer_data {
uint8_t *funding_account;
uint8_t *recipient_account;
uint64_t lamports;
} solana_transfer_data;

// Reference :
// https://docs.solana.com/developing/programming-model/transactions#instruction-format
typedef struct solana_instruction {
uint8_t program_id_index;
uint16_t account_addresses_index_count;
uint8_t *account_addresses_index;
uint16_t opaque_data_length;
uint8_t *opaque_data;
union {
solana_transfer_data transfer;
} program;
} solana_instruction;

// Reference :
// https://docs.solana.com/developing/programming-model/transactions#anatomy-of-a-transaction
typedef struct solana_unsigned_txn {
uint8_t required_signatures_count;
uint8_t read_only_accounts_require_signature_count;
uint8_t read_only_accounts_not_require_signature_count;

uint16_t account_addresses_count;
uint8_t *account_addresses;

uint8_t *blockhash;

uint16_t
instructions_count; // deserialization only supports single instruction
solana_instruction instruction;

} solana_unsigned_txn;

typedef enum solana_account_type {
UNUSED = 0,
SOL_ACC_TYPE1 = 1,
Expand All @@ -118,50 +49,6 @@ typedef enum solana_account_type {
*/
size_t sol_get_derivation_depth(uint16_t tag);

/**
* @brief Get the compact array size and number of bytes used to store the size
*
* @param data the compact array
* @param size the size of the compact array
* @return uint16_t number of bytes used to store the size
*/
uint16_t get_compact_array_size(const uint8_t *data,
uint16_t *size,
int *error);

/**
* @brief Convert byte array representation of unsigned transaction to
* solana_unsigned_txn.
* @details
*
* @param [in] byte_array Byte array of unsigned transaction.
* @param [in] byte_array_size Size of byte array.
* @param [out] utxn Pointer to the solana_unsigned_txn
* instance to store the transaction details.
*
* @return Status of conversion
* @retval 0 if successful
* @retval -1 if unsuccessful
*
* @see
* @since v1.0.0
*
* @note
*/
int solana_byte_array_to_unsigned_txn(uint8_t *byte_array,
uint16_t byte_array_size,
solana_unsigned_txn *utxn);

/**
* @brief Validate the deserialized unsigned transaction
*
* @param utxn Pointer to the solana_unsigned_txn instance to validate the
* transaction
* @return 0 if validation succeeded
* @return -1 if validation failed
*/
int solana_validate_unsigned_txn(const solana_unsigned_txn *utxn);

/**
* @brief Signed unsigned byte array.
* @details
Expand Down Expand Up @@ -217,11 +104,4 @@ int solana_update_blockhash_in_byte_array(uint8_t *byte_array,
*/
bool sol_verify_derivation_path(const uint32_t *path, uint8_t levels);

/**
* @brief Returns the decimal value of solana asset
*
* @return uint8_t decimal value
*/
uint8_t solana_get_decimal();

#endif // SOLANA_HEADER
2 changes: 1 addition & 1 deletion common/cypherock-common

0 comments on commit db2ebdb

Please sign in to comment.