|
1 | | -import json |
2 | | -from pathlib import Path |
3 | | -from typing import Dict, Optional, Union |
| 1 | +import warnings |
4 | 2 |
|
5 | | -import base58 |
6 | | -from nacl.exceptions import BadSignatureError as NaclBadSignatureError |
7 | | -from nacl.public import PrivateKey, SealedBox |
8 | | -from nacl.signing import SigningKey, VerifyKey |
| 3 | +from aleph.sdk.chains.solana import * # noqa |
9 | 4 |
|
10 | | -from ..exceptions import BadSignatureError |
11 | | -from .common import BaseAccount, get_fallback_private_key, get_verification_buffer |
12 | | - |
13 | | - |
14 | | -def encode(item): |
15 | | - return base58.b58encode(bytes(item)).decode("ascii") |
16 | | - |
17 | | - |
18 | | -class SOLAccount(BaseAccount): |
19 | | - CHAIN = "SOL" |
20 | | - CURVE = "curve25519" |
21 | | - _signing_key: SigningKey |
22 | | - _private_key: PrivateKey |
23 | | - |
24 | | - def __init__(self, private_key: bytes): |
25 | | - self.private_key = private_key |
26 | | - self._signing_key = SigningKey(self.private_key) |
27 | | - self._private_key = self._signing_key.to_curve25519_private_key() |
28 | | - |
29 | | - async def sign_message(self, message: Dict) -> Dict: |
30 | | - """Sign a message inplace.""" |
31 | | - message = self._setup_sender(message) |
32 | | - verif = get_verification_buffer(message) |
33 | | - signature = await self.sign_raw(verif) |
34 | | - sig = { |
35 | | - "publicKey": self.get_address(), |
36 | | - "signature": encode(signature), |
37 | | - } |
38 | | - message["signature"] = json.dumps(sig) |
39 | | - return message |
40 | | - |
41 | | - async def sign_raw(self, buffer: bytes) -> bytes: |
42 | | - """Sign a raw buffer.""" |
43 | | - sig = self._signing_key.sign(buffer) |
44 | | - return sig.signature |
45 | | - |
46 | | - def get_address(self) -> str: |
47 | | - return encode(self._signing_key.verify_key) |
48 | | - |
49 | | - def get_public_key(self) -> str: |
50 | | - return bytes(self._signing_key.verify_key.to_curve25519_public_key()).hex() |
51 | | - |
52 | | - async def encrypt(self, content) -> bytes: |
53 | | - value: bytes = bytes(SealedBox(self._private_key.public_key).encrypt(content)) |
54 | | - return value |
55 | | - |
56 | | - async def decrypt(self, content) -> bytes: |
57 | | - value: bytes = SealedBox(self._private_key).decrypt(content) |
58 | | - return value |
59 | | - |
60 | | - |
61 | | -def get_fallback_account(path: Optional[Path] = None) -> SOLAccount: |
62 | | - return SOLAccount(private_key=get_fallback_private_key(path=path)) |
63 | | - |
64 | | - |
65 | | -def generate_key() -> bytes: |
66 | | - privkey = bytes(SigningKey.generate()) |
67 | | - return privkey |
68 | | - |
69 | | - |
70 | | -def verify_signature( |
71 | | - signature: Union[bytes, str], |
72 | | - public_key: Union[bytes, str], |
73 | | - message: Union[bytes, str], |
74 | | -): |
75 | | - """ |
76 | | - Verifies a signature. |
77 | | - Args: |
78 | | - signature: The signature to verify. Can be a base58 encoded string or bytes. |
79 | | - public_key: The public key to use for verification. Can be a base58 encoded string or bytes. |
80 | | - message: The message to verify. Can be an utf-8 string or bytes. |
81 | | - Raises: |
82 | | - BadSignatureError: If the signature is invalid. |
83 | | - """ |
84 | | - if isinstance(signature, str): |
85 | | - signature = base58.b58decode(signature) |
86 | | - if isinstance(message, str): |
87 | | - message = message.encode("utf-8") |
88 | | - if isinstance(public_key, str): |
89 | | - public_key = base58.b58decode(public_key) |
90 | | - try: |
91 | | - VerifyKey(public_key).verify(message, signature) |
92 | | - except NaclBadSignatureError as e: |
93 | | - raise BadSignatureError from e |
| 5 | +warnings.warn( |
| 6 | + "aleph.sdk.chains.sol is deprecated, use aleph.sdk.chains.solana instead", |
| 7 | + DeprecationWarning, |
| 8 | + stacklevel=1, |
| 9 | +) |
0 commit comments