forked from jb55/clightning-dumpkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbip32.h
250 lines (223 loc) · 7.59 KB
/
bip32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef LIBWALLY_CORE_BIP32_H
#define LIBWALLY_CORE_BIP32_H
#include <stdint.h>
#include "secp256k1.h"
/** The required lengths of entropy for `bip32_key_from_seed` */
#define BIP32_ENTROPY_LEN_128 16
#define BIP32_ENTROPY_LEN_256 32
#define BIP32_ENTROPY_LEN_512 64
/** Length of an ext_key serialized using BIP32 format */
#define BIP32_SERIALIZED_LEN 78
/** Child number of the first hardened child */
#define BIP32_INITIAL_HARDENED_CHILD 0x80000000
/** Indicate that we want to derive a private key in `bip32_key_from_parent` */
#define BIP32_FLAG_KEY_PRIVATE 0x0
/** Indicate that we want to derive a public key in `bip32_key_from_parent` */
#define BIP32_FLAG_KEY_PUBLIC 0x1
/** Indicate that we want to skip hash calculation when deriving a key in `bip32_key_from_parent` */
#define BIP32_FLAG_SKIP_HASH 0x2
/** Version codes for extended keys */
#define BIP32_VER_MAIN_PUBLIC 0x0488B21E
#define BIP32_VER_MAIN_PRIVATE 0x0488ADE4
#define BIP32_VER_TEST_PUBLIC 0x043587CF
#define BIP32_VER_TEST_PRIVATE 0x04358394
/** An extended key */
struct ext_key {
/** The chain code for this key */
unsigned char chain_code[32];
/** The Hash160 of this keys parent */
unsigned char parent160[20];
/** The depth of this key */
uint8_t depth;
unsigned char pad1[10];
/** The private key with prefix byte 0 */
unsigned char priv_key[33];
/** The child number of the parent key that this key represents */
uint32_t child_num;
/** The Hash160 of this key */
unsigned char hash160[20];
/** The version code for this key indicating main/testnet and private/public */
uint32_t version;
unsigned char pad2[3];
/** The public key with prefix byte 0x2 or 0x3 */
unsigned char pub_key[33];
};
/**
* Free a key allocated by `bip32_key_from_seed_alloc`
* or `bip32_key_unserialize_alloc`.
*
* :param hdkey: Key to free.
*/
int bip32_key_free(const struct ext_key *hdkey);
/**
*/
int bip32_key_init_alloc(
const secp256k1_context *ctx,
uint32_t version, uint32_t depth, uint32_t child_num,
const unsigned char *chain_code, size_t chain_code_len,
const unsigned char *pub_key, size_t pub_key_len,
const unsigned char *priv_key, size_t priv_key_len,
const unsigned char *hash160, size_t hash160_len,
const unsigned char *parent160, size_t parent160_len,
struct ext_key **output
);
/**
* Create a new master extended key from entropy.
*
* This creates a new master key, i.e. the root of a new HD tree.
* The entropy passed in may produce an invalid key. If this happens,
* WALLY_ERROR will be returned and the caller should retry with
* new entropy.
*
* :param bytes: Entropy to use.
* :param bytes_len: Size of ``bytes`` in bytes. Must be one of ``BIP32_ENTROPY_LEN_128``,
*| ``BIP32_ENTROPY_LEN_256`` or ``BIP32_ENTROPY_LEN_512``.
* :param version: Either ``BIP32_VER_MAIN_PRIVATE`` or ``BIP32_VER_TEST_PRIVATE``,
*| indicating mainnet or testnet/regtest respectively.
* :param flags: Either ``BIP32_FLAG_SKIP_HASH`` to skip hash160 calculation, or 0.
* :param output: Destination for the resulting master extended key.
*/
int bip32_key_from_seed(
const secp256k1_context *ctx,
const unsigned char *bytes,
size_t bytes_len,
uint32_t version,
uint32_t flags,
struct ext_key *output);
/**
* As per `bip32_key_from_seed`, but allocates the key.
*
* .. note:: The returned key should be freed with `bip32_key_free`.
*/
int bip32_key_from_seed_alloc(
const secp256k1_context *ctx,
const unsigned char *bytes,
size_t bytes_len,
uint32_t version,
uint32_t flags,
struct ext_key **output);
/**
* Serialize an extended key to memory using BIP32 format.
*
* :param hdkey: The extended key to serialize.
* :param flags: BIP32_FLAG_KEY_ Flags indicating which key to serialize. You can not
*| serialize a private extended key from a public extended key.
* :param bytes_out: Destination for the serialized key.
* :param len: Size of ``bytes_out`` in bytes. Must be ``BIP32_SERIALIZED_LEN``.
*/
int bip32_key_serialize(
const struct ext_key *hdkey,
uint32_t flags,
unsigned char *bytes_out,
size_t len);
/**
* Un-serialize an extended key from memory.
*
* :param bytes: Storage holding the serialized key.
* :param bytes_len: Size of ``bytes`` in bytes. Must be ``BIP32_SERIALIZED_LEN``.
* :param output: Destination for the resulting extended key.
*/
int bip32_key_unserialize(
const secp256k1_context *ctx,
const unsigned char *bytes,
size_t bytes_len,
struct ext_key *output);
/**
* As per `bip32_key_unserialize`, but allocates the key.
*
* .. note:: The returned key should be freed with `bip32_key_free`.
*/
int bip32_key_unserialize_alloc(
const secp256k1_context *ctx,
const unsigned char *bytes,
size_t bytes_len,
struct ext_key **output);
/**
* Create a new child extended key from a parent extended key.
*
* :param hdkey: The parent extended key.
* :param child_num: The child number to create. Numbers greater
*| than or equal to ``BIP32_INITIAL_HARDENED_CHILD`` represent
*| hardened keys that cannot be created from public parent
*| extended keys.
* :param flags: BIP32_FLAG_KEY_ Flags indicating the type of derivation wanted.
*| You can not derive a private child extended key from a public
*| parent extended key.
* :param output: Destination for the resulting child extended key.
*/
int bip32_key_from_parent(
const secp256k1_context *ctx,
const struct ext_key *hdkey,
uint32_t child_num,
uint32_t flags,
struct ext_key *output);
/**
* As per `bip32_key_from_parent`, but allocates the key.
*
* .. note:: The returned key should be freed with `bip32_key_free`.
*/
int bip32_key_from_parent_alloc(
const secp256k1_context *ctx,
const struct ext_key *hdkey,
uint32_t child_num,
uint32_t flags,
struct ext_key **output);
/**
* Create a new child extended key from a parent extended key and a path.
*
* :param hdkey: The parent extended key.
* :param child_path: The path of child numbers to create.
* :param child_path_len: The number of child numbers in ``child_path``.
* :param flags: BIP32_KEY_ Flags indicating the type of derivation wanted.
* :param output: Destination for the resulting child extended key.
*/
int bip32_key_from_parent_path(
const secp256k1_context *ctx,
const struct ext_key *hdkey,
const uint32_t *child_path,
size_t child_path_len,
uint32_t flags,
struct ext_key *output);
/**
* As per `bip32_key_from_parent_path`, but allocates the key.
*
* .. note:: The returned key should be freed with `bip32_key_free`.
*/
int bip32_key_from_parent_path_alloc(
const secp256k1_context *ctx,
const struct ext_key *hdkey,
const uint32_t *child_path,
size_t child_path_len,
uint32_t flags,
struct ext_key **output);
/**
* Convert an extended key to base58.
*
* :param hdkey: The extended key.
* :param flags: BIP32_FLAG_KEY_ Flags indicating which key to serialize. You can not
*| serialize a private extended key from a public extended key.
* :param output: Destination for the resulting key in bas58.
*/
int bip32_key_to_base58(
const struct ext_key *hdkey,
uint32_t flags,
char **output);
/**
* Convert a base58 encoded extended key to an extended key.
*
* :param wif: The extended key in base58.
* :param output: Destination for the resulting extended key.
*/
int bip32_key_from_base58(
const secp256k1_context *ctx,
const char *base58,
struct ext_key *output);
/**
* As per `bip32_key_from_base58`, but allocates the key.
*
* .. note:: The returned key should be freed with `bip32_key_free`.
*/
int bip32_key_from_base58_alloc(
const secp256k1_context *ctx,
const char *base58, struct ext_key **output);
#endif /* LIBWALLY_CORE_BIP32_H */