Browse Source

Merge #491: [Refactor] Fix get_script_path and get_addr_path

5acb85f [Refactor] Fix get_script_path and get_addr_path (chris-belcher)

Tree-SHA512: 2a30a562b33fe4de14ed604f4bf9730ec4aa62cf46968f6651c157c79d0a7701317db1d047c2c08faf5482f46de9f382f62fa5c3f3317bca69bf7647fbf3f534
master
chris-belcher 6 years ago
parent
commit
b4c29cb52f
No known key found for this signature in database
GPG Key ID: EF734EA677F31129
  1. 2
      jmclient/jmclient/electruminterface.py
  2. 38
      jmclient/jmclient/wallet.py
  3. 2
      jmclient/jmclient/wallet_service.py
  4. 10
      jmclient/jmclient/wallet_utils.py
  5. 14
      jmclient/test/test_wallet.py

2
jmclient/jmclient/electruminterface.py

@ -354,7 +354,7 @@ class ElectrumInterface(BlockchainInterface):
for index in range(wallet.get_next_unused_index(md, internal)):
addrs.add(wallet.get_addr(md, internal, index))
for path in wallet.yield_imported_paths(md):
addrs.add(wallet.get_addr_path(path))
addrs.add(wallet.get_address_from_path(path))
self.listunspent_calls = len(addrs)
for a in addrs:

38
jmclient/jmclient/wallet.py

