Skip to content

Commit

Permalink
feat: Backend, tests, frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdt committed Aug 22, 2024
1 parent ef41c05 commit 56423d2
Show file tree
Hide file tree
Showing 54 changed files with 1,716 additions and 9,724 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/backend-tests.yml

This file was deleted.

5 changes: 3 additions & 2 deletions backend/.env-template
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ OC_HYBRID_ACCOUNT=0x734ab78da8f5ad3290b6abf784d0bea6bd480be1
ENTRY_POINTS=0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789
CHAIN_ID=28882
HC_HELPER_ADDR=0x1c64EC0A5E2C58295c3208a63209A2A719dF68D8
COINRANKING_API_KEY=
OC_PRIVKEY=
OC_PRIVKEY=
OC_OWNER=
OPENAI_APIKEY=
21 changes: 11 additions & 10 deletions backend/Dockerfile
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"]
15 changes: 0 additions & 15 deletions backend/common/environment.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions backend/common/types.ts

This file was deleted.

45 changes: 0 additions & 45 deletions backend/common/utils.ts

This file was deleted.

10 changes: 0 additions & 10 deletions backend/jest.config.js

This file was deleted.

23 changes: 23 additions & 0 deletions backend/offchain/offchain.py
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
106 changes: 106 additions & 0 deletions backend/offchain/offchain_utils.py
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
23 changes: 0 additions & 23 deletions backend/offchain/package.json

This file was deleted.

Loading

0 comments on commit 56423d2

Please sign in to comment.