|
25 | 25 | #include "bignum.h"
|
26 | 26 |
|
27 | 27 | size_t address_prefix_bytes_len(uint32_t address_type) {
|
28 |
| - if (address_type <= 0xFF) return 1; |
29 |
| - if (address_type <= 0xFFFF) return 2; |
30 |
| - if (address_type <= 0xFFFFFF) return 3; |
31 |
| - return 4; |
| 28 | + if(address_type <= 0xFF) return 1; |
| 29 | + if(address_type <= 0xFFFF) return 2; |
| 30 | + if(address_type <= 0xFFFFFF) return 3; |
| 31 | + return 4; |
32 | 32 | }
|
33 | 33 |
|
34 |
| -void address_write_prefix_bytes(uint32_t address_type, uint8_t *out) { |
35 |
| - if (address_type > 0xFFFFFF) *(out++) = address_type >> 24; |
36 |
| - if (address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF; |
37 |
| - if (address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF; |
38 |
| - *(out++) = address_type & 0xFF; |
| 34 | +void address_write_prefix_bytes(uint32_t address_type, uint8_t* out) { |
| 35 | + if(address_type > 0xFFFFFF) *(out++) = address_type >> 24; |
| 36 | + if(address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF; |
| 37 | + if(address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF; |
| 38 | + *(out++) = address_type & 0xFF; |
39 | 39 | }
|
40 | 40 |
|
41 |
| -bool address_check_prefix(const uint8_t *addr, uint32_t address_type) { |
42 |
| - if (address_type <= 0xFF) { |
43 |
| - return address_type == (uint32_t)(addr[0]); |
44 |
| - } |
45 |
| - if (address_type <= 0xFFFF) { |
46 |
| - return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1])); |
47 |
| - } |
48 |
| - if (address_type <= 0xFFFFFF) { |
49 |
| - return address_type == (((uint32_t)addr[0] << 16) | |
50 |
| - ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2])); |
51 |
| - } |
52 |
| - return address_type == |
53 |
| - (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) | |
54 |
| - ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3])); |
| 41 | +bool address_check_prefix(const uint8_t* addr, uint32_t address_type) { |
| 42 | + if(address_type <= 0xFF) { |
| 43 | + return address_type == (uint32_t)(addr[0]); |
| 44 | + } |
| 45 | + if(address_type <= 0xFFFF) { |
| 46 | + return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1])); |
| 47 | + } |
| 48 | + if(address_type <= 0xFFFFFF) { |
| 49 | + return address_type == |
| 50 | + (((uint32_t)addr[0] << 16) | ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2])); |
| 51 | + } |
| 52 | + return address_type == (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) | |
| 53 | + ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3])); |
55 | 54 | }
|
56 | 55 |
|
57 | 56 | #if USE_ETHEREUM
|
58 | 57 | #include "sha3.h"
|
59 | 58 |
|
60 |
| -void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60, |
61 |
| - uint64_t chain_id) { |
62 |
| - const char *hex = "0123456789abcdef"; |
63 |
| - address[0] = '0'; |
64 |
| - address[1] = 'x'; |
65 |
| - for (int i = 0; i < 20; i++) { |
66 |
| - address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF]; |
67 |
| - address[2 + i * 2 + 1] = hex[addr[i] & 0xF]; |
68 |
| - } |
69 |
| - address[42] = 0; |
70 |
| - |
71 |
| - SHA3_CTX ctx = {0}; |
72 |
| - uint8_t hash[32] = {0}; |
73 |
| - keccak_256_Init(&ctx); |
74 |
| - if (rskip60) { |
75 |
| - char prefix[16] = {0}; |
76 |
| - int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0, |
77 |
| - prefix, sizeof(prefix)); |
78 |
| - keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size); |
79 |
| - } |
80 |
| - keccak_Update(&ctx, (const uint8_t *)(address + 2), 40); |
81 |
| - keccak_Final(&ctx, hash); |
| 59 | +void ethereum_address_checksum(const uint8_t* addr, char* address, bool rskip60, uint64_t chain_id) { |
| 60 | + const char* hex = "0123456789abcdef"; |
| 61 | + address[0] = '0'; |
| 62 | + address[1] = 'x'; |
| 63 | + for(int i = 0; i < 20; i++) { |
| 64 | + address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF]; |
| 65 | + address[2 + i * 2 + 1] = hex[addr[i] & 0xF]; |
| 66 | + } |
| 67 | + address[42] = 0; |
82 | 68 |
|
83 |
| - for (int i = 0; i < 20; i++) { |
84 |
| - if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' && |
85 |
| - address[2 + i * 2] <= 'f') { |
86 |
| - address[2 + i * 2] -= 0x20; |
| 69 | + SHA3_CTX ctx = {0}; |
| 70 | + uint8_t hash[32] = {0}; |
| 71 | + keccak_256_Init(&ctx); |
| 72 | + if(rskip60) { |
| 73 | + char prefix[16] = {0}; |
| 74 | + int prefix_size = |
| 75 | + bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0, prefix, sizeof(prefix)); |
| 76 | + keccak_Update(&ctx, (const uint8_t*)prefix, prefix_size); |
87 | 77 | }
|
88 |
| - if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' && |
89 |
| - address[2 + i * 2 + 1] <= 'f') { |
90 |
| - address[2 + i * 2 + 1] -= 0x20; |
| 78 | + keccak_Update(&ctx, (const uint8_t*)(address + 2), 40); |
| 79 | + keccak_Final(&ctx, hash); |
| 80 | + |
| 81 | + for(int i = 0; i < 20; i++) { |
| 82 | + if((hash[i] & 0x80) && address[2 + i * 2] >= 'a' && address[2 + i * 2] <= 'f') { |
| 83 | + address[2 + i * 2] -= 0x20; |
| 84 | + } |
| 85 | + if((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' && address[2 + i * 2 + 1] <= 'f') { |
| 86 | + address[2 + i * 2 + 1] -= 0x20; |
| 87 | + } |
91 | 88 | }
|
92 |
| - } |
93 | 89 | }
|
94 | 90 | #endif
|
0 commit comments