1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

ethereum: refactor hash_rlp_number

This commit is contained in:
Pavol Rusnak 2017-08-31 13:38:50 +02:00
parent 5f9cd15b02
commit ef89fc4e89
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -114,6 +114,27 @@ static void hash_rlp_field(const uint8_t *buf, size_t size)
hash_data(buf, size);
}
/*
* Push an RLP encoded number to the hash buffer.
* Ethereum yellow paper says to convert to big endian and strip leading zeros.
*/
static void hash_rlp_number(uint32_t number)
{
if (!number) {
return;
}
uint8_t data[4];
data[0] = (number >> 24) & 0xff;
data[1] = (number >> 16) & 0xff;
data[2] = (number >> 8) & 0xff;
data[3] = (number) & 0xff;
int offset = 0;
while (!data[offset]) {
offset++;
}
hash_rlp_field(data + offset, 4 - offset);
}
/*
* Calculate the number of bytes needed for an RLP length header.
* NOTE: supports up to 16MB of data (how unlikely...)
@ -134,7 +155,6 @@ static int rlp_calculate_length(int length, uint8_t firstbyte)
}
}
static void send_request_chunk(void)
{
int progress = 1000 - (data_total > 1000000
@ -160,17 +180,8 @@ static void send_signature(void)
/* eip-155 replay protection */
if (chain_id != 0) {
uint8_t data[4];
data[0] = (chain_id >> 24) & 0xff;
data[1] = (chain_id >> 16) & 0xff;
data[2] = (chain_id >> 8) & 0xff;
data[3] = (chain_id) & 0xff;
int offset = 0;
while (!data[offset]) {
offset++;
}
/* hash v=chain_id, r=0, s=0 */
hash_rlp_field(data+offset, 4-offset);
hash_rlp_number(chain_id);
hash_rlp_length(0, 0);
hash_rlp_length(0, 0);
}
@ -239,7 +250,6 @@ static void ethereumFormatAmount(const bignum256 *amnt, const TokenType *token,
case 42: suffix = " tETH"; break; // Ethereum Testnet: Kovan
case 2: suffix = " EXP"; break; // Expanse
case 8: suffix = " UBQ"; break; // UBIQ
case 7762959: suffix = " MUSIC"; break; // Musicoin
default: suffix = " UNKN"; break; // unknown chain
}
}