coins.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the Trezor project, https://trezor.io/
  3. *
  4. * Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
  5. *
  6. * This library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "coins.h"
  20. #include <string.h>
  21. #include "../crypto/address.h"
  22. #include "../crypto/base58.h"
  23. #include "../crypto/ecdsa.h"
  24. const CoinInfo *coinByName(const char *name) {
  25. if (!name) return 0;
  26. for (int i = 0; i < COINS_COUNT; i++) {
  27. if (strcmp(name, coins[i].coin_name) == 0) {
  28. return &(coins[i]);
  29. }
  30. }
  31. return 0;
  32. }
  33. const CoinInfo *coinByAddressType(uint32_t address_type) {
  34. for (int i = 0; i < COINS_COUNT; i++) {
  35. if (address_type == coins[i].address_type) {
  36. return &(coins[i]);
  37. }
  38. }
  39. return 0;
  40. }
  41. const CoinInfo *coinBySlip44(uint32_t coin_type) {
  42. if (coin_type == SLIP44_TESTNET) {
  43. // The slip44 coin type is the same for all testnets, so we return the
  44. // Bitcoin Testnet.
  45. return coinByName("Testnet");
  46. }
  47. for (int i = 0; i < COINS_COUNT; i++) {
  48. if (coin_type == coins[i].coin_type) {
  49. return &(coins[i]);
  50. }
  51. }
  52. return 0;
  53. }
  54. bool coinExtractAddressType(const CoinInfo *coin, const char *addr,
  55. uint32_t *address_type) {
  56. if (!addr) return false;
  57. uint8_t addr_raw[MAX_ADDR_RAW_SIZE] = {0};
  58. int len = base58_decode_check(addr, coin->curve->hasher_base58, addr_raw,
  59. MAX_ADDR_RAW_SIZE);
  60. if (len >= 21) {
  61. return coinExtractAddressTypeRaw(coin, addr_raw, address_type);
  62. }
  63. return false;
  64. }
  65. bool coinExtractAddressTypeRaw(const CoinInfo *coin, const uint8_t *addr_raw,
  66. uint32_t *address_type) {
  67. if (address_check_prefix(addr_raw, coin->address_type)) {
  68. *address_type = coin->address_type;
  69. return true;
  70. }
  71. if (address_check_prefix(addr_raw, coin->address_type_p2sh)) {
  72. *address_type = coin->address_type_p2sh;
  73. return true;
  74. }
  75. *address_type = 0;
  76. return false;
  77. }