Skip to content

Commit

Permalink
Rename script to locking_script
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 16, 2024
1 parent 00b8029 commit 7171d5f
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bitcoinlib/services/bitaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _parse_transaction(self, tx):
sequence=ti['sequence'], index_n=int(n), value=0)
else:
t.add_input(prev_txid=ti['txId'], output_n=ti['vOut'], unlocking_script=ti['scriptSig'],
unlocking_script_unsigned=ti['scriptPubKey'], witnesses=ti.get('txInWitness', []),
locking_script=ti['scriptPubKey'], witnesses=ti.get('txInWitness', []),
address='' if 'address' not in ti else ti['address'], sequence=ti['sequence'],
index_n=int(n), value=ti['amount'], strict=self.strict)

Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/blockchair.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def gettransaction(self, tx_id):
else:
t.add_input(prev_txid=ti['transaction_hash'], output_n=ti['index'],
unlocking_script=ti['spending_signature_hex'], index_n=index_n, value=ti['value'],
address=ti['recipient'], unlocking_script_unsigned=ti['script_hex'],
address=ti['recipient'], locking_script=ti['script_hex'],
sequence=ti['spending_sequence'], strict=self.strict)
index_n += 1
for to in res['data'][tx_id]['outputs']:
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/blocksmurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _parse_transaction(self, tx, block_height=None):
public_hash=bytes.fromhex(ti['public_hash']), address=ti['address'],
witness_type=ti['witness_type'], locktime_cltv=ti['locktime_cltv'],
locktime_csv=ti['locktime_csv'], signatures=ti['signatures'], compressed=ti['compressed'],
encoding=ti['encoding'], unlocking_script_unsigned=ti['script_code'],
encoding=ti['encoding'], locking_script=ti['script_code'],
sigs_required=ti['sigs_required'], sequence=ti['sequence'],
witnesses=[bytes.fromhex(w) for w in ti['witnesses']], script_type=ti['script_type'],
strict=self.strict)
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/blockstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _parse_transaction(self, tx):
unlocking_script=ti['scriptsig'], value=ti['prevout']['value'],
address='' if 'scriptpubkey_address' not in ti['prevout']
else ti['prevout']['scriptpubkey_address'], sequence=ti['sequence'],
unlocking_script_unsigned=ti['prevout']['scriptpubkey'], witnesses=witnesses, strict=self.strict)
locking_script=ti['prevout']['scriptpubkey'], witnesses=witnesses, strict=self.strict)
index_n += 1
index_n = 0
if len(tx['vout']) > 101:
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _parse_transaction(self, tx):
t.add_input(prev_txid=ti['txid'], output_n=ti['vout'],
unlocking_script=ti['scriptsig'], value=ti['prevout']['value'],
address=ti['prevout'].get('scriptpubkey_address', ''),
unlocking_script_unsigned=ti['prevout']['scriptpubkey'], sequence=ti['sequence'],
locking_script=ti['prevout']['scriptpubkey'], sequence=ti['sequence'],
witnesses=None if 'witness' not in ti else [bytes.fromhex(w) for w in ti['witness']],
strict=self.strict)
for to in tx['vout']:
Expand Down
41 changes: 22 additions & 19 deletions bitcoinlib/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Input(object):
"""

def __init__(self, prev_txid, output_n, keys=None, signatures=None, public_hash=b'', unlocking_script=b'',
unlocking_script_unsigned=None, script=None, script_type=None, address='',
locking_script=None, script=None, script_type=None, address='',
sequence=0xffffffff, compressed=None, sigs_required=None, sort=False, index_n=0,
value=0, double_spend=False, locktime_cltv=None, locktime_csv=None, key_path='', witness_type=None,
witnesses=None, encoding=None, strict=True, network=DEFAULT_NETWORK):
Expand All @@ -165,8 +165,8 @@ def __init__(self, prev_txid, output_n, keys=None, signatures=None, public_hash=
:type public_hash: bytes
:param unlocking_script: Unlocking script (scriptSig) to prove ownership. Optional
:type unlocking_script: bytes, hexstring
:param unlocking_script_unsigned: Unlocking script for signing transaction
:type unlocking_script_unsigned: bytes, hexstring
:param locking_script: Unlocking script for signing transaction
:type locking_script: bytes, hexstring
:param script_type: Type of unlocking script used, i.e. p2pkh or p2sh_multisig. Default is p2pkh
:type script_type: str
:param address: Address string or object for input
Expand Down Expand Up @@ -210,8 +210,8 @@ def __init__(self, prev_txid, output_n, keys=None, signatures=None, public_hash=
self.output_n_int = int.from_bytes(output_n, 'big')
self.output_n = output_n
self.unlocking_script = b'' if unlocking_script is None else to_bytes(unlocking_script)
self.unlocking_script_unsigned = b'' if unlocking_script_unsigned is None \
else to_bytes(unlocking_script_unsigned)
self.locking_script = b'' if locking_script is None \
else to_bytes(locking_script)
self.script = None
self.hash_type = SIGHASH_ALL
if isinstance(sequence, numbers.Number):
Expand Down Expand Up @@ -294,8 +294,8 @@ def __init__(self, prev_txid, output_n, keys=None, signatures=None, public_hash=
elif 'p2wsh' in self.script.script_types:
self.script_type = 'p2sh_p2wsh'
self.witness_type = 'p2sh-segwit'
if self.unlocking_script_unsigned and not self.signatures:
ls = Script.parse_bytes(self.unlocking_script_unsigned, strict=strict)
if self.locking_script and not self.signatures:
ls = Script.parse_bytes(self.locking_script, strict=strict)
self.public_hash = self.public_hash if not ls.public_hash else ls.public_hash
if ls.script_types[0] in ['p2wpkh', 'p2wsh']:
self.witness_type = 'segwit'
Expand Down Expand Up @@ -417,7 +417,7 @@ def update_scripts(self, hash_type=SIGHASH_ALL):
if not self.keys and not self.public_hash:
return
self.script_code = b'\x76\xa9\x14' + self.public_hash + b'\x88\xac'
self.unlocking_script_unsigned = self.script_code
self.locking_script = self.script_code
addr_data = self.public_hash
if self.signatures and self.keys:
self.witnesses = [self.signatures[0].as_der_encoded() if hash_type else b'', self.keys[0].public_byte]
Expand All @@ -439,7 +439,7 @@ def update_scripts(self, hash_type=SIGHASH_ALL):
else:
self.public_hash = hash160(self.redeemscript)
addr_data = self.public_hash
self.unlocking_script_unsigned = self.redeemscript
# self.locking_script = self.redeemscript

if self.redeemscript and self.keys:
n_tag = self.redeemscript[0:1]
Expand Down Expand Up @@ -477,7 +477,7 @@ def update_scripts(self, hash_type=SIGHASH_ALL):
elif self.script_type == 'signature':
if self.keys:
self.script_code = varstr(self.keys[0].public_byte) + b'\xac'
self.unlocking_script_unsigned = self.script_code
self.locking_script = self.script_code
addr_data = hash160(self.keys[0].public_byte)
if self.signatures and not self.unlocking_script:
self.unlocking_script = varstr(self.signatures[0].as_der_encoded())
Expand All @@ -491,14 +491,14 @@ def update_scripts(self, hash_type=SIGHASH_ALL):
script_type=self.script_type, witness_type=self.witness_type).address

if self.locktime_cltv:
self.unlocking_script_unsigned = script_add_locktime_cltv(self.locktime_cltv,
self.unlocking_script_unsigned)
self.locking_script = script_add_locktime_cltv(self.locktime_cltv,
self.locking_script)
# if self.unlocking_script:
# self.unlocking_script = script_add_locktime_cltv(self.locktime_cltv, self.unlocking_script)
# if self.witness_type == 'segwit':
# self.witnesses.insert(0, script_add_locktime_cltv(self.locktime_cltv, b''))
if self.locktime_csv:
self.unlocking_script_unsigned = script_add_locktime_csv(self.locktime_csv, self.unlocking_script_unsigned)
self.locking_script = script_add_locktime_csv(self.locktime_csv, self.locking_script)
self.unlocking_script = script_add_locktime_csv(self.locktime_csv, self.unlocking_script)
return True

Expand Down Expand Up @@ -580,7 +580,7 @@ def as_dict(self):
'public_hash': self.public_hash.hex(),
'script_code': self.script_code.hex(),
'unlocking_script': self.unlocking_script.hex(),
'unlocking_script_unsigned': self.unlocking_script_unsigned.hex(),
'locking_script': self.locking_script.hex(),
'witness_type': self.witness_type,
'witness': b''.join(self.witnesses).hex(),
'sort': self.sort,
Expand Down Expand Up @@ -1618,7 +1618,10 @@ def raw(self, sign_id=None, hash_type=SIGHASH_ALL, witness_type=None):
r += b'\1'
r += varstr(i.unlocking_script)
elif sign_id == i.index_n:
r += varstr(i.unlocking_script_unsigned)
if i.script_type == 'p2sh_multisig':
r += varstr(i.redeemscript)
else:
r += varstr(i.locking_script)
else:
r += b'\0'
r += i.sequence.to_bytes(4, 'little')
Expand Down Expand Up @@ -1807,7 +1810,7 @@ def sign_and_update(self, index_n=None):
self.fee_per_kb = int((self.fee / float(self.vsize)) * 1000)

def add_input(self, prev_txid, output_n, keys=None, signatures=None, public_hash=b'', unlocking_script=b'',
unlocking_script_unsigned=None, script_type=None, address='',
locking_script=None, script_type=None, address='',
sequence=0xffffffff, compressed=True, sigs_required=None, sort=False, index_n=None,
value=None, double_spend=False,locktime_cltv=None, locktime_csv=None,
key_path='', witness_type=None, witnesses=None, encoding=None, strict=True):
Expand All @@ -1828,8 +1831,8 @@ def add_input(self, prev_txid, output_n, keys=None, signatures=None, public_hash
:type public_hash: bytes
:param unlocking_script: Unlocking script (scriptSig) to prove ownership. Optional
:type unlocking_script: bytes, hexstring
:param unlocking_script_unsigned: TODO: find better name...
:type unlocking_script_unsigned: bytes, str
:param locking_script: TODO: find better name...
:type locking_script: bytes, str
:param script_type: Type of unlocking script used, i.e. p2pkh or p2sh_multisig. Default is p2pkh
:type script_type: str
:param address: Specify address of input if known, default is to derive from key or scripts
Expand Down Expand Up @@ -1876,7 +1879,7 @@ def add_input(self, prev_txid, output_n, keys=None, signatures=None, public_hash
self.version_int = 2
self.inputs.append(
Input(prev_txid=prev_txid, output_n=output_n, keys=keys, signatures=signatures, public_hash=public_hash,
unlocking_script=unlocking_script, unlocking_script_unsigned=unlocking_script_unsigned,
unlocking_script=unlocking_script, locking_script=locking_script,
script_type=script_type, address=address, sequence=sequence, compressed=compressed,
sigs_required=sigs_required, sort=sort, index_n=index_n, value=value, double_spend=double_spend,
locktime_cltv=locktime_cltv, locktime_csv=locktime_csv, key_path=key_path, witness_type=witness_type,
Expand Down
6 changes: 3 additions & 3 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3795,7 +3795,7 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
for inp in input_arr:
locktime_cltv = None
locktime_csv = None
unlocking_script_unsigned = None
locking_script = None
unlocking_script_type = ''
if isinstance(inp, Input):
prev_txid = inp.prev_txid
Expand All @@ -3804,7 +3804,7 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
value = inp.value
signatures = inp.signatures
unlocking_script = inp.unlocking_script
unlocking_script_unsigned = inp.unlocking_script_unsigned
locking_script = inp.locking_script
unlocking_script_type = inp.script_type
address = inp.address
sequence = inp.sequence
Expand Down Expand Up @@ -3862,7 +3862,7 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
sigs_required=self.multisig_n_required, sort=self.sort_keys,
compressed=key.compressed, value=value, signatures=signatures,
unlocking_script=unlocking_script, address=address,
unlocking_script_unsigned=unlocking_script_unsigned,
locking_script=locking_script,
sequence=sequence, locktime_cltv=locktime_cltv, locktime_csv=locktime_csv,
witness_type=witness_type, key_path=key.path)
# Calculate fees
Expand Down
2 changes: 1 addition & 1 deletion tests/import_test.tx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'sigs_required': 2,
'sort': True,
'unlocking_script': '',
'unlocking_script_unsigned': '52210289d3f95b15f53c666a4b70391e9a7cf6c771f6177d745557750a4160929a932e210331271d364803fd05e4a5b95acb2b0f200e9634dd75e95a577477762b8dacbcd32103547034e1e807362c5edd66d6951381ac2bde926b5244d5ce9cb1a82a4240bc8953ae',
'locking_script': '52210289d3f95b15f53c666a4b70391e9a7cf6c771f6177d745557750a4160929a932e210331271d364803fd05e4a5b95acb2b0f200e9634dd75e95a577477762b8dacbcd32103547034e1e807362c5edd66d6951381ac2bde926b5244d5ce9cb1a82a4240bc8953ae',
'valid': None,
'value': 100000000,
'witness': '',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def test_service_gettransaction_segwit_p2wpkh(self):
'script_code': '76a9140ca7deb0a467679f0011efb2906a6e528a8d22ef88ac',
'sequence': 4294967295,
'sigs_required': 1,
'unlocking_script_unsigned': '76a9140ca7deb0a467679f0011efb2906a6e528a8d22ef88ac',
'locking_script': '76a9140ca7deb0a467679f0011efb2906a6e528a8d22ef88ac',
'value': 506323064}
],
'locktime': 0,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_transaction_input_add_public_key(self):

def test_transaction_input_locking_script(self):
ph = "81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5bd4404d48"
ti = Input(ph, 0, unlocking_script_unsigned='76a91423e102597c4a99516f851406f935a6e634dbccec88ac',
ti = Input(ph, 0, locking_script='76a91423e102597c4a99516f851406f935a6e634dbccec88ac',
witness_type='legacy')
self.assertEqual(ti.address, '14GiCdJHj3bznWpcocjcu9ByCmDPEhEoP8')

Expand Down Expand Up @@ -1638,7 +1638,7 @@ def test_transaction_locktime_cltv(self):
pass
# rawtx = ''
# print(t.raw_hex())
# print(t.inputs[0].unlocking_script_unsigned)
# print(t.inputs[0].locking_script)

def test_transaction_cltv_error(self):
# TODO
Expand Down Expand Up @@ -1827,7 +1827,7 @@ def test_transaction_segwit_redeemscript_bug(self):
keys=['0236ab0a160e381d2eb6f01119937d29e697b78ca8be115617db972e0d95cef6b7',
'038b46795c92b8c9a6b40644a082be367e740f09b581a8b8c26b71d4b39f5cd963',
'03f93f3a5633478ed3ab3ff9d2fe5ddeecb8e755fb267c9a3bc9d7242f58e4f258'],
unlocking_script_unsigned='52210236ab0a160e381d2eb6f01119937d29e697b78ca8be115617db972e0d95ce'
locking_script='52210236ab0a160e381d2eb6f01119937d29e697b78ca8be115617db972e0d95ce'
'f6b721038b46795c92b8c9a6b40644a082be367e740f09b581a8b8c26b71d4b39f'
'5cd9632103f93f3a5633478ed3ab3ff9d2fe5ddeecb8e755fb267c9a3bc9d7242f'
'58e4f25853ae',
Expand Down

0 comments on commit 7171d5f

Please sign in to comment.