OSDN Git Service

444779013ec59b9397a7e38554c48870eace62e1
[bytom/pybtm.git] / README.md
1 pybtm
2 ======
3
4 - [1 Installation](#1-installation)
5 - [2 Usage](#2-usage)
6   - [2.1 Create entropy](#21-create-entropy)
7   - [2.2 Create mnemonics](#22-create-mnemonics)
8   - [2.3 Create seed](#23-create-seed)
9   - [2.4 Create root expanded private key](#24-create-root-expanded-private-key)
10   - [2.5 Create expanded public key](#25-create-expanded-public-key)
11   - [2.6 Create expanded private key](#26-create-expanded-private-key)
12   - [2.7 Create public key](#27-create-public-key)
13   - [2.8 Create child expanded private key](#28-create-child-expanded-private-key)
14   - [2.9 Create child expanded public key](#29-create-child-expanded-public-key)
15   - [2.10 Sign message](#210-sign-message)
16   - [2.11 Verify signature](#211-verify-signature)
17   - [2.12 Create new key](#212-create-new-key)
18   - [2.13 Create HD path](#213-create-hd-path)
19
20 Python3 implementation of the Bytom protocol.
21
22 ## 1 Installation
23
24 ```
25 $ pip install pybtm
26 ```
27
28 ## 2 Usage
29
30 ### 2.1 Create entropy
31
32 get_entropy() create 128 bits entropy.
33
34 Return:
35
36 - entropy_hexstr: 128 bits entropy.
37
38 ```python
39 >>> from pybtm import key
40 >>> key.get_entropy()
41 '100e2704b431f914e3262926bdba6fce'
42 ```
43
44 ### 2.2 Create mnemonics
45
46 get_mnemonic create 12 new mnemonics.
47
48 Parameter:
49
50 - entropy_hexstr(optional): 128 bits entropy, type is hex string.
51
52 Return:
53
54 - mnemonic_str: 12 mnemonics.
55
56 ```python
57 >>> key.get_mnemonic('089fe9bf0cac76760bc4b131d938669e')
58 'ancient young hurt bone shuffle deposit congress normal crack six boost despair'
59 ```
60
61 If no paramater is specified, it will return 12 new random mnemonics.
62
63 ```python
64 >>> from pybtm import key
65 >>> key.get_mnemonic()
66 'nothing gate perfect glide wink lizard journey negative load quote wrong reason'
67 ```
68
69 ### 2.3 Create seed
70
71 get_seed create 512 bits seed from 12 mnemonics.
72
73 Parameter:
74
75 - mnemonic_str: 12 mnemonics.
76
77 Return:
78
79 - seed_hexstr: 512 bits seed, type is hex string.
80
81 ```python
82 >>> from pybtm import key
83 >>> key.get_seed('ancient young hurt bone shuffle deposit congress normal crack six boost despair')
84 'afa3a86bbec2f40bb32833fc6324593824c4fc7821ed32eac1f762b5893e56745f66a6c6f2588b3d627680aa4e0e50efd25065097b3daa8c6a19d606838fe7d4'
85 ```
86
87 ### 2.4 Create root expanded private key
88
89 get_root_xprv create root expanded private key.
90
91 Parameter:
92
93 - seed_hexstr: 512 bits seed, type is hex string.
94
95 Return:
96
97 - root_xprv_hexstring: 512 bits seed, type is hex string.
98
99 ```python
100 >>> from pybtm import key
101 >>> key.get_root_xprv('afa3a86bbec2f40bb32833fc6324593824c4fc7821ed32eac1f762b5893e56745f66a6c6f2588b3d627680aa4e0e50efd25065097b3daa8c6a19d606838fe7d4')
102 '302a25c7c0a68a83fa043f594a2db8b44bc871fced553a8a33144b31bc7fb84887c9e75915bb6ba3fd0b9f94a60b7a5897ab9db6a48f888c2559132dba9152b0'
103 ```
104
105 ### 2.5 Create expanded public key
106
107 get_xpub create expanded public key.
108
109 Parameter:
110
111 - xprv_hexstr: 512 bits expanded private key, type is hex string.
112
113 Return:
114
115 - xpub_hexstr: 512 bits expanded public key, type is hex string.
116
117 ```python
118 >>> from pybtm import key
119 >>> xprv_hexstr = 'c003f4bcccf9ad6f05ad2c84fa5ff98430eb8e73de5de232bc29334c7d074759d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c'
120 >>> key.get_xpub(xprv_hexstr)
121 '1b0541a7664cee929edb54d9ef21996b90546918a920a77e1cd6015d97c56563d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c'
122 ```
123
124 ### 2.6 Create expanded private key
125
126 get_expanded_private_key create expanded private key.
127
128 Parameter:
129
130 - xprv_hexstr: 512 bits expanded private key, type is hex string.
131
132 Return:
133
134 - expanded_private_key_hexstr: 512 bits expanded private key, type is hex string.
135
136 ```python
137 >>> from pybtm import key
138 >>> xprv_hexstr = '406c82307bf7978d17f3ecfeea7705370e9faef2027affa86c8027c6e11a8a50e231e65bd97048850ae6c39d0f46b63ae70aa24f5aac7877727c430c2201e6d6'
139 >>> key.get_expanded_private_key(xprv_hexstr)
140 '406c82307bf7978d17f3ecfeea7705370e9faef2027affa86c8027c6e11a8a50d828bf44b1a109c2bbb4c72685858e2f2ab8b405beef1e4ecc12d1ed8511e8eb'
141 ```
142
143 ### 2.7 Create public key
144
145 get_public_key create 32 bytes public key.
146
147 Parameter:
148
149 - xpub_hexstr: 512 bits expanded public key, type is hex string.
150
151 Return:
152
153 - public_key_hexstr: 256 bits public key, type is hex string.
154
155 ```python
156 >>> from pybtm import key
157 >>> xpub_hexstr = 'ecc2bbb6c0492873cdbc81edf56bd896d3b644047879840e357be735b7fa7b6f4af1be7b8d71cc649ac4ca3816f9ccaf11bf49f4effb845f3c19e16eaf8bfcda'
158 >>> key.get_public_key(xpub_hexstr)
159 'ecc2bbb6c0492873cdbc81edf56bd896d3b644047879840e357be735b7fa7b6f'
160 ```
161
162 ### 2.8 Create child expanded private key
163
164 get_child_xprv create child private key.
165
166 Parameter:
167
168 - xprv_hexstr: 512 bits expanded private key, type is hex string.
169 - path_list: 010203 7906a1
170
171 Return:
172
173 - child_xprv_hexstr: 512 bits private key, type is hex string.
174
175 ```python
176 >>> from pybtm import key
177 >>> xprv_hexstr = 'c003f4bcccf9ad6f05ad2c84fa5ff98430eb8e73de5de232bc29334c7d074759d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c'
178 >>> path_list = ['010203', '7906a1']
179 >>> key.get_child_xprv(xprv_hexstr, path_list)
180 '4853a0b00bdcb139e85855d9594e5f641b65218db7c50426946511397e094759bd9de7f2dcad9d7d45389bc94baecaec88aabf58f6e1d832b1f9995a93ec37ea'
181 ```
182
183 ### 2.9 Create child expanded public key
184
185 get_child_xpub create child public key.
186
187 Parameter:
188
189 - xpub_hexstr: 512 bits expanded public key, type is hex string.
190 - path_list: 010203 7906a1
191
192 Return:
193
194 - child_xpub_hexstr: 512 bits public key, type is hex string.
195
196 ```python
197 >>> from pybtm import key
198 >>> xpub_hexstr = '1b0541a7664cee929edb54d9ef21996b90546918a920a77e1cd6015d97c56563d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c'
199 >>> path_list = ['010203', '7906a1']
200 >>> key.get_child_xpub(xpub_hexstr, path_list)
201 'e65c1a9714e2116c6e5d57dee188a53b98dc901a21def5a5ca46fcf78303f4f2bd9de7f2dcad9d7d45389bc94baecaec88aabf58f6e1d832b1f9995a93ec37ea'
202 ```
203
204 ### 2.10 Sign message
205
206 xprv_sign sign message.
207
208 Parameter:
209
210 - xprv_hexstr: 512 bits expanded private key, type is hex string.
211 - message_hexstr: message, type is hex string.
212
213 Return:
214
215 - signature_hexstr: 512 bits signature, type is hex string.
216
217 ```python
218 >>> from pybtm import key
219 >>> xprv_hexstr = '88c0c40fb54ef9c1b90af8cce8dc4c9d54f915074dde93f79ab61cedae03444101ff37ac4a07869214c2735bba0175e001abe608db18538e083e1e44430a273b'
220 >>> message_hexstr = '1246b84985e1ab5f83f4ec2bdf271114666fd3d9e24d12981a3c861b9ed523c6'
221 >>> key.xprv_sign(xprv_hexstr, message_hexstr)
222 'ab18f49b23d03295bc2a3f2a7d5bb53a2997bed733e1fc408b50ec834ae7e43f7da40fe5d9d50f6ef2d188e1d27f976aa2586cef1ba00dd098b5c9effa046306'
223 ```
224
225 ### 2.11 Verify signature
226
227 xpub_verify verify signature.
228
229 Parameter:
230
231 - xpub_hexstr: 512 bits expanded public key, type is hex string.
232 - message_hexstr: message, type is hex string.
233 - signature_hexstr: 512 bits signature, type is hex string.
234
235 Return:
236
237 - result: True or False.
238
239 ```python
240 >>> from pybtm import key
241 >>> xpub_hexstr = 'cb22ce197d342d6bb440b0bf13ddd674f367275d28a00f893d7f0b10817690fd01ff37ac4a07869214c2735bba0175e001abe608db18538e083e1e44430a273b'
242 >>> message_hexstr = '1246b84985e1ab5f83f4ec2bdf271114666fd3d9e24d12981a3c861b9ed523c6'
243 >>> signature_hexstr = 'ab18f49b23d03295bc2a3f2a7d5bb53a2997bed733e1fc408b50ec834ae7e43f7da40fe5d9d50f6ef2d188e1d27f976aa2586cef1ba00dd098b5c9effa046306'
244 >>> key.xpub_verify(xpub_hexstr, message_hexstr, signature_hexstr)
245 True
246 ```
247
248 ### 2.12 Create new key
249
250 get_new_key create new key.
251
252 Parameter:
253
254 - entropy_hexstr(optional): 128 bits entropy, type is hex string.
255 - mnemonic_str(optional): 12 mnemonics.
256
257 Return:
258
259 - entropy: 128 bits entropy.
260 - mnemonic: 12 mnemonics.
261 - seed: 512 bits seed.
262 - xprv: 512 bits expaneded private key.
263 - xpub: 512 bits expaneded public key.
264 - xprv_base64: xprv hex string qrcode base64.
265
266 ```python
267 >>> from pybtm import key
268 >>> r = key.get_new_key()
269 >>> r['entropy']
270 '8466b1128f92051361c9aa2de52d1bb0'
271 >>> r['mnemonic']
272 'love culture dwarf busy cake meadow mango crystal combine city eight genuine'
273 >>> r['seed']
274 '4d15bf0f72bad754987fdcd0628ea37af03ac24666019c6d362e0200c9b49bee35aa0a788ed09e3a86cd529df0a1c20ea6aa719cf1e0da4ffb15efbc38fba498'
275 >>> r['xprv']
276 'f09ad64c2714b45e23c75e4541ad771def99b97e6da16b0cc6bcdac045f4d34745b62093173fd8f9a67e1da4b81233bc947880b6ed4b9641cf8f5223212fa18d'
277 >>> r['xpub']
278 'ebcc4b14444adb207dd53fd89b2881b21e839de42a1b6687a5a9d83b82c1b5b645b62093173fd8f9a67e1da4b81233bc947880b6ed4b9641cf8f5223212fa18d'
279 >>> r['xprv_base64']
280 {'base64': '/9j/4AAQSkZJRgABAQAAAQABAAD...'}
281 ```
282
283 ```python
284 >>> from pybtm import key
285 >>> r = key.get_new_key(entropy_hexstr='4d33735a9e92f634d22aecbb4044038d')
286 >>> r['entropy']
287 '4d33735a9e92f634d22aecbb4044038d'
288 >>> r['mnemonic']
289 'essay oppose stove diamond control bounce emerge frown robust acquire abstract brick'
290 ```
291
292 ```python
293 >>> from pybtm import key
294 >>> r = key.get_new_key(mnemonic_str='ancient young hurt bone shuffle deposit congress normal crack six boost despair')
295 >>> r['entropy']
296 ''
297 >>> r['mnemonic']
298 'ancient young hurt bone shuffle deposit congress normal crack six boost despair'
299 >>> r['seed']
300 'afa3a86bbec2f40bb32833fc6324593824c4fc7821ed32eac1f762b5893e56745f66a6c6f2588b3d627680aa4e0e50efd25065097b3daa8c6a19d606838fe7d4'
301 ```
302
303 ### 2.13 Create HD path
304
305 get_path_from_index create HD path.
306
307 ```python
308 >>> from pybtm import receiver
309 >>> account_index_int = 1
310 >>> address_index_int = 1
311 >>> change_bool = True
312 >>> receiver.get_path_from_index(account_index_int, address_index_int, change_bool)
313 {'path': ['2c000000', '99000000', '01000000', '01000000', '01000000'], 'path_str': 'm/44/153/1/1/1'}
314 ```
315