update handlers

This commit is contained in:
Carlosokumu 2024-11-25 11:39:12 +03:00
parent d1a4f2ee5d
commit a043f33242
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -1,3 +1,4 @@
import json
import ply.lex as lex import ply.lex as lex
import ply.yacc as yacc import ply.yacc as yacc
import enum import enum
@ -9,11 +10,14 @@ import select
from multiprocessing import Process from multiprocessing import Process
from multiprocessing import Pipe from multiprocessing import Pipe
import subprocess import subprocess
from web3 import Web3
from dotenv import load_dotenv from dotenv import load_dotenv
logg = logging.getLogger() logg = logging.getLogger()
logg.setLevel(logging.DEBUG) logg.setLevel(logging.DEBUG)
tokens = ( tokens = (
'ID', 'ID',
'NOID', 'NOID',
@ -61,6 +65,8 @@ chainId = os.getenv("CHAIN_ID")
rpc = os.getenv("RPC") rpc = os.getenv("RPC")
gasCap = os.getenv("GAS_FEE_CAP") gasCap = os.getenv("GAS_FEE_CAP")
w3 = Web3(Web3.HTTPProvider(rpc))
# #
@ -166,7 +172,7 @@ def p_key_create(p):
if len(p) > 4: if len(p) > 4:
o.k = p[4] o.k = p[4]
p[0] = o p[0] = o
print("PO",p[0])
def p_voucher_mint(p): def p_voucher_mint(p):
@ -312,19 +318,34 @@ class Router:
def noop_handler(cmd): def noop_handler(cmd):
return str(cmd) return str(cmd)
def generate_private_key():
"""Generate a new private key."""
web3 = Web3()
account = web3.eth.account.create()
return account.address
def store_key_in_keystore(private_key, key_name, store_name):
keystore = {
'key_name': key_name,
'private_key': private_key,
'store_name': store_name,
}
store_path = f"{store_name}_{key_name}.json"
# Save to JSON file (simulated keystore)
with open(store_path, 'w') as f:
json.dump(keystore, f)
return store_path
def key_create_handler(cmd): def key_create_handler(cmd):
mnemonic = cmd.i.replace('-', ' ') store_name = cmd.t
command = f'cast wallet pk -v "{mnemonic}" 2 | cut -c 16-' key_name = str(cmd.f).split(":")[1]
result = subprocess.run(command, shell=True, capture_output=True, text=True) private_key = cmd.k
if result.returncode != 0: address = generate_private_key()
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr) store_key_in_keystore(private_key, key_name, store_name)
if result.stderr: return address
raise ValueError(f"Command failed with error: {result.stderr}")
else:
privatekey = result.stdout.strip().split("\n")[1]
return privatekey
def voucher_create_handler(cmd): def voucher_create_handler(cmd):
@ -352,9 +373,13 @@ def voucher_transfer_handler(cmd):
s = "0x" + str(cmd.a).split(":")[1] s = "0x" + str(cmd.a).split(":")[1]
to = "0x" + str(cmd.t).split(":")[1] to = "0x" + str(cmd.t).split(":")[1]
account = w3.eth.account.from_key(privatekey)
nonce = w3.eth.get_transaction_count(account.address,'pending')
command = ( command = (
f'cast send --private-key {privatekey} ' f'cast send --private-key {privatekey} '
f'--rpc-url {rpc} ' f'--rpc-url {rpc} '
f'--nonce {nonce} '
f'{s} ' f'{s} '
f'"transfer(address,uint256)" {to} {value}' f'"transfer(address,uint256)" {to} {value}'
) )
@ -372,6 +397,7 @@ def voucher_mint_handler(cmd):
value = cmd.v value = cmd.v
s = "0x" + str(cmd.a).split(":")[1] s = "0x" + str(cmd.a).split(":")[1]
to = "0x" + str(cmd.t).split(":")[1] to = "0x" + str(cmd.t).split(":")[1]
command = ( command = (
f'cast send --private-key {privatekey} ' f'cast send --private-key {privatekey} '
f'--rpc-url {rpc} ' f'--rpc-url {rpc} '