Browse Source

Rename functions to say "key" instead of "privkey"

Watchonly wallets use pubkeys instead of privkeys, but in a bit of
hack the functions previously called "_get_priv_from_path" would
actually return public keys for watchonly wallets. This could have
pretty terrible consequences one day, so functions like that have
been renamed to use the word "key" instead, which could be either
private or public.
master
chris-belcher 6 years ago
parent
commit
ddb32ce245
No known key found for this signature in database
GPG Key ID: EF734EA677F31129
  1. 10
      jmclient/jmclient/cryptoengine.py
  2. 2
      jmclient/jmclient/taker_utils.py
  3. 38
      jmclient/jmclient/wallet.py
  4. 2
      jmclient/jmclient/wallet_service.py
  5. 2
      jmclient/jmclient/wallet_utils.py
  6. 4
      jmclient/test/test_wallet.py

10
jmclient/jmclient/cryptoengine.py

@ -155,7 +155,7 @@ class BTCEngine(object):
return key return key
@classmethod @classmethod
def privkey_to_script(cls, privkey): def key_to_script(cls, privkey):
pub = cls.privkey_to_pubkey(privkey) pub = cls.privkey_to_pubkey(privkey)
return cls.pubkey_to_script(pub) return cls.pubkey_to_script(pub)
@ -165,7 +165,7 @@ class BTCEngine(object):
@classmethod @classmethod
def privkey_to_address(cls, privkey): def privkey_to_address(cls, privkey):
script = cls.privkey_to_script(privkey) script = cls.key_to_script(privkey)
return btc.script_to_address(script, cls.VBYTE) return btc.script_to_address(script, cls.VBYTE)
@classmethod @classmethod
@ -311,7 +311,7 @@ class BTC_Timelocked_P2WSH(BTCEngine):
return btc.BTC_P2PK_VBYTE["testnet"] return btc.BTC_P2PK_VBYTE["testnet"]
@classmethod @classmethod
def privkey_to_script(cls, privkey_locktime): def key_to_script(cls, privkey_locktime):
privkey, locktime = privkey_locktime privkey, locktime = privkey_locktime
pub = cls.privkey_to_pubkey(privkey) pub = cls.privkey_to_pubkey(privkey)
return cls.pubkey_to_script((pub, locktime)) return cls.pubkey_to_script((pub, locktime))
@ -362,7 +362,7 @@ class BTC_Watchonly_Timelocked_P2WSH(BTC_Timelocked_P2WSH):
path))[-1] path))[-1]
@classmethod @classmethod
def privkey_to_script(cls, pubkey_locktime): def key_to_script(cls, pubkey_locktime):
pub, locktime = pubkey_locktime pub, locktime = pubkey_locktime
return cls.pubkey_to_script((pub, locktime)) return cls.pubkey_to_script((pub, locktime))
@ -388,7 +388,7 @@ class BTC_Watchonly_P2SH_P2WPKH(BTC_P2SH_P2WPKH):
@staticmethod @staticmethod
def privkey_to_pubkey(privkey): def privkey_to_pubkey(privkey):
#in watchonly wallets there are no privkeys, so functions #in watchonly wallets there are no privkeys, so functions
# like _get_priv_from_path() actually return pubkeys and # like _get_key_from_path() actually return pubkeys and
# this function is a noop # this function is a noop
return privkey return privkey

2
jmclient/jmclient/taker_utils.py

