Skip to content

Commit

Permalink
Adds attestation upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
KasparPeterson committed Mar 28, 2024
1 parent 9e2c6ab commit bba61d0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
10 changes: 6 additions & 4 deletions enclave/key_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from typing import Dict

from eth_account import Account
from eth_account.signers.local import LocalAccount
from web3 import Web3
from dotenv import load_dotenv

KEY_PATH = "private_key.txt"
ROOT_DIR = "/app"

w3 = Web3()

Expand All @@ -15,7 +17,7 @@ def main():
print(f'account={account.address}')


def get_account():
def get_account() -> LocalAccount:
key = _get_key()
if key:
account = Account.from_key(key)
Expand All @@ -35,20 +37,20 @@ def _get_key() -> str:


def _save_key(account: Account):
with open("/app/.env", "a") as file:
with open(os.path.join(ROOT_DIR, ".env"), "a") as file:
file.write('\nPRIVATE_KEY="' + w3.to_hex(account.key) + '"')


def save_dot_env(dot_env: Dict):
print("\nsave_dot_env:", dot_env)
with open("/app/.env", "w") as file:
with open(os.path.join(ROOT_DIR, ".env"), "w") as file:
for key, value in dot_env.items():
file.write(key + '="' + value + '"\n')


def save_gcp(gcp_creds_json):
print("\nsave_gcp:", gcp_creds_json)
with open("/app/sidekik.json", "w") as file:
with open(os.path.join(ROOT_DIR, "sidekik.json"), "w") as file:
file.write(gcp_creds_json)


Expand Down
3 changes: 3 additions & 0 deletions enclave/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ cp /app/.env /app/oracles/.env
echo "Pinging for funds"
cd /app && python3.10 oracle_ping_for_funds.py

# Starting the attestation upgrader
cd /app/oracles && python3.10 update_attestation.py &> /app/oracles/attestation_upgrader.log &

# Start oracle setup
echo "Starting the oracle!"
cd /app/oracles && python3.10 oracle.py
5 changes: 4 additions & 1 deletion enclave/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@

OPEN_AI_API_KEY = os.getenv("OPEN_AI_API_KEY")
SERPER_API_KEY = os.getenv("SERPER_API_KEY")
WEB3_RPC_URL = os.getenv("WEB3_RPC_URL", "https://testnet.galadriel.com/")
CHAIN_ID = 696969
WEB3_RPC_URL = os.getenv("WEB3_RPC_URL", "https://devnet.galadriel.com/")
ORACLE_ADDRESS = os.getenv("ORACLE_ADDRESS")
ORACLE_ABI_PATH = os.getenv("ORACLE_ABI_PATH", "oracles/ChatOracle.json")
75 changes: 75 additions & 0 deletions enclave/update_attestation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import asyncio
import json
import base64
import time

from eth_account.signers.local import LocalAccount
from web3 import AsyncWeb3

import key_manager
import settings

SLEEP = 5

web3_client = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider(settings.WEB3_RPC_URL))

with open(settings.ORACLE_ABI_PATH, "r", encoding="utf-8") as f:
oracle_abi = json.loads(f.read())["abi"]


async def main():
while True:
account: LocalAccount = key_manager.get_account()
print("Account:", account.address)
attestation_doc_b64 = get_attestation_doc()
await send_attestation(account, attestation_doc_b64)
print(f"Sleeping for {SLEEP} seconds until next update")
time.sleep(SLEEP)


def get_attestation_doc():
try:
from NsmUtil import NSMUtil
nsm_util = NSMUtil()
attestation_doc = nsm_util.get_attestation_doc()
attestation_doc_b64 = base64.b64encode(attestation_doc).decode()
return attestation_doc_b64
except Exception as exc:
print("Error in getting attestation:", exc)
return "MockAttestation"


async def send_attestation(account: LocalAccount, attestation_doc_b64: str):
nonce = await web3_client.eth.get_transaction_count(account.address)
tx_data = {
"from": account.address,
"nonce": nonce,
# TODO: pick gas amount in a better way
# "gas": 1000000,
"maxFeePerGas": web3_client.to_wei("2", "gwei"),
"maxPriorityFeePerGas": web3_client.to_wei("1", "gwei"),
}
if chain_id := settings.CHAIN_ID:
tx_data["chainId"] = int(chain_id)

oracle_contract = web3_client.eth.contract(
address=settings.ORACLE_ADDRESS, abi=oracle_abi
)
tx = await oracle_contract.functions.addAttestation(
attestation_doc_b64,
).build_transaction(tx_data)
print("\ntx_data:", tx_data)
signed_tx = web3_client.eth.account.sign_transaction(
tx, private_key=account.key
)
print("\nsigned_tx")
tx_hash = await web3_client.eth.send_raw_transaction(
signed_tx.rawTransaction
)
print("\ntx_hash:", tx_hash)
tx_receipt = await web3_client.eth.wait_for_transaction_receipt(tx_hash)
print("\ntx_receipt:", tx_receipt)


if __name__ == '__main__':
asyncio.run(main())

0 comments on commit bba61d0

Please sign in to comment.