def zchain_withdraw_getinfo(chainId): """ 查询提现账户的信�? :param chainId: :return: """ chainId = chainId.lower() logger.info('Zchain.Withdraw.GetInfo') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') records = db.b_config.find_one({'key': 'withdrawaddress'}, {'_id': 0}) address = "" if records == None: db.b_config.insert_one({"key": "withdrawaddress", "value": []}) records = db.b_config.find_one({'key': 'withdrawaddress'}, {'_id': 0}) for r in records["value"]: if r['chainId'] == chainId: address = r['address'] if address == "": if chainId == "eth": address = eth_utils.eth_create_address() # eth_utils.eth_backup() records["value"].append({"chainId": "eth", "address": address}) elif sim_btc_plugin.has_key(chainId): address = sim_btc_plugin[chainId].sim_btc_create_withdraw_address() sim_btc_plugin.sim_btc_backup_wallet() records["value"].append({"chainId": chainId, "address": address}) elif chainId == "hc": address = hc_plugin.hc_create_withdraw_address() hc_plugin.hc_backup_wallet() records["value"].append({"chainId": chainId, "address": address}) elif chainId == "etp": address = etp_utils.etp_create_withdraw_address() records["value"].append({"chainId": "etp", "address": address}) db.b_config.update({"key": "withdrawaddress"}, {"$set": {"value": records["value"]}}) balance = 0.0 if chainId == "eth": balance = eth_utils.eth_get_base_balance(address) elif sim_btc_plugin.has_key(chainId): balance = sim_btc_plugin.sim_btc_get_withdraw_balance() elif chainId == "hc": balance = hc_plugin.hc_get_withdraw_balance() elif chainId == "etp": balance = etp_utils.etp_get_addr_balance(address) else: return error_utils.invalid_chainid_type(chainId) return { 'chainId': chainId, 'address': address, 'balance': balance }
def zchain_trans_queryTrx(chainId, trxid): chainId = chainId.lower() logger.info('Zchain.Trans.queryTrans') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = "" is_cache = False cache_record = db.get_collection("b_query_trans_cache").find_one({"chainId": chainId,"trxid":trxid}) if cache_record is not None: return { 'chainId': chainId, 'data': cache_record["result"] } if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_get_transaction(trxid) if "vout" in result: is_cache = True try: if "confirmations" in result: if result["confirmations"]<=7: is_cache = False else: is_cache = False except Exception,ex: print "query confirmation failed",ex
def zchain_address_validate(chainId,addr): chainId = chainId.lower() logger.info("Zchain.Address.validate") if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if type(addr) != unicode: return error_utils.mismatched_parameter_type('addr', 'STRING') result = None if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_validate_address(addr) elif chainId == "hc": result = hc_plugin.hc_validate_address(addr) elif chainId == "usdt": result = usdt_plugin.omni_validate_address(addr) elif chainId == "btm": result = btm_plugin.btm_validate_address(addr) return { "chainId": chainId, "valid": result } elif chainId == "eth" or 'erc'in chainId: result = eth_utils.eth_validate_address(addr) return { "chainId": chainId, "valid": result } else: return error_utils.invalid_chainid_type(chainId) return { "chainId":chainId, "valid" : result.get("isvalid") }
def zchain_trans_createFullSendTrx(chainId, from_addr,dest_info): logger.info('Zchain.Trans.createTrx') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') chainId = chainId.lower() result = {} if sim_btc_plugin.has_key(chainId): is_fast_record = db.get_collection("b_config").find_one({"key": "is_fast"}) is_fast = False if is_fast_record is not None: is_fast = bool(is_fast_record["value"]) result = sim_btc_plugin[chainId].sim_btc_create_transaction(from_addr,dest_info,is_fast,True) elif chainId == "hc": result = hc_plugin.hc_create_transaction(from_addr, dest_info) elif chainId == "usdt": result = usdt_plugin.omni_create_transaction(from_addr,dest_info) elif chainId == "btm": result = btm_plugin.btm_create_transaction(from_addr, dest_info) else: return error_utils.invalid_chainid_type(chainId) if result == {}: return error_utils.error_response("Cannot create transaction.") return { 'chainId': chainId, 'data': result }
def zchain_trans_decodeTrx(chainId, trx_hex): chainId = chainId.lower() logger.info('Zchain.Trans.DecodeTrx') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_decode_hex_transaction(trx_hex) elif chainId == "hc": result = hc_plugin.hc_decode_hex_transaction(trx_hex) elif chainId == "usdt": result = usdt_plugin.omni_decode_hex_transaction(trx_hex) elif chainId == "btm": result = btm_plugin.btm_decode_hex_transaction(trx_hex) else: return error_utils.invalid_chainid_type(chainId) if result == "": return error_utils.error_response("Cannot create transaction.") return { 'chainId': chainId, 'data': result }
def zchain_multisig_create(chainId, addrs, amount): chainId = chainId.lower() logger.info('Zchain.Multisig.Create') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if type(addrs) != list: return error_utils.mismatched_parameter_type('addrs', 'ARRAY') if type(amount) != int: return error_utils.mismatched_parameter_type('amount', 'INTEGER') address = "" redeemScript = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_create_multisig(addrs, amount) if result is not None: address = result["address"] redeemScript = result["redeemScript"] mutisig_record = db.get_collection("b_"+chainId+"_multisig_address").find_one({"address": address}) if mutisig_record is not None: db.get_collection("b_"+chainId+"_multisig_address").remove({"address": address}) data = {"address": address, "redeemScript": redeemScript, "addr_type":0} db.get_collection("b_"+chainId+"_multisig_address").insert_one(data) elif chainId == "hc": result = hc_plugin.hc_create_multisig(addrs, amount) if result is not None: address = result["address"] redeemScript = result["redeemScript"] mutisig_record = db.get_collection("b_hc_multisig_address").find_one({"address": address}) if mutisig_record is not None: db.get_collection("b_hc_multisig_address").remove({"address": address}) data = {"address": address, "redeemScript": redeemScript, "addr_type":0} db.get_collection("b_hc_multisig_address").insert_one(data) elif chainId == "btm": result = btm_plugin.btm_create_multisig(addrs, amount) if result is not None: address = result["address"] redeemScript = result["redeemScript"] mutisig_record = db.get_collection("b_btm_multisig_address").find_one({"address": address}) if mutisig_record is not None: db.get_collection("b_btm_multisig_address").remove({"address": address}) data = {"address": address, "redeemScript": redeemScript, "addr_type":0} db.get_collection("b_btm_multisig_address").insert_one(data) elif chainId == "usdt": result = usdt_plugin.omni_create_multisig(addrs, amount) if result is not None: address = result["address"] redeemScript = result["redeemScript"] mutisig_record = db.get_collection("b_" + chainId + "_multisig_address").find_one({"address": address}) if mutisig_record is not None: db.get_collection("b_" + chainId + "_multisig_address").remove({"address": address}) data = {"address": address, "redeemScript": redeemScript, "addr_type": 0} db.get_collection("b_" + chainId + "_multisig_address").insert_one(data) else: return error_utils.invalid_chainid_type(chainId) return { 'chainId': chainId, 'address': address, 'redeemScript': redeemScript }
def zchain_address_create(chainId): chainId = chainId.lower() logger.info('Create_address coin: %s' % (chainId)) if chainId == 'eth': address = eth_utils.eth_create_address() elif sim_btc_plugin.has_key(chainId): address = sim_btc_plugin[chainId].sim_btc_create_address() elif chainId == "hc": address = hc_plugin.hc_create_address() else: return error_utils.invalid_chainid_type(chainId) if address != "": if chainId == 'eth': pass # eth_utils.eth_backup() else: pass # btc_utils.btc_backup_wallet() data = db.b_chain_account.find_one({"chainId": chainId, "address": address}) if data != None: return {'chainId': chainId, 'error': '创建地址失败'} d = {"chainId": chainId, "address": address, "name": "", "pubKey": "", "securedPrivateKey": "", "creatorUserId": "", "balance": {}, "memo": "", "createTime": datetime.now().strftime("%Y-%m-%d %H:%M:%S")} db.b_chain_account.insert(d) return {'chainId': chainId, 'address': address} else: return {'chainId': chainId, 'error': '创建地址失败'}
def zchain_crypt_verify_message(chainId, addr, message, signature): chainId = chainId.lower() logger.info('Zchain.Crypt.VerifyMessage') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = False cache_record = db.get_collection("b_verify_cache").find_one({"chainId": chainId,"addr":addr, "message":message, "signature":signature}) if cache_record is not None: result = True return { 'chainId': chainId, 'data': result } if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_verify_signed_message(addr, message, signature) elif chainId == "hc": result = hc_plugin.hc_verify_signed_message(addr, message, signature) elif (chainId == 'eth') or ('erc' in chainId): #print 1 result = eth_utils.eth_verify_signed_message(addr, message, signature) else: return error_utils.invalid_chainid_type(chainId) if result: db.get_collection("b_verify_cache").insert_one( {"chainId": chainId, "addr": addr, "message": message, "signature": signature}) return { 'chainId': chainId, 'data': result }
def zchain_addr_importaddr(chainId, addr): logger.info('Zchain.Addr.importAddr') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if sim_btc_plugin.has_key(chainId): sim_btc_plugin[chainId].sim_btc_import_addr(addr) elif chainId == "hc": hc_plugin.hc_import_addr(addr) elif chainId == "usdt": usdt_plugin.omni_import_addr(addr) elif chainId.lower() == 'eth': if "erc" in addr: temp_chainId = chainId.lower() pos = addr.find("erc") handle_addr = addr[0:pos] asset = db.b_eths_address.find_one({'chainId':temp_chainId,'address':handle_addr}) if asset == None: db.b_eths_address.insert({'chainId': temp_chainId, 'address': handle_addr, 'isContractAddress': True}) else: db.b_eths_address.update({'chainId':temp_chainId,'address':handle_addr},{"$set":{ 'isContractAddress': True}}) else: temp_chainId = chainId.lower() asset = db.b_eths_address.find_one({'chainId': temp_chainId, 'address': addr}) if asset == None: db.b_eths_address.insert({'chainId': temp_chainId, 'address': addr, 'isContractAddress': False}) else: db.b_eths_address.update({'chainId':temp_chainId,'address':addr},{"$set":{ 'isContractAddress': False}}) eth_utils.add_guard_address(addr) elif ('erc' in chainId.lower()): erc_asset = None if erc_chainId_map.has_key(chainId): erc_asset = erc_chainId_map[chainId] if erc_asset != None: if "erc" in addr: pos = addr.find("erc") handle_addr = addr[0:pos] asset = db.b_eths_address.find_one({'chainId': chainId, 'address': handle_addr}) if asset == None: db.b_eths_address.insert( {'chainId': chainId, 'address': handle_addr, 'isContractAddress': True}) else: db.b_eths_address.update({'chainId': chainId, 'address': handle_addr}, {"$set": {'isContractAddress': True}}) else: asset = db.b_eths_address.find_one({'chainId': chainId, 'address': addr}) if asset == None: db.b_eths_address.insert( {'chainId': chainId, 'address': addr, 'isContractAddress': False}) else: db.b_eths_address.update({'chainId': chainId, 'address': addr}, {"$set": {'isContractAddress': False}}) eth_utils.add_guard_address(addr) else: return error_utils.invalid_chainid_type(chainId) return { 'chainId': chainId, 'data': "" }
def zchain_trans_broadcastTrx(chainId, trx): logger.info('Zchain.Trans.broadcastTrx') global last_clean_broadcast_cache_time if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') md = hashlib.md5() md.update(trx) trxId = md.hexdigest() broad_cast_record = db.get_collection("b_broadcast_trans_cache").find_one({ "chainId": chainId, "trx": trxId, "effectiveTime": { "$gt": int(time.time()) - 10 } }) if broad_cast_record is not None: if broad_cast_record['result'] == "": return error_utils.error_response("Cannot broadcast transactions.") return {'chainId': chainId, 'data': broad_cast_record['result']} result = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_broadcaset_trx(trx) elif chainId == "hc": result = hc_plugin.hc_broadcaset_trx(trx) elif chainId.lower() == "eth": result = eth_utils.eth_send_raw_transaction(trx) elif 'erc' in chainId.lower(): result = eth_utils.eth_send_raw_transaction(trx) else: return error_utils.invalid_chainid_type() db.get_collection("b_broadcast_trans_cache").insert_one({ "chainId": chainId, "trx": trxId, "effectiveTime": int(time.time()), "result": result }) if int(time.time()) - last_clean_broadcast_cache_time > 10 * 60: db.get_collection("b_broadcast_trans_cache").delete_many( {"effectiveTime": { "$lt": int(time.time()) - 10 }}) last_clean_broadcast_cache_time = int(time.time()) if result == "": return error_utils.error_response("Cannot broadcast transactions.") return {'chainId': chainId, 'data': result}
def zchain_multisig_add(chainId, addrs, amount, addrType): logger.info('Zchain.Multisig.Add') chainId = chainId.lower() if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if type(addrs) != list: return error_utils.mismatched_parameter_type('addrs', 'ARRAY') if type(amount) != int: return error_utils.mismatched_parameter_type('amount', 'INTEGER') if type(addrType) != int: return error_utils.mismatched_parameter_type('addrType', 'INTEGER') address = "" if sim_btc_plugin.has_key(chainId): multisig_addr = sim_btc_plugin[chainId].sim_btc_add_multisig( addrs, amount) if multisig_addr is not None: addr_info = sim_btc_plugin[chainId].sim_btc_validate_address( multisig_addr) if addr_info == "": multisig_record = db.get_collection( "b_" + chainId + "_multisig_address").find_one( {"address": multisig_addr}) if multisig_record is not None: db.get_collection("b_" + chainId + "_multisig_address").remove( {"address": multisig_addr}) data = { "address": addr_info["address"], "redeemScript": addr_info["hex"], "addr_type": addrType } db.get_collection("b_" + chainId + "_multisig_address").insert_one(data) address = addr_info["address"] elif chainId == "hc": multisig_addr = hc_plugin.hc_add_multisig(addrs, amount) if multisig_addr is not None: addr_info = hc_plugin.hc_validate_address(multisig_addr) if addr_info is not None: multisig_record = db.get_collection( "b_hc_multisig_address").find_one( {"address": multisig_addr}) if multisig_record is not None: db.get_collection("b_hc_multisig_address").remove( {"address": multisig_addr}) data = { "address": addr_info["address"], "redeemScript": addr_info["hex"], "addr_type": addrType } db.get_collection("b_hc_multisig_address").insert_one(data) else: return error_utils.invalid_chainid_type() return {'chainId': chainId, 'data': address}
def zchain_query_getUtxoCount(chainId, address): logger.info('Zchain.Query.getUtxoCount') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') chainId = chainId.lower() result = {} if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_get_trx_out(address) elif chainId == "hc": result = hc_plugin.hc_get_trx_out(address) else: return error_utils.invalid_chainid_type() #print result return len(result)
def zchain_addr_importaddr(chainId, addr): logger.info('Zchain.Addr.importAddr') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if sim_btc_plugin.has_key(chainId): sim_btc_plugin[chainId].sim_btc_import_addr(addr) elif chainId == "hc": hc_plugin.hc_import_addr(addr) else: return error_utils.invalid_chainid_type() return { 'chainId': chainId, 'data': "" }
def zchain_trans_getTrxOuts(chainId, addr): chainId = chainId.lower() logger.info('Zchain.Trans.getTrxOuts') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = False if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_query_tx_out(addr) elif chainId == "hc": result = hc_plugin.hc_query_tx_out(addr) else: return error_utils.invalid_chainid_type() return {'chainId': chainId, 'data': result}
def zchain_address_get_balance(chainId, addr): logger.info('Zchain.Address.GetBalance') chainId = chainId.lower() record_unspent = db.b_balance_unspent.find_one({'chainId': chainId, 'address': addr}) trx_unspent=[] trx_spent = [] balance = 0.0 record_spent = None if record_unspent is not None: trx_unspent = record_unspent.get("trxdata") record_spent = db.b_balance_spent.find_one({'chainId': chainId, 'address': addr}) if record_spent is not None : trx_spent = record_spent.get("trxdata") else: return { 'chainId': chainId, 'address': addr, 'balance': 0 } unspent = [] for trx in trx_unspent : if trx not in trx_spent: unspent.append(trx) for id in unspent: pos1 = len(chainId) id = id[pos1:] pos2 = id.find('I') index = id[pos2 + 1:] id = id[0:pos2] result = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_get_transaction(id) elif chainId == "hc": result = hc_plugin.hc_get_transaction(id) if result is "": continue vout = round(float(result.get("vout")[int(index)].get("value")), 8) balance = round(vout + balance, 8) return { 'chainId': chainId, 'address': addr, 'balance': balance }
def zchain_trans_createTrx(chainId, from_addr, dest_info): logger.info('Zchain.Trans.createTrx') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') chainId = chainId.lower() result = {} if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_create_transaction( from_addr, dest_info) elif chainId == "hc": result = hc_plugin.hc_create_transaction(from_addr, dest_info) else: return error_utils.invalid_chainid_type() if result == {}: return error_utils.error_response("Cannot create transaction.") return {'chainId': chainId, 'data': result}
def zchain_trans_CombineTrx(chainId, transactions): logger.info('Zchain.Trans.CombineTrx') chainId = chainId.lower() if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_combine_trx(transactions) elif chainId == "hc": result = hc_plugin.hc_combine_trx(transactions) else: return error_utils.invalid_chainid_type() if result == "": return error_utils.error_response("Cannot combine transaction.") return {'chainId': chainId, 'data': result}
def zchain_Trans_sign(chainId, addr, trx_hex, redeemScript): logger.info('Zchain.Trans.Sign') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') signed_trx = "" if sim_btc_plugin.has_key(chainId): signed_trx = sim_btc_plugin[chainId].sim_btc_sign_transaction( addr, redeemScript, trx_hex) elif chainId == "hc": signed_trx = hc_plugin.hc_sign_transaction(addr, redeemScript, trx_hex) else: return error_utils.invalid_chainid_type() if signed_trx == "": return error_utils.error_response("Cannot sign trans.") return {'chainId': chainId, 'data': signed_trx}
def zchain_crypt_sign(chainId, addr, message): logger.info('Zchain.Crypt.Sign') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') signed_message = "" if sim_btc_plugin.has_key(chainId): signed_message = sim_btc_plugin[chainId].sim_btc_sign_message( addr, message) elif chainId == "hc": signed_message = hc_plugin.hc_sign_message(addr, message) else: return error_utils.invalid_chainid_type() if signed_message == "": return error_utils.error_response("Cannot sign message.") return {'chainId': chainId, 'data': signed_message}
def zchain_crypt_verify_message(chainId, addr, message, signature): chainId = chainId.lower() logger.info('Zchain.Crypt.VerifyMessage') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = False if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_verify_signed_message(addr, message, signature) elif chainId == "hc": result = hc_plugin.hc_verify_signed_message(addr, message, signature) else: return error_utils.invalid_chainid_type() return { 'chainId': chainId, 'data': result }
def zchain_trans_broadcastTrx(chainId, trx): logger.info('Zchain.Trans.broadcastTrx') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') result = "" if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_broadcaset_trx(trx) elif chainId == "hc": result = hc_plugin.hc_broadcaset_trx(trx) else: return error_utils.invalid_chainid_type() if result == "": return error_utils.error_response("Cannot broadcast transactions.") return { 'chainId': chainId, 'data': result }
def zchain_trans_queryTrx(chainId, trxids): chainId = chainId.lower() logger.info('Zchain.Trans.queryTransBatch') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if type(trxids) != list: return error_utils.mismatched_parameter_type('trxids', 'LIST') res_data = {} for one_txid in trxids: result = "" is_cache = False cache_record = db.get_collection("b_query_trans_cache").find_one({ "chainId": chainId, "trxid": one_txid }) if cache_record is not None: res_data[one_txid] = cache_record["result"] continue if sim_btc_plugin.has_key(chainId): result = sim_btc_plugin[chainId].sim_btc_get_transaction(one_txid) if "vout" in result: is_cache = True try: if "confirmations" in result: if result["confirmations"] <= 0: is_cache = False except Exception, ex: print "query confirmation failed", ex elif chainId == "hc": result = hc_plugin.hc_get_transaction(one_txid) if "vout" in result: is_cache = True try: if "vin" in result and len(result["vin"]) > 0: if result["vin"][0]["blockheight"] <= 0: is_cache = False except Exception, ex: print "query confirmation failed", ex