Skip to content

Commit

Permalink
fixed flake8 and a few other things
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysong committed Dec 17, 2018
1 parent 05dcd6b commit b7c83cc
Show file tree
Hide file tree
Showing 41 changed files with 141 additions and 84 deletions.
2 changes: 1 addition & 1 deletion author_bio.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<section data-type="colophon" class="abouttheauthor">
<h1>About the Author</h1>
<p>Jimmy Song is a developer of over 20 years, starting his career by reading the Programming Perl 2nd Edition published by the very same publisher as this book. He's been involved in way too many startups and got into Bitcoin full-time in 2014. He's contributed to many different Bitcoin open source projects over the years, including Armory, Bitcoin Core, btcd and pycoin.</p>
<p>If you ever meet Jimmy and want him to rant, ask him about Bitcoin, sound money, fasting, carnivory, powerlifting, raising children or cowboy hats.</p>
<p>If you ever meet Jimmy and want him to rant, ask him about Bitcoin, sound money, how fiat money makes everything worse, fasting, carnivory, powerlifting, raising children or cowboy hats.</p>
</section>
6 changes: 4 additions & 2 deletions code-ch01/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
# end::exercise1[]
'''


# tag::answer1[]
def __ne__(self, other):
# this should be the inverse of the == operator
Expand All @@ -134,6 +135,7 @@ def __ne__(self, other):
# end::exercise3[]
'''


# tag::answer3[]
def __sub__(self, other):
if self.prime != other.prime:
Expand Down Expand Up @@ -187,13 +189,13 @@ def __truediv__(self, other):
# this means:
# 1/n == pow(n, p-2, p)
# We return an element of the same class
num = self.num * pow(other.num, self.prime-2, self.prime) % self.prime
num = self.num * pow(other.num, self.prime - 2, self.prime) % self.prime
return self.__class__(num, self.prime)
# end::answer9[]


class ChapterTest(TestCase):

def test_apply(self):
FieldElement.__ne__ = __ne__
FieldElement.__sub__ = __sub__
Expand Down
3 changes: 3 additions & 0 deletions code-ch02/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
# end::exercise2[]
'''


# tag::answer2[]
def __ne__(self, other):
return not (self == other)
# end::answer2[]


'''
# tag::exercise3[]
==== Exercise 3
Expand All @@ -87,6 +89,7 @@ def __ne__(self, other):
# end::exercise7[]
'''


def __add__(self, other):
if self.a != other.a or self.b != other.b:
raise TypeError
Expand Down
12 changes: 5 additions & 7 deletions code-ch03/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
Verify whether these signatures are valid:
```
P = (0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
P = (0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
0x61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34)
# signature 1
Expand All @@ -138,7 +138,7 @@
# tag::answer6[]
>>> from ecc import S256Point, N, G
>>> point = S256Point(
... 0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
... 0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
... 0x61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34)
>>> z = 0xec208baa0fc1c19f708a9ca96fdeff3ac3f230bb4a7ba4aede4942ad003c0f60
>>> r = 0xac8d1c87e51d0d441be8b3dd5b05c8795b48875dffe00b7ffcfac23010d3a395
Expand Down Expand Up @@ -190,9 +190,7 @@

from unittest import TestCase

from ecc import FieldElement, Point, S256Point, ECCTest, G, N
from random import randint
from helper import hash256
from ecc import FieldElement, Point, ECCTest


class ChapterTest(TestCase):
Expand Down Expand Up @@ -226,7 +224,7 @@ def test_add(self):
x3 = FieldElement(x3_raw, prime)
y3 = FieldElement(y3_raw, prime)
p3 = Point(x3, y3, a, b)
self.assertEqual(p1+p2, p3)
self.assertEqual(p1 + p2, p3)
# end::answer3[]

ECCTest.test_add = test_add
3 changes: 1 addition & 2 deletions code-ch03/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ def __add__(self, other):
if self.x == other.x and self.y != other.y:
return self.__class__(None, None, self.a, self.b)


# Case 2: self.x ≠ other.x
# Formula (x3,y3)==(x1,y1)+(x2,y2)
# s=(y2-y1)/(x2-x1)
Expand Down Expand Up @@ -271,7 +270,6 @@ def test_on_curve(self):
Point(x, y, a, b) # <1>
# end::source2[]


def test_add(self):
# tests the following additions on curve y^2=x^3-7 over F_223:
# (192,105) + (17,56)
Expand Down Expand Up @@ -395,6 +393,7 @@ def verify(self, z, sig):
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)
# end::source10[]


class S256Test(TestCase):

def test_order(self):
Expand Down
8 changes: 4 additions & 4 deletions code-ch04/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
`0x37206a0610995c58074999cb9767b87af4c4978db68c06e8e6e81d282047a7c6`
* s =
* s =
`0x8ca63759c1157ebeaec0d03cecca119fc9a75bf8e6d0fa65c841c8e2738cdaec`
# end::exercise3[]
Expand Down Expand Up @@ -155,9 +155,6 @@

from unittest import TestCase

from ecc import PrivateKey, Signature
from helper import encode_base58, hash256

import helper


Expand All @@ -169,13 +166,15 @@
# end::exercise7[]
'''


# tag::answer7[]
def little_endian_to_int(b):
'''little_endian_to_int takes byte sequence as a little-endian number.
Returns an integer'''
return int.from_bytes(b, 'little')
# end::answer7[]


'''
# tag::exercise8[]
==== Exercise 8
Expand All @@ -184,6 +183,7 @@ def little_endian_to_int(b):
# end::exercise8[]
'''


