Skip to content

Commit

Permalink
feat(defi): add clearsigning handlers (param + ui)
Browse files Browse the repository at this point in the history
  • Loading branch information
loicttn committed Jan 30, 2025
1 parent c3077a4 commit 967bc5c
Show file tree
Hide file tree
Showing 29 changed files with 1,350 additions and 66 deletions.
15 changes: 15 additions & 0 deletions src/contracts.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ static const uint32_t KILN_LR_DELEGATE_TO_SELECTOR = 0xeea9064b;
// --- cast sig "undelegate(address)"
static const uint32_t KILN_LR_UNDELEGATE_SELECTOR = 0xda8be864;

// DEFI - ERC20 Strategies
// --- cast sig "deposit(uint256,address)"
static const uint32_t KILN_DEFI_DEPOSIT_SELECTOR = 0x6e553f65;
// --- cast sig "mint(uint256,address)"
static const uint32_t KILN_DEFI_MINT_SELECTOR = 0x94bf804d;
// --- cast sig "withdraw(uint256,address,address)"
static const uint32_t KILN_DEFI_WITHDRAW_SELECTOR = 0xb460af94;
// --- cast sig "redeem(uint256,address,address)"
static const uint32_t KILN_DEFI_REDEEM_SELECTOR = 0xba087652;

