Browse Source

Drop key_type argument to import_private_key

master
Jules Comte 5 years ago
parent
commit
a9668fd2e1
  1. 3
      jmclient/jmclient/snicker_receiver.py
  2. 10
      jmclient/jmclient/wallet.py
  3. 5
      jmclient/jmclient/wallet_utils.py
  4. 28
      jmclient/test/test_wallet.py

3
jmclient/jmclient/snicker_receiver.py

@ -195,8 +195,7 @@ class SNICKERReceiver(object):
# scriptPubKey type as the wallet, this has been implicitly # scriptPubKey type as the wallet, this has been implicitly
# checked above by deriving the scriptPubKey. # checked above by deriving the scriptPubKey.
self.wallet_service.import_private_key(self.import_branch, self.wallet_service.import_private_key(self.import_branch,
self.wallet_service._ENGINE.privkey_to_wif(tweaked_privkey), self.wallet_service._ENGINE.privkey_to_wif(tweaked_privkey))
key_type=self.wallet_service.TYPE)
# TODO condition on automatic brdcst or not # TODO condition on automatic brdcst or not

10
jmclient/jmclient/wallet.py

@ -1540,7 +1540,7 @@ class ImportWalletMixin(object):
if write: if write:
storage.save() storage.save()
def import_private_key(self, mixdepth, wif, key_type=None): def import_private_key(self, mixdepth, wif):
""" """
Import a private key in WIF format. Import a private key in WIF format.
@ -1561,8 +1561,6 @@ class ImportWalletMixin(object):
if not 0 <= mixdepth <= self.max_mixdepth: if not 0 <= mixdepth <= self.max_mixdepth:
raise WalletError("Mixdepth must be positive and at most {}." raise WalletError("Mixdepth must be positive and at most {}."
"".format(self.max_mixdepth)) "".format(self.max_mixdepth))
if key_type is not None and key_type not in self._ENGINES:
raise WalletError("Unsupported key type for imported keys.")
privkey, key_type_wif = self._ENGINE.wif_to_privkey(wif) privkey, key_type_wif = self._ENGINE.wif_to_privkey(wif)
# FIXME: there is no established standard for encoding key type in wif # FIXME: there is no established standard for encoding key type in wif
@ -1570,10 +1568,8 @@ class ImportWalletMixin(object):
# key_type != key_type_wif: # key_type != key_type_wif:
# raise WalletError("Expected key type does not match WIF type.") # raise WalletError("Expected key type does not match WIF type.")
# default to wallet key type if not told otherwise # default to wallet key type
if key_type is None: key_type = self.TYPE
key_type = 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.key_to_script(privkey) in self._script_map: if engine.key_to_script(privkey) in self._script_map:

5
jmclient/jmclient/wallet_utils.py

@ -997,6 +997,9 @@ def wallet_showseed(wallet):
def wallet_importprivkey(wallet, mixdepth, key_type): def wallet_importprivkey(wallet, mixdepth, key_type):
jmprint("WARNING: This imported key will not be recoverable with your 12 " jmprint("WARNING: This imported key will not be recoverable with your 12 "
"word mnemonic phrase. Make sure you have backups.", "warning") "word mnemonic phrase. Make sure you have backups.", "warning")
jmprint("WARNING: Make sure that the type of the public address previously "
"derived from this private key matches the wallet type you are "
"currently using.")
jmprint("WARNING: Handling of raw ECDSA bitcoin private keys can lead to " jmprint("WARNING: Handling of raw ECDSA bitcoin private keys can lead to "
"non-intuitive behaviour and loss of funds.\n Recommended instead " "non-intuitive behaviour and loss of funds.\n Recommended instead "
"is to use the \'sweep\' feature of sendpayment.py.", "warning") "is to use the \'sweep\' feature of sendpayment.py.", "warning")
@ -1009,7 +1012,7 @@ def wallet_importprivkey(wallet, mixdepth, key_type):
# TODO is there any point in only accepting wif format? check what # TODO is there any point in only accepting wif format? check what
# other wallets do # other wallets do
try: try:
path = wallet.import_private_key(mixdepth, wif, key_type=key_type) path = wallet.import_private_key(mixdepth, wif)
except WalletError as e: except WalletError as e:
print("Failed to import key {}: {}".format(wif, e)) print("Failed to import key {}: {}".format(wif, e))
import_failed += 1 import_failed += 1

28
jmclient/test/test_wallet.py

@ -313,16 +313,13 @@ def test_import_key(setup_wallet):
wallet = SegwitLegacyWallet(storage) wallet = SegwitLegacyWallet(storage)
wallet.import_private_key( wallet.import_private_key(
0, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM', 0, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM')
cryptoengine.TYPE_P2SH_P2WPKH)
wallet.import_private_key( wallet.import_private_key(
1, 'cVqtSSoVxFyPqTRGfeESi31uCYfgTF4tGWRtGeVs84fzybiX5TPk', 1, 'cVqtSSoVxFyPqTRGfeESi31uCYfgTF4tGWRtGeVs84fzybiX5TPk')
cryptoengine.TYPE_P2PKH)
with pytest.raises(WalletError): with pytest.raises(WalletError):
wallet.import_private_key( wallet.import_private_key(
1, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM', 1, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM')
cryptoengine.TYPE_P2SH_P2WPKH)
# test persist imported keys # test persist imported keys
wallet.save() wallet.save()
@ -341,7 +338,7 @@ def test_import_key(setup_wallet):
# verify imported addresses # verify imported addresses
assert wallet.get_address_from_path(imported_paths_md0[0]) == '2MzY5yyonUY7zpHspg7jB7WQs1uJxKafQe4' assert wallet.get_address_from_path(imported_paths_md0[0]) == '2MzY5yyonUY7zpHspg7jB7WQs1uJxKafQe4'
assert wallet.get_address_from_path(imported_paths_md1[0]) == 'mpCX9EbdXpcrKMtjEe1fqFhvzctkfzMYTX' assert wallet.get_address_from_path(imported_paths_md1[0]) == '2MwbXnJrPP4rnwpgRhvNPP44J6tMokDexZB'
# test remove key # test remove key
wallet.remove_imported_key(path=imported_paths_md0[0]) wallet.remove_imported_key(path=imported_paths_md0[0])
@ -350,20 +347,17 @@ def test_import_key(setup_wallet):
assert wallet.get_details(imported_paths_md1[0]) == (1, 'imported', 0) assert wallet.get_details(imported_paths_md1[0]) == (1, 'imported', 0)
@pytest.mark.parametrize('wif,keytype,type_check', [ @pytest.mark.parametrize('wif, type_check', [
['cVqtSSoVxFyPqTRGfeESi31uCYfgTF4tGWRtGeVs84fzybiX5TPk', ['cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM', assert_segwit]
cryptoengine.TYPE_P2PKH, assert_not_segwit],
['cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM',
cryptoengine.TYPE_P2SH_P2WPKH, assert_segwit]
]) ])
def test_signing_imported(setup_wallet, wif, keytype, type_check): def test_signing_imported(setup_wallet, wif, type_check):
jm_single().config.set('BLOCKCHAIN', 'network', 'testnet') jm_single().config.set('BLOCKCHAIN', 'network', 'testnet')
storage = VolatileStorage() storage = VolatileStorage()
SegwitLegacyWallet.initialize(storage, get_network()) SegwitLegacyWallet.initialize(storage, get_network())
wallet = SegwitLegacyWallet(storage) wallet = SegwitLegacyWallet(storage)
MIXDEPTH = 0 MIXDEPTH = 0
path = wallet.import_private_key(MIXDEPTH, wif, keytype) path = wallet.import_private_key(MIXDEPTH, wif)
utxo = fund_wallet_addr(wallet, wallet.get_address_from_path(path)) utxo = fund_wallet_addr(wallet, wallet.get_address_from_path(path))
# The dummy output is constructed as an unspendable p2sh: # The dummy output is constructed as an unspendable p2sh:
tx = btc.mktx([utxo], tx = btc.mktx([utxo],
@ -619,8 +613,7 @@ def test_path_repr(setup_wallet):
def test_path_repr_imported(setup_wallet): def test_path_repr_imported(setup_wallet):
wallet = get_populated_wallet(num=0) wallet = get_populated_wallet(num=0)
path = wallet.import_private_key( path = wallet.import_private_key(
0, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM', 0, 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM')
cryptoengine.TYPE_P2SH_P2WPKH)
path_repr = wallet.get_path_repr(path) path_repr = wallet.get_path_repr(path)
path_new = wallet.path_repr_to_path(path_repr) path_new = wallet.path_repr_to_path(path_repr)
@ -708,13 +701,12 @@ def test_addr_script_conversion(setup_wallet):
def test_imported_key_removed(setup_wallet): def test_imported_key_removed(setup_wallet):
wif = 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM' wif = 'cRAGLvPmhpzJNgdMT4W2gVwEW3fusfaDqdQWM2vnWLgXKzCWKtcM'
key_type = cryptoengine.TYPE_P2SH_P2WPKH
storage = VolatileStorage() storage = VolatileStorage()
SegwitLegacyWallet.initialize(storage, get_network()) SegwitLegacyWallet.initialize(storage, get_network())
wallet = SegwitLegacyWallet(storage) wallet = SegwitLegacyWallet(storage)
path = wallet.import_private_key(1, wif, key_type) path = wallet.import_private_key(1, wif)
script = wallet.get_script_from_path(path) script = wallet.get_script_from_path(path)
assert wallet.is_known_script(script) assert wallet.is_known_script(script)

Loading…
Cancel
Save