本文整理汇总了Python中secp256k1.PrivateKey方法的典型用法代码示例。如果您正苦于以下问题:Python secp256k1.PrivateKey方法的具体用法?Python secp256k1.PrivateKey怎么用?Python secp256k1.PrivateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类secp256k1
的用法示例。
在下文中一共展示了secp256k1.PrivateKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def __init__(self, private_key=None):
self.__private_key = private_key or PrivateKey()
self.__address = self.create_address(self.__private_key.pubkey)
self.__last_tx_hash = ""
tx_hash_versions = conf.CHANNEL_OPTION[conf.LOOPCHAIN_DEFAULT_CHANNEL]["hash_versions"]
self.__hash_generators = {
"0x2": build_hash_generator(tx_hash_versions["0x2"], "icx_sendTransaction"),
"0x3": build_hash_generator(tx_hash_versions["0x3"], "icx_sendTransaction")
}
self.to_address = None
self.value = None
self.message = None
self.fee = ICX_FEE
self.nid = '0x3'
self.is_logging = True
示例2: test_ecdsa
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdsa():
data = open(os.path.join(DATA, 'ecdsa_sig.json')).read()
vec = json.loads(data)['vectors']
inst = secp256k1.PrivateKey()
for item in vec:
seckey = bytes(bytearray.fromhex(item['privkey']))
msg32 = bytes(bytearray.fromhex(item['msg']))
sig = bytes(bytearray.fromhex(item['sig'])[:-1])
inst.set_raw_privkey(seckey)
sig_raw = inst.ecdsa_sign(msg32, raw=True)
sig_check = inst.ecdsa_serialize(sig_raw)
assert sig_check == sig
assert inst.ecdsa_serialize(inst.ecdsa_deserialize(sig_check)) == sig_check
assert inst.pubkey.ecdsa_verify(msg32, sig_raw, raw=True)
示例3: test_ecdsa_normalize
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdsa_normalize():
key = secp256k1.PrivateKey()
raw_sig = key.ecdsa_sign(b'hi')
had_to_normalize, normsig = key.ecdsa_signature_normalize(raw_sig)
assert had_to_normalize == False
assert key.ecdsa_serialize(normsig) == key.ecdsa_serialize(raw_sig)
assert key.ecdsa_serialize_compact(normsig) == \
key.ecdsa_serialize_compact(raw_sig)
had_to_normalize, normsig = key.ecdsa_signature_normalize(
raw_sig, check_only=True)
assert had_to_normalize == False
assert normsig == None
sig = b'\xAA' + (b'\xFF' * 31) + b'\xAA' + (b'\xFF' * 31)
raw_sig = key.ecdsa_deserialize_compact(sig)
normalized, normsig = key.ecdsa_signature_normalize(raw_sig)
assert normalized == True
assert key.ecdsa_serialize(normsig) != key.ecdsa_serialize(raw_sig)
normalized, normsig = key.ecdsa_signature_normalize(raw_sig, True)
assert normalized == True
assert normsig == None
示例4: test_privkey
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_privkey():
with pytest.raises(TypeError):
key = 'abc'
secp256k1.PrivateKey(key)
with pytest.raises(TypeError):
key = bytearray.fromhex('a' * 32) # This will result in 16 bytes.
secp256k1.PrivateKey(bytes(key))
with pytest.raises(Exception):
secp256k1.PrivateKey(bytes(bytearray.fromhex('0' * 64)))
with pytest.raises(Exception):
secp256k1.PrivateKey(bytes(bytearray.fromhex('F' * 64)))
with pytest.raises(Exception):
# This is a good raw key, but here it's being passed as serialized.
secp256k1.PrivateKey(b'1' * 32, raw=False)
# "good" key, should be fine.
assert secp256k1.PrivateKey(b'1' * 32)
示例5: test_ecdsa
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdsa():
rawkey = (b'\xc9\xa9)Z\xf8Er\x97\x8b\xa23\x1f\xf7\xb6\x82qQ\xdc9\xc1'
b'\x1d\xac6\xfd\xeb\x11\x05\xb1\xdf\x86\xb3\xe6')
priv = secp256k1.PrivateKey(rawkey)
with pytest.raises(Exception):
# Bad digest function (doesn't produce 256 bits).
priv.ecdsa_sign(b'hi', digest=hashlib.sha1)
raw_sig = priv.ecdsa_sign(b'hi')
assert priv.pubkey.ecdsa_verify(b'hi', raw_sig)
with pytest.raises(AssertionError):
sig = priv.ecdsa_serialize(raw_sig)[:-1]
priv.ecdsa_deserialize(sig)
sig = priv.ecdsa_serialize(raw_sig)
sig = sig[:-1] + bytes(sig[0:1]) # Assuming sig[0] != sig[-1].
invalid_sig = priv.ecdsa_deserialize(sig)
assert not priv.pubkey.ecdsa_verify(b'hi', invalid_sig)
示例6: test_ecdsa_recoverable
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdsa_recoverable():
if not secp256k1.HAS_RECOVERABLE:
pytest.skip('secp256k1_recovery not enabled, skipping')
return
key = '32a8935ffdb984a498b0f7ac8943e0d2ac084e81c809595fd19fde41522f1837'
priv = secp256k1.PrivateKey(bytes(bytearray.fromhex(key)))
sig = priv.ecdsa_sign_recoverable(b'hi')
sig_ser, rec_id = priv.ecdsa_recoverable_serialize(sig)
assert rec_id == 1
with pytest.raises(Exception):
# Invalid rec_id (must be between 0 and 3)
priv.ecdsa_recoverable_deserialize(sig_ser, -1)
# Deserialize using a rec_id that does not match.
sig = priv.ecdsa_recoverable_deserialize(sig_ser, 2)
with pytest.raises(Exception):
# Now try to recover the public key.
priv.ecdsa_recover(b'hi', sig)
# Invalid size.
with pytest.raises(Exception):
priv.ecdsa_recoverable_deserialize(b'hello', 0)
示例7: test_schnorr
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_schnorr():
if not secp256k1.HAS_SCHNORR:
pytest.skip('secp256k1_schnorr not enabled, skipping')
return
inst = secp256k1.PrivateKey()
raw_sig = inst.schnorr_sign(b'hello')
test1 = secp256k1.PublicKey(inst.pubkey.public_key,
flags=secp256k1.NO_FLAGS)
with pytest.raises(Exception):
test1.schnorr_verify(b'hello', raw_sig)
blank = secp256k1.PublicKey(flags=secp256k1.NO_FLAGS)
with pytest.raises(Exception):
blank.schnorr_recover(b'hello', raw_sig)
blank = secp256k1.PublicKey(flags=secp256k1.FLAG_SIGN)
with pytest.raises(Exception):
blank.schnorr_recover(b'hello', raw_sig)
示例8: test_schnorr_partial
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_schnorr_partial():
if not secp256k1.HAS_SCHNORR:
pytest.skip('secp256k1_schnorr not enabled, skipping')
return
signer1 = secp256k1.PrivateKey()
pubnonce1, privnonce1 = signer1.schnorr_generate_nonce_pair(b'hello')
signer2 = secp256k1.PrivateKey()
pubnonce2, privnonce2 = signer2.schnorr_generate_nonce_pair(b'hello')
partial1 = signer1.schnorr_partial_sign(b'hello', privnonce1, pubnonce2)
blank = secp256k1.PublicKey(flags=secp256k1.NO_FLAGS)
with pytest.raises(TypeError):
blank.schnorr_partial_combine([partial1, secp256k1.ffi.NULL])
with pytest.raises(Exception):
blank.schnorr_partial_combine([partial1, b''])
示例9: test_ecdh
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdh():
if not secp256k1.HAS_ECDH:
pytest.skip('secp256k1_ecdh not enabled, skipping')
return
pubkey = secp256k1.PrivateKey().pubkey
p = secp256k1.PublicKey(pubkey.public_key)
with pytest.raises(Exception):
# Bad scalar length.
p.ecdh(b'')
with pytest.raises(Exception):
# Bad scalar type.
p.ecdh([])
res = p.ecdh(b'0' * 32)
assert type(res) == bytes
示例10: test_ecdsa_with_custom_nonce
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_ecdsa_with_custom_nonce():
data = open(os.path.join(DATA, 'ecdsa_custom_nonce_sig.json')).read()
vec = json.loads(data)['vectors']
inst = secp256k1.PrivateKey()
for item in vec:
seckey = bytes(bytearray.fromhex(item['privkey']))
msg32 = bytes(bytearray.fromhex(item['msg']))
sig = bytes(bytearray.fromhex(item['sig']))
randnonce = bytes(bytearray.fromhex(item['nonce']))
inst.set_raw_privkey(seckey)
nf = ffi.addressof(_noncefunc.lib, "nonce_function_rand")
ndata = ffi.new("char [32]", randnonce)
sig_raw = inst.ecdsa_sign(msg32, raw=True, custom_nonce=(nf, ndata))
sig_check = inst.ecdsa_serialize(sig_raw)
assert sig_check == sig
assert inst.ecdsa_serialize(inst.ecdsa_deserialize(sig_check)) == sig_check
assert inst.pubkey.ecdsa_verify(msg32, sig_raw, raw=True)
示例11: test_schnorr_simple
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_schnorr_simple():
if not secp256k1.HAS_SCHNORR:
pytest.skip('secp256k1_schnorr not enabled, skipping')
return
inst = secp256k1.PrivateKey()
raw_sig = inst.schnorr_sign(b'hello')
assert inst.pubkey.schnorr_verify(b'hello', raw_sig)
key2 = secp256k1.PrivateKey()
assert not key2.pubkey.schnorr_verify(b'hello', raw_sig)
blank = secp256k1.PublicKey()
pubkey = blank.schnorr_recover(b'hello', raw_sig)
pub = secp256k1.PublicKey(pubkey)
assert pub.serialize() == inst.pubkey.serialize()
示例12: test_privkey
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_privkey():
with pytest.raises(AssertionError):
secp256k1.PrivateKey(flags=secp256k1.FLAG_VERIFY)
with pytest.raises(AssertionError):
secp256k1.PrivateKey(flags=0)
privkey = secp256k1.PrivateKey(flags=secp256k1.FLAG_SIGN)
sig = privkey.ecdsa_sign(b'hi')
with pytest.raises(Exception):
# FLAG_SIGN was not specified.
privkey.pubkey.ecdsa_verify(b'hi', sig)
assert privkey.flags == privkey.pubkey.flags
privkey = secp256k1.PrivateKey()
sig = privkey.ecdsa_sign(b'hi')
assert privkey.pubkey.ecdsa_verify(b'hi', sig)
示例13: test_pubkey_from_privkey
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_pubkey_from_privkey():
data = open(os.path.join(DATA, 'pubkey.json')).read()
vec = json.loads(data)['vectors']
inst = secp256k1.PrivateKey()
for item in vec:
seckey = bytes(bytearray.fromhex(item['seckey']))
pubkey_uncp = bytes(bytearray.fromhex(item['pubkey']))
pubkey_comp = bytes(bytearray.fromhex(item['compressed']))
inst.set_raw_privkey(seckey)
assert inst.pubkey.serialize(compressed=False) == pubkey_uncp
assert inst.pubkey.serialize(compressed=True) == pubkey_comp
assert inst.deserialize(inst.serialize()) == seckey
示例14: test_pubkey_combine
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_pubkey_combine():
k1 = secp256k1.PrivateKey()
k2 = secp256k1.PrivateKey()
pub1 = k1.pubkey.public_key
pub2 = k2.pubkey.public_key
new = secp256k1.PublicKey()
assert new.public_key is None
res = new.combine([pub1, pub2])
assert new.public_key == res
new = secp256k1.PublicKey()
assert new.public_key is None
res = new.combine([pub1])
assert new.public_key == res
assert new.serialize() == k1.pubkey.serialize()
示例15: test_pubkey_tweak
# 需要导入模块: import secp256k1 [as 别名]
# 或者: from secp256k1 import PrivateKey [as 别名]
def test_pubkey_tweak():
inst = secp256k1.PrivateKey()
pub = inst.pubkey
scalar = [b'\x01' * 32]
with pytest.raises(TypeError):
pub.tweak_add(scalar)
with pytest.raises(TypeError):
pub.tweak_mul(scalar)
scalar = b'\x01' * 31
with pytest.raises(TypeError):
pub.tweak_add(scalar)
with pytest.raises(TypeError):
pub.tweak_mul(scalar)
scalar = scalar + b'\x01'
res = pub.tweak_add(scalar)
assert isinstance(res, secp256k1.PublicKey)
assert res.serialize() != pub.serialize()