Skip to content

Commit

Permalink
Refactoring / cleanup of offchain.py
Browse files Browse the repository at this point in the history
 Changes to be committed:
	modified:   hybrid-compute/offchain.py
  • Loading branch information
mmontour1306 committed May 22, 2024
1 parent 86ba989 commit c9eeca3
Showing 1 changed file with 55 additions and 74 deletions.
129 changes: 55 additions & 74 deletions hybrid-compute/offchain.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os,sys
from web3 import Web3, exceptions
import time

import requests,json
import logging
import requests

from eth_abi import abi as ethabi
import eth_account

import requests,json
import logging
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer,SimpleJSONRPCRequestHandler

HC_CHAIN = int(os.environ['CHAIN_ID'])
Expand Down Expand Up @@ -38,72 +33,19 @@

# -------------------------------------------------------------

def packOp(op):
ret = (
op['sender'],
Web3.to_int(hexstr=op['nonce']),
op['initCode'],
Web3.to_bytes(hexstr=op['callData']),
Web3.to_int(hexstr=op['callGasLimit']),
Web3.to_int(hexstr=op['verificationGasLimit']),
Web3.to_int(hexstr=op['preVerificationGas']),
Web3.to_int(hexstr=op['maxFeePerGas']),
Web3.to_int(hexstr=op['maxPriorityFeePerGas']),
op['paymasterAndData'],
Web3.to_bytes(hexstr=op['signature'])
)
return ret

# -------------------------------------------------------------

def offchain_addsub2(sk, src_addr, src_nonce, oo_nonce, payload, *args):
print(" -> offchain_addsub2 handler called with subkey={} src_addr={} src_nonce={} oo_nonce={} payload={} extra_args={}".format(sk, src_addr, src_nonce, oo_nonce, payload, args))
success = False
err_code = 1
resp2 = Web3.to_bytes(text="unknown error")

try:
skey = Web3.to_bytes(hexstr=sk)
srcAddr = Web3.to_checksum_address(src_addr)
srcNonce = Web3.to_int(hexstr=src_nonce)
opNonce = Web3.to_int(hexstr=oo_nonce)
reqBytes = Web3.to_bytes(hexstr=payload)

dec = ethabi.decode(['uint32','uint32'], reqBytes)

if dec[0] >= dec[1]:
s = dec[0] + dec[1]
d = dec[0] - dec[1]
resp = ethabi.encode(['uint256','uint256'], [s,d])
err_code = 0
success = True
else:
print("offchain_addsub2 underflow error", dec[0], dec[1])
resp = Web3.to_bytes(text="underflow error")
resp2 = ethabi.encode(['address', 'uint256', 'uint32', 'bytes'], [srcAddr, srcNonce, err_code, resp])
except Exception as e:
print("DECODE FAILED", e)

enc1 = ethabi.encode(['bytes32','bytes'], [skey, resp2])
def gen_response(req, err_code, resp_payload):
resp2 = ethabi.encode(['address', 'uint256', 'uint32', 'bytes'], [req['srcAddr'], req['srcNonce'], err_code, resp_payload])
enc1 = ethabi.encode(['bytes32','bytes'], [req['skey'], resp2])
p_enc1 = "0xdfc98ae8" + Web3.to_hex(enc1)[2:]

enc2 = ethabi.encode(['address', 'uint256', 'bytes'], [Web3.to_checksum_address(HelperAddr), 0, Web3.to_bytes(hexstr=p_enc1)])
p_enc2 = "0xb61d27f6" + Web3.to_hex(enc2)[2:]

