9 Commits

Author SHA1 Message Date
Carlosokumu
f6c0e2de04 update handlers 2024-11-26 10:05:11 +03:00
Carlosokumu
541d083fc7 update commands 2024-11-26 10:04:26 +03:00
Carlosokumu
2bfc1ce3b2 update commands 2024-11-25 12:38:18 +03:00
Carlosokumu
5061aace41 update handler 2024-11-25 12:37:52 +03:00
Carlosokumu
a043f33242 update handlers 2024-11-25 11:39:12 +03:00
Carlosokumu
d1a4f2ee5d add example env file 2024-11-23 13:26:01 +03:00
Carlosokumu
083e3e6b69 add commands 2024-11-23 13:23:42 +03:00
Carlosokumu
baf21fca96 update handlers 2024-11-23 13:23:16 +03:00
Carlosokumu
a3d149a659 add handlers 2024-11-19 19:03:58 +03:00
3 changed files with 171 additions and 5 deletions

4
.env.example Normal file
View File

@@ -0,0 +1,4 @@
PRIVATE_KEY=PRIVATE_KEY=1e1d0c151ajjajajsanakaka54ba
CHAIN_ID = 44787
RPC = https://alfajores-forno.celo-testnet.org
GAS_FEE_CAP = 35000000000

167
parse.py
View File

@@ -1,3 +1,5 @@
import json
import re
import ply.lex as lex
import ply.yacc as yacc
import enum
@@ -7,12 +9,16 @@ import logging
import sys
import select
from multiprocessing import Process
from multiprocessing import Value
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',
@@ -51,6 +57,19 @@ def t_error(t):
t.lexer.skip(1)
lexer = lex.lex()
load_dotenv()
#Chain params
privatekey = os.getenv("PRIVATE_KEY")
chainId = os.getenv("CHAIN_ID")
rpc = os.getenv("RPC")
gasCap = os.getenv("GAS_FEE_CAP")
w3 = Web3(Web3.HTTPProvider(rpc))
#
#data = '''
#FOOBAR uf-2etg 0xa3bdefa momo 123
@@ -154,6 +173,7 @@ def p_key_create(p):
if len(p) > 4:
o.k = p[4]
p[0] = o
def p_voucher_mint(p):
@@ -300,6 +320,143 @@ 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()
account = web3.eth.account.create()
return account.address,w3.to_hex(account.key)
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"{key_name}.json"
# Save to JSON file (simulated keystore)
with open(store_path, 'w') as f:
json.dump(keystore, f)
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
key_name = str(cmd.f).split(":")[1]
if cmd.k is None:
address,private_key = generate_private_key()
else:
if private_key.startswith("0x"):
private_key = private_key[2:]
address = w3.eth.account.from_key(privatekey)
store_key_in_keystore(private_key, key_name, store_name)
return address
def voucher_create_handler(cmd):
name = cmd.n
symbol = cmd.s
command = f'ge-publish --private-key {privatekey} ' \
f'--rpc {rpc} --gas-fee-cap {gasCap} --chainid {chainId} ' \
f'p erc20 --name "{name}" --symbol "{symbol}"'
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)
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]
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
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]
command = (
f'cast send --private-key {privatekey} '
f'--rpc-url {rpc} '
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)
if result.stderr:
raise ValueError(f"Command failed with error: {result.stderr}")
return result.stdout
def voucher_mint_handler(cmd):
value = cmd.v
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} '
f'--rpc-url {rpc} '
f' {s} '
f'"mintTo(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)
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()
@@ -367,10 +524,10 @@ class WaitGet:
if __name__ == '__main__':
ifc = None
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)
o.register(CmdId.KEY_CREATE,foo_handler)
o.register(CmdId.VOUCHER_CREATE,voucher_create_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])

5
seed_commands.txt Normal file
View File

@@ -0,0 +1,5 @@
VOUCHER_CREATE - KSF KenyanSafaru 6
KEY_CREATE - lockkey custodialstore
KEY_CREATE - testkey custodialstwo
VOUCHER_MINT - KSF 50000000 lockkey
VOUCHER_TRANSFER - 0x0d3D8A97f970fbdf7486274b02A8308d8aCcE6a1 5000000 lockkey 0x74096a72495fe95710c675e78bd4a10f2bfe08bc