forked from OpenBazaar/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_util.py
36 lines (26 loc) · 1.17 KB
/
crypto_util.py
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
import pyelliptic as ec
from pybitcointools import main as arithmetic
def pubkey_to_pyelliptic(pubkey):
# Strip 04
pubkey = pubkey[2:]
# Split it in half
pub_x = pubkey[0:len(pubkey) / 2]
pub_y = pubkey[len(pubkey) / 2:]
# Add pyelliptic content
print "02ca0020" + pub_x + "0020" + pub_y
return "02ca0020" + pub_x + "0020" + pub_y
def makePrivCryptor(privkey_hex):
privkey_bin = '\x02\xca\x00 ' + arithmetic.changebase(privkey_hex,
16, 256, minlen=32)
pubkey_hex = arithmetic.privkey_to_pubkey(privkey_hex)
pubkey_bin_bare = arithmetic.changebase(pubkey_hex, 16, 256, minlen=65)[1:]
pubkey_bin = '\x02\xca\x00 ' + pubkey_bin_bare[:32] + '\x00 ' + pubkey_bin_bare[32:]
cryptor = ec.ECC(curve='secp256k1', privkey=privkey_bin, pubkey=pubkey_bin)
return cryptor
def makePubCryptor(pubkey):
pubkey_bin = hexToPubkey(pubkey)
return ec.ECC(curve='secp256k1', pubkey=pubkey_bin)
def hexToPubkey(pubkey):
pubkey_raw = arithmetic.changebase(pubkey[2:], 16, 256, minlen=64)
pubkey_bin = '\x02\xca\x00 ' + pubkey_raw[:32] + '\x00 ' + pubkey_raw[32:]
return pubkey_bin