2020-06-27 13:24:04 +02:00
|
|
|
#include "shared_context.h"
|
|
|
|
|
#include "apdu_constants.h"
|
2024-01-15 17:09:30 +01:00
|
|
|
#include "public_keys.h"
|
2022-08-24 09:25:01 +02:00
|
|
|
#include "common_ui.h"
|
|
|
|
|
#include "os_io_seproxyhal.h"
|
2024-02-05 10:21:54 +01:00
|
|
|
#include "network.h"
|
2024-05-07 17:20:20 +02:00
|
|
|
#include "manage_asset_info.h"
|
2024-06-11 10:55:11 +02:00
|
|
|
#ifdef HAVE_LEDGER_PKI
|
|
|
|
|
#include "os_pki.h"
|
|
|
|
|
#endif
|
2020-06-27 13:24:04 +02:00
|
|
|
|
2020-12-01 16:20:13 +01:00
|
|
|
void handleProvideErc20TokenInformation(uint8_t p1,
|
|
|
|
|
uint8_t p2,
|
2022-07-15 12:33:19 +02:00
|
|
|
const uint8_t *workBuffer,
|
2022-07-19 11:42:25 +02:00
|
|
|
uint8_t dataLength,
|
2020-12-01 16:20:13 +01:00
|
|
|
unsigned int *flags,
|
2022-07-19 11:42:25 +02:00
|
|
|
unsigned int *tx) {
|
2020-12-01 16:20:13 +01:00
|
|
|
UNUSED(p1);
|
|
|
|
|
UNUSED(p2);
|
|
|
|
|
UNUSED(flags);
|
2022-07-19 11:42:25 +02:00
|
|
|
UNUSED(tx);
|
2020-12-01 16:20:13 +01:00
|
|
|
uint32_t offset = 0;
|
|
|
|
|
uint8_t tickerLength;
|
2024-02-05 10:21:54 +01:00
|
|
|
uint64_t chain_id;
|
2021-04-30 18:22:15 +02:00
|
|
|
uint8_t hash[INT256_LENGTH];
|
2024-05-07 17:20:20 +02:00
|
|
|
tokenDefinition_t *token = &get_current_asset_info()->token;
|
2024-06-11 10:55:11 +02:00
|
|
|
cx_err_t error = CX_INTERNAL_ERROR;
|
2020-12-01 16:20:13 +01:00
|
|
|
|
2024-05-07 17:37:27 +02:00
|
|
|
PRINTF("Provisioning currentAssetIndex %d\n", tmpCtx.transactionContext.currentAssetIndex);
|
2020-12-01 16:20:13 +01:00
|
|
|
|
|
|
|
|
if (dataLength < 1) {
|
2024-06-11 10:55:11 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
tickerLength = workBuffer[offset++];
|
|
|
|
|
dataLength--;
|
2023-12-18 17:26:53 +01:00
|
|
|
if ((tickerLength + 1) > sizeof(token->ticker)) {
|
2024-06-11 10:55:11 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
if (dataLength < tickerLength + 20 + 4 + 4) {
|
2024-06-11 10:55:11 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
cx_hash_sha256(workBuffer + offset, tickerLength + 20 + 4 + 4, hash, 32);
|
|
|
|
|
memmove(token->ticker, workBuffer + offset, tickerLength);
|
2023-01-10 14:12:39 +01:00
|
|
|
token->ticker[tickerLength] = '\0';
|
2020-12-01 16:20:13 +01:00
|
|
|
offset += tickerLength;
|
|
|
|
|
dataLength -= tickerLength;
|
|
|
|
|
memmove(token->address, workBuffer + offset, 20);
|
|
|
|
|
offset += 20;
|
|
|
|
|
dataLength -= 20;
|
2024-05-07 17:37:27 +02:00
|
|
|
// TODO: 4 bytes for this is overkill
|
2020-12-01 16:20:13 +01:00
|
|
|
token->decimals = U4BE(workBuffer, offset);
|
|
|
|
|
offset += 4;
|
|
|
|
|
dataLength -= 4;
|
2024-05-07 17:37:27 +02:00
|
|
|
// TODO: Handle 64-bit long chain IDs
|
2024-02-05 10:21:54 +01:00
|
|
|
chain_id = U4BE(workBuffer, offset);
|
|
|
|
|
if (!app_compatible_with_chain_id(&chain_id)) {
|
|
|
|
|
UNSUPPORTED_CHAIN_ID_MSG(chain_id);
|
2024-06-11 10:55:11 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
offset += 4;
|
|
|
|
|
dataLength -= 4;
|
2020-10-07 16:56:40 +02:00
|
|
|
|
2024-06-11 10:55:11 +02:00
|
|
|
error = check_signature_with_pubkey("ERC20 Token Info",
|
|
|
|
|
hash,
|
|
|
|
|
sizeof(hash),
|
|
|
|
|
LEDGER_SIGNATURE_PUBLIC_KEY,
|
|
|
|
|
sizeof(LEDGER_SIGNATURE_PUBLIC_KEY),
|
|
|
|
|
#ifdef HAVE_LEDGER_PKI
|
|
|
|
|
CERTIFICATE_PUBLIC_KEY_USAGE_COIN_META,
|
2021-07-09 11:16:23 +02:00
|
|
|
#endif
|
2024-06-11 10:55:11 +02:00
|
|
|
(uint8_t *) (workBuffer + offset),
|
|
|
|
|
dataLength);
|
|
|
|
|
#ifndef HAVE_BYPASS_SIGNATURES
|
|
|
|
|
if (error != CX_OK) {
|
|
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-06-27 13:24:04 +02:00
|
|
|
}
|
2024-06-11 10:55:11 +02:00
|
|
|
#endif
|
2020-06-27 13:24:04 +02:00
|
|
|
|
2024-05-07 18:30:19 +02:00
|
|
|
G_io_apdu_buffer[0] = tmpCtx.transactionContext.currentAssetIndex;
|
2024-05-07 17:20:20 +02:00
|
|
|
validate_current_asset_info();
|
2024-05-07 18:30:19 +02:00
|
|
|
U2BE_ENCODE(G_io_apdu_buffer, 1, APDU_RESPONSE_OK);
|
|
|
|
|
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 3);
|
2020-06-27 13:24:04 +02:00
|
|
|
}
|