Compare commits

..

3 Commits

Author SHA1 Message Date
a3d149a659
add handlers 2024-11-19 19:03:58 +03:00
lash
f25d45af4c Merge branch 'master' into lash/mux-input 2024-11-07 13:16:55 +00:00
lash
fa64acac1d
WIP mux prompt and socket input 2024-10-28 00:45:07 +00:00

176
parse.py
View File

@ -4,9 +4,11 @@ import enum
import uuid
import os
import logging
import sys
import select
from multiprocessing import Process
from multiprocessing import Value
from multiprocessing import Pipe
import subprocess
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
@ -152,6 +154,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):
@ -271,6 +274,7 @@ class Router:
def exec(self, cmd):
logg.debug("router exec {}".format(cmd))
if cmd.c & 0xa0 > 0:
return self.sync(cmd.i)
fn = self.__routes[cmd.c]
@ -280,15 +284,92 @@ class Router:
po.start()
def __del__(self):
def finish(self):
print("syncing")
for k, v in self.__r.items():
print("syncing key " + k)
r = self.sync(k)
logging.debug("synced " + k + ": " + r)
logg.debug("synced " + k + ": " + r)
print("synced " + k + ": " + r)
def __del__(self):
self.finish()
def noop_handler(cmd):
return str(cmd)
#NOTE:
#For testing the handlers,am using my hard-coded private key for the cast commands,replace with your actual private key.
#I have added random characters to the existing.
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
def voucher_create_handler(cmd):
name = cmd.n
symbol = cmd.s
command = f'ge-publish --private-key 1e1d0c1519479f68d9c8d07352a8e7e7cb9e2c676bce422f84502412cf39S4Ca ' \
f'--rpc https://alfajores-forno.celo-testnet.org --gas-fee-cap 35000000000 --chainid 44787 ' \
f'p erc20 --name "{name}" --symbol "{symbol}"'
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output = result.stderr.strip().split("\n")
contract_address = None
for word in output[1].split():
if "contract_address=" in word:
contract_address = word.split("=")[1]
return contract_address
def voucher_transfer_handler(cmd):
value = cmd.v
command = (
f'cast send --private-key 1e1d0c1519479f68d9c8d07352a8e7e7cb9e2c676bce422f84502412cfsarw54ba '
f'--rpc-url https://alfajores-forno.celo-testnet.org/ '
f'0x93bb5f14464A9b7E5D5487DAB12d100417f23323 '
f'"transfer(address,uint256)" 0x7c9eCcC1de442911954e36e6092DFb10373090a6 {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)
if result.stderr:
raise ValueError(f"Command failed with error: {result.stderr}")
return result.stdout
def voucher_mint_handler(cmd):
value = cmd.v
command = (
f'cast send --private-key 1e1d0c1519479f68d9c8d07352a8e7e7cb9e2c676bce422f84502412cf39ASCa '
f'--rpc-url https://alfajores-forno.celo-testnet.org/ '
f'0x3feCC87C2c102984865B996de340bb2C6AdCF01E '
f'"mintTo(address,uint256)" 0xEef7Ad2cCCB317E6898F43eA2B5b1BD1E9C13b1A {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)
if result.stderr:
raise ValueError(f"Command failed with error: {result.stderr}")
return result.stdout
def foo_handler(cmd):
return os.popen('eth-info -p https://celo.grassecon.net').read()
@ -296,21 +377,76 @@ def foo_handler(cmd):
parser = yacc.yacc()
cmds = [
"KEY_CREATE foo-bar Foo BAr",
"KEY_CREATE bar-baz Foo BAr 0xabcdef0",
"VOUCHER_MINT - 0xdeadbeef 13 0xfdaf",
"VOUCHER_MINT baz-baz 0xdeadbeef 13 0xfdaf",
"VOUCHER_MINT bar-bar 0xdeadbeef 42",
"VOUCHER_CREATE foo-foo Foo Bar 42",
"VOUCHER_CREATE inky-pinky-blinky Foo Bar 42 44 233",
"VOUCHER_TRANSFER clyde-sue 0xbeeffeed0123 666 foo 0x0fdc",
]
for v in cmds:
o = Router()
o.register(CmdId.KEY_CREATE, noop_handler)
o.register(CmdId.VOUCHER_MINT, noop_handler)
o.register(CmdId.VOUCHER_CREATE, noop_handler)
o.register(CmdId.VOUCHER_TRANSFER, foo_handler)
running = True
class FileGet:
def __init__(self, o, fp):
self.__running = True
self.__o = o
self.__f = open(fp, 'r')
def run(self):
while self.__running:
v = ifc.get()
if v == None:
break
v = v.strip('\n')
if len(v) == 0:
break
r = parser.parse(v)
o.exec(r)
self.__o.exec(r)
def get(self):
return self.__f.readline()
class WaitGet:
def __init__(self, o, *r):
self.__running = True
self.__o = o
self.__f = r
# TODO: router copy results in missing keys to sync when closing down
def __process(self, f):
while self.__running:
r = select.select([f], [], [])
v = r[0][0].recv()
if v == None:
break
v = v.strip('\n')
if len(v) == 0:
break
r = parser.parse(v)
self.__o.exec(r)
def run(self):
(fo, fi,) = Pipe()
p = Process(target=self.__process, args=(fo,))
p.start()
while self.__running:
v = input("> ")
if v == "":
fi.send(None)
break
fi.send(v)
p.join()
logg.debug('waitget run end')
if __name__ == '__main__':
ifc = None
o = Router()
o.register(CmdId.KEY_CREATE,key_create_handler)
o.register(CmdId.VOUCHER_MINT, voucher_mint_handler)
o.register(CmdId.VOUCHER_CREATE, voucher_create_handler)
o.register(CmdId.VOUCHER_TRANSFER, voucher_transfer_handler)
if len(sys.argv) > 1:
ifc = FileGet(o, sys.argv[1])
else:
ifc = WaitGet(o, sys.stdin)
ifc.run()
o.finish()