def get(self, coin): # verify api key api_key = request.args['api_key'] use_api_key(api_key) # Bitcoin if coin == 'btc': user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'btc_wallet.prkey': False}) user_addr = [] for addr in wallet['btc_wallet']: user_addr.append(addr) # Bitcoin Cash elif coin == 'bch': user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'bch_wallet.prkey': False}) user_addr = [] for addr in wallet['bch_wallet']: user_addr.append(addr) elif coin == 'eth': user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'eth_wallet.prkey': False}) user_addr = [] for addr in wallet['eth_wallet']: user_addr.append(addr) else: abort(404, message="currency not found") return jsonify(addresses=user_addr)
def get(self, coin): # verify api key api_key = request.args['api_key'] currency = request.args['currency'] use_api_key(api_key) # Bitcoin if coin == 'btc': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'btc_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['btc_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) balance = 0 for prkey in prkeys: balance = balance + float(btc_balance(currency, prkey)) # Bitcoin cash elif coin == 'bch': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'bch_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['bch_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) balance = 0 for prkey in prkeys: balance = balance + float(bch_balance(currency, prkey)) # Ethereum elif coin == 'eth': # get public address user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'eth_wallet.address': True}) balance = 0 for address in wallet['eth_wallet']: balance = balance + float( eth_balance(address['address'], currency)) return jsonify(network=coin.upper(), balance=balance, balance_currency=currency)
def get(self, coin): # verify api key api_key = request.args['api_key'] address = request.args['address'] use_api_key(api_key) # Bitcoin if coin == 'btc': valid = btc_addr_is_valid(address) # Bitcoin cash elif coin == 'bch': valid = bch_addr_is_valid(address) return jsonify(network=coin.upper(), address=address, is_valid=valid)
def get(self, coin): # verify api key api_key = request.args['api_key'] use_api_key(api_key) # Bitcoin if coin == 'btc': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'btc_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['btc_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) history = [] for prkey in prkeys: history.append(btc_tx_history(prkey)) # Bitcoin cash elif coin == 'bch': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'bch_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['bch_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) history = [] for prkey in prkeys: history.append(bch_tx_history(prkey)) # Ethereum elif coin == 'eth': user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'eth_wallet.address': True}) history = 0 for address in wallet['eth_wallet']: history = history + int( eth_tx_count(address['address'], 'latest')) return jsonify(transactions=history)
def get(self, coin): # verify api key api_key = request.args['api_key'] use_api_key(api_key) # Bitcoin if coin == 'btc': btc_key = GenerateBtcKey() btc_std_addr = btc_key.generate_std_addr() btc_wit_addr = btc_key.generate_wit_addr() btc_prkey = btc_key.generate_prkey() # encrypt and save prkey btc_prkey = encrypt(btc_prkey) btc_wallet = BtcWallet( std_address = btc_std_addr, wit_address = btc_wit_addr, prkey = btc_prkey ) user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = Wallet.objects.get(username=user['username']) wallet.btc_wallet.append(btc_wallet) wallet.save() # reponse response = { "network": "BTC", "address": btc_std_addr, "wit_address": btc_wit_addr } # Bitcoin cash elif coin == 'bch': bch_key = GenerateBchKey() bch_std_addr = bch_key.generate_std_addr() bch_prkey = bch_key.generate_prkey() # encrypt and save prkey bch_prkey = encrypt(bch_prkey) bch_wallet = BchWallet( address = bch_std_addr, prkey = bch_prkey ) user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = Wallet.objects.get(username=user['username']) wallet.bch_wallet.append(bch_wallet) wallet.save() # reponse response = { "network": "BCH", "address": bch_std_addr, } else: abort(404, message="currency not found") return jsonify(status="success", data=response)
def get(self, coin): # verify api key api_key = request.args['api_key'] currency = request.args['currency'] use_api_key(api_key) # Bitcoin if coin == 'btc': rate = btc_to_fiat(1, currency) # Bitcoin cash elif coin == 'bch': rate = bch_to_fiat(1, currency) return jsonify(network=coin.upper(), rate=rate, currency=currency.upper())
def get(self, coin): # verify api key api_key = request.args['api_key'] use_api_key(api_key) # Bitcoin if coin == 'btc': unit = 'satoshis/byte' tx_fees = btc_tx_fees() tx_fees_fast = btc_tx_fees(fast=True) # Bitcoin cash elif coin == 'bch': unit = 'satoshis/byte' tx_fees = bch_tx_fees() tx_fees_fast = False return jsonify(network=coin.upper(), unit=unit, tx_fees=tx_fees, tx_fees_fast=tx_fees_fast)
def get(self, coin): # verify api key api_key = request.args['api_key'] amount = request.args['amount'] currency = request.args['currency'] use_api_key(api_key) # Bitcoin if coin == 'btc': rate = fiat_to_btc(float(amount), currency) # Bitcoin cash elif coin == 'bch': rate = fiat_to_bch(float(amount), currency) return jsonify(network=coin.upper(), rate=rate, currency=currency.upper())
def get(self, coin): # verify api key api_key = request.args['api_key'] use_api_key(api_key) # Bitcoin if coin == 'btc': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'btc_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['btc_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) history = [] for prkey in prkeys: history.append(btc_tx_history(prkey)) # Bitcoin cash elif coin == 'bch': # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'bch_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['bch_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) history = [] for prkey in prkeys: history.append(bch_tx_history(prkey)) return jsonify(history)
def post(self, coin): # verify api key api_key = request.args['api_key'] recipient = request.json['recipient'] amount = request.json['amount'] currency = request.json['currency'] use_api_key(api_key) # Bitcoin if coin == 'btc': # validate recipient address if btc_addr_is_valid(recipient) == False: abort(400, message="Recipient\'s address not valid") # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'btc_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['btc_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) for prkey in prkeys: tx = btc_tx(recipient, amount, currency, prkey) if tx != False: break if tx == False: status = 'failed' else: status = 'success' # Bitcoin cash elif coin == 'bch': # validate recipient address if bch_addr_is_valid(recipient) == False: abort(400, message="Recipient\'s address not valid") # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, {'bch_wallet.prkey': True}) enc_prkeys = [] for prkey in wallet['bch_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) for prkey in prkeys: tx = bch_tx(recipient, amount, currency, prkey) if tx != False: break if tx == False: status = 'failed' else: status = 'success' # Ethereum elif coin == 'eth': # validate recipient address if eth_addr_is_valid(recipient) == False: abort(400, message="Recipient\'s address not valid") # get and decrypt prkeys user = db.user.find_one({'api_keys.key': api_key}, {'username': True}) wallet = db.wallet.find_one({'username': user['username']}, { 'eth_wallet.address': True, 'eth_wallet.prkey': True }) enc_prkeys = [] for prkey in wallet['eth_wallet']: enc_prkeys.append(prkey['prkey']) prkeys = [] for prkey in enc_prkeys: prkeys.append(decrypt(str.encode(prkey))) addresses = [] for address in wallet['eth_wallet']: addresses.append(address['address']) for address, prkey in zip(addresses, prkeys): tx = eth_tx(amount, currency, recipient, sender=address, prkey=prkey) if tx != False: break if tx == False: status = 'failed' else: status = 'success' tx = str(tx.hex()) return jsonify(status=status, transaction_id=tx, recipient=recipient, amount=amount, currency=currency.upper(), network=coin.upper())