Skip to content

Commit

Permalink
Merge pull request #311 from 1200wd/fix_litecoin_pt2r_addresses
Browse files Browse the repository at this point in the history
Fix litecoin pt2r addresses
  • Loading branch information
mccwdev committed Jul 7, 2023
2 parents 19309ed + f0d0121 commit 331f45a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def parse(cls, address, compressed=None, encoding=None, depth=None, change=None,
address_index=address_index, network=network, network_overrides=network_overrides)

def __init__(self, data='', hashed_data='', prefix=None, script_type=None,
compressed=None, encoding=None, witness_type=None, depth=None, change=None,
compressed=None, encoding=None, witness_type=None, witver=0, depth=None, change=None,
address_index=None, network=DEFAULT_NETWORK, network_overrides=None):
"""
Initialize an Address object. Specify a public key, redeemscript or a hash.
Expand All @@ -550,6 +550,8 @@ def __init__(self, data='', hashed_data='', prefix=None, script_type=None,
:type prefix: str, bytes
:param script_type: Type of script, i.e. p2sh or p2pkh.
:type script_type: str
:param witver: Witness version. Used for p2tr addresses
:type witver: int
:param encoding: Address encoding. Default is base58 encoding, for native segwit addresses specify bech32 encoding
:type encoding: str
:param witness_type: Specify 'legacy', 'segwit' or 'p2sh-segwit'. Legacy for old-style bitcoin addresses, segwit for native segwit addresses and p2sh-segwit for segwit embedded in a p2sh script. Leave empty to derive automatically from script type if possible
Expand All @@ -570,15 +572,15 @@ def __init__(self, data='', hashed_data='', prefix=None, script_type=None,
self.script_type = script_type
self.encoding = encoding
self.compressed = compressed
self.witver = 0
self.witver = witver
if witness_type is None:
if self.script_type in ['p2wpkh', 'p2wsh']:
witness_type = 'segwit'
elif self.script_type in ['p2sh_p2wpkh', 'p2sh_p2wsh']:
witness_type = 'p2sh-segwit'
elif self.script_type == 'p2tr':
witness_type = 'taproot'
self.witver = 1
self.witver = 1 if self.witver == 0 else self.witver
self.witness_type = witness_type
self.depth = depth
self.change = change
Expand Down
16 changes: 8 additions & 8 deletions bitcoinlib/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,8 @@ class Output(object):
"""

def __init__(self, value, address='', public_hash=b'', public_key=b'', lock_script=b'', spent=False,
output_n=0, script_type=None, encoding=None, spending_txid='', spending_index_n=None, strict=True,
network=DEFAULT_NETWORK):
output_n=0, script_type=None, witver=0, encoding=None, spending_txid='', spending_index_n=None,
strict=True, network=DEFAULT_NETWORK):
"""
Create a new transaction output
Expand All @@ -1125,6 +1125,8 @@ def __init__(self, value, address='', public_hash=b'', public_key=b'', lock_scri
:type output_n: int
:param script_type: Script type of output (p2pkh, p2sh, segwit p2wpkh, etc). Extracted from lock_script if provided.
:type script_type: str
:param witver: Witness version
:type witver: int
:param encoding: Address encoding used. For example bech32/base32 or base58. Leave empty to derive from address or default base58 encoding
:type encoding: str
:param spending_txid: Transaction hash of input spending this transaction output
Expand Down Expand Up @@ -1171,6 +1173,7 @@ def __init__(self, value, address='', public_hash=b'', public_key=b'', lock_scri
self.spent = spent
self.output_n = output_n
self.script = Script.parse_bytes(self.lock_script, strict=strict)
self.witver = witver

if self._address_obj:
self.script_type = self._address_obj.script_type if script_type is None else script_type
Expand All @@ -1185,6 +1188,8 @@ def __init__(self, value, address='', public_hash=b'', public_key=b'', lock_scri
self.public_hash = self.script.public_hash
if self.script.keys:
self.public_key = self.script.keys[0].public_hex
if self.script_type == 'p2tr':
self.witver = self.script.commands[0] - 80

if self.public_key and not self.public_hash:
k = Key(self.public_key, is_private=False, network=network)
Expand Down Expand Up @@ -1213,11 +1218,6 @@ def __init__(self, value, address='', public_hash=b'', public_key=b'', lock_scri
self.script_type = 'p2pkh'
if self.encoding == 'bech32':
self.script_type = 'p2wpkh'
# if self.public_hash and not self._address:
# self.address_obj = Address(hashed_data=self.public_hash, script_type=self.script_type,
# encoding=self.encoding, network=self.network)
# self.address = self.address_obj.address
# self.versionbyte = self.address_obj.prefix
if not self.script and strict and (self.public_hash or self.public_key):
self.script = Script(script_types=[self.script_type], public_hash=self.public_hash, keys=[self.public_key])
self.lock_script = self.script.serialize()
Expand All @@ -1240,7 +1240,7 @@ def address_obj(self):
if not self._address_obj:
if self.public_hash:
self._address_obj = Address(hashed_data=self.public_hash, script_type=self.script_type,
encoding=self.encoding, network=self.network)
witver=self.witver, encoding=self.encoding, network=self.network)
self._address = self._address_obj.address
self.versionbyte = self._address_obj.prefix
return self._address_obj
Expand Down

0 comments on commit 331f45a

Please sign in to comment.