def insert_data(target, connection, **kw): with open('/Users/fraccaman/Projects/BlockchainBar/network/data.data', 'rb') as data: transactions = pickle.loads(data.read()) # len(transactions) - math.floor(len(transactions) * 0.9) known_transactions = random.sample(transactions, k=250) for known_tx in known_transactions: d = bytearray.fromhex(known_tx[0]) d_short_hash = Crypto.get_instance().get_hasher().short_hash(d) d_full_hash = Crypto.get_instance().get_hasher().hash(d) connection.execute(target.insert(), { 'data': d, 'short_id': d_short_hash, 'full_id': d_full_hash })
async def send_pom(self): while True: key, pom_message = await PubSub.get_subscriber_pom_instance( ).consume() bns = BootstrapIdentity.get_all() for bn in bns: bn_public_key = Crypto.get_instance().get_ec( ).load_public_key_from_string(bn.public_key) if Crypto.get_instance().get_ec().verify( pom_message.token.bn_signature, (pom_message.token.base + pom_message.token.proof + pom_message.token.epoch).encode('utf-8'), bn_public_key): writer = self.connections[bn.address] writer.write(pom_message.serialize()) await writer.drain() break
def sort(peer_list, epoch) -> Union[List[Peer], List[None]]: peer_list.sort(key=lambda x: int(x.__dict__['public_key'].split('.')[0]), reverse=True) n_of_peers = len(peer_list) random_indexes = Crypto.get_instance().get_random().prng_unique(epoch, n_of_peers, n_of_peers) shuffled_peer_list = [None for _ in range(n_of_peers)] for index, random_index in enumerate(random_indexes): shuffled_peer_list[random_index] = peer_list[index] return shuffled_peer_list
def bn_sign(self) -> NoReturn: message = (self.base + self.proof + self.epoch).encode('utf-8') self.bn_signature = Crypto.get_instance().get_ec().sign(message)
def compute_key(self): return Crypto.get_instance().get_hasher().hash( Crypto.get_instance().get_ec().private_key.to_bytes( length=256, byteorder='big', signed=True) + self.epoch.encode() + self.get_peer_public_key().encode())
def is_valid_signature(self) -> bool: return Crypto.get_instance().get_ec().verify( self.bn_signature, (self.base + self.proof + str(self.epoch)).encode('utf-8'), Crypto.get_instance().get_ec().public_key)
def get_public_key(private_key: int) -> Point: return Crypto.get_instance().get_ec().generate_public_from_private( private_key)
def _compute_prev_hash(prev_msg: Message): return Crypto.get_instance().get_hasher().hash( prev_msg.serialize()) if prev_msg is not None else '0' * 64
def verify(self, bn_public_key): return Crypto.get_instance().get_ec().verify( self.token.token, (self.token.base + self.token.proof + self.token.epoch).encode('utf-8'), bn_public_key)
def generate_puzzle(public_key: Point) -> str: nonce = Crypto().get_instance().get_random().generate_random_bytes() formatted_public_key = Crypto.get_instance().get_ec().dump_public_key( public_key) return '{}-{}'.format(formatted_public_key, nonce)