Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kobigurk feat/eth support #15

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APPVERSION_P=10
APPVERSION=$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)

# Celo
APP_LOAD_PARAMS += --path "44'/52752'"
APP_LOAD_PARAMS += --path "44'/52752'" --path "44'/60'/0'/0/0" --path "44'/60'/0'" --path "44'/60'/0'/0"
APPNAME = "Celo"
APP_LOAD_FLAGS=--appFlags 0
ifeq ($(TARGET_NAME),$(filter $(TARGET_NAME),TARGET_NANOX TARGET_STAX))
Expand Down
34 changes: 30 additions & 4 deletions src/celo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ static const uint8_t WITHDRAW_METHOD_ID[] = { 0x2e, 0x1a, 0x7d, 0x4d };
static const uint8_t RELOCK_METHOD_ID[] = { 0xb2, 0xfb, 0x30, 0xcb };
static const uint8_t CREATE_ACCOUNT_METHOD_ID[] = { 0x9d, 0xca, 0x36, 0x2f };

static uint32_t ALLOWED_CHAIN_IDS[4]={ 42220, 44787, 17323, 62320 };
#define NUM_ALLOWED_CHAIN_IDS 4

void io_seproxyhal_send_status(uint32_t sw) {
G_io_apdu_buffer[0] = ((sw >> 8) & 0xff);
G_io_apdu_buffer[1] = (sw & 0xff);
Expand Down Expand Up @@ -232,15 +235,16 @@ customStatus_e customProcessor(txContext_t *context) {
copyTxData(context,
dataContext.withdrawContext.data + context->currentFieldPos,
copySize);
break;
case PROVISION_RELOCK:
copyTxData(context,
dataContext.relockContext.data + context->currentFieldPos,
copySize);
break;
case PROVISION_CREATE_ACCOUNT:
copyTxData(context,
dataContext.createAccountContext.data + context->currentFieldPos,
copySize);

break;
default:
break;
Expand Down Expand Up @@ -331,9 +335,31 @@ void finalizeParsing(bool direct) {
uint32_t i;
uint8_t decimals = WEI_TO_ETHER;
uint8_t feeDecimals = WEI_TO_ETHER;
char *ticker = CHAINID_COINNAME " ";
char *feeTicker = CHAINID_COINNAME " ";
const char *ticker = CHAINID_COINNAME " ";
const char *feeTicker = CHAINID_COINNAME " ";
uint8_t tickerOffset = 0;
uint32_t v;
bool foundV = false;

// Check for allowed chain IDs
v = getV(&tmpContent.txContent);

for (i=0; i<NUM_ALLOWED_CHAIN_IDS; i++) {
if (v == ALLOWED_CHAIN_IDS[i]) {
foundV = true;
break;
}
}
if (!foundV) {
if (direct) {
THROW(0x6A80);
}
else {
io_seproxyhal_send_status(0x6A80);
ui_idle();
return;
}
}

// Display correct currency if fee currency field sent
if (tmpContent.txContent.feeCurrencyLength != 0) {
Expand Down Expand Up @@ -555,4 +581,4 @@ void finalizeParsing(bool direct) {
}
}
#endif // NO_CONSENT
}
}
2 changes: 1 addition & 1 deletion src/celo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void reset_app_context();
tokenDefinition_t* getKnownToken(uint8_t *tokenAddr);

customStatus_e customProcessor(txContext_t *context);
void initTx(txContext_t *context, cx_sha3_t *sha3, txContent_t *content, ustreamProcess_t customProcessor, void *extra);
void initTx(txContext_t *context, cx_sha3_t *sha3, txContent_t *content, ustreamProcess_t customProcessor, bool isEthereum, void *extra);
void finalizeParsing(bool direct);

// TODO: this should not be exposed
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void handleSign(uint8_t p1, uint8_t p2, const uint8_t *workBuffer, uint16_t data
dataPresent = false;
provisionType = PROVISION_NONE;
//0x8000003c is the Ethereum path
initTx(&txContext, &sha3, &tmpContent.txContent, customProcessor, NULL);
initTx(&txContext, &sha3, &tmpContent.txContent, customProcessor, tmpCtx.transactionContext.derivationPath.path[1] == 0x8000003c, NULL);
}
else
if (p1 != P1_MORE) {
Expand Down
15 changes: 12 additions & 3 deletions src_common/ethUstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,16 @@ static parserStatus_e processTxInternal(txContext_t *context) {
}
break;
case TX_RLP_FEECURRENCY:
if (processFeeCurrency(context)) {
return USTREAM_FAULT;
//if this is an Ethereum transaction, skip the Celo fields
if (context->isEthereum) {
context->currentField+=3;
if (processTo(context)) {
return USTREAM_FAULT;
}
} else {
if (processFeeCurrency(context)) {
return USTREAM_FAULT;
}
}
break;
case TX_RLP_GATEWAYTO:
Expand Down Expand Up @@ -542,11 +550,12 @@ parserStatus_e continueTx(txContext_t *context) {
}

void initTx(txContext_t *context, cx_sha3_t *sha3, txContent_t *content,
ustreamProcess_t customProcessor, void *extra) {
ustreamProcess_t customProcessor, bool isEthereum, void *extra) {
memset(context, 0, sizeof(txContext_t));
context->sha3 = sha3;
context->content = content;
context->customProcessor = customProcessor;
context->isEthereum = isEthereum;
context->extra = extra;
context->currentField = TX_RLP_CONTENT;
#ifndef TESTING
Expand Down
3 changes: 2 additions & 1 deletion src_common/ethUstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ typedef struct txContent_t {

typedef struct txContext_t {
rlpTxField_e currentField;
bool isEthereum;
cx_sha3_t *sha3;
uint32_t currentFieldLength;
uint32_t currentFieldPos;
Expand All @@ -110,7 +111,7 @@ typedef struct txContext_t {
} txContext_t;

void initTx(txContext_t *context, cx_sha3_t *sha3, txContent_t *content,
ustreamProcess_t customProcessor, void *extra);
ustreamProcess_t customProcessor, bool isEthereum, void *extra);
parserStatus_e processTx(txContext_t *context, const uint8_t *buffer, size_t length);
parserStatus_e continueTx(txContext_t *context);
int copyTxData(txContext_t *context, uint8_t *out, size_t length);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_tx_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void test_celo_tx_invalid_address(void **state) {
txContent_t content;
cx_sha3_t sha3;

initTx(&context, &sha3, &content, NULL, NULL);
initTx(&context, &sha3, &content, NULL, false, NULL);
assert_int_equal(processTx(&context, tx_data, sizeof(tx_data)), USTREAM_FAULT);
}

Expand All @@ -44,7 +44,7 @@ static void test_celo_tx(void **state) {
txContent_t content;
cx_sha3_t sha3;

initTx(&context, &sha3, &content, NULL, NULL);
initTx(&context, &sha3, &content, NULL, false, NULL);
assert_int_equal(processTx(&context, tx_data, sizeof(tx_data)), USTREAM_FINISHED);
assert_int_equal(content.destinationLength, MAX_ADDRESS);
assert_memory_equal(content.destination, to, MAX_ADDRESS);
Expand Down
Loading