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_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_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) else: return error_utils.invalid_chainid_type() return { 'chainId': chainId, 'address': address, 'redeemScript': redeemScript }
def zchain_transaction_all_history(chainId,coinName,desc,contractAddr,contact,payCode): logger.info('Zchain.Coin.Add.Request') chainId = chainId.lower() if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') if type(coinName) != unicode: return error_utils.mismatched_parameter_type('coinName', 'STRING') if type(contractAddr) != unicode: return error_utils.mismatched_parameter_type('contractAddr', 'STRING') if type(payCode) != unicode: return error_utils.mismatched_parameter_type('payCode', 'STRING') if chainId == "hx": #add record to mongodb db.get_collection("b_addcoin").insert_one({"chainId":chainId,"coinName":coinName,"desc":desc,"contractAddr":contractAddr,"contact":contact,"payCode":payCode}) return True
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_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_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
def zchain_query_getBlockHeight(chainId): logger.info('Zchain.Query.getBlockHeight') if type(chainId) != unicode: return error_utils.mismatched_parameter_type('chainId', 'STRING') chainId = chainId.lower() result = {} if 'erc' in chainId: result = eth_utils.eth_get_block_height() elif 'eth' == chainId: result = eth_utils.eth_get_block_height() elif 'hc' == chainId: cache_record = db.get_collection("b_config").find_one({"key" : "hcsyncblocknum"}) if cache_record is not None: result = int(cache_record["value"]) else: result=0 else: return error_utils.invalid_chainid_type() if result == {}: return error_utils.error_response("Cannot eth trx count.") #print result return result
source,respit = eth_utils.get_transaction_data(trxid) so_re_dic = {'source_trx':source,'respit_trx':respit} if "input" in source: is_cache = True if source != None and respit != None: result = so_re_dic else: return error_utils.invalid_chainid_type(chainId) if result == "": return error_utils.error_response("Cannot query transaction.") if result.has_key("vin"): for i in range(len(result["vin"])): result["vin"][i]["scriptSig"]={"asm":"","hex":""} if is_cache: db.get_collection("b_query_trans_cache").insert_one( {"chainId": chainId, "trxid": trxid,"result":result}) return { 'chainId': chainId, 'data': result } @jsonrpc.method('Zchain.Trans.queryTransBatch(chainId=str, trxids=list)') 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: