def get_users(usernames): reply = {} if usernames is None: return jsonify(error_reply("No usernames given")) if ',' not in usernames: username = usernames info = get_user_profile(username) reply[username] = info if 'error' in info: return jsonify(reply), 502 return jsonify(reply), 200 try: usernames = usernames.rsplit(',') except: return jsonify(error_reply("Invalid input format")) for username in usernames: try: reply[username] = get_user_profile(username) except: pass return jsonify(reply), 200
def get_openname_profile(): try: key = 'u/' + request.args.get('openname').lower() except: return jsonify(error_reply("No openname given")) if MEMCACHED_ENABLED: cache_reply = mc.get("profile_" + str(key)) else: cache_reply = None #print "cache off" if cache_reply is None: try: info = full_profile_mem(key) jsonify(info) except: return error_reply("Malformed profile") if MEMCACHED_ENABLED: mc.set("profile_" + str(key), json.dumps(info), int(time() + MEMCACHED_TIMEOUT)) #print "cache miss full_profile" else: #print "cache hit full_profile" info = json.loads(cache_reply) if 'status' in info: if info['status'] == 404: abort(404) return jsonify(info)
def sendtousername(self, username, bitcoin_amount): # Step 1: get the bitcoin address from coinrpc import namecoind data = namecoind.get_full_profile('u/' + username) try: bitcoin_address = data['bitcoin']['address'] except: bitcoin_address = "" reply = {} # Step 2: send bitcoins to that address if bitcoin_address != "": if self.unlock_wallet(): # send the bitcoins # ISSUE: should not be float, needs fix info = self.sendtoaddress(bitcoin_address, float(bitcoin_amount)) if 'status' in info and info['status'] == -1: return error_reply("couldn't send transaction") reply['status'] = 200 reply['tx'] = info return reply return error_reply("couldn't send BTC")
def transfer(self, key,new_address,value=None): #check if this name exists and if it does, find the value field #note that update command needs an arg of <new value>. #in case we're simply transferring, we need to obtain old value first key_details = self.name_show(key) if 'code' in key_details and key_details.get('code') == -4: return error_reply("Key does not exist") #get new 'value' if given, otherwise use the old 'value' if value is None: value = json.dumps(key_details['value']) #now unlock the wallet if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) if utf8len(value) > VALUE_MAX_LIMIT: return error_reply("value larger than " + str(VALUE_MAX_LIMIT)) #transfer the name (underlying call is still name_update) info = self.namecoind.name_update(key, value, new_address) return info
def get_openname_profile(): try: key = 'u/' + request.args.get('openname').lower() except: return jsonify(error_reply("No openname given")) if MEMCACHED_ENABLED: cache_reply = mc.get("profile_" + str(key)) else: cache_reply = None #print "cache off" if cache_reply is None: try: info = full_profile_mem(key) jsonify(info) except: return error_reply("Malformed profile") if MEMCACHED_ENABLED: mc.set("profile_" + str(key),json.dumps(info),int(time() + MEMCACHED_TIMEOUT)) #print "cache miss full_profile" else: #print "cache hit full_profile" info = json.loads(cache_reply) if 'status' in info: if info['status'] == 404: abort(404) return jsonify(info)
def name_transfer(self, key, new_address, value=None): """ Check if this name exists and if it does, find the value field note that update command needs an arg of <new value>. in case we're simply transferring, need to obtain old value first """ key_details = self.name_show(key) if 'code' in key_details and key_details.get('code') == -4: return error_reply("Key does not exist") # get new 'value' if given, otherwise use the old 'value' if value is None: value = json.dumps(key_details['value']) if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) # transfer the name (underlying call is still name_update) try: # update the 'value' reply = self.obj.name_update(key, value, new_address) except JSONRPCException as e: return e.error return reply
def importprivkey(self, namecoinprivkey, label='import', rescan=False): if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) try: reply = self.obj.importprivkey(namecoinprivkey, label, rescan) except JSONRPCException as e: return e.error return reply
def name_update(self, key, value): if utf8len(value) > VALUE_MAX_LIMIT: return error_reply("value larger than " + str(VALUE_MAX_LIMIT)) if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) try: # update the 'value' reply = self.obj.name_update(key, value) except JSONRPCException as e: return e.error return reply
def validateaddress(self, bitcoinaddress): try: status = self.obj.validateaddress(bitcoinaddress) return status except Exception as e: return error_reply(str(e))
def name_show(self, input_key): reply = {} try: value = self.namecoind.name_show(input_key) except JSONRPCException as e: return e.error try: profile = json.loads(value.get('value')) except: profile = value.get('value') if 'code' in value and value.get('code') == -4: return error_reply("Not found", 404) for key in value.keys(): reply['namecoin_address'] = value['address'] if(key == 'value'): try: reply[key] = json.loads(value[key]) except: reply[key] = value[key] return reply
def name_update(self, key,value): if utf8len(value) > VALUE_MAX_LIMIT: return error_reply("value larger than " + str(VALUE_MAX_LIMIT)) #now unlock the wallet if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) #update the 'value' try: info = self.namecoind.name_update(key, value) except JSONRPCException as e: return e.error return info
def firstupdate(self, key, rand, value, tx=None): if utf8len(value) > VALUE_MAX_LIMIT: return error_reply("value larger than " + str(VALUE_MAX_LIMIT)) if not self.unlock_wallet(self.passphrase): error_reply("Error unlocking wallet", 403) try: if tx is not None: reply = self.obj.name_firstupdate(key, rand, tx, value) else: reply = self.obj.name_firstupdate(key, rand, value) except JSONRPCException as e: return e.error return reply
def name_new(self, key, value): # check if this key already exists # namecoind lets you do name_new on keys that exist if self.check_registration(key): return error_reply("This key already exists") if not self.unlock_wallet(self.passphrase): return error_reply("Error unlocking wallet", 403) # create new name # returns a list of [tx, rand] try: reply = self.obj.name_new(key) except JSONRPCException as e: return e.error return reply
def name_new(self, key,value,force_registration=False): #check if this key already exists if self.check_registration(key) and not force_registration: return error_reply("This key already exists") #check if passphrase is valid if not self.unlock_wallet(self.passphrase): return error_reply("Error unlocking wallet", 403) #create new name #returns a list of [longhex, rand] try: info = self.namecoind.name_new(key) except JSONRPCException as e: return e.error return info
def sendtoaddress(self, bitcoinaddress, amount): self.unlock_wallet() try: status = self.bitcoind.sendtoaddress(bitcoinaddress, float(amount)) return status except Exception as e: return error_reply(str(e))
def importprivkey(self, bitcoinprivkey, label='import', rescan=False): self.unlock_wallet() try: status = self.obj.importprivkey(bitcoinprivkey, label, rescan) return status except Exception as e: return error_reply(str(e))
def broadcast_transaction(self, hex_tx): """ Dispatch a raw transaction to the network. """ resp = self.obj.sendrawtransaction(hex_tx) if len(resp) > 0: return {'transaction_hash': resp, 'success': True} else: return error_reply('Invalid response from bitcoind.')
def sendtoaddress(self, bitcoinaddress, amount): self.unlock_wallet() try: # ISSUE: should not be float, needs fix status = self.obj.sendtoaddress(bitcoinaddress, float(amount)) return status except Exception as e: return error_reply(str(e))
def get_key_value(): try: key = request.args.get('key').lower() except: return jsonify(error_reply("No key given")) info = name_show_mem(key) if 'status' in info: if info['status'] == 404: abort(404) return jsonify(info)
def get_bulk_profiles(): usernames = request.args.get('usernames') if usernames is None: return jsonify(error_reply("No usernames given")) usernames = usernames.rsplit(',') list = [] for username in usernames: result = {} result["username"] = username result["profile"] = full_profile_mem('u/' + username.lower()) list.append(result) return jsonify(results=list)