Skip to content

Commit

Permalink
feat: First utils version
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Nov 4, 2024
1 parent 0bf2af5 commit 6643d99
Show file tree
Hide file tree
Showing 14 changed files with 791 additions and 737 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pypi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish release to PyPi

on:
release:
types: [ created ]

jobs:
pypi-publish:
name: Publish release to PyPI
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/libertai-utils
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
- name: Install dependencies
run: poetry install
- name: Build package
run: poetry build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# libertai-utils
Libertai python Utilities

LibertAI internal types and functions
15 changes: 0 additions & 15 deletions libertai/crypto/common.py

This file was deleted.

15 changes: 0 additions & 15 deletions libertai/utils/signature.py

This file was deleted.

File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from web3 import Web3


def get_address_from_signature(message: str, signature: str) -> str:
"""Recover the Ethereum address used to sign the message"""
def is_eth_signature_valid(message: str, signature: str, address: str) -> bool:
"""Check if a message signature with an Ethereum wallet is valid"""
w3 = Web3(Web3.HTTPProvider(""))
encoded_message = encode_defunct(text=message)
return w3.eth.account.recover_message(
recovered_address = w3.eth.account.recover_message(
encoded_message,
signature=HexBytes(signature),
)
return format_eth_address(address) == format_eth_address(recovered_address)


def format_eth_address(address: str) -> str:
Expand Down
20 changes: 20 additions & 0 deletions libertai_utils/blockchains/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from libertai_utils.blockchains.ethereum import is_eth_signature_valid, format_eth_address
from libertai_utils.blockchains.solana import is_solana_signature_valid
from libertai_utils.interfaces.subscription import SubscriptionChain


def is_signature_valid(chain: SubscriptionChain, message: str, signature: str, address: str) -> bool:
valid = False

if chain == SubscriptionChain.base:
valid = is_eth_signature_valid(message, signature, address)
elif chain == SubscriptionChain.solana:
valid = is_solana_signature_valid(message, signature, address)

return valid


def format_address(address: str, chain: SubscriptionChain) -> str:
if chain == SubscriptionChain.base:
return format_eth_address(address)
return address
26 changes: 26 additions & 0 deletions libertai_utils/blockchains/solana.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import base64

from nacl.exceptions import BadSignatureError
from nacl.signing import VerifyKey
from solders.pubkey import Pubkey


def is_solana_signature_valid(message: str, signature: str, address: str) -> bool:
"""Check if a message signature with a Solana wallet is valid
Args:
message: Message that was signed
signature: base64-encoded message signature
address: base58-encoded public key
"""
public_key = Pubkey.from_string(address)
bytes_message = bytes(message, encoding="utf-8")
bytes_signature = bytes(signature, "utf-8")

try:
retrieved_message = VerifyKey(public_key.__bytes__()).verify(
smessage=bytes_message, signature=base64.b64decode(bytes_signature)
)
return bytes_message == retrieved_message
except BadSignatureError:
return False
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SubscriptionType(str, Enum):

class SubscriptionChain(str, Enum):
base = "base"
solana = "solana"


class SubscriptionAccount(BaseModel):
Expand Down
Loading

0 comments on commit 6643d99

Please sign in to comment.