本文整理匯總了Python中coincurve.PrivateKey方法的典型用法代碼示例。如果您正苦於以下問題:Python coincurve.PrivateKey方法的具體用法?Python coincurve.PrivateKey怎麽用?Python coincurve.PrivateKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類coincurve
的用法示例。
在下文中一共展示了coincurve.PrivateKey方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: login_required
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def login_required(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
private_key_hex = session.get('private_key')
error = None
if private_key_hex is not None:
if private_key_hex.startswith(('0x', '0X')):
private_key_hex = private_key_hex[2:]
try:
private_key_bytes = bytes.fromhex(private_key_hex)
private_key = PrivateKey(private_key_bytes)
except (ValueError, TypeError):
error = 'invalid-private-key'
else:
g.user = User(private_key)
return f(*args, **kwargs)
return redirect(url_for('.get_login', next=request.url, error=error))
return decorated_function
示例2: mine
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def mine(private_key: PrivateKey, sleep: float):
app.app_context().push()
while True:
Block.sync()
block = Block.create(
User(private_key),
[m
for m in Move.query.filter_by(block=None).limit(20).all()
if m.valid],
echo=echo,
sleep=sleep,
)
if block:
serialized = block.serialize(
use_bencode=False,
include_suffix=True,
include_moves=True,
include_hash=True
)
multicast(serialized=serialized, broadcast=broadcast_block)
echo(block)
示例3: test_prevent_hack_and_slash_when_dead
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def test_prevent_hack_and_slash_when_dead(
fx_test_client: FlaskClient, fx_session: Session, fx_user: User,
fx_private_key: PrivateKey, fx_novice_status: typing.Dict[str, str],
):
move = fx_user.create_novice(fx_novice_status)
Block.create(fx_user, [move])
assert fx_user.avatar().dead is False
while fx_user.avatar().hp > 0:
move = fx_user.hack_and_slash()
Block.create(fx_user, [move])
assert fx_user.avatar().dead is True
response = fx_test_client.post('/session_moves', data={
'name': 'hack_and_slash'
})
assert response.status_code == 302
示例4: sign
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def sign(privkey: PrivateKey, msg_hash: bytes, v: int = 0) -> bytes:
if not isinstance(msg_hash, bytes):
raise TypeError("sign(): msg_hash is not an instance of bytes")
if len(msg_hash) != 32:
raise ValueError("sign(): msg_hash has to be exactly 32 bytes")
if not isinstance(privkey, bytes):
raise TypeError("sign(): privkey is not an instance of bytes")
if v not in {0, 27}:
raise ValueError(f"sign(): got v = {v} expected 0 or 27.")
pk = PrivateKey(privkey)
sig: bytes = pk.sign_recoverable(msg_hash, hasher=None)
assert len(sig) == 65
pub = pk.public_key
recovered = PublicKey.from_signature_and_message(sig, msg_hash, hasher=None)
assert pub == recovered
sig = sig[:-1] + bytes([sig[-1] + v])
return sig
示例5: get_web3
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def get_web3(eth_tester: EthereumTester, deployer_key: PrivateKey) -> Web3:
"""Returns an initialized Web3 instance"""
provider = EthereumTesterProvider(eth_tester)
web3 = Web3(provider)
# add faucet account to tester
eth_tester.add_account(deployer_key.to_hex())
# make faucet rich
eth_tester.send_transaction(
{
"from": eth_tester.get_accounts()[0],
"to": private_key_to_address(deployer_key.to_hex()),
"gas": 21000,
"value": FAUCET_ALLOWANCE,
}
)
return web3
示例6: add_privkeys
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def add_privkeys(priv1, priv2, usehex):
'''Add privkey 1 to privkey 2.
Input keys must be in binary either compressed or not.
Returned key will have the same compression state.
Error if compression state of both input keys is not the same.'''
y, z = [read_privkey(x) for x in [priv1, priv2]]
if y[0] != z[0]:
raise Exception("cannot add privkeys, mixed compression formats")
else:
compressed = y[0]
newpriv1, newpriv2 = (y[1], z[1])
p1 = secp256k1.PrivateKey(newpriv1)
res = p1.add(newpriv2).secret
if compressed:
res += b'\x01'
return res
示例7: hex2prv
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve 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: test_hdkf
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def test_hdkf(self):
derived = HKDF(b"secret", 32, b"", SHA256).hex()
self.assertEqual(
derived, "2f34e5ff91ec85d53ca9b543683174d0cf550b60d5f52b24c97b386cfcf6cbbf"
)
k1 = PrivateKey(secret=bytes([2]))
self.assertEqual(k1.to_int(), 2)
k2 = PrivateKey(secret=bytes([3]))
self.assertEqual(k2.to_int(), 3)
self.assertEqual(encapsulate(k1, k2.public_key), decapsulate(k1.public_key, k2))
self.assertEqual(
encapsulate(k1, k2.public_key).hex(),
"6f982d63e8590c9d9b5b4c1959ff80315d772edd8f60287c9361d548d5200f82",
)
示例9: ecsign
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def ecsign(rawhash, key):
if coincurve and hasattr(coincurve, "PrivateKey"):
pk = coincurve.PrivateKey(key)
signature = pk.sign_recoverable(rawhash, hasher=None)
v = safe_ord(signature[64]) + 27
r = big_endian_to_int(signature[0:32])
s = big_endian_to_int(signature[32:64])
else:
v, r, s = ecdsa_raw_sign(rawhash, key)
return v, r, s
示例10: ecsign
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def ecsign(rawhash, key):
if coincurve and hasattr(coincurve, 'PrivateKey'):
pk = coincurve.PrivateKey(key)
signature = pk.sign_recoverable(rawhash, hasher=None)
v = safe_ord(signature[64]) + 27
r = big_endian_to_int(signature[0:32])
s = big_endian_to_int(signature[32:64])
else:
v, r, s = ecdsa_raw_sign(rawhash, key)
return v, r, s
# Functions - sha3
示例11: convert
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def convert(self, value, param, ctx) -> PrivateKey:
val = value[2:] if value.startswith(('0x', '0X')) else value
try:
num = bytes.fromhex(val)
return PrivateKey(num)
except (ValueError, TypeError):
self.fail('%s is not a valid private key of 64 hexadecimal digits')
示例12: fx_user2
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def fx_user2(fx_session):
user = User(PrivateKey())
user.session = fx_session
return user
示例13: fx_private_key
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def fx_private_key() -> PrivateKey:
return PrivateKey()
示例14: fx_other_user
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def fx_other_user(fx_other_session):
user = User(PrivateKey())
user.session = fx_other_session
return user
示例15: test_new_character_creation
# 需要導入模塊: import coincurve [as 別名]
# 或者: from coincurve import PrivateKey [as 別名]
def test_new_character_creation(fx_test_client, fx_session):
privkey = PrivateKey()
fx_test_client.post('/login', data={
'private_key': privkey.to_hex(),
'name': 'test_user',
}, follow_redirects=True)
assert fx_session.query(Move).filter_by(
user_address=get_address(privkey.public_key),
user_public_key=privkey.public_key.format(compressed=True),
name='create_novice'
).first()