bip32.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(
  53. uint32_t depth,
  54. uint32_t child_num,
  55. const uint8_t* chain_code,
  56. const uint8_t* public_key,
  57. const char* curve,
  58. HDNode* out);
  59. int hdnode_from_xprv(
  60. uint32_t depth,
  61. uint32_t child_num,
  62. const uint8_t* chain_code,
  63. const uint8_t* private_key,
  64. const char* curve,
  65. HDNode* out);
  66. int hdnode_from_seed(const uint8_t* seed, int seed_len, const char* curve, HDNode* out);
  67. #define hdnode_private_ckd_prime(X, I) hdnode_private_ckd((X), ((I) | 0x80000000))
  68. int hdnode_private_ckd(HDNode* inout, uint32_t i);
  69. int hdnode_public_ckd_cp(
  70. const ecdsa_curve* curve,
  71. const curve_point* parent,
  72. const uint8_t* parent_chain_code,
  73. uint32_t i,
  74. curve_point* child,
  75. uint8_t* child_chain_code);
  76. int hdnode_public_ckd(HDNode* inout, uint32_t i);
  77. void hdnode_public_ckd_address_optimized(
  78. const curve_point* pub,
  79. const uint8_t* chain_code,
  80. uint32_t i,
  81. uint32_t version,
  82. HasherType hasher_pubkey,
  83. HasherType hasher_base58,
  84. char* addr,
  85. int addrsize,
  86. int addrformat);
  87. #if USE_BIP32_CACHE
  88. void bip32_cache_clear(void);
  89. int hdnode_private_ckd_cached(
  90. HDNode* inout,
  91. const uint32_t* i,
  92. size_t i_count,
  93. uint32_t* fingerprint);
  94. #endif
  95. uint32_t hdnode_fingerprint(HDNode* node);
  96. int hdnode_fill_public_key(HDNode* node);
  97. #if USE_ETHEREUM
  98. int hdnode_get_ethereum_pubkeyhash(const HDNode* node, uint8_t* pubkeyhash);
  99. #endif
  100. #if USE_NEM
  101. int hdnode_get_nem_address(HDNode* node, uint8_t version, char* address);
  102. int hdnode_get_nem_shared_key(
  103. const HDNode* node,
  104. const ed25519_public_key peer_public_key,
  105. const uint8_t* salt,
  106. ed25519_public_key mul,
  107. uint8_t* shared_key);
  108. int hdnode_nem_encrypt(
  109. const HDNode* node,
  110. const ed25519_public_key public_key,
  111. const uint8_t* iv,
  112. const uint8_t* salt,
  113. const uint8_t* payload,
  114. size_t size,
  115. uint8_t* buffer);
  116. int hdnode_nem_decrypt(
  117. const HDNode* node,
  118. const ed25519_public_key public_key,
  119. uint8_t* iv,
  120. const uint8_t* salt,
  121. const uint8_t* payload,
  122. size_t size,
  123. uint8_t* buffer);
  124. #endif
  125. int hdnode_sign(
  126. HDNode* node,
  127. const uint8_t* msg,
  128. uint32_t msg_len,
  129. HasherType hasher_sign,
  130. uint8_t* sig,
  131. uint8_t* pby,
  132. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  133. int hdnode_sign_digest(
  134. HDNode* node,
  135. const uint8_t* digest,
  136. uint8_t* sig,
  137. uint8_t* pby,
  138. int (*is_canonical)(uint8_t by, uint8_t sig[64]));
  139. int hdnode_get_shared_key(
  140. const HDNode* node,
  141. const uint8_t* peer_public_key,
  142. uint8_t* session_key,
  143. int* result_size);
  144. int hdnode_serialize_public(
  145. const HDNode* node,
  146. uint32_t fingerprint,
  147. uint32_t version,
  148. char* str,
  149. int strsize);
  150. int hdnode_serialize_private(
  151. const HDNode* node,
  152. uint32_t fingerprint,
  153. uint32_t version,
  154. char* str,
  155. int strsize);
  156. int hdnode_deserialize_public(
  157. const char* str,
  158. uint32_t version,
  159. const char* curve,
  160. HDNode* node,
  161. uint32_t* fingerprint);
  162. int hdnode_deserialize_private(
  163. const char* str,
  164. uint32_t version,
  165. const char* curve,
  166. HDNode* node,
  167. uint32_t* fingerprint);
  168. int hdnode_get_address_raw(HDNode* node, uint32_t version, uint8_t* addr_raw);
  169. int hdnode_get_address(HDNode* node, uint32_t version, char* addr, int addrsize);
  170. const curve_info* get_curve_by_name(const char* curve_name);
  171. #endif