2016-10-10 09:22:03 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2016 Daira Hopwood
|
|
|
|
* Copyright (c) 2016 Pavol Rusnak
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "address.h"
|
2018-06-19 17:52:39 +00:00
|
|
|
#include "bignum.h"
|
2016-10-10 09:22:03 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
size_t address_prefix_bytes_len(uint32_t address_type) {
|
|
|
|
if (address_type <= 0xFF) return 1;
|
|
|
|
if (address_type <= 0xFFFF) return 2;
|
|
|
|
if (address_type <= 0xFFFFFF) return 3;
|
|
|
|
return 4;
|
2016-10-10 09:22:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void address_write_prefix_bytes(uint32_t address_type, uint8_t *out) {
|
|
|
|
if (address_type > 0xFFFFFF) *(out++) = address_type >> 24;
|
|
|
|
if (address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF;
|
|
|
|
if (address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF;
|
|
|
|
*(out++) = address_type & 0xFF;
|
2016-10-10 09:22:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
bool address_check_prefix(const uint8_t *addr, uint32_t address_type) {
|
|
|
|
if (address_type <= 0xFF) {
|
|
|
|
return address_type == (uint32_t)(addr[0]);
|
|
|
|
}
|
|
|
|
if (address_type <= 0xFFFF) {
|
|
|
|
return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1]));
|
|
|
|
}
|
|
|
|
if (address_type <= 0xFFFFFF) {
|
|
|
|
return address_type == (((uint32_t)addr[0] << 16) |
|
|
|
|
((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
|
|
|
|
}
|
|
|
|
return address_type ==
|
|
|
|
(((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
|
|
|
|
((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
|
2016-10-10 09:22:03 +00:00
|
|
|
}
|
2017-07-11 17:10:08 +00:00
|
|
|
|
|
|
|
#if USE_ETHEREUM
|
|
|
|
#include "sha3.h"
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
|
2021-08-31 09:29:30 +00:00
|
|
|
uint64_t chain_id) {
|
2019-03-29 14:44:55 +00:00
|
|
|
const char *hex = "0123456789abcdef";
|
2022-01-12 20:00:54 +00:00
|
|
|
address[0] = '0';
|
|
|
|
address[1] = 'x';
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2022-01-12 20:00:54 +00:00
|
|
|
address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
|
|
|
|
address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
2022-01-12 20:00:54 +00:00
|
|
|
address[42] = 0;
|
2018-06-19 17:52:39 +00:00
|
|
|
|
2019-10-02 14:31:36 +00:00
|
|
|
SHA3_CTX ctx = {0};
|
|
|
|
uint8_t hash[32] = {0};
|
2019-03-29 14:44:55 +00:00
|
|
|
keccak_256_Init(&ctx);
|
|
|
|
if (rskip60) {
|
2019-10-02 14:31:36 +00:00
|
|
|
char prefix[16] = {0};
|
2019-03-29 14:44:55 +00:00
|
|
|
int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false,
|
|
|
|
prefix, sizeof(prefix));
|
|
|
|
keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);
|
|
|
|
}
|
2022-01-12 20:00:54 +00:00
|
|
|
keccak_Update(&ctx, (const uint8_t *)(address + 2), 40);
|
2019-03-29 14:44:55 +00:00
|
|
|
keccak_Final(&ctx, hash);
|
2018-06-19 17:52:39 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2022-01-12 20:00:54 +00:00
|
|
|
if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' &&
|
|
|
|
address[2 + i * 2] <= 'f') {
|
|
|
|
address[2 + i * 2] -= 0x20;
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
2022-01-12 20:00:54 +00:00
|
|
|
if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' &&
|
|
|
|
address[2 + i * 2 + 1] <= 'f') {
|
|
|
|
address[2 + i * 2 + 1] -= 0x20;
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:10:08 +00:00
|
|
|
}
|
|
|
|
#endif
|