signature test
This commit is contained in:
50
test.py
Executable file
50
test.py
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
from eth_account import Account
|
||||
from eth_account.messages import encode_defunct
|
||||
from sha3 import keccak_256
|
||||
from eth_abi.packed import encode_packed
|
||||
from eth_utils import to_bytes
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def signer_check(message, signature):
|
||||
|
||||
message_hash = encode_defunct(message)
|
||||
signer_address = Account.recover_message(message_hash, signature=signature)
|
||||
|
||||
return signer_address
|
||||
|
||||
signature = 1
|
||||
|
||||
def sign_message(message, private_key):
|
||||
account = Account.from_key(private_key)
|
||||
message_hash = encode_defunct(message)
|
||||
signature = account.sign_message(message_hash)
|
||||
return signature.signature.hex()
|
||||
|
||||
def generate_hash(pix_target, amount, pix_timestamp):
|
||||
# Concatenate the bytes
|
||||
concatenated_bytes = encode_packed(
|
||||
['bytes32', 'uint80', 'uint256'],
|
||||
[to_bytes(text=pix_target), amount, pix_timestamp])
|
||||
|
||||
# Return the keccak256 hash
|
||||
return keccak_256(concatenated_bytes).digest()
|
||||
|
||||
# Example usage
|
||||
pix_target = '1234'
|
||||
amount = 10
|
||||
pix_timestamp = 162
|
||||
|
||||
message = generate_hash(pix_target, amount, pix_timestamp)
|
||||
print("Message:", message.hex())
|
||||
signature = sign_message(message, getenv('PRIVATE_KEY'))
|
||||
print("Signature:", signature)
|
||||
signer = signer_check(message, signature)
|
||||
print("Signer: ", signer)
|
||||
account = Account.from_key(getenv('PRIVATE_KEY'))
|
||||
print("Account:", account.address)
|
||||
Reference in New Issue
Block a user