generated from bobanetwork/aa-hc-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
1,716 additions
and
9,724 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
# Stage 1: Build the application | ||
FROM node:18 AS builder | ||
# Use an official Python runtime as a parent image | ||
FROM python:3.9-slim | ||
|
||
# Set the working directory in the container | ||
WORKDIR /app | ||
|
||
# Copy package.json and package-lock.json (if available) | ||
COPY ./package*.json ./ | ||
# Copy the requirements file into the container | ||
COPY requirements.txt . | ||
|
||
# Install dependencies | ||
RUN npm i | ||
# Install any needed packages specified in requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copy the rest of the application code | ||
COPY *.ts . | ||
COPY *.py . | ||
|
||
# Expose the port the app runs on | ||
# Make port 1234 available to the world outside this container | ||
EXPOSE ${OC_LISTEN_PORT:-1234} | ||
|
||
# Build the TypeScript application | ||
CMD ["npm", "start"] | ||
# Run the application when the container launches | ||
CMD ["python", "offchain.py"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from web3 import Web3 | ||
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, SimpleJSONRPCRequestHandler | ||
from text2call import offchain_text2multi | ||
|
||
def selector(name): | ||
nameHash = Web3.to_hex(Web3.keccak(text=name)) | ||
return nameHash[2:10] | ||
|
||
class RequestHandler(SimpleJSONRPCRequestHandler): | ||
rpc_paths = ('/', '/hc') | ||
|
||
def offchain_hello(ver, sk, src_addr, src_nonce, oo_nonce, payload, *args): | ||
return { "success":True, "response":payload, "signature":"0x" } | ||
|
||
def server_loop(): | ||
server = SimpleJSONRPCServer(('0.0.0.0', 1234), requestHandler=RequestHandler) | ||
server.register_function(offchain_hello, "hello") | ||
server.register_function(offchain_text2multi, selector("text2multi(string)")) | ||
print("Serving ") | ||
print("PORT => {}".format(1234)) | ||
server.serve_forever() | ||
|
||
server_loop() # Run until killed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os | ||
from web3 import Web3 | ||
from eth_abi import abi as ethabi | ||
import eth_account | ||
|
||
HC_CHAIN = int(os.environ['CHAIN_ID']) | ||
assert (HC_CHAIN != 0) | ||
|
||
EP_ADDR = os.environ['ENTRY_POINTS'] | ||
if (len(EP_ADDR) == 0): | ||
EP_ADDR = os.environ['ENTRY_POINT'] # Fallback | ||
assert (len(EP_ADDR) == 42) | ||
EntryPointAddr = Web3.to_checksum_address(EP_ADDR) | ||
|
||
HH_ADDR = os.environ['HC_HELPER_ADDR'] | ||
assert (len(HH_ADDR) == 42) | ||
HelperAddr = Web3.to_checksum_address(HH_ADDR) | ||
|
||
HA_ADDR = os.environ['OC_HYBRID_ACCOUNT'] | ||
assert (len(HA_ADDR) == 42) | ||
HybridAcctAddr = Web3.to_checksum_address(HA_ADDR) | ||
|
||
HA_OWNER = os.environ['OC_OWNER'] | ||
assert (len(HA_OWNER) == 42) | ||
hc1_addr = Web3.to_checksum_address(HA_OWNER) | ||
|
||
hc1_key = os.environ['OC_PRIVKEY'] | ||
assert (len(hc1_key) == 66) | ||
|
||
API_KEY = os.environ['OPENAI_APIKEY'] | ||
assert (len(API_KEY) > 1) | ||
|
||
|
||
def selector(name): | ||
nameHash = Web3.to_hex(Web3.keccak(text=name)) | ||
return nameHash[2:10] | ||
|
||
|
||
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 = "0x" + selector("PutResponse(bytes32,bytes)") + \ | ||
Web3.to_hex(enc1)[2:] # dfc98ae8 | ||
|
||
enc2 = ethabi.encode(['address', 'uint256', 'bytes'], [ | ||
Web3.to_checksum_address(HelperAddr), 0, Web3.to_bytes(hexstr=p_enc1)]) | ||
p_enc2 = "0x" + selector("execute(address,uint256,bytes)") + \ | ||
Web3.to_hex(enc2)[2:] # b61d27f6 | ||
|
||
limits = { | ||
'verificationGasLimit': "0x10000", | ||
'preVerificationGas': "0x10000", | ||
} | ||
callGas = 705*len(resp_payload) + 170000 | ||
|
||
print("callGas calculation", len(resp_payload), 4+len(enc2), callGas) | ||
p = ethabi.encode([ | ||
'address', | ||
'uint256', | ||
'bytes32', | ||
'bytes32', | ||
'uint256', | ||
'uint256', | ||
'uint256', | ||
'uint256', | ||
'uint256', | ||
'bytes32', | ||
], [ | ||
HybridAcctAddr, | ||
req['opNonce'], | ||
Web3.keccak(Web3.to_bytes(hexstr='0x')), # initCode | ||
Web3.keccak(Web3.to_bytes(hexstr=p_enc2)), | ||
callGas, | ||
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) | ||
|
||
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_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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.