const char ocv2_exit_queues[OCV2_MAX_EXIT_QUEUES][ADDRESS_STR_LEN] = {
"0x8d6Fd650500f82c7D978a440348e5a9b886943bF", // Kiln
"0x86358F7B33b599c484e0335B8Ee4f7f7f92d8b60" // Coinbase
Expand Down Expand Up @@ -125,4 +135,9 @@ const uint32_t KILN_SELECTORS[NUM_SELECTORS] = {
KILN_LR_COMPLETE_QUEUED_WITHDRAWALS_SELECTOR,
KILN_LR_DELEGATE_TO_SELECTOR,
KILN_LR_UNDELEGATE_SELECTOR,
// DEFI
KILN_DEFI_DEPOSIT_SELECTOR,
KILN_DEFI_MINT_SELECTOR,
KILN_DEFI_WITHDRAW_SELECTOR,
KILN_DEFI_REDEEM_SELECTOR,
};
13 changes: 13 additions & 0 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ void handle_finalize(ethPluginFinalize_t *msg) {
msg->numScreens = 1;
msg->result = ETH_PLUGIN_RESULT_OK;
break;

case KILN_DEFI_DEPOSIT:
case KILN_DEFI_MINT:
msg->numScreens = 2;
msg->result = ETH_PLUGIN_RESULT_OK;
break;

case KILN_DEFI_WITHDRAW:
case KILN_DEFI_REDEEM:
msg->numScreens = 3;
msg->result = ETH_PLUGIN_RESULT_OK;
break;

default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
13 changes: 13 additions & 0 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ void handle_init_contract(ethPluginInitContract_t *msg) {
context->next_param = LR_UNDELEGATE_ADDRESS;
break;

case KILN_DEFI_DEPOSIT:
context->next_param = DEFI_DEPOSIT_ASSETS_AMOUNT;
break;
case KILN_DEFI_MINT:
context->next_param = DEFI_MINT_SHARES_AMOUNT;
break;
case KILN_DEFI_WITHDRAW:
context->next_param = DEFI_WITHDRAW_ASSETS_AMOUNT;
break;
case KILN_DEFI_REDEEM:
context->next_param = DEFI_REDEEM_SHARES_AMOUNT;
break;

default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
7 changes: 7 additions & 0 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ void handle_query_contract_id(ethQueryContractID_t *msg) {
strlcpy(msg->version, "EigenLayer", msg->versionLength);
break;

case KILN_DEFI_DEPOSIT:
case KILN_DEFI_MINT:
case KILN_DEFI_WITHDRAW:
case KILN_DEFI_REDEEM:
strlcpy(msg->version, "DeFi", msg->versionLength);
break;

default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
71 changes: 70 additions & 1 deletion src/kiln_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@
// --- 15. delegateTo(address,(bytes,uint256),bytes32)
// --- 16. undelegate(address)
//
#define NUM_SELECTORS 17
// DEFI
// --- 17. deposit(uint256,address)
// --- 18. mint(uint256,address)
// --- 19. withdraw(uint256,address,address)
// --- 20. redeem(uint256,address,address)
//
#define NUM_SELECTORS 21
extern const uint32_t KILN_SELECTORS[NUM_SELECTORS];

// Selectors available (see mapping above).
Expand All @@ -78,6 +84,10 @@ typedef enum {
KILN_LR_COMPLETE_QUEUED_WITHDRAWALS,
KILN_LR_DELEGATE_TO,
KILN_LR_UNDELEGATE,
KILN_DEFI_DEPOSIT,
KILN_DEFI_MINT,
KILN_DEFI_WITHDRAW,
KILN_DEFI_REDEEM,
} selector_t;

// ****************************************************************************
Expand Down Expand Up @@ -332,6 +342,60 @@ typedef struct {
bitfield is_redelegated;
} lr_complete_queued_withdrawals_t;

// ****************************************************************************
// * DEFI
// ****************************************************************************

typedef enum {
DEFI_DEPOSIT_UNEXPECTED_PARAMETER = 0,
DEFI_DEPOSIT_ASSETS_AMOUNT,
DEFI_DEPOSIT_RECEIVER_ADDRESS,
} defi_deposit_parameters;

typedef enum {
DEFI_MINT_UNEXPECTED_PARAMETER = 0,
DEFI_MINT_SHARES_AMOUNT,
DEFI_MINT_RECEIVER_ADDRESS,
} defi_mint_parameters;

typedef enum {
DEFI_WITHDRAW_UNEXPECTED_PARAMETER = 0,
DEFI_WITHDRAW_ASSETS_AMOUNT,
DEFI_WITHDRAW_RECEIVER_ADDRESS,
DEFI_WITHDRAW_OWNER_ADDRESS,
} defi_withdraw_parameters;

typedef enum {
DEFI_REDEEM_UNEXPECTED_PARAMETER = 0,
DEFI_REDEEM_SHARES_AMOUNT,
DEFI_REDEEM_RECEIVER_ADDRESS,
DEFI_REDEEM_OWNER_ADDRESS,
} defi_redeem_parameters;

// ****************************************************************************

typedef struct {
uint8_t assets_amount[INT256_LENGTH];
char receiver_address[ADDRESS_STR_LEN];
} defi_deposit_t;

typedef struct {
uint8_t shares_amount[INT256_LENGTH];
char receiver_address[ADDRESS_STR_LEN];
} defi_mint_t;

typedef struct {
uint8_t assets_amount[INT256_LENGTH];
char receiver_address[ADDRESS_LENGTH];
char owner_address[ADDRESS_STR_LEN];
} defi_withdraw_t;

typedef struct {
uint8_t shares_amount[INT256_LENGTH];
char receiver_address[ADDRESS_LENGTH];
char owner_address[ADDRESS_STR_LEN];
} defi_redeem_t;

// ****************************************************************************
// * SHARED PLUGIN CONTEXT MEMORY
// ****************************************************************************
Expand All @@ -350,6 +414,11 @@ typedef struct context_t {
lr_delegate_to_t lr_delegate_to;
lr_queue_withdrawals_t lr_queue_withdrawals;
lr_complete_queued_withdrawals_t lr_complete_queued_withdrawals;

defi_deposit_t defi_deposit;
defi_mint_t defi_mint;
defi_withdraw_t defi_withdraw;
defi_redeem_t defi_redeem;
} param_data;

selector_t selectorIndex;
Expand Down
185 changes: 185 additions & 0 deletions src/provide_parameter/defi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*******************************************************************************
*
* ██╗ ██╗██╗██╗ ███╗ ██╗
* ██║ ██╔╝██║██║ ████╗ ██║
* █████╔╝ ██║██║ ██╔██╗ ██║
* ██╔═██╗ ██║██║ ██║╚██╗██║
* ██║ ██╗██║███████╗██║ ╚████║
* ╚═╝ ╚═╝╚═╝╚══════╝╚═╝ ╚═══╝
*
* Kiln Ethereum Ledger App
* (c) 2022-2024 Kiln
*
* [email protected]
********************************************************************************/

#include "kiln_plugin.h"

void handle_defi_deposit(ethPluginProvideParameter_t *msg, context_t *context) {
// **************************************************************************
// FUNCTION TO PARSE
// **************************************************************************
//
// function deposit(uint256 assets, address receiver)
//
// **************************************************************************
// example
// [0] selector
// [4] assets_amount
// [36] receiver_address
// **************************************************************************
defi_deposit_t *params = &context->param_data.defi_deposit;

switch (context->next_param) {
case DEFI_DEPOSIT_ASSETS_AMOUNT:
copy_parameter(params->assets_amount, msg->parameter, sizeof(params->assets_amount));
context->next_param = DEFI_DEPOSIT_RECEIVER_ADDRESS;
break;
case DEFI_DEPOSIT_RECEIVER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
char address_buffer[ADDRESS_STR_LEN];
getEthDisplayableAddress(buffer, address_buffer, sizeof(address_buffer), 0);
memcpy(params->receiver_address, address_buffer, sizeof(params->receiver_address));
break;
}
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}

void handle_defi_mint(ethPluginProvideParameter_t *msg, context_t *context) {
// **************************************************************************
// FUNCTION TO PARSE
// **************************************************************************
//
// function mint(uint256 shares, address receiver)
//
// **************************************************************************
// example
// [0] selector
// [4] shares_amount
// [36] receiver_address
// **************************************************************************
defi_mint_t *params = &context->param_data.defi_mint;

switch (context->next_param) {
case DEFI_MINT_SHARES_AMOUNT:
copy_parameter(params->shares_amount, msg->parameter, sizeof(params->shares_amount));
context->next_param = DEFI_MINT_RECEIVER_ADDRESS;
break;
case DEFI_MINT_RECEIVER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
getEthDisplayableAddress(buffer,
params->receiver_address,
sizeof(params->receiver_address),
0);
break;
}
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}

void handle_defi_withdraw(ethPluginProvideParameter_t *msg, context_t *context) {
// **************************************************************************
// FUNCTION TO PARSE
// **************************************************************************
//
// function withdraw(uint256 assets, address receiver, address owner)
//
// **************************************************************************
// example
// [0] selector
// [4] assets_amount
// [36] receiver_address
// [68] owner_address
// **************************************************************************
defi_withdraw_t *params = &context->param_data.defi_withdraw;

switch (context->next_param) {
case DEFI_WITHDRAW_ASSETS_AMOUNT:
copy_parameter(params->assets_amount, msg->parameter, sizeof(params->assets_amount));
context->next_param = DEFI_WITHDRAW_RECEIVER_ADDRESS;
break;
case DEFI_WITHDRAW_RECEIVER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
getEthDisplayableAddress(buffer,
params->receiver_address,
sizeof(params->receiver_address),
0);
context->next_param = DEFI_WITHDRAW_OWNER_ADDRESS;
break;
}
case DEFI_WITHDRAW_OWNER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
getEthDisplayableAddress(buffer,
params->owner_address,
sizeof(params->owner_address),
0);
break;
}
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}

void handle_defi_redeem(ethPluginProvideParameter_t *msg, context_t *context) {
// **************************************************************************
// FUNCTION TO PARSE
// **************************************************************************
//
// function redeem(uint256 shares, address receiver, address owner)
//
// **************************************************************************
// example
// [0] selector
// [4] shares_amount
// [36] receiver_address
// [68] owner_address
// **************************************************************************
defi_redeem_t *params = &context->param_data.defi_redeem;

switch (context->next_param) {
case DEFI_REDEEM_SHARES_AMOUNT:
copy_parameter(params->shares_amount, msg->parameter, sizeof(params->shares_amount));
context->next_param = DEFI_REDEEM_RECEIVER_ADDRESS;
break;
case DEFI_REDEEM_RECEIVER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
getEthDisplayableAddress(buffer,
params->receiver_address,
sizeof(params->receiver_address),
0);
context->next_param = DEFI_REDEEM_OWNER_ADDRESS;
break;
}
case DEFI_REDEEM_OWNER_ADDRESS: {
uint8_t buffer[ADDRESS_LENGTH];
copy_address(buffer, msg->parameter, sizeof(buffer));
getEthDisplayableAddress(buffer,
params->owner_address,
sizeof(params->owner_address),
0);
break;
}
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}
13 changes: 13 additions & 0 deletions src/provide_parameter/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ void handle_provide_parameter(ethPluginProvideParameter_t *msg) {
handle_lr_undelegate(msg, context);
break;

case KILN_DEFI_DEPOSIT:
handle_defi_deposit(msg, context);
break;
case KILN_DEFI_MINT:
handle_defi_mint(msg, context);
break;
case KILN_DEFI_WITHDRAW:
handle_defi_withdraw(msg, context);
break;
case KILN_DEFI_REDEEM:
handle_defi_redeem(msg, context);
break;

default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
5 changes: 5 additions & 0 deletions src/provide_parameter/provide_parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ void handle_lr_queue_withdrawals(ethPluginProvideParameter_t *msg, context_t *co
void handle_lr_complete_queued_withdrawals(ethPluginProvideParameter_t *msg, context_t *context);
void handle_lr_delegate_to(ethPluginProvideParameter_t *msg, context_t *context);
void handle_lr_undelegate(ethPluginProvideParameter_t *msg, context_t *context);

void handle_defi_deposit(ethPluginProvideParameter_t *msg, context_t *context);
void handle_defi_mint(ethPluginProvideParameter_t *msg, context_t *context);
void handle_defi_withdraw(ethPluginProvideParameter_t *msg, context_t *context);
void handle_defi_redeem(ethPluginProvideParameter_t *msg, context_t *context);
Loading

0 comments on commit 967bc5c

Please sign in to comment.