def list_voter_public_keys(private_keys): temp = [] for i in range(len(private_keys)): temp.append( crypto.Ed25519SigningKey( private_keys[i]).get_verifying_key().encode().decode()) return temp
def key_pair_from_ed25519_key(hex_private_key): """Generate base58 encode public-private key pair from a hex encoded private key""" priv_key = crypto.Ed25519SigningKey(bytes.fromhex(hex_private_key)[:32], encoding='bytes') public_key = priv_key.get_verifying_key() return CryptoKeypair( private_key=priv_key.encode(encoding='base58').decode('utf-8'), public_key=public_key.encode(encoding='base58').decode('utf-8'))
def transfer_ballot(voter_private_key, candidate_public_key, verified_transfer_ids, candidate_dict, voter_ids): # find corresponding public_key by its private_key voter_public_key = crypto.Ed25519SigningKey(voter_private_key).get_verifying_key().encode().decode() for voter_id in voter_ids: retrieve_block = bdb.transactions.retrieve(voter_id) # call by txid if retrieve_block['transaction']['fulfillments'][0]['owners_before'][0] == voter_public_key: logging.info("Voter (" + retrieve_block['transaction']['fulfillments'][0]['owners_before'][0] + ")" + " is trying to transfer ballot to Candidate (" + str(candidate_public_key) + ")") if not poll_id_check(retrieve_block, candidate_public_key, candidate_dict): # voter is trying to vote to a candidate in different poll OR # Candidate is not in the list of candidates of the poll. return None cid = 0 condition = retrieve_block['transaction']['conditions'][cid] transfer_input = {'fulfillment': condition['condition']['details'], 'input': {'cid': cid, 'txid': retrieve_block['id'], }, 'owners_before': condition['owners_after'], } prepared_transfer_tx = bdb.transactions.prepare( operation='TRANSFER', asset=retrieve_block['transaction']['asset'], inputs=transfer_input, owners_after=candidate_public_key, # asset endpoint ) fulfilled_transfer_tx = bdb.transactions.fulfill(prepared_transfer_tx, private_keys=voter_private_key, ) try: sent_transfer_tx = bdb.transactions.send(fulfilled_transfer_tx) except TransportError as Error: logging.warning( 'Already voted to another candidate Or It is an invalid transaction') # Duplicate vote error logging return None try: time.sleep(delay) status = bdb.transactions.status(sent_transfer_tx['id']) if status['status'] == 'valid': # When the transaction is already sent and valid. logging.warning('Already voted to same candidate') # log vote success return None elif status['status'] == 'backlog': # When the transaction is sent and waiting to be valid. logging.info("Transaction ID : " + sent_transfer_tx['id'] + " Voter (" + retrieve_block['transaction']['fulfillments'][0]['owners_before'][0] + ")" + " tied to transfer ballot to Candidate (" + str(candidate_public_key) + ")") return sent_transfer_tx['id'] # return only the sent_transfer_tx's id except NotFoundError as e: logger.info('Transaction "%s" was not found.', txid) return None return None
def get_pair(): prv = base58.b58encode( unhexlify( "2470319889fed2139a6dd99ea3aef2d396e9e7475132337bdb04d47ca7d2028c") ) # prv = base58.b58encode(unhexlify("92c9578337c55e4f0a706e1fb93a43e8c20bdf7dcdec473f52f4ab55e663dcb6")) # prv = base64.b58decode("uc+qr3k04DB\Nf0YONdnCA==\\000DhqC9A3ooLgD3i4V34H") # prv = "uc+qr3k04DBFNf0YONdnCA==\\000DhqC9A3ooLgD3i4V34H" sk = crypto.Ed25519SigningKey(key=prv) pk = sk.get_verifying_key().encode(encoding='base58') return prv, pk
def transfer_ballot(voter_private_key, candidate_public_key, voter_ids): voter_public_key = crypto.Ed25519SigningKey( voter_private_key).get_verifying_key().encode().decode() for voter_id in voter_ids: retrieve_block = bdb.transactions.retrieve(voter_id) # call by txid if retrieve_block['transaction']['fulfillments'][0]['owners_before'][ 0] == voter_public_key: # if not poll_id_check(retrieve_block, candidate_dict): # # voter is trying to vote to a candidate in different poll # logging.warning('Vote to different Poll Committed') # return None # if not candidate_check(candidate_public_key, candidate_list): # # Candidate is not in the list of candidates of the poll. # logging.warning('Candidate is not in the poll') # return None # if not duplicate_check(retrieve_block, verified_transfer_ids) : # logging.warning('Duplicate Vote Committed') # Duplicate vote error logging # return None try: transfer_block = bdb.transactions.transfer( retrieve_block, # txid called block candidate_public_key, # asset endpoint asset=retrieve_block['transaction'] ['asset'], # txid called asset signing_key=voter_private_key # voter login key ) except TransportError as Error: logging.warning( 'Duplicate Vote Committed') # Duplicate vote error logging return None # log vote success logging.info("Transaction ID : " + transfer_block['id'] + " Voter (" + retrieve_block['transaction'] ['fulfillments'][0]['owners_before'][0] + ")" + " transferred ballot to Candidate (" + str(candidate_public_key) + ")") time.sleep(delay) return [transfer_block['id'], voter_public_key] # return only the transfer_block_id logging.warning("There's no voter data on DB") return None
def sign(self, signing_dict, signing_key): signing_str = self.serialize(signing_dict) signing_key = crypto.Ed25519SigningKey(signing_key) return signing_key.sign(signing_str.encode()).decode()
def get_public_key(private_key): """get corresponding public key """ public_key = crypto.Ed25519SigningKey( private_key).get_verifying_key().to_ascii() return public_key.decode()