本文整理汇总了Python中coincurve.PublicKey方法的典型用法代码示例。如果您正苦于以下问题:Python coincurve.PublicKey方法的具体用法?Python coincurve.PublicKey怎么用?Python coincurve.PublicKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类coincurve
的用法示例。
在下文中一共展示了coincurve.PublicKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: valid
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def valid(self):
"""Check if this object is valid or not"""
if not self.signature:
return False
assert isinstance(self.signature, bytes)
assert 68 <= len(self.signature) <= 71
assert isinstance(self.user_public_key, bytes)
assert len(self.user_public_key) == 33
assert isinstance(self.user_address, str)
assert re.match(r'^(?:0[xX])?[0-9a-fA-F]{40}$', self.user_address)
public_key = PublicKey(self.user_public_key)
verified = public_key.verify(
self.signature,
self.serialize(include_signature=False),
)
if not verified:
return False
if get_address(public_key) != self.user_address:
return False
return self.id == self.hash
示例2: is_valid_pubkey
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def is_valid_pubkey(pubkey, usehex, require_compressed=False):
""" Returns True if the serialized pubkey is a valid secp256k1
pubkey serialization or False if not; returns False for an
uncompressed encoding if require_compressed is True.
"""
# sanity check for public key
# see https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h
if require_compressed:
valid_uncompressed = False
elif len(pubkey) == 65 and pubkey[:1] in (b'\x04', b'\x06', b'\x07'):
valid_uncompressed = True
else:
valid_uncompressed = False
if not ((len(pubkey) == 33 and pubkey[:1] in (b'\x02', b'\x03')) or
valid_uncompressed):
return False
# serialization is valid, but we must ensure it corresponds
# to a valid EC point:
try:
dummy = secp256k1.PublicKey(pubkey)
except:
return False
return True
示例3: multiply
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def multiply(s, pub, usehex, rawpub=True, return_serialized=True):
'''Input binary compressed pubkey P(33 bytes)
and scalar s(32 bytes), return s*P.
The return value is a binary compressed public key,
or a PublicKey object if return_serialized is False.
Note that the called function does the type checking
of the scalar s.
('raw' options passed in)
'''
newpub = secp256k1.PublicKey(pub)
#see note to "tweak_mul" function in podle.py
if sys.version_info >= (3,0):
res = newpub.multiply(native_bytes(s))
else:
res = newpub.multiply(bytes_to_native_str(s))
if not return_serialized:
return res
return res.format()
示例4: ecdsa_raw_verify
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def ecdsa_raw_verify(msg, pub, sig, usehex, rawmsg=False):
'''Take the binary message msg and binary signature sig,
and verify it against the pubkey pub.
If rawmsg is True, no sha256 hash is applied to msg before verifying.
In this case, msg must be a precalculated hash (256 bit).
If rawmsg is False, the secp256k1 lib will hash the message as part
of the ECDSA-SHA256 verification algo.
Return value: True if the signature is valid for this pubkey, False
otherwise.
Since the arguments may come from external messages their content is
not guaranteed, so return False on any parsing exception.
'''
try:
if rawmsg:
assert len(msg) == 32
newpub = secp256k1.PublicKey(pub)
if rawmsg:
retval = newpub.verify(sig, msg, hasher=None)
else:
retval = newpub.verify(sig, msg)
except:
return False
return retval
示例5: __init__
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def __init__(self, ledger, pubkey, chain_code, n, depth, parent=None):
super().__init__(ledger, chain_code, n, depth, parent)
if isinstance(pubkey, PublicKey):
self.verifying_key = pubkey
else:
self.verifying_key = self._verifying_key_from_pubkey(pubkey)
示例6: _verifying_key_from_pubkey
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def _verifying_key_from_pubkey(cls, pubkey):
""" Converts a 33-byte compressed pubkey into an PublicKey object. """
if not isinstance(pubkey, (bytes, bytearray)):
raise TypeError('pubkey must be raw bytes')
if len(pubkey) != 33:
raise ValueError('pubkey must be 33 bytes')
if pubkey[0] not in (2, 3):
raise ValueError('invalid pubkey prefix byte')
return PublicKey(pubkey)
示例7: get_address
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def get_address(public_key: PublicKey) -> str:
"""Derive an Ethereum-style address from the given public key."""
return '0x' + sha3_256(public_key.format(False)[1:]).hexdigest()[-40:]
示例8: __init__
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def __init__(self, private_key: PrivateKey, session=db.session):
if not isinstance(private_key, PrivateKey):
raise TypeError(
f'private_key must be an instance of {PrivateKey.__module__}.'
f'{PrivateKey.__qualname__}, not {private_key!r}'
)
self.private_key = private_key
self.public_key: PublicKey = private_key.public_key
self.session = session
示例9: test_get_address
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def test_get_address():
raw_key = bytes.fromhex(
'04fb0af727d1839557ea5214a7b7dd799c05dab9da63329a6c6d9836fd19a29ce'
'bc34f7ba31877b22f6767bb1d9f376a33fc0f28f37ada368611b011c01dbef90f'
)
pubkey = PublicKey(raw_key)
assert '0x80e0b0a7cc8001086a37648f993b2bd855d0ab59' == get_address(pubkey)
示例10: public_key_to_address
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def public_key_to_address(public_key: PublicKey) -> Address:
""" Converts a public key to an Ethereum address. """
key_bytes = public_key.format(compressed=False)
return Address(keccak(key_bytes[1:])[-20:])
示例11: podle_PublicKey
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def podle_PublicKey(P):
"""Returns a PublicKey object from a binary string
"""
return secp256k1.PublicKey(P)
示例12: add_pubkeys
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def add_pubkeys(pubkeys, usehex):
'''Input a list of binary compressed pubkeys
and return their sum as a binary compressed pubkey.'''
pubkey_list = [secp256k1.PublicKey(x) for x in pubkeys]
r = secp256k1.PublicKey.combine_keys(pubkey_list)
return r.format()
示例13: encrypt
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def encrypt(receiver_pk: Union[str, bytes], msg: bytes) -> bytes:
"""
Encrypt with receiver's secp256k1 public key
Parameters
----------
receiver_pk: Union[str, bytes]
Receiver's public key (hex str or bytes)
msg: bytes
Data to encrypt
Returns
-------
bytes
Encrypted data
"""
ephemeral_key = generate_key()
if isinstance(receiver_pk, str):
receiver_pubkey = hex2pub(receiver_pk)
elif isinstance(receiver_pk, bytes):
receiver_pubkey = PublicKey(receiver_pk)
else:
raise TypeError("Invalid public key type")
aes_key = encapsulate(ephemeral_key, receiver_pubkey)
cipher_text = aes_encrypt(aes_key, msg)
return ephemeral_key.public_key.format(False) + cipher_text
示例14: decrypt
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def decrypt(receiver_sk: Union[str, bytes], msg: bytes) -> bytes:
"""
Decrypt with receiver's secp256k1 private key
Parameters
----------
receiver_sk: Union[str, bytes]
Receiver's private key (hex str or bytes)
msg: bytes
Data to decrypt
Returns
-------
bytes
Plain text
"""
if isinstance(receiver_sk, str):
private_key = hex2prv(receiver_sk)
elif isinstance(receiver_sk, bytes):
private_key = PrivateKey(receiver_sk)
else:
raise TypeError("Invalid secret key type")
pubkey = msg[0:65] # uncompressed pubkey's length is 65 bytes
encrypted = msg[65:]
ephemeral_public_key = PublicKey(pubkey)
aes_key = decapsulate(ephemeral_public_key, private_key)
return aes_decrypt(aes_key, encrypted)
示例15: hex2pub
# 需要导入模块: import coincurve [as 别名]
# 或者: from coincurve import PublicKey [as 别名]
def hex2pub(pub_hex: str) -> PublicKey:
"""
Convert ethereum hex to EllipticCurvePublicKey
The hex should be 65 bytes, but ethereum public key only has 64 bytes
So have to add \x04
Parameters
----------
pub_hex: str
Public key hex string
Returns
-------
coincurve.PublicKey
A secp256k1 public key
>>> data = b'0'*32
>>> data_hash = sha256(data)
>>> eth_prv = generate_eth_key()
>>> cc_prv = hex2prv(eth_prv.to_hex())
>>> eth_prv.sign_msg_hash(data_hash).to_bytes() == cc_prv.sign_recoverable(data)
True
>>> pk_hex = eth_prv.public_key.to_hex()
>>> computed_pub = hex2pub(pk_hex)
>>> computed_pub == cc_prv.public_key
True
"""
uncompressed = decode_hex(pub_hex)
if len(uncompressed) == 64: # eth public key format
uncompressed = b"\x04" + uncompressed
return PublicKey(uncompressed)