Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def commit_claims(state, claims, caps=None):
for label, content in claims:
state[label] = content
for reader_dh_pk, label in (caps or []):
state.grant_access(reader_dh_pk, label)
store = {}
chain = hippiehug.Chain(store)
head = state.commit(chain)
block = store[head]
block_content = block.items[0]
nonce = ascii2bytes(block_content['nonce'])
# Get associated Merkle tree
mtr_hash = ascii2bytes(block_content['mtr_hash'])
tree = hippiehug.Tree(store, root_hash=mtr_hash)
return nonce, chain, tree
def test_read_claim_from_other_chain():
for i in range(1,100):
alice_params = LocalParams.generate()
alice_state = State()
alice_store = {}
alice_chain = Chain(alice_store, None)
alice_state.identity_info = "Hi, I'm " + pet2ascii(alice_params.dh.pk)
with alice_params.as_default():
alice_head = alice_state.commit(alice_chain)
alice_chain = Chain(alice_store, alice_head)
bob_params = LocalParams.generate()
bob_state = State()
bob_store = {}
bob_chain = Chain(bob_store, None)
bob_state.identity_info = "Hi, I'm " + pet2ascii(bob_params.dh.pk)
with bob_params.as_default():
bob_head = bob_state.commit(bob_chain)
bob_chain = Chain(bob_store, bob_head)
bob_pk = bob_params.dh.pk
with alice_params.as_default():
alice_state[b"bobs_key"] = b"123abc"
alice_state.grant_access(bob_pk, [b"bobs_key"])
alice_head = alice_state.commit(alice_chain)
tree = Tree()
for lookup_key, enc_item in enc_claims + capabilities:
tree.add(key=lookup_key, item=enc_item)
_, evidence = tree.evidence(key=lookup_key)
assert tree.is_in(enc_item, key=lookup_key)
enc_item_hash = evidence[-1].item
tree.store[enc_item_hash] = enc_item
t1 = time.time()
print("\t\tTiming for building non-equiv. tree: %1.1f ms" % ((t1-t0) * 1000))
# Build a chain and a block
t0 = time.time()
c0 = 200
for _ in range(c0):
chain = Chain(tree.store)
payload = Payload.build(tree, nonce).export()
def sign_block(block):
sig = sign(block.hash())
block.aux = pet2ascii(sig)
chain.multi_add([payload], pre_commit_fn=sign_block)
# Pack block
block = chain.store[chain.head]
packed_block = packb(
("S", block.index, block.fingers, block.items, block.aux))
t1 = time.time()
print("\t\tTiming for building a block: %1.1f ms" % ((t1-t0) / c0 * 1000))
def receive_message(self, sender, message_metadata,
other_recipients=None):
sender_head, public_contacts, message_store = message_metadata
if other_recipients is None:
other_recipients = set()
with self.params.as_default():
# Merge stores temporarily
merged_store = ObjectStore(self.global_store)
for key, obj in message_store.items():
merged_store[key] = obj
sender_latest_block = merged_store[sender_head]
self.global_store[sender_head] = sender_latest_block
self.queued_views[sender] = View(
Chain(self.global_store, root_hash=sender_head))
full_sender_view = View(
Chain(merged_store, root_hash=sender_head))
# Add relevant objects from the message store
contacts = self.get_accessible_contacts(
sender, message_metadata, other_recipients)
for contact in contacts:
contact_head = self.get_contact_head_from_view(
full_sender_view, contact)
if contact_head is None:
continue
contact_latest_block = message_store.get(contact_head)
if contact_latest_block is not None:
self.global_store[contact_head] = contact_latest_block
# NOTE: Assumes people send only contacts' latest blocks
Chain(merged_store, root_hash=sender_head))
# Add relevant objects from the message store
contacts = self.get_accessible_contacts(
sender, message_metadata, other_recipients)
for contact in contacts:
contact_head = self.get_contact_head_from_view(
full_sender_view, contact)
if contact_head is None:
continue
contact_latest_block = message_store.get(contact_head)
if contact_latest_block is not None:
self.global_store[contact_head] = contact_latest_block
# NOTE: Assumes people send only contacts' latest blocks
contact_chain = Chain(self.global_store, root_hash=contact_head)
self.global_views[sender][contact] = View(contact_chain)
# Recompute the latest beliefs
for contact in {sender} | contacts:
self.get_latest_view(contact)
other_recipients=None):
sender_head, public_contacts, message_store = message_metadata
if other_recipients is None:
other_recipients = set()
with self.params.as_default():
# Merge stores temporarily
merged_store = ObjectStore(self.global_store)
for key, obj in message_store.items():
merged_store[key] = obj
sender_latest_block = merged_store[sender_head]
self.global_store[sender_head] = sender_latest_block
self.queued_views[sender] = View(
Chain(self.global_store, root_hash=sender_head))
full_sender_view = View(
Chain(merged_store, root_hash=sender_head))
# Add relevant objects from the message store
contacts = self.get_accessible_contacts(
sender, message_metadata, other_recipients)
for contact in contacts:
contact_head = self.get_contact_head_from_view(
full_sender_view, contact)
if contact_head is None:
continue
contact_latest_block = message_store.get(contact_head)
if contact_latest_block is not None:
self.global_store[contact_head] = contact_latest_block
# NOTE: Assumes people send only contacts' latest blocks
contact_chain = Chain(self.global_store, root_hash=contact_head)
self.global_views[sender][contact] = View(contact_chain)