Skip to content

Commit

Permalink
Merge pull request #172 from oditorium/p3
Browse files Browse the repository at this point in the history
Mostly Python 3 fixes
  • Loading branch information
vbuterin committed Jul 27, 2017
2 parents a82b006 + 87806f3 commit aeb0a2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 8 additions & 1 deletion bitcoin/bci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import json, re
import random
import sys

from bitcoin.main import from_string_to_bytes


try:
from urllib.request import build_opener
except:
Expand Down Expand Up @@ -256,7 +260,10 @@ def history(*args):
def bci_pushtx(tx):
if not re.match('^[0-9a-fA-F]*$', tx):
tx = tx.encode('hex')
return make_request('https://blockchain.info/pushtx', 'tx='+tx)
return make_request(
'https://blockchain.info/pushtx',
from_string_to_bytes('tx='+tx)
)


def eligius_pushtx(tx):
Expand Down
6 changes: 5 additions & 1 deletion bitcoin/deterministic.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ def bip32_ckd(data, i):


def bip32_master_key(seed, vbytes=MAINNET_PRIVATE):
I = hmac.new(from_string_to_bytes("Bitcoin seed"), seed, hashlib.sha512).digest()
I = hmac.new(
from_string_to_bytes("Bitcoin seed"),
from_string_to_bytes(seed),
hashlib.sha512
).digest()
return bip32_serialize((vbytes, 0, b'\x00'*4, 0, I[32:], I[:32]+b'\x01'))


Expand Down
21 changes: 20 additions & 1 deletion bitcoin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def add_privkeys(p1, p2):
f1, f2 = get_privkey_format(p1), get_privkey_format(p2)
return encode_privkey((decode_privkey(p1, f1) + decode_privkey(p2, f2)) % N, f1)


def mul_privkeys(p1, p2):
f1, f2 = get_privkey_format(p1), get_privkey_format(p2)
return encode_privkey((decode_privkey(p1, f1) * decode_privkey(p2, f2)) % N, f1)
Expand Down Expand Up @@ -327,6 +328,8 @@ def subtract_privkeys(p1, p2):
k2 = decode_privkey(p2, f2)
return encode_privkey((decode_privkey(p1, f1) - k2) % N, f1)



# Hashes


Expand Down Expand Up @@ -414,7 +417,8 @@ def random_key():


def random_electrum_seed():
entropy = os.urandom(32) \
#entropy = os.urandom(32) \ # fails in Python 3, hence copied from random_key()
entropy = random_string(32) \
+ str(random.randrange(2**256)) \
+ str(int(time.time() * 1000000))
return sha256(entropy)[:32]
Expand Down Expand Up @@ -579,3 +583,18 @@ def ecdsa_recover(msg, sig):
v,r,s = decode_sig(sig)
Q = ecdsa_raw_recover(electrum_sig_hash(msg), (v,r,s))
return encode_pubkey(Q, 'hex_compressed') if v >= 31 else encode_pubkey(Q, 'hex')



# add/subtract
def add(p1,p2):
if is_privkey(p1):
return add_privkeys(p1, p2)
else:
return add_pubkeys(p1, p2)

def subtract(p1,p2):
if is_privkey(p1):
return subtract_privkeys(p1, p2)
else:
return subtract_pubkeys(p1, p2)

0 comments on commit aeb0a2b

Please sign in to comment.