1
0

define constants for tx output types

This commit is contained in:
ThomasV
2016-01-14 17:15:50 +01:00
parent d57af0db33
commit 0d52911561
14 changed files with 54 additions and 48 deletions

View File

@@ -34,6 +34,12 @@ RECOMMENDED_FEE = 50000
COINBASE_MATURITY = 100
COIN = 100000000
# supported types of transction outputs
TYPE_ADDRESS = 0
TYPE_PUBKEY = 1
TYPE_SCRIPT = 2
# AES encryption
EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))

View File

@@ -20,7 +20,7 @@ from collections import defaultdict, namedtuple
from random import choice, randint, shuffle
from math import floor, log10
from bitcoin import COIN
from bitcoin import COIN, TYPE_ADDRESS
from transaction import Transaction
from util import NotEnoughFunds, PrintError, profiler
@@ -72,7 +72,7 @@ class CoinChooserBase(PrintError):
# size of the change output, add it to the transaction.
dust = sum(amount for amount in amounts if amount < dust_threshold)
amounts = [amount for amount in amounts if amount >= dust_threshold]
change = [('address', addr, amount)
change = [(TYPE_ADDRESS, addr, amount)
for addr, amount in zip(change_addrs, amounts)]
self.print_error('change:', change)
if dust:

View File

@@ -31,7 +31,7 @@ from decimal import Decimal
import util
from util import print_msg, format_satoshis, print_stderr
import bitcoin
from bitcoin import is_address, hash_160_to_bc_address, hash_160, COIN
from bitcoin import is_address, hash_160_to_bc_address, hash_160, COIN, TYPE_ADDRESS
from transaction import Transaction
import paymentrequest
from paymentrequest import PR_PAID, PR_UNPAID, PR_UNKNOWN, PR_EXPIRED
@@ -200,7 +200,7 @@ class Commands:
break
else:
raise BaseException('Transaction output not in wallet', prevout_hash+":%d"%prevout_n)
outputs = map(lambda x: ('address', x[0], int(COIN*x[1])), outputs.items())
outputs = map(lambda x: (TYPE_ADDRESS, x[0], int(COIN*x[1])), outputs.items())
tx = Transaction.from_io(tx_inputs, outputs)
if not unsigned:
self.wallet.sign_transaction(tx, self._password)
@@ -404,14 +404,14 @@ class Commands:
if fee is None:
for i in inputs:
self.wallet.add_input_info(i)
output = ('address', address, amount)
output = (TYPE_ADDRESS, address, amount)
dummy_tx = Transaction.from_io(inputs, [output])
fee_per_kb = self.wallet.fee_per_kb(self.config)
fee = dummy_tx.estimated_fee(fee_per_kb)
amount -= fee
else:
amount = int(COIN*Decimal(amount))
final_outputs.append(('address', address, amount))
final_outputs.append((TYPE_ADDRESS, address, amount))
coins = self.wallet.get_spendable_coins(domain)
tx = self.wallet.make_unsigned_transaction(coins, final_outputs, self.config, fee, change_addr)

View File

@@ -36,6 +36,7 @@ import random
NO_SIGNATURE = 'ff'
class SerializationError(Exception):
""" Thrown when there's a problem deserializing or serializing """
@@ -393,20 +394,20 @@ def get_address_from_output_script(bytes):
# 65 BYTES:... CHECKSIG
match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
if match_decoded(decoded, match):
return 'pubkey', decoded[0][1].encode('hex')
return TYPE_PUBKEY, decoded[0][1].encode('hex')
# Pay-by-Bitcoin-address TxOuts look like:
# DUP HASH160 20 BYTES:... EQUALVERIFY CHECKSIG
match = [ opcodes.OP_DUP, opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIG ]
if match_decoded(decoded, match):
return 'address', hash_160_to_bc_address(decoded[2][1])
return TYPE_ADDRESS, hash_160_to_bc_address(decoded[2][1])
# p2sh
match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ]
if match_decoded(decoded, match):
return 'address', hash_160_to_bc_address(decoded[1][1],5)
return TYPE_ADDRESS, hash_160_to_bc_address(decoded[1][1],5)
return 'script', bytes
return TYPE_SCRIPT, bytes
@@ -542,7 +543,7 @@ class Transaction:
pubkey = public_key_from_private_key(privkey)
address = address_from_private_key(privkey)
u = network.synchronous_get(('blockchain.address.listunspent',[address]))
pay_script = klass.pay_script('address', address)
pay_script = klass.pay_script(TYPE_ADDRESS, address)
for item in u:
item['scriptPubKey'] = pay_script
item['redeemPubkey'] = pubkey
@@ -560,7 +561,7 @@ class Transaction:
return
total = sum(i.get('value') for i in inputs) - fee
outputs = [('address', to_address, total)]
outputs = [(TYPE_ADDRESS, to_address, total)]
self = klass.from_io(inputs, outputs)
self.sign(keypairs)
return self
@@ -577,9 +578,9 @@ class Transaction:
@classmethod
def pay_script(self, output_type, addr):
if output_type == 'script':
if output_type == TYPE_SCRIPT:
return addr.encode('hex')
elif output_type == 'address':
elif output_type == TYPE_ADDRESS:
addrtype, hash_160 = bc_address_to_hash_160(addr)
if addrtype == 0:
script = '76a9' # op_dup, op_hash_160
@@ -636,7 +637,7 @@ class Transaction:
script += push_script(redeem_script)
elif for_sig==i:
script = txin['redeemScript'] if p2sh else self.pay_script('address', address)
script = txin['redeemScript'] if p2sh else self.pay_script(TYPE_ADDRESS, address)
else:
script = ''
@@ -797,9 +798,9 @@ class Transaction:
"""convert pubkeys to addresses"""
o = []
for type, x, v in self.outputs:
if type == 'address':
if type == TYPE_ADDRESS:
addr = x
elif type == 'pubkey':
elif type == TYPE_PUBKEY:
addr = public_key_to_bc_address(x.decode('hex'))
else:
addr = 'SCRIPT ' + x.encode('hex')

View File

@@ -658,7 +658,7 @@ class Abstract_Wallet(PrintError):
for i in inputs:
self.add_input_info(i)
addr = self.addresses(False)[0]
output = ('address', addr, sendable)
output = (TYPE_ADDRESS, addr, sendable)
dummy_tx = Transaction.from_io(inputs, [output])
if fee is None:
fee_per_kb = self.fee_per_kb(config)
@@ -750,9 +750,9 @@ class Abstract_Wallet(PrintError):
for n, txo in enumerate(tx.outputs):
ser = tx_hash + ':%d'%n
_type, x, v = txo
if _type == 'address':
if _type == TYPE_ADDRESS:
addr = x
elif _type == 'pubkey':
elif _type == TYPE_PUBKEY:
addr = public_key_to_bc_address(x.decode('hex'))
else:
addr = None
@@ -924,7 +924,7 @@ class Abstract_Wallet(PrintError):
def make_unsigned_transaction(self, coins, outputs, config, fixed_fee=None, change_addr=None):
# check outputs
for type, data, value in outputs:
if type == 'address':
if type == TYPE_ADDRESS:
assert is_address(data), "Address " + data + " is invalid!"
# Avoid index-out-of-range with coins[0] below