def do_import(self, arguments): item = get_arg(arguments) if item: if item == 'wif': if not self.Wallet: print("Please open a wallet before importing WIF") return wif = get_arg(arguments, 1) if wif: prikey = KeyPair.PrivateKeyFromWIF(wif) if prikey: 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)) else: print("invalid wif") return elif item == 'contract': return self.load_smart_contract(arguments) elif item == 'contract_addr': return ImportContractAddr(self.Wallet, arguments[1:]) print("please specify something to import") return
def do_import(self, arguments): item = self.get_arg(arguments) if item and item == 'wif': if not self.Wallet: print("Please open a wallet before importing WIF") return wif = self.get_arg(arguments, 1) if wif: # self.Wallet. prikey = KeyPair.PrivateKeyFromWIF(wif) if prikey: 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)) else: print("invalid wif") return else: print("Please specify a wif") return print("please specify something to import") return
def run(self): to_addr = self.target_address dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks) dbloop.start(.1) Blockchain.Default().PersistBlocks() wallets = [] i = 0 tx_json = '' print("Signing new transaction with 3 of 4 node keys...") for pkey, wif in nodekeys.items(): walletpath = "wallet{}.db3".format(i + 1) if os.path.exists(walletpath): os.remove(walletpath) wallet = UserWallet.Create(path=walletpath, password=mypassword) wallets.append(wallet) print("Importing node private key to to {}".format(walletpath)) prikey = KeyPair.PrivateKeyFromWIF(wif) wallet.CreateKey(prikey) print("Importing multi-sig contract to {}".format(walletpath)) multisig_args = [pkey, '3'] multisig_args.extend(list(nodekeys.keys())) ImportMultiSigContractAddr(wallet, multisig_args) dbloop = task.LoopingCall(wallet.ProcessBlocks) dbloop.start(1) while Blockchain.Default().Height < 2: print("Waiting for wallet to sync...") time.sleep(1) #print("Wallet %s " % json.dumps(wallet.ToJson(), indent=4)) if i == 0: print("Creating spend transaction to {}".format(to_addr)) tx_json = self.begin_send( wallet, ['neo', to_addr, '100000000', multisig_addr]) if tx_json is None: break else: tx_json = self.sign_and_finish(wallet, tx_json) if tx_json == 'success': print( "Finished, {} should now own all the NEO on the private network." .format(to_addr)) break i += 1 self.quit()
def test_6_import_wif(self): wallet = self.GetWallet1() key_to_import = 'L3MBUkKU5kYg16KSZnqcaTj2pG5ei3fN9A4X7rxXys18GBDa3bH8' prikey = KeyPair.PrivateKeyFromWIF(key_to_import) keypair = wallet.CreateKey(prikey) key_out = keypair.PublicKey.encode_point(True).decode('utf-8') self.assertEqual( key_out, '03f3a3b5a4d873933fc7f4b53113e8eb999fb20038271fbbb10255585670c3c312' )
def test_should_throw_error_on_invalid_checksum(self): # build fake wif fakewif = bytearray(34 * 'A', 'utf8') fakewif[0] = 0x80 fakewif[33] = 0x01 # fake checksum fakewif.append(0xDE) fakewif.append(0xAD) fakewif.append(0xBE) fakewif.append(0xEF) encodedFakeWIF = base58.b58encode(bytes(fakewif)) with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromWIF(encodedFakeWIF) self.assertEqual('Invalid WIF Checksum!', str(context.exception))
def claim_initial_neo(self, target_address): wallets = [] i = 0 tx_json = None dbloops = [] print("Signing new transaction with 3 of 4 node keys...") for pkey, wif in nodekeys.items(): walletpath = "wallet{}.db3".format(i + 1) if os.path.exists(walletpath): os.remove(walletpath) wallet = UserWallet.Create(path=walletpath, password=self.wallet_pwd) wallets.append(wallet) print("Importing node private key to to {}".format(walletpath)) prikey = KeyPair.PrivateKeyFromWIF(wif) wallet.CreateKey(prikey) print("Importing multi-sig contract to {}".format(walletpath)) multisig_args = [pkey, '3'] multisig_args.extend(list(nodekeys.keys())) ImportMultiSigContractAddr(wallet, multisig_args) dbloop = task.LoopingCall(wallet.ProcessBlocks) dbloop.start(1) dbloops.append(dbloop) # print("Wallet %s " % json.dumps(wallet.ToJson(), indent=4)) if i == 0: print( "Creating spend transaction to {}".format(target_address)) tx_json = self.send_neo(wallet, multisig_addr, target_address, '100000000') if tx_json is None: break else: tx_json = self.sign_and_finish(wallet, tx_json) if tx_json == 'success': print( "Finished, {} should now own all the NEO on the private network." .format(target_address)) break i += 1
def test_c(self): key = KeyPair(priv_key=self.decrypted_pk) self.assertEqual(key.PublicKey.x, self.pubkey_x) self.assertEqual(key.PublicKey.y, self.pubkey_y) self.assertEqual(key.PublicKey.encode_point(True), self.pubkey_encoded) self.assertEqual(key.PublicKey.encode_point(False), self.pubkey_not_comp) self.assertIsInstance(key.PublicKeyHash, UInt160) self.assertEqual(key.PublicKeyHash.ToBytes(), self.pubkeyhash) self.assertEqual(key.Export(), self.wif) private_key_from_wif = KeyPair.PrivateKeyFromWIF(self.wif) self.assertEqual(private_key_from_wif, self.pk)
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_work(self): privkey = KeyPair.PrivateKeyFromWIF("L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP") self.assertEqual(binascii.hexlify(privkey), b"cbf4b9f70470856bb4f40f80b87edb90865997ffee6df315ab166d713af433a5")
def test_should_throw_error_on_invalid_wif(self): with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromWIF(52 * 'A') self.assertEqual('Invalid format!', str(context.exception))
def test_should_throw_error_on_too_short_wif(self): with self.assertRaises(ValueError) as context: KeyPair.PrivateKeyFromWIF('brokenwif') self.assertIn('Please provide a wif with a length of 52 bytes', str(context.exception))