# tag::answer8[]
def int_to_little_endian(n, length):
'''endian_to_little_endian takes an integer and returns the little-endian
Expand Down
1 change: 0 additions & 1 deletion code-ch04/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def __add__(self, other):
if self.x == other.x and self.y != other.y:
return self.__class__(None, None, self.a, self.b)


# Case 2: self.x ≠ other.x
# Formula (x3,y3)==(x1,y1)+(x2,y2)
# s=(y2-y1)/(x2-x1)
Expand Down
2 changes: 1 addition & 1 deletion code-ch04/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def encode_base58(s):
else:
break
prefix = b'1' * count
num = int(s.hex(), 16)
num = int.from_bytes(s, 'big')
result = bytearray()
while num > 0: # <2>
num, mod = divmod(num, 58)
Expand Down
16 changes: 15 additions & 1 deletion code-ch05/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
'''


from io import BytesIO
from unittest import TestCase

from helper import little_endian_to_int, read_varint
Expand All @@ -71,6 +70,7 @@

methods = []


'''
# tag::exercise1[]
==== Exercise 1
Expand Down Expand Up @@ -99,6 +99,8 @@ def parse(cls, s, testnet=False):
return cls(version, inputs, None, None, testnet=testnet)
# end::answer2.1[]
'''


# tag::answer2.2[]
@classmethod
def parse(cls, s):
Expand All @@ -111,8 +113,11 @@ def parse(cls, s):
sequence = little_endian_to_int(s.read(4))
return cls(prev_tx, prev_index, script_sig, sequence)
# end::answer2.2[]


methods.append(parse)


'''
# tag::exercise3[]
==== Exercise 3
Expand All @@ -137,6 +142,7 @@ def parse(cls, s, testnet=False):
# end::answer3.1[]
'''


# tag::answer3.2[]
@classmethod
def parse(cls, s):
Expand All @@ -147,8 +153,11 @@ def parse(cls, s):
script_pubkey = Script.parse(s)
return cls(amount, script_pubkey)
# end::answer3.2[]


methods.append(parse)


'''
# tag::exercise4[]
==== Exercise 4
Expand All @@ -157,6 +166,7 @@ def parse(cls, s):
# end::exercise4[]
'''


# tag::answer4[]
@classmethod
def parse(cls, s, testnet=False):
Expand All @@ -172,8 +182,11 @@ def parse(cls, s, testnet=False):
locktime = little_endian_to_int(s.read(4))
return cls(version, inputs, outputs, locktime, testnet=testnet)
# end::answer4[]


methods.append(parse)


'''
# tag::exercise6[]
==== Exercise 6
Expand All @@ -182,6 +195,7 @@ def parse(cls, s, testnet=False):
# end::exercise6[]
'''


# tag::answer6[]
def fee(self, testnet=False):
input_sum, output_sum = 0, 0
Expand Down
1 change: 0 additions & 1 deletion code-ch05/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def __add__(self, other):
if self.x == other.x and self.y != other.y:
return self.__class__(None, None, self.a, self.b)


# Case 2: self.x ≠ other.x
# Formula (x3,y3)==(x1,y1)+(x2,y2)
# s=(y2-y1)/(x2-x1)
Expand Down
4 changes: 2 additions & 2 deletions code-ch05/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def encode_base58(s):
else:
break
prefix = b'1' * count
# convert from binary to hex, then hex to integer
num = int(s.hex(), 16)
# convert to big endian integer
num = int.from_bytes(s, 'big')
result = bytearray()
while num > 0:
num, mod = divmod(num, 58)
Expand Down
1 change: 1 addition & 0 deletions code-ch05/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def serialize(self): # <1>
return result
# end::source4[]


class TxTest(TestCase):
cache_file = '../tx.cache'

Expand Down
5 changes: 3 additions & 2 deletions code-ch06/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@
'''


from io import BytesIO
from unittest import TestCase

import op

from ecc import S256Point, Signature
from helper import hash160
from op import encode_num
from script import Script


'''
Expand All @@ -89,6 +87,7 @@
# end::exercise1[]
'''


# tag::answer1[]
def op_hash160(stack):
if len(stack) < 1:
Expand All @@ -99,6 +98,7 @@ def op_hash160(stack):
return True
# end::answer1[]


'''
# tag::exercise2[]
==== Exercise 2
Expand All @@ -107,6 +107,7 @@ def op_hash160(stack):
# end::exercise2[]
'''


# tag::answer2[]
def op_checksig(stack, z):
if len(stack) < 2:
Expand Down
1 change: 0 additions & 1 deletion code-ch06/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def __add__(self, other):
if self.x == other.x and self.y != other.y:
return self.__class__(None, None, self.a, self.b)


# Case 2: self.x ≠ other.x
# Formula (x3,y3)==(x1,y1)+(x2,y2)
# s=(y2-y1)/(x2-x1)
Expand Down
4 changes: 2 additions & 2 deletions code-ch06/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def encode_base58(s):
else:
break
prefix = b'1' * count
# convert from binary to hex, then hex to integer
num = int(s.hex(), 16)
# convert to big endian integer
num = int.from_bytes(s, 'big')
result = bytearray()
while num > 0:
num, mod = divmod(num, 58)
Expand Down
1 change: 1 addition & 0 deletions code-ch06/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def evaluate(self, z):
return True # <10>
# end::source5[]


class ScriptTest(TestCase):

def test_parse(self):
Expand Down
Loading

0 comments on commit b7c83cc

Please sign in to comment.