[#2] Add methods sign and verify
This commit is contained in:
parent
97bc53c6f4
commit
4433fc425a
9 changed files with 102 additions and 10 deletions
15
frostfs_api/client/signer.py
Normal file
15
frostfs_api/client/signer.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
import ecdsa
|
||||
from hashlib import sha256
|
||||
|
||||
|
||||
class Signer:
|
||||
def sign_rfc6979(self, private_key: bytes, message: bytes) -> bytes:
|
||||
if len(private_key) == 0 or private_key is None:
|
||||
raise ValueError(f"Incorrect private_key: {private_key}")
|
||||
|
||||
# SECP256k1 is the Bitcoin elliptic curve
|
||||
sk = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1, hashfunc=sha256)
|
||||
|
||||
signature = sk.sign(message, sigencode=ecdsa.util.sigencode_string)
|
||||
|
||||
return signature
|
18
frostfs_api/client/verifier.py
Normal file
18
frostfs_api/client/verifier.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
import ecdsa
|
||||
from hashlib import sha256
|
||||
|
||||
|
||||
class Verifier:
|
||||
def verify_rfc6979(self, public_key: bytes, message: bytes, signature: bytes) -> bool:
|
||||
if len(public_key) == 0 or public_key is None:
|
||||
raise ValueError(f"Incorrect public key: {public_key}")
|
||||
|
||||
if message is None or signature is None:
|
||||
return False
|
||||
|
||||
vk = ecdsa.VerifyingKey.from_string(public_key, curve=ecdsa.SECP256k1, hashfunc=sha256)
|
||||
|
||||
try:
|
||||
return vk.verify(signature, message)
|
||||
except ecdsa.BadSignatureError:
|
||||
return False
|
|
@ -1,11 +1,21 @@
|
|||
import base58
|
||||
import ecdsa
|
||||
|
||||
|
||||
class KeyExtension:
|
||||
def get_private_key_from_wif(self, wif: str):
|
||||
def get_private_key_from_wif(self, wif: str) -> bytes:
|
||||
if len(wif) == 0 or wif is None:
|
||||
raise ValueError(f"Empty WIF private key: {wif}")
|
||||
|
||||
decoded = base58.b58decode_check(wif)
|
||||
if len(decoded) != 34 or decoded[0] != 0x80 or decoded[-1] != 0x01:
|
||||
raise ValueError("Incorrect WIF private key")
|
||||
raise ValueError("Not decode WIF by base58")
|
||||
|
||||
private_key = decoded[1:-1]
|
||||
return private_key
|
||||
|
||||
def get_public_key(self, private_key: bytes):
|
||||
if len(private_key) == 0 or private_key is None:
|
||||
raise ValueError(f"Empty private key: {private_key}")
|
||||
|
||||
return ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1).get_verifying_key().to_string()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue