Skip to content

Commit

Permalink
Merge pull request #215 from UnitapApp/refactor/mv-web3-usage-tokenta…
Browse files Browse the repository at this point in the history
…p-helpers-to-web3utils

mv web3 usage tokentap to core
  • Loading branch information
PooyaFekri authored Dec 21, 2023
2 parents 3c400d9 + 322e055 commit e8c0761
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
23 changes: 23 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from contextlib import contextmanager

import pytz
from eth_account.messages import encode_defunct
from web3 import Account, Web3
from django.core.cache import cache
from web3 import Web3
from web3.contract.contract import Contract, ContractFunction
Expand Down Expand Up @@ -175,6 +177,27 @@ def from_wei(self, value: int, unit: str = "ether"):
def to_checksum_address(address: str):
return Web3.to_checksum_address(address.lower())

@staticmethod
def hash_message(user, token, amount, nonce):
message_hash = Web3().solidity_keccak(
["address", "address", "uint256", "uint32"],
[
Web3.to_checksum_address(user),
Web3.to_checksum_address(token),
amount,
nonce,
],
)
hashed_message = encode_defunct(hexstr=message_hash.hex())

return hashed_message

@staticmethod
def sign_hashed_message(private_key, hashed_message):
account = Account.from_key(private_key)
signed_message = account.sign_message(hashed_message)
return signed_message.signature.hex()

def get_transaction_receipt(self, tx_hash):
return self.w3.eth.get_transaction_receipt(tx_hash)

Expand Down
22 changes: 3 additions & 19 deletions tokenTap/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import random

from eth_account.messages import encode_defunct
from web3 import Account, Web3

from core.models import NetworkTypes, WalletAccount
from core.utils import Web3Utils
from faucet.faucet_manager.credit_strategy import RoundCreditStrategy
from faucet.models import GlobalSettings

Expand All @@ -21,27 +19,13 @@ def create_uint32_random_nonce():


def hash_message(user, token, amount, nonce):
message_hash = Web3().solidity_keccak(
["address", "address", "uint256", "uint32"],
[
Web3.to_checksum_address(user),
Web3.to_checksum_address(token),
amount,
nonce,
],
)
hashed_message = encode_defunct(hexstr=message_hash.hex())

hashed_message = Web3Utils.hash_message(user, token, amount, nonce)
return hashed_message


def sign_hashed_message(hashed_message):
private_key = WalletAccount.objects.get(network_type=NetworkTypes.EVM).private_key
account = Account.from_key(private_key)

signed_message = account.sign_message(hashed_message)

return signed_message.signature.hex()
return Web3Utils.sign_hashed_message(private_key, hashed_message)


def has_weekly_credit_left(user_profile):
Expand Down

0 comments on commit e8c0761

Please sign in to comment.