Skip to content

Commit

Permalink
Use segwit as default for Address class
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 16, 2024
1 parent 7c72d94 commit 00b8029
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
12 changes: 4 additions & 8 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,11 @@ def __init__(self, data='', hashed_data='', prefix=None, script_type=None,
self.address_index = address_index

if self.encoding is None:
# FIXME: Address should default to segwit if nothing is provided
# if self.script_type in ['p2pkh', 'p2sh', 'multisig', 'p2pk'] or self.witness_type == 'legacy':
# self.encoding = 'base58'
# else:
# self.encoding = 'bech32'
if self.script_type in ['p2wpkh', 'p2wsh', 'p2tr'] or self.witness_type == 'segwit':
self.encoding = 'bech32'
else:
if (self.script_type in ['p2pkh', 'p2sh', 'multisig', 'p2pk'] or self.witness_type == 'legacy' or
self.witness_type == 'p2sh-segwit'):
self.encoding = 'base58'
else:
self.encoding = 'bech32'
self.hash_bytes = to_bytes(hashed_data)
self.prefix = prefix
self.redeemscript = b''
Expand Down
5 changes: 2 additions & 3 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,9 +1727,8 @@ def _new_key_multisig(self, public_keys, name, account_id, change, cosigner_id,
# todo: pass key object, reuse key objects
redeemscript = Script(script_types=['multisig'], keys=public_key_list,
sigs_required=self.multisig_n_required).serialize()
script_type = 'p2sh'
if witness_type == 'p2sh-segwit':
script_type = 'p2sh_p2wsh'
script_type = 'p2sh' if witness_type == 'legacy' else \
('p2sh_p2wsh' if witness_type == 'p2sh-segwit' else 'p2wsh')
address = Address(redeemscript, script_type=script_type, network=network, witness_type=witness_type)
already_found_key = self.session.query(DbKey).filter_by(wallet_id=self.wallet_id,
address=address.address).first()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,10 @@ def test_keys_address_import_conversion(self):

def test_keys_address_encodings(self):
pk = '7cc7ed043b4240945e744387f8943151de86843025682bf40fa94ef086eeb686'
a = Address(pk, network='testnet')
a = Address(pk, network='testnet', witness_type='legacy')
self.assertEqual(a.address, 'mmAXD1HJtV9pdffPvBJkuT4qQrbFMwb4pR')
self.assertEqual(a.with_prefix(b'\x88'), 'wpcbpijWdzjj5W9ZXfdj2asW9U2q7gYCmw')
a = Address(pk, script_type='p2sh', network='testnet')
a = Address(pk, script_type='p2sh', network='testnet', witness_type='legacy')
self.assertEqual(a.address, '2MxtnuEcoEpYJ9WWkzqcr87ChujVRk1DFsZ')
a = Address(pk, encoding='bech32', network='testnet')
self.assertEqual(a.address, 'tb1q8hehumvm039nxnwwtqdjr7qmm46sfxrdw7vc3g')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ def test_transaction_create_with_address_objects(self):
transaction_output = Output(value=91234, address=addr)
t = Transaction([transaction_input], [transaction_output])
self.assertEqual(t.inputs[0].address, "1MMMMSUb1piy2ufrSguNUdFmAcvqrQF8M5")
self.assertEqual(t.outputs[0].address, "1KKKK6N21XKo48zWKuQKXdvSsCf95ibHFa")
self.assertEqual(t.outputs[0].address, "bc1qer5sn9k8ccyqacrzs3sqc6zwmyzdznzupzevph")

def test_transaction_info(self):
t = Transaction()
Expand Down

0 comments on commit 00b8029

Please sign in to comment.