Skip to content

Commit

Permalink
[REV] Remove witness_type from create key methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Oct 13, 2023
1 parent 9e6a1d9 commit b807b1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class DbWallet(Base):
__table_args__ = (
CheckConstraint(scheme.in_(['single', 'bip32']), name='constraint_allowed_schemes'),
CheckConstraint(encoding.in_(['base58', 'bech32']), name='constraint_default_address_encodings_allowed'),
CheckConstraint(witness_type.in_(['legacy', 'segwit', 'p2sh-segwit', 'p2tr']),
CheckConstraint(witness_type.in_(['legacy', 'segwit', 'p2sh-segwit', 'p2tr', 'mixed']),
name='wallet_constraint_allowed_types'),
)

Expand Down
31 changes: 20 additions & 11 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,9 @@ def _create(cls, name, key, owner, network, account_id, purpose, scheme, parent_
_logger.info("Create new wallet '%s'" % name)
if not name:
raise WalletError("Please enter wallet name")
wallet_witness_type = witness_type
if witness_type == 'mixed':
witness_type = DEFAULT_WITNESS_TYPE

if not isinstance(key_path, list):
key_path = key_path.split('/')
Expand All @@ -1062,7 +1065,7 @@ def _create(cls, name, key, owner, network, account_id, purpose, scheme, parent_
key_path = '/'.join(key_path)
session.merge(DbNetwork(name=network))
new_wallet = DbWallet(name=name, owner=owner, network_name=network, purpose=purpose, scheme=scheme,
sort_keys=sort_keys, witness_type=witness_type, parent_id=parent_id, encoding=encoding,
sort_keys=sort_keys, witness_type=wallet_witness_type, parent_id=parent_id, encoding=encoding,
multisig=multisig, multisig_n_required=sigs_required, cosigner_id=cosigner_id,
key_path=key_path)
session.add(new_wallet)
Expand Down Expand Up @@ -1191,8 +1194,11 @@ def create(cls, name, keys=None, owner='', network=None, account_id=0, purpose=0
multisig = False
if scheme not in ['bip32', 'single']:
raise WalletError("Only bip32 or single key scheme's are supported at the moment")
if witness_type not in [None, 'legacy', 'p2sh-segwit', 'segwit']:
if witness_type not in [None, 'legacy', 'p2sh-segwit', 'segwit', 'mixed']:
raise WalletError("Witness type %s not supported at the moment" % witness_type)
wallet_witness_type = witness_type
if witness_type == 'mixed':
witness_type = DEFAULT_WITNESS_TYPE
if name.isdigit():
raise WalletError("Wallet name '%s' invalid, please include letter characters" % name)

Expand Down Expand Up @@ -1256,12 +1262,13 @@ def create(cls, name, keys=None, owner='', network=None, account_id=0, purpose=0
raise WalletError("Pure segwit addresses are not supported for Dogecoin wallets. "
"Please use p2sh-segwit instead")

witness_type_keys = witness_type if witness_type != 'mixed' else 'segwit'
if not key_path:
if scheme == 'single':
key_path = ['m']
purpose = 0
else:
ks = [k for k in WALLET_KEY_STRUCTURES if k['witness_type'] == witness_type and
ks = [k for k in WALLET_KEY_STRUCTURES if k['witness_type'] == witness_type_keys and
k['multisig'] == multisig and k['purpose'] is not None]
if len(ks) > 1:
raise WalletError("Please check definitions in WALLET_KEY_STRUCTURES. Multiple options found for "
Expand All @@ -1275,7 +1282,7 @@ def create(cls, name, keys=None, owner='', network=None, account_id=0, purpose=0
if purpose is None:
purpose = 0
if not encoding:
encoding = get_encoding_from_witness(witness_type)
encoding = get_encoding_from_witness(witness_type_keys)

if multisig:
key = ''
Expand All @@ -1299,7 +1306,7 @@ def create(cls, name, keys=None, owner='', network=None, account_id=0, purpose=0
main_key_path = 'm'

hdpm = cls._create(name, key, owner=owner, network=network, account_id=account_id, purpose=purpose,
scheme=scheme, parent_id=None, sort_keys=sort_keys, witness_type=witness_type,
scheme=scheme, parent_id=None, sort_keys=sort_keys, witness_type=wallet_witness_type,
encoding=encoding, multisig=multisig, sigs_required=sigs_required, cosigner_id=cosigner_id,
key_path=main_key_path, db_uri=db_uri, db_cache_uri=db_cache_uri, db_password=db_password)

Expand Down Expand Up @@ -1389,7 +1396,10 @@ def __init__(self, wallet, db_uri=None, db_cache_uri=None, session=None, main_ke
self.main_key_id: self.main_key
}
self.providers = None
self.wallet_witness_type = db_wlt.witness_type
self.witness_type = db_wlt.witness_type
if db_wlt.witness_type == 'mixed':
self.witness_type = DEFAULT_WITNESS_TYPE
self.encoding = db_wlt.encoding
self.multisig = db_wlt.multisig
self.cosigner_id = db_wlt.cosigner_id
Expand Down Expand Up @@ -1716,7 +1726,7 @@ def _new_key_multisig(self, public_keys, name, account_id, change, cosigner_id,
self._commit()
return self.key(multisig_key.id)

def new_key(self, name='', account_id=None, change=0, cosigner_id=None, witness_type=None, network=None):
def new_key(self, name='', account_id=None, change=0, cosigner_id=None, network=None):
"""
Create a new HD Key derived from this wallet's masterkey. An account will be created for this wallet
with index 0 if there is no account defined yet.
Expand Down Expand Up @@ -2090,7 +2100,7 @@ def path_expand(self, path, level_offset=None, account_id=None, cosigner_id=0, a
witness_type=self.witness_type, network=network)

def key_for_path(self, path, level_offset=None, name=None, account_id=None, cosigner_id=None,
address_index=0, change=0, witness_type=None, network=None, recreate=False):
address_index=0, change=0, network=None, recreate=False):
"""
Return key for specified path. Derive all wallet keys in path if they not already exists
Expand Down Expand Up @@ -2135,7 +2145,6 @@ def key_for_path(self, path, level_offset=None, name=None, account_id=None, cosi
"""

network, account_id, _ = self._get_account_defaults(network, account_id)
witness_type = self.witness_type if not witness_type else witness_type
cosigner_id = cosigner_id if cosigner_id is not None else self.cosigner_id
level_offset_key = level_offset
if level_offset and self.main_key and level_offset > 0:
Expand All @@ -2146,7 +2155,7 @@ def key_for_path(self, path, level_offset=None, name=None, account_id=None, cosi
key_path = self.cosigner[cosigner_id].key_path
fullpath = path_expand(path, key_path, level_offset_key, account_id=account_id, cosigner_id=cosigner_id,
purpose=self.purpose, address_index=address_index, change=change,
witness_type=witness_type, network=network)
witness_type=self.witness_type, network=network)

if self.multisig and self.cosigner:
public_keys = []
Expand Down Expand Up @@ -2202,7 +2211,7 @@ def key_for_path(self, path, level_offset=None, name=None, account_id=None, cosi
key_name = key_name.replace("'", "").replace("_", " ")
nk = WalletKey.from_key(key=ck, name=key_name, wallet_id=self.wallet_id, account_id=account_id,
change=change, purpose=self.purpose, path=newpath, parent_id=parent_id,
encoding=self.encoding, witness_type=witness_type,
encoding=self.encoding, witness_type=self.witness_type,
cosigner_id=cosigner_id, network=network, session=self._session)
self._key_objects.update({nk.key_id: nk})
parent_id = nk.key_id
Expand Down Expand Up @@ -4167,7 +4176,7 @@ def info(self, detail=3):
if self.multisig:
print(" Multisig Wallet IDs %s" % str([w.wallet_id for w in self.cosigner]).strip('[]'))
print(" Cosigner ID %s" % self.cosigner_id)
print(" Witness type %s" % self.witness_type)
print(" Witness type %s" % self.wallet_witness_type)
print(" Main network %s" % self.network.name)
print(" Latest update %s" % self.last_updated)

Expand Down

0 comments on commit b807b1e

Please sign in to comment.