示例#1
0
def create_stub_enr(pubkey: datatypes.PublicKey, address: AddressAPI) -> ENR:
    return ENR(
        0, {
            IDENTITY_SCHEME_ENR_KEY: V4CompatIdentityScheme.id,
            V4CompatIdentityScheme.public_key_enr_key:
            pubkey.to_compressed_bytes(),
            IP_V4_ADDRESS_ENR_KEY: address.ip_packed,
            UDP_PORT_ENR_KEY: address.udp_port,
            TCP_PORT_ENR_KEY: address.tcp_port,
        },
        signature=b'')
示例#2
0
def ecdh_agree(private_key: bytes, public_key: bytes) -> bytes:
    """Perform the ECDH key agreement.

    The public key is expected in uncompressed format and the resulting secret point will be
    formatted as a 0x02 or 0x03 prefix (depending on the sign of the secret's y component)
    followed by 32 bytes of the x component.
    """
    # We cannot use `cryptography.hazmat.primitives.asymmetric.ec.ECDH only gives us the x
    # component of the shared secret point, but we need both x and y.
    public_key_eth_keys = PublicKey(public_key)
    public_key_compressed = public_key_eth_keys.to_compressed_bytes()
    public_key_coincurve = coincurve.keys.PublicKey(public_key_compressed)
    secret_coincurve = public_key_coincurve.multiply(private_key)
    return secret_coincurve.format()
示例#3
0
 def from_pubkey_and_addr(cls: Type[TNode], pubkey: datatypes.PublicKey,
                          address: AddressAPI) -> TNode:
     enr = ENR(0, {
         IDENTITY_SCHEME_ENR_KEY:
         V4CompatIdentityScheme.id,
         V4CompatIdentityScheme.public_key_enr_key:
         pubkey.to_compressed_bytes(),
         IP_V4_ADDRESS_ENR_KEY:
         address.ip_packed,
         UDP_PORT_ENR_KEY:
         address.udp_port,
         TCP_PORT_ENR_KEY:
         address.tcp_port,
     },
               signature=b'')
     return cls(enr)