本文整理汇总了Python中eth_keys.keys.PrivateKey方法的典型用法代码示例。如果您正苦于以下问题:Python keys.PrivateKey方法的具体用法?Python keys.PrivateKey怎么用?Python keys.PrivateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eth_keys.keys
的用法示例。
在下文中一共展示了keys.PrivateKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unsigned_to_eip155_signed_transaction
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def test_unsigned_to_eip155_signed_transaction(txn_fixture, transaction_class):
if txn_fixture['chainId'] is None:
pytest.skip('No chain id for EIP155 signing')
elif not hasattr(transaction_class, 'chain_id'):
pytest.skip('Transaction class is not chain aware')
key = keys.PrivateKey(decode_hex(txn_fixture['key']))
unsigned_txn = transaction_class.create_unsigned_transaction(
nonce=txn_fixture['nonce'],
gas_price=txn_fixture['gasPrice'],
gas=txn_fixture['gas'],
to=(
to_canonical_address(txn_fixture['to'])
if txn_fixture['to']
else b''
),
value=txn_fixture['value'],
data=decode_hex(txn_fixture['data']),
)
signed_txn = unsigned_txn.as_signed_transaction(key, chain_id=txn_fixture['chainId'])
assert is_same_address(signed_txn.sender, key.public_key.to_canonical_address())
assert signed_txn.chain_id == txn_fixture['chainId']
示例2: _manually_driven_discovery
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def _manually_driven_discovery(seed, socket, nursery):
_, port = socket.getsockname()
discovery = ManuallyDrivenDiscoveryService(
keys.PrivateKey(keccak(seed)),
port,
port,
bootstrap_nodes=[],
event_bus=None,
socket=socket,
node_db=NodeDB(default_identity_scheme_registry, MemoryDB()))
async with background_trio_service(discovery):
# Wait until we're fully initialized (i.e. until the ENR stub created in the constructor
# is replaced with the real one).
while discovery.this_node.enr.sequence_number == 0:
await trio.hazmat.checkpoint()
yield discovery
示例3: ETHV63PeerPairFactory
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def ETHV63PeerPairFactory(*,
alice_peer_context: ChainContext = None,
alice_remote: kademlia.Node = None,
alice_private_key: keys.PrivateKey = None,
alice_client_version: str = 'alice',
bob_peer_context: ChainContext = None,
bob_remote: kademlia.Node = None,
bob_private_key: keys.PrivateKey = None,
bob_client_version: str = 'bob',
event_bus: EndpointAPI = None,
peer_factory_class: Type[ETHPeerFactory] = ETHV63PeerFactory,
) -> AsyncContextManager[Tuple[ETHPeer, ETHPeer]]:
return LatestETHPeerPairFactory(
alice_peer_context=alice_peer_context,
alice_remote=alice_remote,
alice_private_key=alice_private_key,
alice_client_version=alice_client_version,
bob_peer_context=bob_peer_context,
bob_remote=bob_remote,
bob_private_key=bob_private_key,
bob_client_version=bob_client_version,
event_bus=event_bus,
peer_factory_class=peer_factory_class
)
示例4: ETHV64PeerPairFactory
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def ETHV64PeerPairFactory(*,
alice_peer_context: ChainContext = None,
alice_remote: kademlia.Node = None,
alice_private_key: keys.PrivateKey = None,
alice_client_version: str = 'alice',
bob_peer_context: ChainContext = None,
bob_remote: kademlia.Node = None,
bob_private_key: keys.PrivateKey = None,
bob_client_version: str = 'bob',
event_bus: EndpointAPI = None,
peer_factory_class: Type[ETHPeerFactory] = ETHV64PeerFactory,
) -> AsyncContextManager[Tuple[ETHPeer, ETHPeer]]:
return LatestETHPeerPairFactory(
alice_peer_context=alice_peer_context,
alice_remote=alice_remote,
alice_private_key=alice_private_key,
alice_client_version=alice_client_version,
bob_peer_context=bob_peer_context,
bob_remote=bob_remote,
bob_private_key=bob_private_key,
bob_client_version=bob_client_version,
event_bus=event_bus,
peer_factory_class=peer_factory_class
)
示例5: ETHV65PeerPairFactory
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def ETHV65PeerPairFactory(*,
alice_peer_context: ChainContext = None,
alice_remote: kademlia.Node = None,
alice_private_key: keys.PrivateKey = None,
alice_client_version: str = 'alice',
bob_peer_context: ChainContext = None,
bob_remote: kademlia.Node = None,
bob_private_key: keys.PrivateKey = None,
bob_client_version: str = 'bob',
event_bus: EndpointAPI = None,
peer_factory_class: Type[ETHPeerFactory] = ETHV65PeerFactory,
) -> AsyncContextManager[Tuple[ETHPeer, ETHPeer]]:
return LatestETHPeerPairFactory(
alice_peer_context=alice_peer_context,
alice_remote=alice_remote,
alice_private_key=alice_private_key,
alice_client_version=alice_client_version,
bob_peer_context=bob_peer_context,
bob_remote=bob_remote,
bob_private_key=bob_private_key,
bob_client_version=bob_client_version,
event_bus=event_bus,
peer_factory_class=peer_factory_class
)
示例6: MemoryTransportPairFactory
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def MemoryTransportPairFactory(alice_remote: NodeAPI = None,
alice_private_key: keys.PrivateKey = None,
bob_remote: NodeAPI = None,
bob_private_key: keys.PrivateKey = None,
) -> Tuple[TransportAPI, TransportAPI]:
if alice_remote is None:
alice_remote = NodeFactory()
if alice_private_key is None:
alice_private_key = PrivateKeyFactory()
if bob_remote is None:
bob_remote = NodeFactory()
if bob_private_key is None:
bob_private_key = PrivateKeyFactory()
# the remotes are intentionally switched since they represent the *other*
# side of the connection.
alice_transport, bob_transport = MemoryTransport.connected_pair(
alice=(bob_remote, alice_private_key),
bob=(alice_remote, bob_private_key),
)
return alice_transport, bob_transport
示例7: hex2prv
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def hex2prv(prv_hex: str) -> PrivateKey:
"""
Convert ethereum hex to EllipticCurvePrivateKey
Parameters
----------
prv_hex: str
Private key hex string
Returns
-------
coincurve.PrivateKey
A secp256k1 private key
>>> k = generate_eth_key()
>>> sk_hex = k.to_hex()
>>> pk_hex = k.public_key.to_hex()
>>> computed_prv = hex2prv(sk_hex)
>>> computed_prv.to_int() == int(k.to_hex(), 16)
True
"""
return PrivateKey(decode_hex(prv_hex))
示例8: sign
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def sign(self, key):
'''Sign this transaction with a private key.
A potentially already existing signature would be overridden.
'''
h = blake2b(digest_size=32)
h.update(rlp.encode(self, ThorTransaction.exclude(["Signature"])))
rawhash = h.digest()
if key in (0, "", b"\x00" * 32, "0" * 64):
raise Exception("Zero privkey cannot sign")
if len(key) == 64:
key = to_bytes(hexstr=key) # we need a binary key
pk = keys.PrivateKey(key)
self.Signature = pk.sign_msg_hash(rawhash).to_bytes()
#
# estimate eth gas
#
示例9: generate_privkey
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def generate_privkey() -> datatypes.PrivateKey:
"""Generate a new SECP256K1 private key and return it"""
privkey = ec.generate_private_key(CURVE, default_backend())
return keys.PrivateKey(
pad32(int_to_big_endian(privkey.private_numbers().private_value))
)
示例10: ecdh_agree
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def ecdh_agree(privkey: datatypes.PrivateKey, pubkey: datatypes.PublicKey) -> bytes:
"""Performs a key exchange operation using the ECDH algorithm."""
privkey_as_int = int(cast(int, privkey))
ec_privkey = ec.derive_private_key(privkey_as_int, CURVE, default_backend())
pubkey_bytes = b"\x04" + pubkey.to_bytes()
pubkey_nums = ec.EllipticCurvePublicNumbers.from_encoded_point(CURVE, pubkey_bytes)
ec_pubkey = pubkey_nums.public_key(default_backend())
return ec_privkey.exchange(ec.ECDH(), ec_pubkey)
示例11: decrypt
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def decrypt(
data: bytes, privkey: datatypes.PrivateKey, shared_mac_data: bytes = b""
) -> bytes:
"""Decrypt data with ECIES method using the given private key
1) generate shared-secret = kdf( ecdhAgree(myPrivKey, msg[1:65]) )
2) verify tag
3) decrypt
ecdhAgree(r, recipientPublic) == ecdhAgree(recipientPrivate, R)
[where R = r*G, and recipientPublic = recipientPrivate*G]
"""
if data[:1] != b"\x04":
raise DecryptionError("wrong ecies header")
# 1) generate shared-secret = kdf( ecdhAgree(myPrivKey, msg[1:65]) )
shared = data[1 : 1 + PUBKEY_LEN]
key_material = ecdh_agree(privkey, keys.PublicKey(shared))
key = kdf(key_material)
key_enc, key_mac = key[: KEY_LEN // 2], key[KEY_LEN // 2 :]
key_mac = sha256(key_mac).digest()
tag = data[-KEY_LEN:]
# 2) Verify tag
expected_tag = hmac_sha256(
key_mac, data[1 + PUBKEY_LEN : -KEY_LEN] + shared_mac_data
)
if not bytes_eq(expected_tag, tag):
raise DecryptionError("Failed to verify tag")
# 3) Decrypt
algo = CIPHER(key_enc)
blocksize = algo.block_size // 8
iv = data[1 + PUBKEY_LEN : 1 + PUBKEY_LEN + blocksize]
ciphertext = data[1 + PUBKEY_LEN + blocksize : -KEY_LEN]
ctx = Cipher(algo, MODE(iv), default_backend()).decryptor()
return ctx.update(ciphertext) + ctx.finalize()
示例12: get_discovery_protocol
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def get_discovery_protocol(seed=b"seed", address=None):
privkey = keys.PrivateKey(keccak(seed))
if address is None:
address = random_address()
return discovery.DiscoveryProtocol(
privkey, address, [], 1, CancelToken("discovery-test")
)
示例13: random_node
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def random_node():
seed = to_bytes(text="".join(random.sample(string.ascii_lowercase, 10)))
priv_key = keys.PrivateKey(keccak(seed))
return kademlia.Node(priv_key.public_key, random_address())
示例14: __init__
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def __init__(self, bootnodes):
privkey = keys.PrivateKey(keccak(b"seed"))
super().__init__(
privkey, random_address(), bootnodes, 1, CancelToken("discovery-test")
)
示例15: test_decrypt_known_good_handshake
# 需要导入模块: from eth_keys import keys [as 别名]
# 或者: from eth_keys.keys import PrivateKey [as 别名]
def test_decrypt_known_good_handshake():
# Data taken from https://gist.github.com/fjl/3a78780d17c755d22df2
privkey = keys.PrivateKey(
decode_hex("c45f950382d542169ea207959ee0220ec1491755abe405cd7498d6b16adb6df8")
)
auth_ciphertext = decode_hex(
"04a0274c5951e32132e7f088c9bdfdc76c9d91f0dc6078e848f8e3361193dbdc43b94351ea3d89e4ff33ddcefbc80070498824857f499656c4f79bbd97b6c51a514251d69fd1785ef8764bd1d262a883f780964cce6a14ff206daf1206aa073a2d35ce2697ebf3514225bef186631b2fd2316a4b7bcdefec8d75a1025ba2c5404a34e7795e1dd4bc01c6113ece07b0df13b69d3ba654a36e35e69ff9d482d88d2f0228e7d96fe11dccbb465a1831c7d4ad3a026924b182fc2bdfe016a6944312021da5cc459713b13b86a686cf34d6fe6615020e4acf26bf0d5b7579ba813e7723eb95b3cef9942f01a58bd61baee7c9bdd438956b426a4ffe238e61746a8c93d5e10680617c82e48d706ac4953f5e1c4c4f7d013c87d34a06626f498f34576dc017fdd3d581e83cfd26cf125b6d2bda1f1d56"
) # noqa: E501
auth_plaintext = decode_hex(
"884c36f7ae6b406637c1f61b2f57e1d2cab813d24c6559aaf843c3f48962f32f46662c066d39669b7b2e3ba14781477417600e7728399278b1b5d801a519aa570034fdb5419558137e0d44cd13d319afe5629eeccb47fd9dfe55cc6089426e46cc762dd8a0636e07a54b31169eba0c7a20a1ac1ef68596f1f283b5c676bae4064abfcce24799d09f67e392632d3ffdc12e3d6430dcb0ea19c318343ffa7aae74d4cd26fecb93657d1cd9e9eaf4f8be720b56dd1d39f190c4e1c6b7ec66f077bb1100"
) # noqa: E501
decrypted = ecies.decrypt(auth_ciphertext, privkey)
assert auth_plaintext == decrypted