diff --git a/bitcoinlib/keys.py b/bitcoinlib/keys.py index cf201d38..79afd289 100644 --- a/bitcoinlib/keys.py +++ b/bitcoinlib/keys.py @@ -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. @@ -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 @@ -570,7 +572,7 @@ 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' @@ -578,7 +580,7 @@ def __init__(self, data='', hashed_data='', prefix=None, script_type=None, 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 diff --git a/bitcoinlib/transactions.py b/bitcoinlib/transactions.py index bf984016..fb205cd2 100644 --- a/bitcoinlib/transactions.py +++ b/bitcoinlib/transactions.py @@ -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 @@ -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 @@ -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 @@ -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) @@ -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() @@ -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