Python3 implementation of the Bytom protocol. https://pypi.org/project/pybtm/
修订版 | 779ddc047d820e119fdaf6cdb9c910fed3a0a58a (tree) |
---|---|
时间 | 2019-03-26 20:07:22 |
作者 | Chengcheng Zhang <943420582@qq.c...> |
Commiter | Chengcheng Zhang |
fix bug
@@ -1,2 +1,2 @@ | ||
1 | 1 | name = "pybtm" |
2 | -version = "0.0.9" | |
\ No newline at end of file | ||
2 | +version = "0.0.14" | |
\ No newline at end of file |
@@ -206,7 +206,7 @@ def prune_intermediate_scalar(f): | ||
206 | 206 | def get_child_xprv(xprv_hexstr, path_list): |
207 | 207 | for i in range(len(path_list)): |
208 | 208 | selector_bytes = bytes.fromhex(path_list[i]) |
209 | - xpub_hexstr = xprv_to_xpub(xprv_hexstr)['xpub'] | |
209 | + xpub_hexstr = get_xpub(xprv_hexstr) | |
210 | 210 | xpub_bytes = bytes.fromhex(xpub_hexstr) |
211 | 211 | xprv_bytes = bytes.fromhex(xprv_hexstr) |
212 | 212 | hc_bytes = hmac.HMAC(xpub_bytes[32:], b'N'+xpub_bytes[:32]+selector_bytes, digestmod=hashlib.sha512).digest() |
@@ -305,7 +305,7 @@ def get_child_xpub(xpub_hexstr, path_list): | ||
305 | 305 | # message_hexstr: 1246b84985e1ab5f83f4ec2bdf271114666fd3d9e24d12981a3c861b9ed523c6 |
306 | 306 | # signature_hexstr: ab18f49b23d03295bc2a3f2a7d5bb53a2997bed733e1fc408b50ec834ae7e43f7da40fe5d9d50f6ef2d188e1d27f976aa2586cef1ba00dd098b5c9effa046306 |
307 | 307 | def xprv_sign(xprv_hexstr, message_hexstr): |
308 | - xprv_hexstr = xprv_to_expanded_private_key(xprv_hexstr)['expanded_private_key'] | |
308 | + xprv_hexstr = get_expanded_private_key(xprv_hexstr) | |
309 | 309 | xprv_bytes = bytes.fromhex(xprv_hexstr) |
310 | 310 | message_bytes = bytes.fromhex(message_hexstr) |
311 | 311 | data_bytes = xprv_bytes[32:64] + message_bytes |
@@ -317,7 +317,7 @@ def xprv_sign(xprv_hexstr, message_hexstr): | ||
317 | 317 | |
318 | 318 | scalar = decodeint(message_digest_reduced) |
319 | 319 | encoded_r = encodepoint(scalarmultbase(scalar)) |
320 | - xpub_hexstr = xprv_to_xpub(xprv_hexstr)['xpub'] | |
320 | + xpub_hexstr = get_xpub(xprv_hexstr) | |
321 | 321 | xpub_bytes = bytes.fromhex(xpub_hexstr) |
322 | 322 | hram_digest_data = encoded_r + xpub_bytes[:32] + message_bytes |
323 | 323 |
@@ -357,16 +357,16 @@ def xprv_sign(xprv_hexstr, message_hexstr): | ||
357 | 357 | # signature_hexstr: ab18f49b23d03295bc2a3f2a7d5bb53a2997bed733e1fc408b50ec834ae7e43f7da40fe5d9d50f6ef2d188e1d27f976aa2586cef1ba00dd098b5c9effa046306 |
358 | 358 | def xpub_verify(xpub_hexstr, message_hexstr, signature_hexstr): |
359 | 359 | result = False |
360 | - result = verify(xpub_to_public_key(xpub_hexstr)['public_key'], signature_hexstr, message_hexstr)['result'] | |
360 | + result = verify(get_public_key(xpub_hexstr), signature_hexstr, message_hexstr) | |
361 | 361 | return result |
362 | 362 | |
363 | 363 | |
364 | -def get_new_key(entropy_hexstr=None, mnemonic_hexstr=None): | |
364 | +def get_new_key(entropy_hexstr=None, mnemonic_str=None): | |
365 | 365 | if (entropy_hexstr is None) and (mnemonic_str is None): |
366 | 366 | entropy_hexstr = get_entropy() |
367 | 367 | mnemonic_str = get_mnemonic(entropy_hexstr) |
368 | 368 | if (entropy_hexstr is None) and (mnemonic_str is not None): |
369 | - pass | |
369 | + entropy_hexstr = '' | |
370 | 370 | if entropy_hexstr is not None: |
371 | 371 | mnemonic_str = get_mnemonic(entropy_hexstr) |
372 | 372 | seed_hexstr = get_seed(mnemonic_str) |
@@ -33,6 +33,4 @@ def verify(public_key_str, signature_str, message_str): | ||
33 | 33 | result = True |
34 | 34 | except ed25519.BadSignatureError: |
35 | 35 | result = False |
36 | - return { | |
37 | - "result": result | |
38 | - } | |
36 | + return result | |
\ No newline at end of file |
@@ -1,6 +1,14 @@ | ||
1 | 1 | import qrcode |
2 | 2 | import pybase64 |
3 | +import six | |
4 | +import hmac | |
5 | +import hashlib | |
6 | +import sha3 | |
3 | 7 | from io import BytesIO |
8 | +from binascii import hexlify | |
9 | +from binascii import unhexlify | |
10 | +from .edwards25519 import * | |
11 | +# from bytom.signatures import ed25519 | |
4 | 12 | |
5 | 13 | # create_qrcode_base64 create qrcode, then encode it to base64 |
6 | 14 | # type(s) is str |
@@ -11,4 +19,62 @@ def create_qrcode_base64(s): | ||
11 | 19 | base64_str = pybase64.b64encode(buffered.getvalue()).decode("utf-8") |
12 | 20 | return { |
13 | 21 | "base64": base64_str |
14 | - } | |
\ No newline at end of file | ||
22 | + } | |
23 | + | |
24 | + | |
25 | +if six.PY3: | |
26 | + def byte2int(b): | |
27 | + return b | |
28 | + def int2byte(i): | |
29 | + return bytes(chr(i % 256), encoding="UTF-8") | |
30 | + | |
31 | +elif six.PY2: | |
32 | + def byte2int(b): | |
33 | + return ord(b) | |
34 | + def int2byte(i): | |
35 | + return chr(i % 256) | |
36 | + | |
37 | +L = 2 ** 252 + 27742317777372353535851937790883648493 | |
38 | + | |
39 | +def sha3_digest_256(data): | |
40 | + s = sha3.sha3_256() | |
41 | + s.update(data) | |
42 | + return s.digest() | |
43 | + | |
44 | +def hmac_sha_512(data, key): | |
45 | + digest = hmac.new(key, msg=data, digestmod=hashlib.sha512).digest() | |
46 | + return digest | |
47 | + | |
48 | +def sha_512(data): | |
49 | + md = hashlib.sha512() | |
50 | + md.update(data) | |
51 | + return md.digest() | |
52 | + | |
53 | +def hex2int(hex): | |
54 | + ## converts a hex string to integer | |
55 | + unhex = unhexlify(hex) | |
56 | + s = 0 | |
57 | + for i in range(len(unhex)): | |
58 | + s += 256 ** i * byte2int(unhex[i]) | |
59 | + return s | |
60 | + | |
61 | +def int2hex(int): | |
62 | + ## converts an integer to a little endian encoded hex string | |
63 | + return hexlify(encodeint(int)) | |
64 | + | |
65 | +def sc_reduce32(input): | |
66 | + ## convert hex string input to integer | |
67 | + int = hex2int(input) | |
68 | + ## reduce mod l | |
69 | + modulo = int % L | |
70 | + ## convert back to hex string for return value | |
71 | + return int2hex(modulo) | |
72 | + | |
73 | +def sc_muladd(a, b, c): | |
74 | + a_int = hex2int(a) | |
75 | + b_int = hex2int(b) | |
76 | + c_int = hex2int(c) | |
77 | + | |
78 | + s = a_int * b_int + c_int | |
79 | + modulo = s % L | |
80 | + return int2hex(modulo) |
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh: | ||
5 | 5 | |
6 | 6 | setuptools.setup( |
7 | 7 | name="pybtm", |
8 | - version="0.0.9", | |
8 | + version="0.0.14", | |
9 | 9 | author="zcc0721", |
10 | 10 | author_email="zcc0721@foxmail.com", |
11 | 11 | description="Python3 implementation of the Bytom protocol.", |