From f6c0e2de04ca7a03b32d65a815e637dd81e9ce83 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 26 Nov 2024 10:05:11 +0300 Subject: [PATCH] update handlers --- parse.py | 62 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/parse.py b/parse.py index 51a729b..570b2c8 100644 --- a/parse.py +++ b/parse.py @@ -1,4 +1,5 @@ import json +import re import ply.lex as lex import ply.yacc as yacc import enum @@ -318,6 +319,10 @@ class Router: def noop_handler(cmd): return str(cmd) + +def remove_ansi_escape_codes(text): + return re.sub(r'\u001b\[.*?m', '', text) + def generate_private_key(): """Generate a new private key.""" web3 = Web3() @@ -330,7 +335,7 @@ def store_key_in_keystore(private_key, key_name, store_name): 'private_key': private_key, 'store_name': store_name, } - store_path = f"{store_name}_{key_name}.json" + store_path = f"{key_name}.json" # Save to JSON file (simulated keystore) with open(store_path, 'w') as f: @@ -338,6 +343,14 @@ def store_key_in_keystore(private_key, key_name, store_name): return store_path +def store_voucher(voucher_creator,voucher_symbol,voucher_address): + voucher = { + voucher_symbol: voucher_address, + 'owner': voucher_creator + } + with open("vouchers.json", 'w') as f: + json.dump(voucher, f) + def key_create_handler(cmd): store_name = cmd.t @@ -371,20 +384,31 @@ def voucher_create_handler(cmd): if "contract_address=" in word: contract_address = word.split("=")[1] print("Voucher created with Address:",contract_address) + store_voucher(privatekey,symbol,remove_ansi_escape_codes(contract_address)) return contract_address def voucher_transfer_handler(cmd): value = cmd.v # Amount to transfer - s = "0x" + str(cmd.a).split(":")[1] - to = "0x" + str(cmd.t).split(":")[1] + if str(cmd.a).startswith("NameAgent"): + voucher_name = str(cmd.a).split(":")[1] + with open("vouchers.json", "r") as file: + data = json.load(file) + s = data[voucher_name] + elif str(cmd.a).startswith("AddressAgent"): + s = "0x" + str(cmd.a).split(":")[1] + + if str(cmd.t).startswith("NameAgent"): + key_name = str(cmd.t).split(":")[1] + with open(f"{key_name}.json", "r") as file: + data = json.load(file) + acct = w3.eth.account.from_key(data["private_key"]) + to = acct.address + elif str(cmd.t).startswith("AddressAgent"): + 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}' ) @@ -400,8 +424,22 @@ def voucher_transfer_handler(cmd): def voucher_mint_handler(cmd): value = cmd.v - s = "0x" + str(cmd.a).split(":")[1] - to = "0x" + str(cmd.t).split(":")[1] + if str(cmd.t).startswith("NameAgent"): + key_name = str(cmd.t).split(":")[1] + with open(f"{key_name}.json", "r") as file: + data = json.load(file) + acct = w3.eth.account.from_key(data["private_key"]) + to = acct.address + elif str(cmd.t).startswith("AddressAgent"): + to = "0x" + str(cmd.t).split(":")[1] + + if str(cmd.a).startswith("NameAgent"): + voucher_name = str(cmd.a).split(":")[1] + with open("vouchers.json", "r") as file: + data = json.load(file) + s = data[voucher_name] + elif str(cmd.a).startswith("AddressAgent"): + s = "0x" + str(cmd.a).split(":")[1] command = ( f'cast send --private-key {privatekey} ' @@ -486,10 +524,10 @@ class WaitGet: if __name__ == '__main__': ifc = None o = Router() - o.register(CmdId.KEY_CREATE,key_create_handler) + o.register(CmdId.KEY_CREATE,foo_handler) o.register(CmdId.VOUCHER_CREATE,voucher_create_handler) - o.register(CmdId.VOUCHER_MINT, voucher_create_handler) - o.register(CmdId.VOUCHER_TRANSFER,voucher_transfer_handler) + o.register(CmdId.VOUCHER_MINT, foo_handler) + o.register(CmdId.VOUCHER_TRANSFER,foo_handler) if len(sys.argv) > 1: ifc = FileGet(o, sys.argv[1])