@ -87,7 +87,7 @@ def direct_send(wallet_service, amount, mixdepth, destination, answeryes=False,
address_type = FidelityBondMixin.BIP32_BURN_ID address_type = FidelityBondMixin.BIP32_BURN_ID
index = wallet_service.wallet.get_next_unused_index(mixdepth, address_type) index = wallet_service.wallet.get_next_unused_index(mixdepth, address_type)
path = wallet_service.wallet.get_path(mixdepth, address_type, index) path = wallet_service.wallet.get_path(mixdepth, address_type, index)
privkey, engine = wallet_service.wallet._get_priv_from_path(path) privkey, engine = wallet_service.wallet._get_key_from_path(path)
pubkey = engine.privkey_to_pubkey(privkey) pubkey = engine.privkey_to_pubkey(privkey)
pubkeyhash = bin_hash160(pubkey) pubkeyhash = bin_hash160(pubkey)

38
jmclient/jmclient/wallet.py

@ -439,7 +439,7 @@ class BaseWallet(object):
for index, (script, amount) in scripts.items(): for index, (script, amount) in scripts.items():
assert amount > 0 assert amount > 0
path = self.script_to_path(script) path = self.script_to_path(script)
privkey, engine = self._get_priv_from_path(path) privkey, engine = self._get_key_from_path(path)
tx = btc.deserialize(engine.sign_transaction(tx, index, privkey, tx = btc.deserialize(engine.sign_transaction(tx, index, privkey,
amount, **kwargs)) amount, **kwargs))
return tx return tx
@ -451,7 +451,7 @@ class BaseWallet(object):
""" """
script = self._ENGINE.address_to_script(addr) script = self._ENGINE.address_to_script(addr)
path = self.script_to_path(script) path = self.script_to_path(script)
privkey = self._get_priv_from_path(path)[0] privkey = self._get_key_from_path(path)[0]
return hexlify(privkey).decode('ascii') return hexlify(privkey).decode('ascii')
def _get_addr_int_ext(self, address_type, mixdepth): def _get_addr_int_ext(self, address_type, mixdepth):
@ -499,7 +499,7 @@ class BaseWallet(object):
def script_to_addr(self, script): def script_to_addr(self, script):
assert self.is_known_script(script) assert self.is_known_script(script)
path = self.script_to_path(script) path = self.script_to_path(script)
engine = self._get_priv_from_path(path)[1] engine = self._get_key_from_path(path)[1]
return engine.script_to_address(script) return engine.script_to_address(script)
def get_script_code(self, script): def get_script_code(self, script):
@ -511,7 +511,7 @@ class BaseWallet(object):
For non-segwit wallets, raises EngineError. For non-segwit wallets, raises EngineError.
""" """
path = self.script_to_path(script) path = self.script_to_path(script)
priv, engine = self._get_priv_from_path(path) priv, engine = self._get_key_from_path(path)
pub = engine.privkey_to_pubkey(priv) pub = engine.privkey_to_pubkey(priv)
return engine.pubkey_to_script_code(pub) return engine.pubkey_to_script_code(pub)
@ -549,7 +549,7 @@ class BaseWallet(object):
return self.get_wif_path(self.get_path(mixdepth, address_type, index)) return self.get_wif_path(self.get_path(mixdepth, address_type, index))
def get_wif_path(self, path): def get_wif_path(self, path):
priv, engine = self._get_priv_from_path(path) priv, engine = self._get_key_from_path(path)
return engine.privkey_to_wif(priv) return engine.privkey_to_wif(priv)
def get_path(self, mixdepth=None, address_type=None, index=None): def get_path(self, mixdepth=None, address_type=None, index=None):
@ -830,7 +830,7 @@ class BaseWallet(object):
path = self.get_path(mixdepth, address_type, index) path = self.get_path(mixdepth, address_type, index)
return self.get_script_from_path(path) return self.get_script_from_path(path)
def _get_priv_from_path(self, path): def _get_key_from_path(self, path):
raise NotImplementedError() raise NotImplementedError()
def get_path_repr(self, path): def get_path_repr(self, path):
@ -885,7 +885,7 @@ class BaseWallet(object):
returns: returns:
signature as base64-encoded string signature as base64-encoded string
""" """
priv, engine = self._get_priv_from_path(path) priv, engine = self._get_key_from_path(path)
return engine.sign_message(priv, message) return engine.sign_message(priv, message)
def get_wallet_name(self): def get_wallet_name(self):
@ -1110,7 +1110,7 @@ class ImportWalletMixin(object):
#key_type = key_type_wif if key_type_wif is not None else self.TYPE #key_type = key_type_wif if key_type_wif is not None else self.TYPE
engine = self._ENGINES[key_type] engine = self._ENGINES[key_type]
if engine.privkey_to_script(privkey) in self._script_map: if engine.key_to_script(privkey) in self._script_map:
raise WalletError("Cannot import key, already in wallet: {}" raise WalletError("Cannot import key, already in wallet: {}"
"".format(wif)) "".format(wif))
@ -1155,7 +1155,7 @@ class ImportWalletMixin(object):
engine = self._ENGINES[key_type] engine = self._ENGINES[key_type]
path = (self._IMPORTED_ROOT_PATH, mixdepth, index) path = (self._IMPORTED_ROOT_PATH, mixdepth, index)
self._script_map[engine.privkey_to_script(privkey)] = path self._script_map[engine.key_to_script(privkey)] = path
return path return path
@ -1166,9 +1166,9 @@ class ImportWalletMixin(object):
assert len(path) == 3 assert len(path) == 3
return path[1] return path[1]
def _get_priv_from_path(self, path): def _get_key_from_path(self, path):
if not self._is_imported_path(path): if not self._is_imported_path(path):
return super(ImportWalletMixin, self)._get_priv_from_path(path) return super(ImportWalletMixin, self)._get_key_from_path(path)
assert len(path) == 3 assert len(path) == 3
md, i = path[1], path[2] md, i = path[1], path[2]
@ -1220,8 +1220,8 @@ class ImportWalletMixin(object):
if not self._is_imported_path(path): if not self._is_imported_path(path):
return super(ImportWalletMixin, self).get_script_from_path(path) return super(ImportWalletMixin, self).get_script_from_path(path)
priv, engine = self._get_priv_from_path(path) priv, engine = self._get_key_from_path(path)
return engine.privkey_to_script(priv) return engine.key_to_script(priv)
class BIP39WalletMixin(object): class BIP39WalletMixin(object):
@ -1410,8 +1410,8 @@ class BIP32Wallet(BaseWallet):
#concept of a "next address" cant be used #concept of a "next address" cant be used
return self.get_new_script_override_disable(md, address_type) return self.get_new_script_override_disable(md, address_type)
priv, engine = self._get_priv_from_path(path) priv, engine = self._get_key_from_path(path)
script = engine.privkey_to_script(priv) script = engine.key_to_script(priv)
return script return script
@ -1478,7 +1478,7 @@ class BIP32Wallet(BaseWallet):
return path[len(self._get_bip32_base_path())] return path[len(self._get_bip32_base_path())]
def _get_priv_from_path(self, path): def _get_key_from_path(self, path):
if not self._is_my_bip32_path(path): if not self._is_my_bip32_path(path):
raise WalletError("Invalid path, unknown root: {}".format(path)) raise WalletError("Invalid path, unknown root: {}".format(path))
@ -1707,7 +1707,7 @@ class FidelityBondMixin(object):
def _get_key_ident(self): def _get_key_ident(self):
first_path = self.get_path(0, 0) first_path = self.get_path(0, 0)
priv, engine = self._get_priv_from_path(first_path) priv, engine = self._get_key_from_path(first_path)
pub = engine.privkey_to_pubkey(priv) pub = engine.privkey_to_pubkey(priv)
return sha256(sha256(pub).digest()).digest()[:3] return sha256(sha256(pub).digest()).digest()[:3]
@ -1738,7 +1738,7 @@ class FidelityBondMixin(object):
def _get_supported_address_types(cls): def _get_supported_address_types(cls):
return (cls.BIP32_EXT_ID, cls.BIP32_INT_ID, cls.BIP32_TIMELOCK_ID, cls.BIP32_BURN_ID) return (cls.BIP32_EXT_ID, cls.BIP32_INT_ID, cls.BIP32_TIMELOCK_ID, cls.BIP32_BURN_ID)
def _get_priv_from_path(self, path): def _get_key_from_path(self, path):
if self.is_timelocked_path(path): if self.is_timelocked_path(path):
key_path = path[:-1] key_path = path[:-1]
locktime = path[-1] locktime = path[-1]
@ -1746,7 +1746,7 @@ class FidelityBondMixin(object):
privkey = engine.derive_bip32_privkey(self._master_key, key_path) privkey = engine.derive_bip32_privkey(self._master_key, key_path)
return (privkey, locktime), engine return (privkey, locktime), engine
else: else:
return super(FidelityBondMixin, self)._get_priv_from_path(path) return super(FidelityBondMixin, self)._get_key_from_path(path)
def get_path(self, mixdepth=None, address_type=None, index=None, timenumber=None): def get_path(self, mixdepth=None, address_type=None, index=None, timenumber=None):
if address_type == None or address_type in (self.BIP32_EXT_ID, self.BIP32_INT_ID, if address_type == None or address_type in (self.BIP32_EXT_ID, self.BIP32_INT_ID,

2
jmclient/jmclient/wallet_service.py

@ -565,7 +565,7 @@ class WalletService(Service):
index += 1 index += 1
self.wallet.set_next_index(mixdepth, address_type, index, force=True) self.wallet.set_next_index(mixdepth, address_type, index, force=True)
path = self.wallet.get_path(mixdepth, address_type, index) path = self.wallet.get_path(mixdepth, address_type, index)
path_privkey, engine = self.wallet._get_priv_from_path(path) path_privkey, engine = self.wallet._get_key_from_path(path)
path_pubkey = engine.privkey_to_pubkey(path_privkey) path_pubkey = engine.privkey_to_pubkey(path_privkey)
path_pubkeyhash = btc.bin_hash160(path_pubkey) path_pubkeyhash = btc.bin_hash160(path_pubkey)
for burner_tx in burner_txes: for burner_tx in burner_txes:

2
jmclient/jmclient/wallet_utils.py

@ -502,7 +502,7 @@ def wallet_display(wallet_service, showprivkey, displayall=False,
path_repr = wallet_service.get_path_repr(path) path_repr = wallet_service.get_path_repr(path)
path_repr_b = path_repr.encode() path_repr_b = path_repr.encode()
privkey, engine = wallet_service._get_priv_from_path(path) privkey, engine = wallet_service._get_key_from_path(path)
pubkey = engine.privkey_to_pubkey(privkey) pubkey = engine.privkey_to_pubkey(privkey)
pubkeyhash = btc.bin_hash160(pubkey) pubkeyhash = btc.bin_hash160(pubkey)
output = "BURN-" + binascii.hexlify(pubkeyhash).decode() output = "BURN-" + binascii.hexlify(pubkeyhash).decode()

4
jmclient/test/test_wallet.py

@ -795,7 +795,7 @@ def test_watchonly_wallet(setup_wallet):
scripts = [wallet.get_script_from_path(wallet.path_repr_to_path(path)) scripts = [wallet.get_script_from_path(wallet.path_repr_to_path(path))
for path in paths] for path in paths]
privkey, engine = wallet._get_priv_from_path(wallet.path_repr_to_path(burn_path)) privkey, engine = wallet._get_key_from_path(wallet.path_repr_to_path(burn_path))
burn_pubkey = engine.privkey_to_pubkey(privkey) burn_pubkey = engine.privkey_to_pubkey(privkey)
master_pub_key = wallet.get_bip32_pub_export( master_pub_key = wallet.get_bip32_pub_export(
@ -809,7 +809,7 @@ def test_watchonly_wallet(setup_wallet):
watchonly_scripts = [watchonly_wallet.get_script_from_path( watchonly_scripts = [watchonly_wallet.get_script_from_path(
watchonly_wallet.path_repr_to_path(path)) for path in paths] watchonly_wallet.path_repr_to_path(path)) for path in paths]
privkey, engine = wallet._get_priv_from_path(wallet.path_repr_to_path(burn_path)) privkey, engine = wallet._get_key_from_path(wallet.path_repr_to_path(burn_path))
watchonly_burn_pubkey = engine.privkey_to_pubkey(privkey) watchonly_burn_pubkey = engine.privkey_to_pubkey(privkey)
for script, watchonly_script in zip(scripts, watchonly_scripts): for script, watchonly_script in zip(scripts, watchonly_scripts):

Loading…
Cancel
Save