address.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2016 Daira Hopwood
  3. * Copyright (c) 2016 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "address.h"
  24. #include "bignum.h"
  25. size_t address_prefix_bytes_len(uint32_t address_type) {
  26. if(address_type <= 0xFF) return 1;
  27. if(address_type <= 0xFFFF) return 2;
  28. if(address_type <= 0xFFFFFF) return 3;
  29. return 4;
  30. }
  31. void address_write_prefix_bytes(uint32_t address_type, uint8_t* out) {
  32. if(address_type > 0xFFFFFF) *(out++) = address_type >> 24;
  33. if(address_type > 0xFFFF) *(out++) = (address_type >> 16) & 0xFF;
  34. if(address_type > 0xFF) *(out++) = (address_type >> 8) & 0xFF;
  35. *(out++) = address_type & 0xFF;
  36. }
  37. bool address_check_prefix(const uint8_t* addr, uint32_t address_type) {
  38. if(address_type <= 0xFF) {
  39. return address_type == (uint32_t)(addr[0]);
  40. }
  41. if(address_type <= 0xFFFF) {
  42. return address_type == (((uint32_t)addr[0] << 8) | ((uint32_t)addr[1]));
  43. }
  44. if(address_type <= 0xFFFFFF) {
  45. return address_type ==
  46. (((uint32_t)addr[0] << 16) | ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
  47. }
  48. return address_type == (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
  49. ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
  50. }
  51. #if USE_ETHEREUM
  52. #include "sha3.h"
  53. void ethereum_address_checksum(const uint8_t* addr, char* address, bool rskip60, uint64_t chain_id) {
  54. const char* hex = "0123456789abcdef";
  55. address[0] = '0';
  56. address[1] = 'x';
  57. for(int i = 0; i < 20; i++) {
  58. address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
  59. address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
  60. }
  61. address[42] = 0;
  62. SHA3_CTX ctx = {0};
  63. uint8_t hash[32] = {0};
  64. keccak_256_Init(&ctx);
  65. if(rskip60) {
  66. char prefix[16] = {0};
  67. int prefix_size =
  68. bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0, prefix, sizeof(prefix));
  69. keccak_Update(&ctx, (const uint8_t*)prefix, prefix_size);
  70. }
  71. keccak_Update(&ctx, (const uint8_t*)(address + 2), 40);
  72. keccak_Final(&ctx, hash);
  73. for(int i = 0; i < 20; i++) {
  74. if((hash[i] & 0x80) && address[2 + i * 2] >= 'a' && address[2 + i * 2] <= 'f') {
  75. address[2 + i * 2] -= 0x20;
  76. }
  77. if((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' && address[2 + i * 2 + 1] <= 'f') {
  78. address[2 + i * 2 + 1] -= 0x20;
  79. }
  80. }
  81. }
  82. #endif