Skip to content

Commit

Permalink
Fix opcode, unit and uri (broken by changes in network)
Browse files Browse the repository at this point in the history
  • Loading branch information
esneider committed May 18, 2015
1 parent d17e425 commit 89d1c23
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
9 changes: 6 additions & 3 deletions bitforge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from network import Network
from privkey import PrivateKey
# from pubkey import PublicKey
# from address import Address
# from script import Script
# from hdprivkey import HDPrivateKey
from network import Network
from unit import Unit
from uri import URI

import network
import privkey
# from unit import Unit
# from uri import URI
import unit
import uri
33 changes: 15 additions & 18 deletions bitforge/opcode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys, inspect
from numbers import Number
from errors import *
import sys
import inspect

from bitforge import error


# Below is a list of all *named* opcodes. Their values, integers in the
Expand Down Expand Up @@ -155,28 +156,26 @@

class Opcode(object):

class Error(BitforgeError):
class Error(error.BitforgeError):
pass

class UnknownOpcodeName(Error, StringError):
class UnknownOpcodeName(Error, error.StringError):
"No known operation named {string}"

class UnknownOpcodeNumber(Error, NumberError):
class UnknownOpcodeNumber(Error, error.NumberError):
"No known operation numbered {number}"

class InvalidConstPushLength(Error, StringError):
class InvalidConstPushLength(Error, error.StringError):
"No constant push opcode can push {length} bytes (only [1-75])"

class InvalidPushLength(Error, NumberError):
class InvalidPushLength(Error, error.NumberError):
"No Opcode can push {number} bytes"

class TypeError(Error, ObjectError):
class TypeError(Error, error.ObjectError):
"Opcodes are initialized from numbers and names, got object {object}"


opcode_number_to_name = {} # Filled after class definition


def __init__(self, number):
if not (0 <= number <= 255):
raise Opcode.UnknownOpcodeNumber(number)
Expand Down Expand Up @@ -218,7 +217,6 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.number)


@staticmethod
def for_number(n):
if 0 <= n <= 16:
Expand All @@ -243,13 +241,13 @@ def const_push_for(length):
@staticmethod
def var_push_for(length):
if length < 1:
raise InvalidPushLength(length)
raise Opcode.InvalidPushLength(length)

for opcode in [OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4]:
if length <= Opcode.data_length_max(opcode):
return opcode

raise InvalidPushLength(length)
raise Opcode.InvalidPushLength(length)

@staticmethod
def push_for(length):
Expand Down Expand Up @@ -282,7 +280,6 @@ def data_length_nbytes(opcode):
}[opcode]



# Walk the OP_* variables, mapping them to their names and creating Opcode objs:
_module = sys.modules[__name__]

Expand All @@ -294,7 +291,7 @@ def data_length_nbytes(opcode):
# Replace integer values with actual Opcode instances:
setattr(_module, name, Opcode(number))

Opcode.opcode_number_to_name[OP_0] = 'OP_0' # shares number with OP_FALSE
Opcode.opcode_number_to_name[OP_1] = 'OP_1' # shares number with OP_TRUE
Opcode.opcode_number_to_name[OP_0] = 'OP_0' # shares number with OP_FALSE
Opcode.opcode_number_to_name[OP_1] = 'OP_1' # shares number with OP_TRUE

del _module # the expected use for this module is to import *
del _module # the expected use for this module is to import *
4 changes: 2 additions & 2 deletions bitforge/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Unit(object):

@staticmethod
def from_fiat(value, rate):
return Unit(btc = value / float(rate))
return Unit(btc = value / float(rate))

def __init__(self, satoshis = None, bits = None, mbtc = None, btc = None):
if satoshis is not None:
Expand All @@ -35,4 +35,4 @@ def __str__(self):
return '%s satoshis' % self.satoshis

def __repr__(self):
return '<Unit: %s>' % str(self)
return '<Unit: %s>' % str(self)
4 changes: 3 additions & 1 deletion bitforge/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from bitforge import Address, Unit


class URI(object):

@staticmethod
Expand Down Expand Up @@ -49,7 +50,8 @@ def to_uri(self):
query['amount'] = self.amount.btc
for key in ['message', 'label', 'r']:
value = getattr(self, key, False)
if value: query[key] = value
if value:
query[key] = value

query = urlencode(query)
return 'bitcoin:' + self.address.to_string() + ('?' + query if query else '')
Expand Down
2 changes: 1 addition & 1 deletion tests/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class TestUnit:

def test_btc_accessors(self):
u = Unit(btc = 1.2)
assert u.btc == 1.2
Expand Down
22 changes: 10 additions & 12 deletions tests/uri.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from pytest import raises

from bitforge import URI
from bitforge import Address
from bitforge import Unit
from bitforge import network

import bitforge.network

class TestURI:

Expand Down Expand Up @@ -40,20 +38,20 @@ def test_is_valid(self):
def test_uri_address(self):
uri = URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj')
assert isinstance(uri.address, Address)
assert uri.address.network == bitforge.network.livenet
assert uri.address.network == network.livenet

uri = URI('bitcoin:mkYY5NRvikVBY1EPtaq9fAFgquesdjqECw')
assert isinstance(uri.address, Address)
assert uri.address.network == bitforge.network.testnet
assert uri.address.network == network.testnet

def test_uri_amount(self):
uri = URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=123.22')
assert isinstance(uri.amount, Unit)
assert uri.amount.satoshis == 12322000000

def test_uri_extras(self):
uri = URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param')
assert uri.extras['other'] == u'param'
def test_uri_extras_2(self):
uri = URI('bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.2&other=param')
assert uri.extras['other'] == u'param'

def test_create_params(self):
uri = URI({
Expand Down Expand Up @@ -94,10 +92,10 @@ def test_str(self):
assert uri.to_uri() == 'bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj'

uri = URI({
'address': '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
'amount': 110001000,
'message': 'Hello World',
'something': 'else'
'address': '1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj',
'amount': 110001000,
'message': 'Hello World',
'something': 'else'
})

assert uri.to_uri() == 'bitcoin:1DP69gMMvSuYhbnxsi4EJEFufUAbDrEQfj?amount=1.10001&message=Hello+World&something=else'
Expand Down

0 comments on commit 89d1c23

Please sign in to comment.