def parse_origin_tx(parsed, protobuf=None): origin = parsed.get('requestedClaims')[0].get('origin') tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) if protobuf: itx = forge_utils.parse_to_proto(tx.itx.value, protobuf) return tx, itx else: return tx, None
def __init__(self, state): super().__init__(state) template = json.loads(self.template) self.title = template.get('title') self.start_time = template.get('start_time') self.end_time = template.get('end_time') self.location = template.get('location') self.img_url = template.get('img_url') event_info = forge_utils.parse_to_proto(self.data.value, protos.EventInfo) self.details = event_info.details self.consume_tx = forge_utils.parse_to_proto(event_info.consume_asset_tx, forge_protos.Transaction)
def __init__(self, asset_state): self.address = asset_state.address self.owner = asset_state.owner self.moniker = asset_state.moniker self.readonly = asset_state.readonly self.transferrable = asset_state.transferrable self.ttl = asset_state.ttl self.consumed_time = asset_state.consumed_time self.issuer = asset_state.issuer self.context = asset_state.context self.stake = asset_state.stake self.event_info = forge_utils.parse_to_proto( asset_state.data.value, protos.EventInfo, ) self.type_url = asset_state.data.type_url self.remaining = self.event_info.remaining self.tickets = self.event_info.tickets self.participants = self.event_info.participants self.display_start_time = utils.to_display_time( self.event_info.start_time, ) # for front end display purpose self.display_end_time = utils.to_display_time( self.event_info.end_time, ) self.duration = utils.time_diff( self.event_info.start_time, self.event_info.end_time, ).days self.display_price = int(self.event_info.ticket_price / 1e+16)
def post_handler(**args): wallet_res = args.get('wallet_res') asset_address = wallet_res.get_asset_address() ticket = controllers.get_ticket_state(asset_address) new_tx = controllers.update_tx_multisig(tx=forge_utils.parse_to_proto( forge_utils.multibase_b58decode(ticket.consume_tx), forge_protos.Transaction), signer=wallet_res.get_address(), pk=wallet_res.get_user_pk(), data=forge_utils.encode_to_any( 'fg:x:address', asset_address, )) token = args.get('token') utils.mark_token_status(token, 'succeed') params = { 'tx': new_tx, 'url': utils.server_url( f'/api/did/consume_ticket/consume?_t_={token}&ticket_address={ticket.address}' ), 'description': 'Confirm to use the ticket.', 'workflow': 'use-ticket', 'user_did': wallet_res.get_address() } res = utils.send_did_request(request_type='signature', **params, **args.get('app_params')) logger.debug(f"POST Response: {res}") return json.loads(res)
def buy_ticket_post(middle_user_info): origin = get_origin(middle_user_info) tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) signature = forge_rpc.sign_tx(user, tx) data = send_back_signature(middle_user_info, user.address, signature) r = requests.post(url=buy_url, data=json.dumps(data)) return r.json()
def consume_asset(middle_user_info,ticket_address): consume_url = f'http://10.1.10.177:5000/api/mobile/consume/{ticket_address}' origin = get_origin(middle_user_info) tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin), forge_protos.Transaction) signature = forge_rpc.sign_tx(user, tx) data = send_back_signature(middle_user_info, user.address, signature) r = requests.post(url=consume_url, data=json.dumps(data)) return r.json()
def buy_ticket_mobile(tx, signature): acquire_asset_tx = forge_utils.parse_to_proto(tx.itx.value, forge_protos.AcquireAssetTx) tx.signature = signature ticket_address = acquire_asset_tx.specs[0].address res = forge_rpc.send_tx(tx) if forge_utils.is_response_ok(res): return ticket_address, res.hash else: return None, None
def __init__(self, asset_state): super().__init__(asset_state) state = forge_utils.parse_to_proto(asset_state.data.value, protos.AssetFactoryState) self.description = state.description self.limit = state.limit self.price = state.price self.template = state.template self.allowed_spec_args = state.allowed_spec_args self.asset_name = state.asset_name self.attributes = state.attributes self.num_created = state.num_created self.data = state.data
def deploy(m_wallet): with open(input) as f: raw = json.load(f).get('event_chain') logger.info("Protocol json loaded!") decoded = utils.multibase_b64decode(raw) itx = utils.parse_to_proto(decoded, protos.DeployProtocolTx) itx_hash = mcrypto.Hasher('sha3').hash(itx.SerializeToString()) addr = did.AbtDid(role_type='tx', form='short').hash_to_did(itx_hash) itx.address = addr res = forge.rpc.send_itx(tx=itx, wallet=m_wallet, type_url='fg:t:deploy_protocol', nonce=0) if res.code == 0: logger.info("Success: event_chain tx deployed.") else: logger.error("Fail: Error in deploying event_chain tx.") logger.error(res)
def deploy(forge, input_file): m_wallet = get_moderator_wallet() with open(input_file) as f: raw = list(json.load(f).values())[0] logger.info("Protocol json loaded!") decoded = utils.multibase_b64decode(raw) itx = utils.parse_to_proto(decoded, protos.DeployProtocolTx) itx_hash = mcrypto.Hasher('sha3').hash(itx.SerializeToString()) addr = did.AbtDid(role_type='tx', form='short').hash_to_did(itx_hash) itx.address = addr res = forge.rpc.send_itx(tx=itx, wallet=m_wallet, type_url='fg:t:deploy_protocol', nonce=0) if res.code == 0: logger.info("Successfully deployed new transaction protocol.") else: logger.error("Fail to deploy new transaction protocol.") logger.error(res)
def __init__(self, asset_state): self.address = asset_state.address self.owner = asset_state.owner self.moniker = asset_state.moniker self.readonly = asset_state.readonly self.transferrable = asset_state.transferrable self.ttl = asset_state.ttl self.consumed_time = asset_state.consumed_time self.issuer = asset_state.issuer self.context = asset_state.context self.stake = asset_state.stake self.ticket_info = forge_utils.parse_to_proto( asset_state.data.value, protos.TicketInfo, ) self.type_url = asset_state.data.type_url self.id = self.ticket_info.id self.event_address = self.ticket_info.event_address self.is_used = self.ticket_info.is_used
def post_handler(**args): wallet_res = args.get('wallet_res') token = args.get('token') tx = wallet_res.get_origin_tx() tx.signature = wallet_res.get_signature() parsed_tx = forge_utils.parse_to_proto(tx.itx.value, forge_protos.AcquireAssetTx) ticket_address = parsed_tx.specs[0].address res = forge.rpc.send_tx(tx) if res.hash: utils.mark_token_status(token, 'succeed') logger.debug(f"Successfully bought ticket. Hash: " + f"{res.hash}") return { 'status': 0, 'ticket': ticket_address, 'hash': res.hash, 'tx': forge_utils.multibase_b58encode(tx.SerializeToString()) } else: utils.mark_token_status(token, 'error') logger.debug(f"Fail to buy ticket with error: " + f"{res}") return {'error': f"Oops, error code: {res.code}"}
def get_current_txs_info(db_height: int): ''' Get all aggregate transaction info from the height of which db already has a record to the current height of the chain, for db update only ------ Args: db_height(int): the height of which db has a record Output: res(list): list of dicts which contains the info of each aggregate tx, including the info of sku (name, price, purchase time) and the info of vm and parties (names and addresses) db_height(int): new db height, which is the current height of the chain ''' block_height = rpc.get_chain_info().info.block_height logger.debug(block_height) # get blocks info from (current db height + 1) to current block height args = {"from": db_height + 1, "to": block_height} cursor = None next_page = True res = [] while next_page: logger.debug("a new page starts") page = rpc.get_blocks(height_filter=protos.RangeFilter(**args), empty_excluded=True, paging=protos.PageInput(cursor=cursor)) next_page = page.page.next cursor = page.page.cursor blocks = page.blocks logger.debug(f"blocks are \n {blocks}") for i in range(len(blocks)): single_block_txs_hashes = blocks[i].txs_hashes logger.debug(blocks[i]) logger.debug("hashes are ", single_block_txs_hashes) for hash in single_block_txs_hashes: t = rpc.get_single_tx_info(hash) if t.code == 0 and t.tx.itx.type_url == 'fg:t:aggregate': itx = utils.parse_to_proto(t.tx.itx.value, AggregateTx) value = utils.biguint_to_int(itx.value) time = datetime.fromtimestamp(itx.time.seconds) info = dict( hash=hash, vm_addr=getattr(t.tx, "from"), sku=itx.sku, price=value, time=time, op_addr=itx.operator, ma_addr=itx.manufacturer, su_addr=itx.supplier, lo_addr=itx.location, ) info.update( dict( vm=get_wallet_moniker(info['vm_addr']), op=get_wallet_moniker(info['op_addr']), ma=get_wallet_moniker(info['ma_addr']), su=get_wallet_moniker(info['su_addr']), lo=get_wallet_moniker(info['lo_addr']), )) res.append(info) return res, block_height