def test_should_throw_error_on_invalid_password(self): nep2_key = "6PYVPVe1fQznphjbUxXP9KZJqPMVnVwCx5s5pr5axRJ8uHkMtZg97eT5kL" pwd = "invalid-pwd" with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromNEP2(nep2_key, pwd) self.assertEqual('Wrong passphrase', str(context.exception))
def test_should_work(self): nep2_key = "6PYVPVe1fQznphjbUxXP9KZJqPMVnVwCx5s5pr5axRJ8uHkMtZg97eT5kL" pwd = "TestingOneTwoThree" should_equal_private_key = b"cbf4b9f70470856bb4f40f80b87edb90865997ffee6df315ab166d713af433a5" privkey = KeyPair.PrivateKeyFromNEP2(nep2_key, pwd) privkey_hex = binascii.hexlify(privkey) self.assertEqual(privkey_hex, should_equal_private_key)
def do_import(self, arguments): item = get_arg(arguments) if not item: print("please specify something to import") return if item == 'wif': if not self.Wallet: print("Please open a wallet before importing WIF") return wif = get_arg(arguments, 1) if not wif: print("Please supply a valid WIF key") return try: prikey = KeyPair.PrivateKeyFromWIF(wif) key = self.Wallet.CreateKey(prikey) print("Imported key %s " % wif) print("Pubkey: %s \n" % key.PublicKey.encode_point(True).hex()) print("Wallet: %s " % json.dumps(self.Wallet.ToJson(), indent=4)) except ValueError as e: print(str(e)) except Exception as e: print(str(e)) return elif item == 'nep2': if not self.Wallet: print("Please open a wallet before importing a NEP2 key") return nep2_key = get_arg(arguments, 1) if not nep2_key: print("Please supply a valid nep2 encrypted private key") return nep2_passwd = prompt("[Key Password]> ", is_password=True) try: prikey = KeyPair.PrivateKeyFromNEP2(nep2_key, nep2_passwd) key = self.Wallet.CreateKey(prikey) print("Imported nep2 key: %s " % nep2_key) print("Pubkey: %s \n" % key.PublicKey.encode_point(True).hex()) print("Wallet: %s " % json.dumps(self.Wallet.ToJson(), indent=4)) except ValueError as e: print(str(e)) except Exception as e: print(str(e)) return elif item == 'contract': return self.load_smart_contract(arguments) elif item == 'contract_addr': return ImportContractAddr(self.Wallet, arguments[1:]) elif item == 'watch_addr': return ImportWatchAddr(self.Wallet, get_arg(arguments, 1)) elif item == 'multisig_addr': return ImportMultiSigContractAddr(self.Wallet, arguments[1:]) elif item == 'token': return ImportToken(self.Wallet, get_arg(arguments, 1)) else: print("Import of '%s' not implemented" % item)
def test_should_throw_error_on_invalid_nep2_key(self): with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromNEP2(58 * 'A', 'pwd') self.assertEqual('Invalid nep2_key', str(context.exception))
def test_should_throw_error_on_too_short_nep2_key(self): with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromNEP2('invalid', 'pwd') self.assertIn('Please provide a nep2_key with a length of 58 bytes', str(context.exception))