def create_edge(key0, key1, nonce): # TODO: Create removal edge if bytes(key1.verify_key) < bytes(key0.verify_key): key0, key1 = key1, key0 edge = Edge() edge.peer0 = PublicKey() edge.peer0.keyType = 0 edge.peer0.data = bytes(key0.verify_key) edge.peer1 = PublicKey() edge.peer1.keyType = 0 edge.peer1.data = bytes(key1.verify_key) edge.nonce = nonce val = bytes([0]) + bytes(edge.peer0.data) + bytes([0]) + bytes( edge.peer1.data) + struct.pack('Q', nonce) hsh = hashlib.sha256(val).digest() enc58 = base58.b58encode(hsh) edge.signature0 = Signature() edge.signature0.keyType = 0 edge.signature0.data = key0.sign(hashlib.sha256(val).digest()).signature edge.signature1 = Signature() edge.signature1.keyType = 0 edge.signature1.data = key1.sign(hashlib.sha256(val).digest()).signature edge.removal_info = None return edge
def create_and_sign_routed_peer_message(routed_msg_body, target_node, my_key_pair_nacl): routed_msg = RoutedMessage() routed_msg.target = PeerIdOrHash() routed_msg.target.enum = 'PeerId' routed_msg.target.PeerId = PublicKey() routed_msg.target.PeerId.keyType = 0 routed_msg.target.PeerId.data = base58.b58decode( target_node.node_key.pk[len(ED_PREFIX):]) routed_msg.author = PublicKey() routed_msg.author.keyType = 0 routed_msg.author.data = bytes(my_key_pair_nacl.verify_key) routed_msg.ttl = 100 routed_msg.body = routed_msg_body routed_msg.signature = Signature() routed_msg.signature.keyType = 0 routed_msg_arr = bytes( bytearray([0, 0]) + routed_msg.target.PeerId.data + bytearray([0]) + routed_msg.author.data + BinarySerializer(schema).serialize(routed_msg.body)) routed_msg_hash = hashlib.sha256(routed_msg_arr).digest() routed_msg.signature.data = my_key_pair_nacl.sign( routed_msg_hash).signature peer_message = PeerMessage() peer_message.enum = 'Routed' peer_message.Routed = routed_msg return peer_message
def send_resigned_transactions(tx_path, home_dir): with open(os.path.join(home_dir, 'node0/', 'node_key.json'), 'r') as fin: key_pair_json = json.load(fin) key_pair = Key(key_pair_json['account_id'], key_pair_json['public_key'], key_pair_json['secret_key']) base_block_hash = mocknet_helpers.get_latest_block_hash(addr=LOCALHOST) my_account = Account(key_pair, init_nonce=0, base_block_hash=base_block_hash, rpc_infos=[(LOCALHOST, "3030")]) schema = dict(tx_schema + crypto_schema + bridge_schema) with open(tx_path) as fin: txs = json.load(fin, object_pairs_hook=OrderedDict) for original_signed_tx in txs: tx = convert_json_rust_instance_to_py_object( original_signed_tx['transaction'], Transaction) if hasattr(tx, 'blockHash'): tx.blockHash = base_block_hash if hasattr(tx, 'actions'): try: tx.actions = [ convert_json_action_to_py_action(action_dict) for action_dict in tx.actions ] except ValueError: continue tx.publicKey = PublicKey() tx.publicKey.keyType = 0 tx.publicKey.data = key_pair.decoded_pk() msg = BinarySerializer(schema).serialize(tx) hash_ = hashlib.sha256(msg).digest() signature = Signature() signature.keyType = 0 signature.data = key_pair.sign_bytes(hash_) resigned_tx = SignedTransaction() resigned_tx.transaction = tx resigned_tx.signature = signature resigned_tx.hash = hash_ my_account.send_tx(BinarySerializer(schema).serialize(resigned_tx))
def create_handshake(my_key_pair_nacl, their_pk_serialized, listen_port, version=0): """ Create handshake message but with placeholders in: - version - genesis_id.chain_id - genesis_id.hash - edge_info.signature """ handshake = Handshake() handshake.version = version handshake.oldest_supported_version = version handshake.peer_id = PublicKey() handshake.target_peer_id = PublicKey() handshake.listen_port = listen_port handshake.chain_info = PeerChainInfoV2() handshake.edge_info = EdgeInfo() handshake.peer_id.keyType = 0 handshake.peer_id.data = bytes(my_key_pair_nacl.verify_key) handshake.target_peer_id.keyType = 0 handshake.target_peer_id.data = base58.b58decode( their_pk_serialized[len(ED_PREFIX):]) handshake.chain_info.genesis_id = GenesisId() handshake.chain_info.height = 0 handshake.chain_info.tracked_shards = [] handshake.chain_info.archival = False handshake.chain_info.genesis_id.chain_id = 'moo' handshake.chain_info.genesis_id.hash = bytes([0] * 32) handshake.edge_info.nonce = 1 handshake.edge_info.signature = Signature() handshake.edge_info.signature.keyType = 0 handshake.edge_info.signature.data = bytes([0] * 64) peer_message = PeerMessage() peer_message.enum = 'Handshake' peer_message.Handshake = handshake return peer_message