address.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 == (((uint32_t)addr[0] << 16) |
  46. ((uint32_t)addr[1] << 8) | ((uint32_t)addr[2]));
  47. }
  48. return address_type ==
  49. (((uint32_t)addr[0] << 24) | ((uint32_t)addr[1] << 16) |
  50. ((uint32_t)addr[2] << 8) | ((uint32_t)addr[3]));
  51. }
  52. #if USE_ETHEREUM
  53. #include "sha3.h"
  54. void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
  55. uint64_t chain_id) {
  56. const char *hex = "0123456789abcdef";
  57. address[0] = '0';
  58. address[1] = 'x';
  59. for (int i = 0; i < 20; i++) {
  60. address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
  61. address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
  62. }
  63. address[42] = 0;
  64. SHA3_CTX ctx = {0};
  65. uint8_t hash[32] = {0};
  66. keccak_256_Init(&ctx);
  67. if (rskip60) {
  68. char prefix[16] = {0};
  69. int prefix_size = bn_format_uint64(chain_id, NULL, "0x", 0, 0, false, 0,
  70. prefix, sizeof(prefix));
  71. keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);
  72. }
  73. keccak_Update(&ctx, (const uint8_t *)(address + 2), 40);
  74. keccak_Final(&ctx, hash);
  75. for (int i = 0; i < 20; i++) {
  76. if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' &&
  77. address[2 + i * 2] <= 'f') {
  78. address[2 + i * 2] -= 0x20;
  79. }
  80. if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' &&
  81. address[2 + i * 2 + 1] <= 'f') {
  82. address[2 + i * 2 + 1] -= 0x20;
  83. }
  84. }
  85. }
  86. #endif