Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# prompt hidden user entry
salt = getpass.getpass("Enter your passphrase (salt): ")
# prompt hidden user entry
password = getpass.getpass("Enter your password: ")
# create key from credentials
key = SigningKey.from_credentials(salt, password)
pubkey_from = key.pubkey
# prompt entry
pubkey_to = input("Enter certified pubkey: ")
# capture current block to get version and currency and blockstamp
current_block = await client(bma.blockchain.current)
# create our Identity document to sign the Certification document
identity = await get_identity_document(client, current_block, pubkey_to)
if identity is None:
print("Identity not found for pubkey {0}".format(pubkey_to))
# Close client aiohttp session
await client.close()
sys.exit(1)
# send the Certification document to the node
certification = get_certification_document(current_block, identity, pubkey_from)
# sign document
certification.sign([key])
# Here we request for the path wot/certify
block_number=ud_data["block_number"],
timestamp=ud_data["time"],
amount=ud_data["amount"],
base=ud_data["base"])
if start <= dividend.block_number <= end:
self._logger.debug("Dividend of block {0}".format(dividend.block_number))
block_numbers.append(dividend.block_number)
if self._dividends_processor.commit(dividend):
dividends[connection].append(dividend)
for tx in transactions[connection]:
txdoc = TransactionDoc.from_signed_raw(tx.raw)
for input in txdoc.inputs:
# For each dividends inputs, if it is consumed (not present in ud history)
if input.source == "D" and input.origin_id == connection.pubkey and input.index not in block_numbers:
block_data = await self._bma_connector.get(self.currency, bma.blockchain.block,
req_args={'number': input.index})
block = Block.from_signed_raw(block_data["raw"] + block_data["signature"] + "\n")
dividend = Dividend(currency=self.currency,
pubkey=connection.pubkey,
block_number=input.index,
timestamp=block.mediantime,
amount=block.ud,
base=block.unit_base)
self._logger.debug("Dividend of block {0}".format(dividend.block_number))
if self._dividends_processor.commit(dividend):
dividends[connection].append(dividend)
return dividends
:param sakia.data.entities.Connection connection: the connection publishing ms doc
:param str secret_key: The account SigningKey salt
:param str password: The account SigningKey password
:param str mstype: The type of membership demand. "IN" to join, "OUT" to leave
"""
self._logger.debug("Send membership")
blockUID = self._blockchain_processor.current_buid(connection.currency)
membership = Membership(10, connection.currency,
connection.pubkey, blockUID, mstype, connection.uid,
connection.blockstamp, None)
key = SigningKey(secret_key, password, connection.scrypt_params)
membership.sign([key])
self._logger.debug("Membership : {0}".format(membership.signed_raw()))
responses = await self._bma_connector.broadcast(connection.currency, bma.blockchain.membership,
req_args={'membership': membership.signed_raw()})
result = await parse_bma_responses(responses)
return result
async def get_block(self, number=None):
"""
Get a block
:param int number: The block number. If none, returns current block.
"""
if number is None:
block_number = self.network.current_blockUID.number
data = await self.bma_access.future_request(bma.blockchain.Block,
req_args={'number': block_number})
else:
logging.debug("Requesting block {0}".format(number))
data = await self.bma_access.future_request(bma.blockchain.Block,
req_args={'number': number})
return data
def filter_nodes(self, request, nodes):
def compare_versions(node, version):
if node.version and node.version != '':
try:
return parse_version(node.version) >= parse_version(version)
except TypeError:
return False
else:
return True
filters = {
bma.ud.History: lambda n: compare_versions(n, "0.11.0"),
bma.tx.History: lambda n: compare_versions(n, "0.11.0"),
bma.blockchain.Membership: lambda n: compare_versions(n, "0.14")
}
if request in filters:
return [n for n in nodes if filters[request](n)]
else:
return nodes
async def refresh_dividend_data(self, currency, blockchain):
self._logger.debug("Requesting blocks with dividend")
with_ud = await self._bma_connector.get(currency, bma.blockchain.ud)
blocks_with_ud = with_ud['result']['blocks']
if len(blocks_with_ud) > 0:
self._logger.debug("Requesting last block with dividend")
try:
nb_previous_reevaluations = int((blockchain.median_time - blockchain.parameters.ud_reeval_time_0)
/ blockchain.parameters.dt_reeval)
last_reeval_offset = (blockchain.median_time -
(blockchain.parameters.ud_reeval_time_0 +
nb_previous_reevaluations * blockchain.parameters.dt_reeval)
)
dt_reeval_block_target = max(blockchain.current_buid.number - int(last_reeval_offset
/ blockchain.parameters.avg_gen_time),
0)
async def initialize_identity(self, identity, log_stream, progress):
"""
Initialize memberships and other data for given identity
:param sakia.data.entities.Identity identity:
:param function log_stream: callback to log text
:param function progress: callback for progressbar
"""
log_stream("Requesting membership data")
progress(1/3)
try:
memberships_data = await self._bma_connector.get(identity.currency, bma.blockchain.memberships,
req_args={'search': identity.pubkey})
if block_uid(memberships_data['sigDate']) == identity.blockstamp \
and memberships_data['uid'] == identity.uid:
identity.written = True
for ms in memberships_data['memberships']:
if ms['written'] and ms['written'] > identity.membership_written_on:
identity.membership_buid = BlockUID(ms['blockNumber'], ms['blockHash'])
identity.membership_type = ms['membership']
identity.membership_written_on = ms['written']
progress(1 / 3)
if identity.membership_buid:
log_stream("Requesting membership timestamp")
ms_block_data = await self._bma_connector.get(identity.currency, bma.blockchain.block,
req_args={'number': identity.membership_buid.number})
if ms_block_data:
async def get_expiration_date(self, community):
try:
membership = await self.membership(community)
join_block_number = membership['blockNumber']
try:
join_block = await community.bma_access.future_request(bma.blockchain.Block,
req_args={'number': join_block_number})
parameters = await community.bma_access.future_request(bma.blockchain.Parameters)
join_date = join_block['medianTime']
expiration_date = join_date + parameters['sigValidity']
except NoPeerAvailable:
expiration_date = None
except errors.DuniterError as e:
logging.debug("Expiration date not found")
expiration_date = None
except MembershipNotFoundError:
expiration_date = None
return expiration_date
async def ud_before(self, currency, block_number, udblocks=[]):
"""
:param currency:
:param block_number:
:param udblocks: /blockchain/ud/history data of given key
:return:
"""
try:
if not udblocks:
udblocks = await self._bma_connector.get(currency, bma.blockchain.ud)
udblocks = udblocks['result']['blocks']
ud_block_number = next(b for b in udblocks if b <= block_number)
block = await self._bma_connector.get(currency, bma.blockchain.block, {'number': ud_block_number})
return block['dividend'], block['unitbase']
except StopIteration:
self._logger.debug("No dividend generated before {0}".format(block_number))
except NoPeerAvailable as e:
self._logger.debug(str(e))
except errors.DuniterError as e:
if e.ucode == errors.BLOCK_NOT_FOUND:
self._logger.debug(str(e))
else:
raise
return 0, 0