def confirm_staged_grant(emitter, grant_request: Dict, federated: bool, seconds_per_period=None) -> None: pretty_request = grant_request.copy() # WARNING: Do not mutate if federated: # Boring table = [[field.capitalize(), value] for field, value in pretty_request.items()] emitter.echo(tabulate(table, tablefmt="simple")) return period_rate = Web3.fromWei(pretty_request['n'] * pretty_request['rate'], 'gwei') pretty_request['rate'] = f"{pretty_request['rate']} wei/period * {pretty_request['n']} nodes" expiration = pretty_request['expiration'] periods = calculate_period_duration(future_time=MayaDT.from_datetime(expiration), seconds_per_period=seconds_per_period) periods += 1 # current period is always included pretty_request['expiration'] = f"{pretty_request['expiration']} ({periods} periods)" # M of N pretty_request['Threshold Shares'] = f"{pretty_request['m']} of {pretty_request['n']}" del pretty_request['m'] del pretty_request['n'] def prettify_field(field): field_words = [word.capitalize() for word in field.split('_')] field = ' '.join(field_words) return field table = [[prettify_field(field), value] for field, value in pretty_request.items()] table.append(['Period Rate', f'{period_rate} gwei']) table.append(['Policy Value', f'{period_rate * periods} gwei']) emitter.echo("\nSuccessfully staged grant, Please review the details:\n", color='green') emitter.echo(tabulate(table, tablefmt="simple")) click.confirm('\nGrant access and sign transaction?', abort=True)
def maya_dt(self): """ :rtype: MayaDT """ maya_dt = MayaDT.from_datetime(self.datetime) return maya_dt
def time_remaining(n): tomorrow = datetime.utcnow() + timedelta(days=1) midnight = datetime(year=tomorrow.year, month=tomorrow.month, day=tomorrow.day, hour=0, minute=0, second=0, microsecond=0) seconds_remaining = MayaDT.from_datetime(midnight).slang_time() return html.Div( [html.H4("Next Period"), html.H5(seconds_remaining)])
def guild_info_parser(bot, update): message = update.message text = message.text if is_guild_info(text): group_id = message.chat_id timestamp = MayaDT.from_datetime( message.forward_date).datetime().replace(tzinfo=tzlocal()) try: parse_guild_info(group_id, text, timestamp=timestamp) except GuildExistError: message.reply_text( "sorry, can't proccess the data. this group belongs to other guild." ) # print(group_id) # print(message.forward_from) # print(message.date) # print(message.forward_date) # bot.send_message(chat_id=message.chat_id, text="ok") else: bot.send_message(chat_id=group_id, text="something is wrong", parse_mode=telegram.ParseMode.MARKDOWN)
def receive_treasure_map(): """ Okay, so we've received a TreasureMap to store. We begin verifying the treasure map by first validating the request and the received treasure map itself. We set the datastore identifier as the HRAC iff the node is running as a decentralized node. Otherwise, we use the map_id in federated mode. """ if not this_node.federated_only: from nucypher.policy.collections import SignedTreasureMap as _MapClass else: from nucypher.policy.collections import TreasureMap as _MapClass # Step 1: First, we verify the signature of the received treasure map. # This step also deserializes the treasure map iff it's signed correctly. try: received_treasure_map = _MapClass.from_bytes( bytes_representation=request.data, verify=True) except _MapClass.InvalidSignature: log.info( f"Bad TreasureMap HRAC Signature; not storing for HRAC {received_treasure_map._hrac.hex()}" ) return Response("This TreasureMap's HRAC is not properly signed.", status=401) # Additionally, we determine the map identifier from the type of node. # If the node is federated, we also set the expiration for a week. if not this_node.federated_only: map_identifier = received_treasure_map._hrac.hex() else: map_identifier = received_treasure_map.public_id() expiration_date = MayaDT.from_datetime(datetime.utcnow() + timedelta(days=7)) # Step 2: Check if we already have the treasure map. try: with datastore.describe(TreasureMap, map_identifier) as stored_treasure_map: if _MapClass.from_bytes(stored_treasure_map.treasure_map ) == received_treasure_map: return Response("Already have this map.", status=303) except RecordNotFound: # This appears to be a new treasure map that we don't have! pass # Step 3: If the node is decentralized, we check that the received # treasure map is valid pursuant to an active policy. # We also set the expiration from the data on the blockchain here. if not this_node.federated_only: policy_data, alice_checksum_address = this_node.policy_agent.fetch_policy( received_treasure_map._hrac, with_owner=True) # If the Policy doesn't exist, the policy_data is all zeros. if not policy_data[5]: log.info( f"TreasureMap is for non-existent Policy; not storing {map_identifier}" ) return Response( "The Policy for this TreasureMap doesn't exist.", status=409) # Check that this treasure map is from Alice per the Policy. if not received_treasure_map.verify_blockchain_signature( checksum_address=alice_checksum_address): log.info(f"Bad TreasureMap ID; not storing {map_identifier}") return Response( "This TreasureMap doesn't match a paid Policy.", status=402) # Check that this treasure map is valid for the Policy datetime and that it's not disabled. if policy_data[0] or datetime.utcnow( ) >= datetime.utcfromtimestamp(policy_data[5]): log.info( f"Received TreasureMap for an expired/disabled policy; not storing {map_identifier}" ) return Response( "This TreasureMap is for an expired/disabled policy.", status=402) expiration_date = MayaDT.from_datetime( datetime.utcfromtimestamp(policy_data[5])) # Step 4: Finally, we store our treasure map under its identifier! log.info(f"{this_node} storing TreasureMap {map_identifier}") with datastore.describe(TreasureMap, map_identifier, writeable=True) as new_treasure_map: new_treasure_map.treasure_map = bytes(received_treasure_map) new_treasure_map.expiration = expiration_date return Response("Treasure map stored!", status=201)