Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Lock.host python example, see: [Lock.host](https://github.com/rhodey/lock.host)
This demonstration uses OpenAI to control a Solana wallet:
+ Unmodified OpenAI lib
+ Unmodified Solana lib
+ Hit /api/ask?message=your best joke&addr=abc123
+ Hit /api/joke?message=your best joke&addr=abc123
+ OAI is asked "You are to decide if a joke is funny or not"
+ If so 0.001 SOL is sent to addr

Expand All @@ -17,9 +17,9 @@ just build-app
{
"Measurements": {
"HashAlgorithm": "Sha384 { ... }",
"PCR0": "e6227931d1d846e84ba568a33fbab583d24ce6f016e465eb6735af6f65048fd583911979233d1f53784a7a28c612878f",
"PCR0": "25411de098dd21a83a252fcb64bb2a28d7a5fe26df1a820f59abb73222a91f6cc9605d376ca37838d297c4e0e5e187a7",
"PCR1": "4b4d5b3661b3efc12920900c80e126e4ce783c522de6c02a2a5bf7af3a2b9327b86776f188e4be1c1c404a129dbda493",
"PCR2": "8e2166ac3d0a5afcc2461eb4009b0dde9b600f9d7216c90c8c2ae2331912d82bb502ef1bbc5914f456ba8b7864b22562"
"PCR2": "ee5d87b4c5f0f5ab6b3a0abb79bfb161f52e455f6ab66e7f3c38e98e2cba710e63ae9bfd340a106535afaf9729886b4e"
}
}
```
Expand All @@ -34,7 +34,7 @@ just serve-alpine
just build-test-app make-test-fifos
cp example.env .env
docker compose up -d
just ask-funds 'why did the worker quit his job at the recycling factory? because it was soda pressing.'
just joke 'why did the worker quit his job at the recycling factory? because it was soda pressing.'
...
addr = A7xYaa6PGwUFGYY5FYfMrZe6HJp5pSY7dBthdnPNbFE
sol = 0.025
Expand All @@ -45,7 +45,7 @@ json = {
"thoughts": "The joke is a clever play on words, combining the concept of being 'so depressing' with 'soda pressing' related to recycling. It's humorous and lighthearted."
}
sol = 0.026
(look inside python/ask-funds.py)
(look inside python/joke.py)
```

## Atsocat
Expand Down
21 changes: 2 additions & 19 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo := "$(docker info > /dev/null 2>&1 || echo 'sudo')"
certs := "$(cat cert.key > /dev/null 2>&1 && echo '-v ./cert.key:/runtime/cert.key -v ./cert.crt:/runtime/cert.crt')"

#########################
## Reproducible builds ##
Expand Down Expand Up @@ -59,24 +58,8 @@ build-test-app:
just make-test-net
{{sudo}} docker buildx build --platform="linux/amd64" --build-arg PROD=false -f Dockerfile.app -t lockhost-python-test-app .

add-funds:
{{sudo}} docker run --rm --entrypoint /app/add-funds.sh --env-file .env lockhost-python-test-app

ask-funds joke:
{{sudo}} docker run --rm --entrypoint /app/ask-funds.sh --network locknet --env-file .env lockhost-python-test-app http://atsocat:8889 {{joke}}

mkcert:
echo "authorityKeyIdentifier=keyid,issuer" > domains.ext
echo "basicConstraints=CA:FALSE" >> domains.ext
echo "keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment" >> domains.ext
echo "subjectAltName = @alt_names" >> domains.ext
echo "[alt_names]" >> domains.ext
echo "DNS.1 = localhost" >> domains.ext
openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout ca.key -out ca.pem -subj "/C=US/CN=Lock-Host-CA"
openssl x509 -outform pem -in ca.pem -out ca.crt
openssl req -new -nodes -newkey rsa:2048 -keyout cert.key -out cert.csr -subj "/C=US/ST=YourState/L=YourCity/O=Example-Certificates/CN=localhost"
openssl x509 -req -sha256 -days 1024 -in cert.csr -CA ca.pem -CAkey ca.key -CAcreateserial -extfile domains.ext -out cert.crt
rm ca.key ca.pem ca.srl cert.csr domains.ext
joke joke:
{{sudo}} docker run --rm --entrypoint /app/joke.sh --network locknet --env-file .env lockhost-python-test-app http://atsocat:8889 {{joke}}


#########################
Expand Down
26 changes: 0 additions & 26 deletions python/add-funds.py

This file was deleted.

4 changes: 0 additions & 4 deletions python/add-funds.sh

This file was deleted.

21 changes: 9 additions & 12 deletions python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ async def add_cors_headers(request, handler):
return response

# called by user
async def wallet_handler(request):
print("got wallet request")
async def balance_handler(request):
print("get balance")
pubkey = request.query.get("addr", "")
pubkey = Pubkey.from_string(pubkey) if pubkey != "" else sol_key.pubkey()
sol_client = request.app["sol_client"]
Expand Down Expand Up @@ -76,8 +76,8 @@ async def wallet_handler(request):
}]

# called by user
async def ask_handler(request):
print("got oai request")
async def joke_handler(request):
print("get joke")
addr = request.query.get("addr", "")
addr = Pubkey.from_string(addr)
message = unquote(request.query.get("message", ""))
Expand All @@ -93,14 +93,12 @@ async def ask_handler(request):

reply = reply.choices[0].message.tool_calls[0]
reply = json.loads(reply.function.arguments)
print(f"got oai reply {reply}")
print(f"oai reply {reply}")
funny = reply["decision"] == "funny"

if funny == False:
print("oai = not funny")
return web.json_response({"thoughts": reply["thoughts"]})

print("oai = funny")
lamports_to_send = 1_000_000
ixns = [transfer(
TransferParams(
Expand All @@ -115,7 +113,6 @@ async def ask_handler(request):
latest_blockhash = await sol_client.get_latest_blockhash()
txn = Transaction([sol_key], message, latest_blockhash.value.blockhash)
signature = await sol_client.send_transaction(txn)
print(f"signature = {str(signature.value)}")

latest_blockhash = await sol_client.get_latest_blockhash()
await sol_client.confirm_transaction(tx_sig=signature.value, last_valid_block_height=latest_blockhash.value.last_valid_block_height)
Expand All @@ -129,10 +126,10 @@ async def create_app():
sol_client = await MySolClient.create()
app = web.Application(middlewares=[add_cors_headers])
app["sol_client"] = sol_client
app.router.add_route("OPTIONS", "/api/wallet", cors_handler)
app.router.add_route("OPTIONS", "/api/ask", cors_handler)
app.router.add_route("GET", "/api/wallet", wallet_handler)
app.router.add_route("GET", "/api/ask", ask_handler)
app.router.add_route("OPTIONS", "/api/balance", cors_handler)
app.router.add_route("OPTIONS", "/api/joke", cors_handler)
app.router.add_route("GET", "/api/balance", balance_handler)
app.router.add_route("GET", "/api/joke", joke_handler)
return app

# connections arrive from runtime
Expand Down
4 changes: 0 additions & 4 deletions python/ask-funds.sh

This file was deleted.

2 changes: 1 addition & 1 deletion python/ask-funds.py → python/joke.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def main():

# send request
api = sys.argv[1]
api = f"{api}/api/ask"
api = f"{api}/api/joke"
data = requests.get(api, params=params)
data = data.json()
pretty = json.dumps(data, indent=2)
Expand Down
4 changes: 4 additions & 0 deletions python/joke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -e

python -u joke.py "$@"