Skip to content

Commit

Permalink
BIP-143
Browse files Browse the repository at this point in the history
  • Loading branch information
Teran McKinney committed Dec 6, 2017
1 parent 37b74cb commit e126766
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
24 changes: 14 additions & 10 deletions bitcash/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bitcash.format import address_to_public_key_hash
from bitcash.network.rates import currency_to_satoshi_cached
from bitcash.utils import (
bytes_to_hex, chunk_data, hex_to_bytes, int_to_unknown_bytes
bytes_to_hex, chunk_data, hex_to_bytes, int_to_unknown_bytes, int_to_varint
)

VERSION_1 = 0x01.to_bytes(4, byteorder='little')
Expand Down Expand Up @@ -34,26 +34,29 @@


class TxIn:
__slots__ = ('script', 'script_len', 'txid', 'txindex')
__slots__ = ('script', 'script_len', 'txid', 'txindex', 'amount')

def __init__(self, script, script_len, txid, txindex):
def __init__(self, script, script_len, txid, txindex, amount):
self.script = script
self.script_len = script_len
self.txid = txid
self.txindex = txindex
self.amount = amount

def __eq__(self, other):
return (self.script == other.script and
self.script_len == other.script_len and
self.txid == other.txid and
self.txindex == other.txindex)
self.txindex == other.txindex and
self.amount == other.amount)

def __repr__(self):
return 'TxIn({}, {}, {}, {})'.format(
return 'TxIn({}, {}, {}, {}, {})'.format(
repr(self.script),
repr(self.script_len),
repr(self.txid),
repr(self.txindex)
repr(self.txindex),
repr(self.amount)
)


Expand Down Expand Up @@ -205,11 +208,12 @@ def create_p2pkh_transaction(private_key, unspents, outputs):
script_len = int_to_unknown_bytes(len(script), byteorder='little')
txid = hex_to_bytes(unspent.txid)[::-1]
txindex = unspent.txindex.to_bytes(4, byteorder='little')
amount = unspent.amount.to_bytes(8, byteorder='little')

inputs.append(TxIn(script, script_len, txid, txindex))
inputs.append(TxIn(script, script_len, txid, txindex, amount))

hashPrevouts = double_sha256(b''.join([i.txid+i.txindex for i in inputs.TxIn]))
hashSequence = double_sha256(b''.join([i.sequence for i in inputs.TxIn]))
hashPrevouts = double_sha256(b''.join([i.txid+i.txindex for i in inputs]))
hashSequence = double_sha256(b''.join([SEQUENCE for i in inputs]))
hashOutputs = double_sha256(b''.join([bytes(o) for o in output_block]))

for i, txin in enumerate(inputs):
Expand All @@ -223,7 +227,7 @@ def create_p2pkh_transaction(private_key, unspents, outputs):
scriptCode_len +
scriptCode +
txin.amount +
txin.sequence +
SEQUENCE +
hashOutputs +
lock_time +
hash_type
Expand Down
12 changes: 12 additions & 0 deletions bitcash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ def hex_to_int(hexed):

def flip_hex_byte_order(string):
return bytes_to_hex(hex_to_bytes(string)[::-1])


def int_to_varint(val):

if val < 253:
return val.to_bytes(1, 'little')
elif val <= 65535:
return b'\xfd'+val.to_bytes(2, 'little')
elif val <= 4294967295:
return b'\xfe'+val.to_bytes(4, 'little')
else:
return b'\xff'+val.to_bytes(8, 'little')
24 changes: 22 additions & 2 deletions bitcash/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from bitcash.crypto import ECPrivateKey
from bitcash.curve import Point
from bitcash.format import (
bytes_to_wif, public_key_to_address, public_key_to_coords, wif_to_bytes
bytes_to_wif, public_key_to_address, public_key_to_coords, wif_to_bytes,
address_to_public_key_hash
)
from bitcash.network import NetworkAPI, get_fee_cached, satoshi_to_currency_cached
from bitcash.network.meta import Unspent
from bitcash.transaction import calc_txid, create_p2pkh_transaction, sanitize_tx_data
from bitcash.transaction import (
calc_txid, create_p2pkh_transaction, sanitize_tx_data,
OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, OP_HASH160, OP_PUSH_20
)


def wif_to_key(wif):
Expand Down Expand Up @@ -136,6 +140,7 @@ def __init__(self, wif=None):
super().__init__(wif=wif)

self._address = None
self._scriptcode = None

self.balance = 0
self.unspents = []
Expand All @@ -148,6 +153,13 @@ def address(self):
self._address = public_key_to_address(self._public_key, version='main')
return self._address

@property
def scriptcode(self):
self._scriptcode = (OP_DUP + OP_HASH160 + OP_PUSH_20 +
address_to_public_key_hash(self.address) +
OP_EQUALVERIFY + OP_CHECKSIG)
return self._scriptcode

def to_wif(self):
return bytes_to_wif(
self._pk.secret,
Expand Down Expand Up @@ -420,6 +432,7 @@ def __init__(self, wif=None):
super().__init__(wif=wif)

self._address = None
self._scriptcode = None

self.balance = 0
self.unspents = []
Expand All @@ -432,6 +445,13 @@ def address(self):
self._address = public_key_to_address(self._public_key, version='test')
return self._address

@property
def scriptcode(self):
self._scriptcode = (OP_DUP + OP_HASH160 + OP_PUSH_20 +
address_to_public_key_hash(self.address) +
OP_EQUALVERIFY + OP_CHECKSIG)
return self._scriptcode

def to_wif(self):
return bytes_to_wif(
self._pk.secret,
Expand Down

0 comments on commit e126766

Please sign in to comment.