Skip to content

Commit

Permalink
temp checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkiss committed May 11, 2023
1 parent db7f39f commit e179c8a
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pycoin/coins/bitcoin/Tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def parse(class_, f, allow_segwit=None):
if allow_segwit is None:
allow_segwit = class_.ALLOW_SEGWIT
version, = parse_struct("L", f)
if version == 2:
return
v1 = ord(f.read(1))
is_segwit = allow_segwit and (v1 == 0)
v2 = None
Expand Down
101 changes: 97 additions & 4 deletions pycoin/coins/litecoin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
class Tx:
pass
import struct

from pycoin.block import Block
from pycoin.coins.bitcoin.Tx import Tx
from pycoin.satoshi.satoshi_int import parse_satoshi_int
from pycoin.satoshi.satoshi_string import parse_satoshi_string
from pycoin.satoshi.satoshi_struct import parse_struct

class Block:
pass
SERIALIZE_NO_MWEB = 0x20000000



class LTCTx(Tx):

@classmethod
def parse(class_, f, use_mweb_extensions, allow_segwit=None):
"""Parse a Bitcoin transaction Tx.
:param f: a file-like object that contains a binary streamed transaction
:param allow_segwit: (optional) set to True to allow parsing of segwit transactions.
The default value is defined by the class variable ALLOW_SEGWIT
"""
if allow_segwit is None:
allow_segwit = class_.ALLOW_SEGWIT
version, = parse_struct("L", f)
if version == 2:
return None
v1 = ord(f.read(1))
is_segwit = allow_segwit and (v1 == 0)
v2 = None
if is_segwit:
flag = f.read(1)
if flag == b'\0':
raise ValueError("bad flag in segwit")
if flag == b'\1':
v1 = None
else:
is_segwit = False
v2 = ord(flag)
count = parse_satoshi_int(f, v=v1)
txs_in = []
for i in range(count):
txs_in.append(class_.TxIn.parse(f))
count = parse_satoshi_int(f, v=v2)
txs_out = []
for i in range(count):
txs_out.append(class_.TxOut.parse(f))

if is_segwit:
for tx_in in txs_in:
stack = []
count = parse_satoshi_int(f)
for i in range(count):
stack.append(parse_satoshi_string(f))
tx_in.witness = stack
lock_time, = parse_struct("L", f)
return class_(version, txs_in, txs_out, lock_time)



class LTCBlock(Block):

Tx = LTCTx

@classmethod
def parse(class_, f, include_transactions=True, include_offsets=None, check_merkle_hash=False):
"""
Parse the Block from the file-like object
"""
breakpoint()
block = class_.parse_as_header(f)
if include_transactions:
count = parse_struct("I", f)[0]
mweb = (block.version & SERIALIZE_NO_MWEB) != 0
txs = block._parse_transactions(f, count, use_mweb_extensions=mweb, include_offsets=include_offsets)
block.set_txs(txs, check_merkle_hash=check_merkle_hash)
return block

@classmethod
def _parse_transactions(class_, f, count, use_mweb_extensions, include_offsets=None):
txs = []
try:
for i in range(count):
if include_offsets:
offset_in_block = f.tell()
tx = class_.Tx.parse(f, use_mweb_extensions=use_mweb_extensions)
if i == count - 1 and use_mweb_extensions:
if tx is None:
break
txs.append(tx)
if include_offsets:
tx.offset_in_block = offset_in_block
except struct.error:
if include_offsets:
print(offset_in_block)
breakpoint()
pass

return txs
51 changes: 50 additions & 1 deletion pycoin/symbols/ltc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
import struct

from pycoin.coins.litecoin import LTCTx, LTCBlock
from pycoin.networks.bitcoinish import create_bitcoinish_network
from pycoin.satoshi.satoshi_struct import parse_struct

#LTCTx = Tx


if 0:
class LTCBlock(Block):

Tx = LTCTx

@classmethod
def parse(class_, f, include_transactions=True, include_offsets=None, check_merkle_hash=True):
"""
Parse the Block from the file-like object
"""
breakpoint()
block = class_.parse_as_header(f)
if include_transactions:
count = parse_struct("I", f)[0]
mweb = (block.version & SERIALIZE_NO_MWEB) != 0
txs = block._parse_transactions(f, count, use_mweb_extensions=mweb, include_offsets=include_offsets)
block.set_txs(txs, check_merkle_hash=check_merkle_hash)
return block

@classmethod
def _parse_transactions(class_, f, count, use_mweb_extensions, include_offsets=None):
txs = []
try:
for i in range(count):
if include_offsets:
offset_in_block = f.tell()
tx = class_.Tx.parse(f, use_mweb_extensions=use_mweb_extensions)
if i == count - 1 and use_mweb_extensions:
if tx is None:
break
txs.append(tx)
if include_offsets:
tx.offset_in_block = offset_in_block
except struct.error as ex:
if include_offsets:
print(offset_in_block)
breakpoint()
pass

return txs


network = create_bitcoinish_network(
network_name="Litecoin", symbol="LTC", subnet_name="mainnet",
wif_prefix_hex="b0", sec_prefix="LTCSEC:", address_prefix_hex="30", pay_to_script_prefix_hex="32",
bip32_prv_prefix_hex="019d9cfe", bip32_pub_prefix_hex="019da462", bech32_hrp="ltc")
bip32_prv_prefix_hex="019d9cfe", bip32_pub_prefix_hex="019da462", bech32_hrp="ltc",
block=LTCBlock)

0 comments on commit e179c8a

Please sign in to comment.