def importKey(self, account, wallet, ignoreDupKeyWarning=False): warningMsg="Key already in wallet" cmd="%s %s wallet import --name %s --private-key %s" % ( Utils.EosClientPath, self.endpointArgs, wallet.name, account.ownerPrivateKey) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") if warningMsg in msg: if not ignoreDupKeyWarning: Utils.Print("WARNING: This key is already imported into the wallet.") else: Utils.Print("ERROR: Failed to import account owner key %s. %s" % (account.ownerPrivateKey, msg)) return False if account.activePrivateKey is None: Utils.Print("WARNING: Active private key is not defined for account \"%s\"" % (account.name)) else: cmd="%s %s wallet import --name %s --private-key %s" % ( Utils.EosClientPath, self.endpointArgs, wallet.name, account.activePrivateKey) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") if warningMsg in msg: if not ignoreDupKeyWarning: Utils.Print("WARNING: This key is already imported into the wallet.") else: Utils.Print("ERROR: Failed to import account active key %s. %s" % (account.activePrivateKey, msg)) return False return True
def create(self, name, accounts=None): wallet = self.wallets.get(name) if wallet is not None: if Utils.Debug: Utils.Print("Wallet \"%s\" already exists. Returning same." % name) return wallet p = re.compile(r'\n\"(\w+)\"\n', re.MULTILINE) cmd = "%s %s wallet create --name %s" % (Utils.EosClientPath, self.endpointArgs, name) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr = Utils.checkOutput(cmd.split()) #Utils.Print("create: %s" % (retStr)) m = p.search(retStr) if m is None: Utils.Print("ERROR: wallet password parser failure") return None p = m.group(1) wallet = Wallet(name, p, self.host, self.port) self.wallets[name] = wallet if accounts: self.importKeys(accounts, wallet) return wallet
def createAccountKeys(count): accounts=[] p = re.compile('Private key: (.+)\nPublic key: (.+)\n', re.MULTILINE) for _ in range(0, count): try: cmd="%s create key --to-console" % (Utils.EosClientPath) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) keyStr=Utils.checkOutput(cmd.split()) m=p.search(keyStr) if m is None: Utils.Print("ERROR: Owner key creation regex mismatch") break ownerPrivate=m.group(1) ownerPublic=m.group(2) cmd="%s create key --to-console" % (Utils.EosClientPath) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) keyStr=Utils.checkOutput(cmd.split()) m=p.match(keyStr) if m is None: Utils.Print("ERROR: Active key creation regex mismatch") break activePrivate=m.group(1) activePublic=m.group(2) name=''.join(random.choice(string.ascii_lowercase) for _ in range(12)) account=Account(name) account.ownerPrivateKey=ownerPrivate account.ownerPublicKey=ownerPublic account.activePrivateKey=activePrivate account.activePublicKey=activePublic accounts.append(account) if Utils.Debug: Utils.Print("name: %s, key(owner): ['%s', '%s], key(active): ['%s', '%s']" % (name, ownerPublic, ownerPrivate, activePublic, activePrivate)) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Exception during key creation. %s" % (msg)) break if count != len(accounts): Utils.Print("Account keys creation failed. Expected %d, actual: %d" % (count, len(accounts))) return None return accounts
def createAccountKeys(count): accounts=[] p = re.compile('Private key: (.+)\nPublic key: (.+)\n', re.MULTILINE) for _ in range(0, count): try: cmd="%s create key" % (Utils.EosClientPath) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) keyStr=Utils.checkOutput(cmd.split()) m=p.search(keyStr) if m is None: Utils.Print("ERROR: Owner key creation regex mismatch") break ownerPrivate=m.group(1) ownerPublic=m.group(2) cmd="%s create key" % (Utils.EosClientPath) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) keyStr=Utils.checkOutput(cmd.split()) m=p.match(keyStr) if m is None: Utils.Print("ERROR: Active key creation regex mismatch") break activePrivate=m.group(1) activePublic=m.group(2) name=''.join(random.choice(string.ascii_lowercase) for _ in range(12)) account=Account(name) account.ownerPrivateKey=ownerPrivate account.ownerPublicKey=ownerPublic account.activePrivateKey=activePrivate account.activePublicKey=activePublic accounts.append(account) if Utils.Debug: Utils.Print("name: %s, key(owner): ['%s', '%s], key(active): ['%s', '%s']" % (name, ownerPublic, ownerPrivate, activePublic, activePrivate)) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Exception during key creation. %s" % (msg)) break if count != len(accounts): Utils.Print("Account keys creation failed. Expected %d, actual: %d" % (count, len(accounts))) return None return accounts
def importKey(self, account, wallet, ignoreDupKeyWarning=False): warningMsg = "Key already in wallet" cmd = "%s %s wallet import --name %s --private-key %s" % ( Utils.EosClientPath, self.endpointArgs, wallet.name, account.ownerPrivateKey) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg = ex.output.decode("utf-8") if warningMsg in msg: if not ignoreDupKeyWarning: Utils.Print( "WARNING: This key is already imported into the wallet." ) else: Utils.Print( "ERROR: Failed to import account owner key %s. %s" % (account.ownerPrivateKey, msg)) return False if account.activePrivateKey is None: Utils.Print( "WARNING: Active private key is not defined for account \"%s\"" % (account.name)) else: cmd = "%s %s wallet import --name %s --private-key %s" % ( Utils.EosClientPath, self.endpointArgs, wallet.name, account.activePrivateKey) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg = ex.output.decode("utf-8") if warningMsg in msg: if not ignoreDupKeyWarning: Utils.Print( "WARNING: This key is already imported into the wallet." ) else: Utils.Print( "ERROR: Failed to import account active key %s. %s" % (account.activePrivateKey, msg)) return False return True
def myFunc(): psOut=None try: if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) psOut=Utils.checkOutput(cmd.split()) return psOut except subprocess.CalledProcessError as _: pass return None
def launch(self): if not self.walletd: Utils.Print("ERROR: Wallet Manager wasn't configured to launch kbcd") return False if self.isLaunched(): return True if self.isLocal(): self.port=self.findAvailablePort() pgrepCmd=Utils.pgrepCmd(Utils.GstWalletName) if Utils.Debug: portTaken=False if self.isLocal(): if not Utils.arePortsAvailable(self.port): portTaken=True psOut=Utils.checkOutput(pgrepCmd.split(), ignoreError=True) if psOut or portTaken: statusMsg="" if psOut: statusMsg+=" %s - {%s}." % (pgrepCmd, psOut) if portTaken: statusMsg+=" port %d is NOT available." % (self.port) Utils.Print("Launching %s, note similar processes running. %s" % (Utils.GstWalletName, statusMsg)) cmd="%s --data-dir %s --config-dir %s --http-server-address=%s:%d --verbose-http-errors" % ( Utils.GstWalletPath, WalletMgr.__walletDataDir, WalletMgr.__walletDataDir, self.host, self.port) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) with open(WalletMgr.__walletLogOutFile, 'w') as sout, open(WalletMgr.__walletLogErrFile, 'w') as serr: popen=subprocess.Popen(cmd.split(), stdout=sout, stderr=serr) self.__walletPid=popen.pid # Give kbcd time to warm up time.sleep(2) try: if Utils.Debug: Utils.Print("Checking if %s launched. %s" % (Utils.GstWalletName, pgrepCmd)) psOut=Utils.checkOutput(pgrepCmd.split()) if Utils.Debug: Utils.Print("Launched %s. {%s}" % (Utils.GstWalletName, psOut)) except subprocess.CalledProcessError as ex: Utils.errorExit("Failed to launch the wallet manager") return True
def create(self, name, accounts=None, exitOnError=True): wallet = self.wallets.get(name) if wallet is not None: if Utils.Debug: Utils.Print("Wallet \"%s\" already exists. Returning same." % name) return wallet p = re.compile(r'\n\"(\w+)\"\n', re.MULTILINE) cmdDesc = "wallet create" cmd = "%s %s %s --name %s --to-console" % ( Utils.RsnClientPath, self.endpointArgs, cmdDesc, name) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr = None maxRetryCount = 4 retryCount = 0 while True: try: retStr = Utils.checkOutput(cmd.split()) break except subprocess.CalledProcessError as ex: retryCount += 1 if retryCount < maxRetryCount: delay = 10 if Utils.Debug: Utils.Print( "%s was not accepted, delaying for %d seconds and trying again" % (cmdDesc, delay)) time.sleep(delay) continue msg = ex.output.decode("utf-8") errorMsg = "ERROR: Failed to create wallet - %s. %s" % (name, msg) if exitOnError: Utils.errorExit("%s" % (errorMsg)) Utils.Print("%s" % (errorMsg)) return None m = p.search(retStr) if m is None: if exitOnError: Utils.cmdError("could not create wallet %s" % (name)) Utils.errorExit("Failed to create wallet %s" % (name)) Utils.Print("ERROR: wallet password parser failure") return None p = m.group(1) wallet = Wallet(name, p, self.host, self.port) self.wallets[name] = wallet if accounts: self.importKeys(accounts, wallet) return wallet
def getKeys(self, wallet): keys=[] p = re.compile(r'\n\s+\"(\w+)\"\n', re.MULTILINE) cmd="%s %s wallet private_keys --name %s --password %s " % (Utils.EosClientPath, self.endpointArgs, wallet.name, wallet.password) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=Utils.checkOutput(cmd.split()) #Utils.Print("retStr: %s" % (retStr)) m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet private_keys parser failure") return None keys=m return keys
def getOpenWallets(self): wallets=[] p = re.compile(r'\s+\"(\w+)\s\*\",?\n', re.MULTILINE) cmd="%s %s wallet list" % (Utils.EosClientPath, self.endpointArgs) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=Utils.checkOutput(cmd.split()) #Utils.Print("retStr: %s" % (retStr)) m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet list parser failure") return None wallets=m return wallets
def getKeys(self, wallet): keys=[] p = re.compile(r'\n\s+\"(\w+)\"\n', re.MULTILINE) cmd="%s %s wallet private_keys --name %s --password %s " % (Utils.ETAClientPath, self.endpointArgs, wallet.name, wallet.password) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=Utils.checkOutput(cmd.split()) #Utils.Print("retStr: %s" % (retStr)) m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet private_keys parser failure") return None keys=m return keys
def getOpenWallets(self): wallets=[] p = re.compile(r'\s+\"(\w+)\s\*\",?\n', re.MULTILINE) cmd="%s %s wallet list" % (Utils.ETAClientPath, self.endpointArgs) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=Utils.checkOutput(cmd.split()) #Utils.Print("retStr: %s" % (retStr)) m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet list parser failure") return None wallets=m return wallets
def getAccountCodeHash(self, account): cmd="%s %s get code %s" % (Utils.EosClientPath, self.endpointArgs, account) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: retStr=Utils.checkOutput(cmd.split()) #Utils.Print ("get code> %s"% retStr) p=re.compile(r'code\shash: (\w+)\n', re.MULTILINE) m=p.search(retStr) if m is None: msg="Failed to parse code hash." Utils.Print("ERROR: "+ msg) return None return m.group(1) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Exception during code hash retrieval. %s" % (msg)) return None
def getAccountCodeHash(self, account): cmd="%s %s get code %s" % (Utils.EnuClientPath, self.endpointArgs, account) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) try: retStr=Utils.checkOutput(cmd.split()) #Utils.Print ("get code> %s"% retStr) p=re.compile(r'code\shash: (\w+)\n', re.MULTILINE) m=p.search(retStr) if m is None: msg="Failed to parse code hash." Utils.Print("ERROR: "+ msg) return None return m.group(1) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Exception during code hash retrieval. %s" % (msg)) return None
def getKeys(self, wallet): keys=[] p = re.compile(r'\n\s+\"(\w+)\"\n', re.MULTILINE) cmd="%s %s wallet private_keys --name %s --password %s " % (Utils.GstClientPath, self.getArgs(), wallet.name, wallet.password) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=None try: retStr=Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Failed to get keys. %s" % (msg)) return False m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet private_keys parser failure") return None keys=m return keys
def getOpenWallets(self): wallets=[] p = re.compile(r'\s+\"(\w+)\s\*\",?\n', re.MULTILINE) cmd="%s %s wallet list" % (Utils.GstClientPath, self.getArgs()) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=None try: retStr=Utils.checkOutput(cmd.split()) except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Failed to open wallets. %s" % (msg)) return False m=p.findall(retStr) if m is None: Utils.Print("ERROR: wallet list parser failure") return None wallets=m return wallets
def getClientVersion(verbose=False): """Returns client version (string)""" p = re.compile(r'^Build version:\s(\w+)\n$') try: cmd="%s version client" % (Utils.EosClientPath) if verbose: Utils.Print("cmd: %s" % (cmd)) response=Utils.checkOutput(cmd.split()) assert(response) assert(isinstance(response, str)) if verbose: Utils.Print("response: <%s>" % (response)) m=p.match(response) if m is None: Utils.Print("ERROR: client version regex mismatch") return None verStr=m.group(1) return verStr except subprocess.CalledProcessError as ex: msg=ex.output.decode("utf-8") Utils.Print("ERROR: Exception during client version query. %s" % (msg)) raise
def create(self, name, accounts=None): wallet=self.wallets.get(name) if wallet is not None: if Utils.Debug: Utils.Print("Wallet \"%s\" already exists. Returning same." % name) return wallet p = re.compile(r'\n\"(\w+)\"\n', re.MULTILINE) cmd="%s %s wallet create --name %s" % (Utils.EosClientPath, self.endpointArgs, name) if Utils.Debug: Utils.Print("cmd: %s" % (cmd)) retStr=Utils.checkOutput(cmd.split()) #Utils.Print("create: %s" % (retStr)) m=p.search(retStr) if m is None: Utils.Print("ERROR: wallet password parser failure") return None p=m.group(1) wallet=Wallet(name, p, self.host, self.port) self.wallets[name] = wallet if accounts: self.importKeys(accounts,wallet) return wallet