update handlers
This commit is contained in:
parent
d1a4f2ee5d
commit
a043f33242
60
parse.py
60
parse.py
@ -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,21 +318,36 @@ 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):
|
||||||
name = cmd.n
|
name = cmd.n
|
||||||
symbol = cmd.s
|
symbol = cmd.s
|
||||||
@ -352,12 +373,16 @@ 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}'
|
||||||
)
|
)
|
||||||
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr)
|
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr)
|
||||||
@ -372,8 +397,9 @@ 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} '
|
||||||
f' {s} '
|
f' {s} '
|
||||||
f'"mintTo(address,uint256)" {to} {value}'
|
f'"mintTo(address,uint256)" {to} {value}'
|
||||||
@ -456,9 +482,9 @@ if __name__ == '__main__':
|
|||||||
ifc = None
|
ifc = None
|
||||||
o = Router()
|
o = Router()
|
||||||
o.register(CmdId.KEY_CREATE,key_create_handler)
|
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_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:
|
if len(sys.argv) > 1:
|
||||||
ifc = FileGet(o, sys.argv[1])
|
ifc = FileGet(o, sys.argv[1])
|
||||||
|
Loading…
Reference in New Issue
Block a user