-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fa88db
commit faf526b
Showing
8 changed files
with
169 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from ._relay_pyo3 import ( | ||
_generate_key_pair, | ||
generate_relay_id, | ||
_create_register_challenge, | ||
_validate_register_response, | ||
is_version_supported, | ||
RustPublicKey, | ||
RustSecretKey, | ||
UnpackErrorBadSignature, | ||
) | ||
|
||
import json | ||
import uuid | ||
|
||
class SecretKey: | ||
def __init__(self, inner): | ||
self.inner = inner | ||
|
||
@classmethod | ||
def parse(cls, string): | ||
return SecretKey(RustSecretKey.parse(string)) | ||
|
||
def sign(self, value): | ||
return self.inner.sign(value) | ||
|
||
def pack(self, data): | ||
# TODO(@anonrig): Look into separators requirement | ||
packed = json.dumps(data, separators=(",", ":")).encode() | ||
return packed, self.sign(packed) | ||
|
||
def __str__(self): | ||
return self.inner.__str__() | ||
|
||
def __repr__(self): | ||
return self.inner.__repr__() | ||
|
||
|
||
class PublicKey: | ||
def __init__(self, inner): | ||
self.inner = inner | ||
|
||
@classmethod | ||
def parse(cls, string): | ||
return SecretKey(RustPublicKey.parse(string)) | ||
|
||
def verify(self, buf, sig, max_age=None): | ||
return self.inner.verify(buf, sig, max_age) | ||
|
||
def unpack( | ||
self, | ||
buf, | ||
sig, | ||
max_age=None, | ||
): | ||
if not self.verify(buf, sig, max_age): | ||
raise UnpackErrorBadSignature() | ||
return json.loads(buf) | ||
|
||
def __str__(self): | ||
return self.inner.__str__() | ||
|
||
def __repr__(self): | ||
return self.inner.__repr__() | ||
|
||
|
||
|
||
|
||
def generate_key_pair(): | ||
rsk, rpk = _generate_key_pair() | ||
return (SecretKey(rsk), PublicKey(rpk)) | ||
|
||
def create_register_challenge( | ||
data, | ||
signature, | ||
secret, | ||
max_age=60, | ||
): | ||
|
||
challenge = _create_register_challenge( | ||
data, | ||
signature, | ||
secret, | ||
max_age, | ||
) | ||
|
||
return { | ||
"relay_id": uuid.UUID(challenge["relay_id"]), | ||
"token": challenge["token"], | ||
} | ||
|
||
def validate_register_response( | ||
data, | ||
signature, | ||
secret, | ||
max_age=60, | ||
): | ||
response = _validate_register_response( | ||
data, | ||
signature, | ||
secret, | ||
max_age, | ||
) | ||
|
||
return { | ||
"relay_id": uuid.UUID(response["relay_id"]), | ||
"token": response["token"], | ||
"public_key": response["public_key"], | ||
"version": response["version"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from ._relay_pyo3 import ( | ||
RelayError, | ||
Unknown, | ||
InvalidJsonError, | ||
KeyParseErrorBadEncoding, | ||
KeyParseErrorBadKey, | ||
UnpackErrorBadSignature, | ||
UnpackErrorBadPayload, | ||
UnpackErrorSignatureExpired, | ||
UnpackErrorBadEncoding, | ||
ProcessingErrorInvalidTransaction, | ||
ProcessingErrorInvalidGeoIp, | ||
InvalidReleaseErrorTooLong, | ||
InvalidReleaseErrorRestrictedName, | ||
InvalidReleaseErrorBadCharacters, | ||
RelayErrorCode | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters