Created
March 6, 2024 21:32
-
-
Save MorningLightMountain713/af3d05b0984542c2f1327ca912ff1362 to your computer and use it in GitHub Desktop.
Sandbox code to register flux app (needs refactor and httpx)
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
import time | |
import hashlib | |
import json | |
from rich.pretty import pprint | |
from Cryptodome.Random import get_random_bytes | |
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress | |
import requests | |
from bitcoin.signmessage import BitcoinMessage, VerifyMessage, SignMessage | |
import urllib.parse | |
def sign_message(key, msg): | |
secret = CBitcoinSecret(key) | |
message = BitcoinMessage(msg) | |
return SignMessage(secret, message) | |
# this will create a new private key | |
h = hashlib.sha256(get_random_bytes(64)).digest() | |
seckey = CBitcoinSecret.from_secret_bytes(h) | |
with open("secret_key", "w") as stream: | |
stream.write(str(seckey)) | |
print("Public key:", seckey.pub.hex()) | |
address = P2PKHBitcoinAddress.from_pubkey(seckey.pub) | |
print("Address:", address) | |
typeversion = "fluxappregister1" | |
timestamp = int(time.time() * 1000) | |
# expire_blocks = 5000 | |
expire_blocks = 264000 | |
app_config = { | |
"version": 6, | |
"name": "BigApp55", | |
"description": "App description", | |
"owner": str(address), | |
"compose": [ | |
{ | |
"name": "Component1", | |
"description": "component description", | |
"repotag": "mysql:latest", | |
"ports": [33333], | |
"domains": [""], | |
"environmentParameters": [], | |
"commands": [], | |
"containerPorts": [3000], | |
"containerData": "/data", | |
"cpu": 0.5, | |
"ram": 2000, | |
"hdd": 40, | |
"tiered": False, | |
} | |
], | |
"instances": 3, | |
"contacts": ["[email protected]"], | |
"geolocation": [], | |
"expire": expire_blocks, | |
} | |
# must strip whitespace for signature input (that's the separators thing) | |
to_sign = ( | |
f"{typeversion}{json.dumps(app_config, separators=(',', ':'))}{timestamp}" | |
) | |
print("To Sign:") | |
pprint(to_sign) | |
sig = sign_message(str(seckey), to_sign) | |
print("sig", sig) | |
# print(VerifyMessage(address, BitcoinMessage(to_sign), sig)) | |
to_register = { | |
"type": "fluxappregister", | |
"version": 1, | |
"appSpecification": app_config, | |
"timestamp": timestamp, | |
"signature": sig.decode(), | |
} | |
pprint(json.dumps(to_register)) | |
base_url = "https://api.runonflux.io" | |
headers = {"Content-Type": "application/x-www-form-urlencoded"} | |
r = requests.post( | |
f"{base_url}/apps/calculateprice", | |
data=json.dumps(app_config, separators=(",", ":")), | |
headers=headers, | |
).json() | |
app_price = None | |
if r.get("status", None) == "success": | |
print(r) | |
app_price = r.get("data") | |
# pretty sure this is wrong | |
app_price = round(app_price * 100000000) | |
print("app price:", app_price) | |
r = requests.get(f"{base_url}/id/loginphrase") | |
res: dict = r.json() | |
auth_res = {} | |
if status := res.get("status") == "success": | |
if challenge := res.get("data", None): | |
s = sign_message(str(seckey), challenge) | |
msg = { | |
"zelid": str(address), | |
"signature": s.decode(), | |
"loginPhrase": challenge, | |
} | |
auth_res = requests.post(f"{base_url}/id/verifylogin", data=msg).json() | |
register_res = {} | |
if auth_res.get("status", None) == "success": | |
headers = { | |
"zelidauth": urllib.parse.urlencode(msg), | |
"Content-Type": "application/x-www-form-urlencoded", | |
} | |
# validated = requests.post(f"{base_url}/apps/verifyappregistrationspecifications", data=json.dumps(app_config, separators=(",", ":")), headers=headers) | |
# print(validated.json()) | |
register_res = requests.post( | |
f"{base_url}/apps/appregister", | |
data=json.dumps(to_register, separators=(",", ":")), | |
headers=headers, | |
).json() | |
print("REGISTER RESPONSE") | |
pprint(register_res) | |
if register_res.get("status") == "success": | |
print() | |
if payment_msg := register_res.get("data", None): | |
print("PAYMENT") | |
print(payment_msg) |
pip install wheel python-bitcoinlib rich requests pycryptodomex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is using an old specversion. Think the latest is 7 now.