bip32.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 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. #ifndef __BIP32_H__
  24. #define __BIP32_H__
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include "ecdsa.h"
  29. #include "ed25519-donna/ed25519.h"
  30. #include "options.h"
  31. // Maximum length of a Base58Check-encoded extended public or private key.
  32. #define XPUB_MAXLEN 112
  33. // Maximum length of a Base58Check-encoded address.
  34. #define ADDRESS_MAXLEN 39
  35. typedef struct {
  36. const char *bip32_name; // string for generating BIP32 xprv from seed
  37. const ecdsa_curve *params; // ecdsa curve parameters, null for ed25519
  38. HasherType hasher_base58;
  39. HasherType hasher_sign;
  40. HasherType hasher_pubkey;
  41. HasherType hasher_script;
  42. } curve_info;
  43. typedef struct {
  44. uint32_t depth;
  45. uint32_t child_num;
  46. uint8_t chain_code[32];
  47. uint8_t private_key[32];
  48. uint8_t private_key_extension[32];
  49. uint8_t public_key[33];
  50. const curve_info *curve;
  51. } HDNode;
  52. int hdnode_from_xpub(uint32_t depth, uint32_t child_num,
  53. const uint8_t *chain_code, const uint8_t *public_key,
  54. const char *curve, HDNode *out);
  55. int hdnode_from_xprv(uint32_t depth, uint32_t child_num,
  56. const uint8_t *chain_code, const uint8_t *private_key,
  57. const char *curve, HDNode *out);
  58. int hdnode_from_seed(const uint8_t *seed, int seed_len, const char *curve,
  59. HDNode *out);
  60. #define hdnode_private_ckd_prime(X, I) \
  61. hdnode_private_ckd((X), ((I) | 0x80000000))
  62. int hdnode_private_ckd(HDNode *inout, uint32_t i);
  63. int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent,
  64. const uint8_t *parent_chain_code, uint32_t i,
  65. curve_point *child, uint8_t *child_chain_code);
  66. int hdnode_public_ckd(HDNode *inout, uint32_t i);
  67. void hdnode_public_ckd_address_optimized(const curve_point *pub,
  68. const uint8_t *chain_code, uint32_t i,
  69. uint32_t version,
  70. HasherType hasher_pubkey,
  71. HasherType hasher_base58, char *addr,
  72. int addrsize, int addrformat);
  73. #if USE_BIP32_CACHE
  74. void bip32_cache_clear(void);
  75. int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count,
  76. uint32_t *fingerprint);
  77. #endif
  78. uint32_t hdnode_fingerprint(HDNode *node);
  79. int hdnode_fill_public_key(HDNode *node);
  80. #if USE_ETHEREUM
  81. int hdnode_get_ethereum_pubkeyhash(const HDNode *node, uint8_t *pubkeyhash);
  82. #endif
  83. #if USE_NEM
  84. int hdnode_get_nem_address(HDNode *node, uint8_t version, char *address);
  85. int hdnode_get_nem_shared_key(const HDNode *node,
  86. const ed25519_public_key peer_public_key,
  87. const uint8_t *salt, ed25519_public_key mul,
  88. uint8_t *shared_key);
  89. int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key,
  90. const uint8_t *iv, const uint8_t *salt,
  91. const uint8_t *payload, size_t size, uint8_t *buffer);
  92. int hdnode_nem_decrypt(const HDNode *node, const ed25519_public_key public_key,
  93. uint8_t *iv, const uint8_t *salt, const uint8_t *payload,
  94. size_t size, uint8_t *buffer);
  95. #endif
  96. int hdnode_sign(HDNode *node, const uint8_t *msg, uint32_t msg_len,
  97. HasherType hasher_sign, uint8_t *sig, uint8_t *pby,
  98. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  99. int hdnode_sign_digest(HDNode *node, const uint8_t *digest, uint8_t *sig,
  100. uint8_t *pby,
  101. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  102. int hdnode_get_shared_key(const HDNode *node, const uint8_t *peer_public_key,
  103. uint8_t *session_key, int *result_size);
  104. int hdnode_serialize_public(const HDNode *node, uint32_t fingerprint,
  105. uint32_t version, char *str, int strsize);
  106. int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint,
  107. uint32_t version, char *str, int strsize);
  108. int hdnode_deserialize_public(const char *str, uint32_t version,
  109. const char *curve, HDNode *node,
  110. uint32_t *fingerprint);
  111. int hdnode_deserialize_private(const char *str, uint32_t version,
  112. const char *curve, HDNode *node,
  113. uint32_t *fingerprint);
  114. int hdnode_get_address_raw(HDNode *node, uint32_t version, uint8_t *addr_raw);
  115. int hdnode_get_address(HDNode *node, uint32_t version, char *addr,
  116. int addrsize);
  117. const curve_info *get_curve_by_name(const char *curve_name);
  118. #endif