@ -511,8 +511,8 @@ class BaseWallet(object):
script = self.get_script(mixdepth, internal, index)
return self.script_to_addr(script)
def get_addr_path(self, path):
script = self.get_script_path(path)
def get_address_from_path(self, path):
script = self.get_script_from_path(path)
return self.script_to_addr(script)
def get_new_addr(self, mixdepth, internal):
@ -565,7 +565,7 @@ class BaseWallet(object):
removed_utxos = {}
for (txid, index), val in ret.items():
val['address'] = self.get_addr_path(val['path'])
val['address'] = self.get_address_from_path(val['path'])
removed_utxos[hexlify(txid).decode('ascii') + ':' + str(index)] = val
return removed_utxos
@ -585,7 +585,7 @@ class BaseWallet(object):
if md is False:
continue
path, value, height = self._utxos.remove_utxo(txid, index, md)
script = self.get_script_path(path)
script = self.get_script_from_path(path)
removed_utxos[(txid, index)] = {'script': script,
'path': path,
'value': value}
@ -601,7 +601,7 @@ class BaseWallet(object):
added_utxos = {}
for (txid_bin, index), val in ret.items():
addr = self.get_addr_path(val['path'])
addr = self.get_address_from_path(val['path'])
val['address'] = addr
added_utxos[txid + ':' + str(index)] = val
return added_utxos
@ -670,7 +670,7 @@ class BaseWallet(object):
maxheight=maxheight)
ret_conv = {}
for utxo, data in ret.items():
addr = self.get_addr_path(data['path'])
addr = self.get_address_from_path(data['path'])
utxo_txt = hexlify(utxo[0]).decode('ascii') + ':' + str(utxo[1])
ret_conv[utxo_txt] = {'address': addr, 'value': data['value']}
return ret_conv
@ -704,7 +704,7 @@ class BaseWallet(object):
mixdepth, amount, utxo_filter, select_fn, maxheight=maxheight)
for data in ret.values():
data['script'] = self.get_script_path(data['path'])
data['script'] = self.get_script_from_path(data['path'])
return ret
@ -744,7 +744,7 @@ class BaseWallet(object):
for md, utxos in ret.items():
for utxo, data in utxos.items():
utxo_str = hexlify(utxo[0]).decode('ascii') + ':' + str(utxo[1])
addr = self.get_addr_path(data['path'])
addr = self.get_address_from_path(data['path'])
data['address'] = addr
utxos_conv[md][utxo_str] = data
return utxos_conv
@ -767,7 +767,7 @@ class BaseWallet(object):
for utxo, (path, value, height) in data.items():
if not include_disabled and self._utxos.is_disabled(*utxo):
continue
script = self.get_script_path(path)
script = self.get_script_from_path(path)
script_utxos[md][utxo] = {'script': script,
'path': path,
'value': value}
@ -793,7 +793,7 @@ class BaseWallet(object):
def _get_mixdepth_from_path(self, path):
raise NotImplementedError()
def get_script_path(self, path):
def get_script_from_path(self, path):
"""
internal note: This is the final sink for all operations that somehow
need to derive a script. If anything goes wrong when deriving a
@ -808,7 +808,7 @@ class BaseWallet(object):
def get_script(self, mixdepth, internal, index):
path = self.get_path(mixdepth, internal, index)
return self.get_script_path(path)
return self.get_script_from_path(path)
def _get_priv_from_path(self, path):
raise NotImplementedError()
@ -1120,7 +1120,7 @@ class ImportWalletMixin(object):
assert len(path) == 3
if not script:
script = self.get_script_path(path)
script = self.get_script_from_path(path)
# we need to retain indices
self._imported[path[1]][path[2]] = (b'', -1)
@ -1192,9 +1192,9 @@ class ImportWalletMixin(object):
return super(ImportWalletMixin, self).get_details(path)
return path[1], 'imported', path[2]
def get_script_path(self, path):
def get_script_from_path(self, path):
if not self._is_imported_path(path):
return super(ImportWalletMixin, self).get_script_path(path)
return super(ImportWalletMixin, self).get_script_from_path(path)
priv, engine = self._get_priv_from_path(path)
return engine.privkey_to_script(priv)
@ -1328,7 +1328,7 @@ class BIP32Wallet(BaseWallet):
for int_type in (self.BIP32_EXT_ID, self.BIP32_INT_ID):
for i in range(self._index_cache[md][int_type]):
path = self.get_path(md, int_type, i)
script = self.get_script_path(path)
script = self.get_script_from_path(path)
self._script_map[script] = path
def save(self):
@ -1360,7 +1360,7 @@ class BIP32Wallet(BaseWallet):
def _derive_bip32_master_key(cls, seed):
return cls._ENGINE.derive_bip32_master_key(seed)
def get_script_path(self, path):
def get_script_from_path(self, path):
if not self._is_my_bip32_path(path):
raise WalletError("unable to get script for unknown key path")
@ -1461,19 +1461,19 @@ class BIP32Wallet(BaseWallet):
return self.get_new_script_override_disable(mixdepth, internal)
def get_new_script_override_disable(self, mixdepth, internal):
# This is called by get_script_path and calls back there. We need to
# This is called by get_script_from_path and calls back there. We need to
# ensure all conditions match to avoid endless recursion.
int_type = self._get_internal_type(internal)
index = self._index_cache[mixdepth][int_type]
self._index_cache[mixdepth][int_type] += 1
path = self.get_path(mixdepth, int_type, index)
script = self.get_script_path(path)
script = self.get_script_from_path(path)
self._script_map[script] = path
return script
def get_script(self, mixdepth, internal, index):
path = self.get_path(mixdepth, internal, index)
return self.get_script_path(path)
return self.get_script_from_path(path)
@deprecated
def get_key(self, mixdepth, internal, index):

2
jmclient/jmclient/wallet_service.py

@ -735,7 +735,7 @@ class WalletService(Service):
saved_indices[md][internal] = next_unused
# include any imported addresses
for path in self.yield_imported_paths(md):
addresses.add(self.get_addr_path(path))
addresses.add(self.get_address_from_path(path))
return addresses, saved_indices

10
jmclient/jmclient/wallet_utils.py

@ -330,8 +330,8 @@ def get_tx_info(txid):
def get_imported_privkey_branch(wallet_service, m, showprivkey):
entries = []
for path in wallet_service.yield_imported_paths(m):
addr = wallet_service.get_addr_path(path)
script = wallet_service.get_script_path(path)
addr = wallet_service.get_address_from_path(path)
script = wallet_service.get_script_from_path(path)
balance = 0.0
for data in wallet_service.get_utxos_by_mixdepth(include_disabled=True,
hexfmt=False)[m].values():
@ -432,7 +432,7 @@ def wallet_display(wallet_service, showprivkey, displayall=False,
unused_index = wallet_service.get_next_unused_index(m, forchange)
for k in range(unused_index + wallet_service.gap_limit):
path = wallet_service.get_path(m, forchange, k)
addr = wallet_service.get_addr_path(path)
addr = wallet_service.get_address_from_path(path)
balance, used = get_addr_status(
path, utxos[m], k >= unused_index, forchange)
if showprivkey:
@ -637,7 +637,7 @@ def wallet_fetch_history(wallet, options):
'FROM transactions '
'WHERE (blockhash IS NOT NULL AND blocktime IS NOT NULL) OR conflicts = 0 '
'ORDER BY blocktime').fetchall()
wallet_script_set = set(wallet.get_script_path(p)
wallet_script_set = set(wallet.get_script_from_path(p)
for p in wallet.yield_known_paths())
def s():
@ -915,7 +915,7 @@ def wallet_importprivkey(wallet, mixdepth, key_type):
print("Failed to import key {}: {}".format(wif, e))
import_failed += 1
else:
imported_addr.append(wallet.get_addr_path(path))
imported_addr.append(wallet.get_address_from_path(path))
if not imported_addr:
jmprint("Warning: No keys imported!", "error")

14
jmclient/test/test_wallet.py

@ -277,8 +277,8 @@ def test_import_key(setup_wallet):
assert len(imported_paths_md1) == 1
# verify imported addresses
assert wallet.get_addr_path(imported_paths_md0[0]) == '2MzY5yyonUY7zpHspg7jB7WQs1uJxKafQe4'
assert wallet.get_addr_path(imported_paths_md1[0]) == 'mpCX9EbdXpcrKMtjEe1fqFhvzctkfzMYTX'
assert wallet.get_address_from_path(imported_paths_md0[0]) == '2MzY5yyonUY7zpHspg7jB7WQs1uJxKafQe4'
assert wallet.get_address_from_path(imported_paths_md1[0]) == 'mpCX9EbdXpcrKMtjEe1fqFhvzctkfzMYTX'
# test remove key
wallet.remove_imported_key(path=imported_paths_md0[0])
@ -301,10 +301,10 @@ def test_signing_imported(setup_wallet, wif, keytype, type_check):
MIXDEPTH = 0
path = wallet.import_private_key(MIXDEPTH, wif, keytype)
utxo = fund_wallet_addr(wallet, wallet.get_addr_path(path))
utxo = fund_wallet_addr(wallet, wallet.get_address_from_path(path))
tx = btc.deserialize(btc.mktx(['{}:{}'.format(hexlify(utxo[0]).decode('ascii'), utxo[1])],
['00'*17 + ':' + str(10**8 - 9000)]))
script = wallet.get_script_path(path)
script = wallet.get_script_from_path(path)
tx = wallet.sign_tx(tx, {0: (script, 10**8)})
type_check(tx)
txout = jm_single().bc_interface.pushtx(btc.serialize(tx))
@ -580,7 +580,7 @@ def test_addr_script_conversion(setup_wallet):
wallet = get_populated_wallet(num=1)
path = wallet.get_path(0, True, 0)
script = wallet.get_script_path(path)
script = wallet.get_script_from_path(path)
addr = wallet.script_to_addr(script)
assert script == wallet.addr_to_script(addr)
@ -597,14 +597,14 @@ def test_imported_key_removed(setup_wallet):
wallet = SegwitLegacyWallet(storage)
path = wallet.import_private_key(1, wif, key_type)
script = wallet.get_script_path(path)
script = wallet.get_script_from_path(path)
assert wallet.is_known_script(script)
wallet.remove_imported_key(path=path)
assert not wallet.is_known_script(script)
with pytest.raises(WalletError):
wallet.get_script_path(path)
wallet.get_script_from_path(path)
def test_wallet_mixdepth_simple(setup_wallet):

Loading…
Cancel
Save