From a043f332424dbceff34ab2683d0fb4325e60392c Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 25 Nov 2024 11:39:12 +0300 Subject: [PATCH] update handlers --- parse.py | 60 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/parse.py b/parse.py index 07f2bf8..7345d0c 100644 --- a/parse.py +++ b/parse.py @@ -1,3 +1,4 @@ +import json import ply.lex as lex import ply.yacc as yacc import enum @@ -9,11 +10,14 @@ import select from multiprocessing import Process from multiprocessing import Pipe import subprocess +from web3 import Web3 from dotenv import load_dotenv logg = logging.getLogger() logg.setLevel(logging.DEBUG) + + tokens = ( 'ID', 'NOID', @@ -61,6 +65,8 @@ chainId = os.getenv("CHAIN_ID") rpc = os.getenv("RPC") gasCap = os.getenv("GAS_FEE_CAP") +w3 = Web3(Web3.HTTPProvider(rpc)) + # @@ -166,7 +172,7 @@ def p_key_create(p): if len(p) > 4: o.k = p[4] p[0] = o - print("PO",p[0]) + def p_voucher_mint(p): @@ -312,21 +318,36 @@ class Router: def noop_handler(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): - mnemonic = cmd.i.replace('-', ' ') - command = f'cast wallet pk -v "{mnemonic}" 2 | cut -c 16-' - result = subprocess.run(command, shell=True, capture_output=True, text=True) - if result.returncode != 0: - raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr) - if result.stderr: - raise ValueError(f"Command failed with error: {result.stderr}") - else: - privatekey = result.stdout.strip().split("\n")[1] - return privatekey - - + store_name = cmd.t + key_name = str(cmd.f).split(":")[1] + private_key = cmd.k + address = generate_private_key() + store_key_in_keystore(private_key, key_name, store_name) + return address + def voucher_create_handler(cmd): name = cmd.n symbol = cmd.s @@ -352,12 +373,16 @@ def voucher_transfer_handler(cmd): s = "0x" + str(cmd.a).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 = ( f'cast send --private-key {privatekey} ' f'--rpc-url {rpc} ' + f'--nonce {nonce} ' f'{s} ' f'"transfer(address,uint256)" {to} {value}' - ) + ) result = subprocess.run(command, shell=True, capture_output=True, text=True) if result.returncode != 0: raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr) @@ -372,8 +397,9 @@ def voucher_mint_handler(cmd): value = cmd.v s = "0x" + str(cmd.a).split(":")[1] to = "0x" + str(cmd.t).split(":")[1] + command = ( - f'cast send --private-key {privatekey}' + f'cast send --private-key {privatekey} ' f'--rpc-url {rpc} ' f' {s} ' f'"mintTo(address,uint256)" {to} {value}' @@ -456,9 +482,9 @@ if __name__ == '__main__': ifc = None o = Router() o.register(CmdId.KEY_CREATE,key_create_handler) - o.register(CmdId.VOUCHER_CREATE, voucher_create_handler) + o.register(CmdId.VOUCHER_CREATE,voucher_create_handler) o.register(CmdId.VOUCHER_MINT, voucher_mint_handler) - o.register(CmdId.VOUCHER_TRANSFER, voucher_transfer_handler) + o.register(CmdId.VOUCHER_TRANSFER,voucher_transfer_handler) if len(sys.argv) > 1: ifc = FileGet(o, sys.argv[1])