oo = {
'sender':HybridAcctAddr,
'nonce': Web3.to_hex(opNonce),
'initCode':'0x',
'callData': p_enc2,
limits = {
'callGasLimit': "0x30000",
'verificationGasLimit': "0x10000",
'preVerificationGas': "0x10000",
'maxFeePerGas': Web3.to_hex(Web3.to_wei(0,'gwei')),
'maxPriorityFeePerGas': Web3.to_hex(Web3.to_wei(0,'gwei')),
'paymasterAndData': '0x',
'signature': '0x'
}

p = ethabi.encode([
'address',
'uint256',
Expand All @@ -117,28 +59,67 @@ def offchain_addsub2(sk, src_addr, src_nonce, oo_nonce, payload, *args):
'bytes32',
],[
HybridAcctAddr,
opNonce,
Web3.keccak(Web3.to_bytes(hexstr='0x')),
req['opNonce'],
Web3.keccak(Web3.to_bytes(hexstr='0x')), # initCode
Web3.keccak(Web3.to_bytes(hexstr=p_enc2)),
Web3.to_int(hexstr=oo['callGasLimit']),
Web3.to_int(hexstr=oo['verificationGasLimit']),
Web3.to_int(hexstr=oo['preVerificationGas']),
Web3.to_int(hexstr=oo['maxFeePerGas']),
Web3.to_int(hexstr=oo['maxPriorityFeePerGas']),
Web3.keccak(Web3.to_bytes(hexstr='0x')),
Web3.to_int(hexstr=limits['callGasLimit']),
Web3.to_int(hexstr=limits['verificationGasLimit']),
Web3.to_int(hexstr=limits['preVerificationGas']),
0, # maxFeePerGas
0, # maxPriorityFeePerGas
Web3.keccak(Web3.to_bytes(hexstr='0x')), #paymasterANdData
])
ooHash = Web3.keccak(ethabi.encode(['bytes32','address','uint256'],[Web3.keccak(p),EntryPointAddr,HC_CHAIN]))
signAcct = eth_account.account.Account.from_key(hc1_key)
eMsg = eth_account.messages.encode_defunct(ooHash)
sig = signAcct.sign_message(eMsg)

print("Method returning success={} response={} signature={}".format(success, Web3.to_hex(resp), Web3.to_hex(sig.signature)))
success = (err_code == 0)
print("Method returning success={} response={} signature={}".format(success, Web3.to_hex(resp_payload), Web3.to_hex(sig.signature)))
return ({
"success": success,
"response": Web3.to_hex(resp),
"response": Web3.to_hex(resp_payload),
"signature": Web3.to_hex(sig.signature)
})

return response

def parse_req(sk, src_addr, src_nonce, oo_nonce, payload):
req = dict()
req['skey'] = Web3.to_bytes(hexstr=sk)
req['srcAddr'] = Web3.to_checksum_address(src_addr)
req['srcNonce'] = Web3.to_int(hexstr=src_nonce)
req['opNonce'] = Web3.to_int(hexstr=oo_nonce)
req['reqBytes'] = Web3.to_bytes(hexstr=payload)
return req

# -------------------------------------------------------------

# Demo method, given (a,b) returns (a+b , a-b) or an underflow error if b > a
def offchain_addsub2(sk, src_addr, src_nonce, oo_nonce, payload, *args):
print(" -> offchain_addsub2 handler called with subkey={} src_addr={} src_nonce={} oo_nonce={} payload={} extra_args={}".format(sk, src_addr, src_nonce, oo_nonce, payload, args))
err_code = 1
resp = Web3.to_bytes(text="unknown error")

try:
req = parse_req(sk, src_addr, src_nonce, oo_nonce, payload)
dec = ethabi.decode(['uint32','uint32'], req['reqBytes'])

if dec[0] >= dec[1]:
s = dec[0] + dec[1]
d = dec[0] - dec[1]
resp = ethabi.encode(['uint256','uint256'], [s,d])
err_code = 0
else:
print("offchain_addsub2 underflow error", dec[0], dec[1])
resp = Web3.to_bytes(text="underflow error")
except Exception as e:
print("DECODE FAILED", e)

return gen_response(req, err_code, resp)

# -------------------------------------------------------------

class RequestHandler(SimpleJSONRPCRequestHandler):
rpc_paths = ('/', '/hc')

Expand Down

0 comments on commit c9eeca3

Please sign in to comment.