-
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.
Merge pull request #2 from cybercongress/tx_service
proofing service
- Loading branch information
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
WALLET_SEED = 'rack canyon puzzle grow afford faint heavy kick furnace economy change loop debate tip acquire render rib truth bachelor monster page range wine measure' | ||
CONTRACT_ADDRESS = 'pussy15s8v0pa5g60uhvmjpfj73p6nem6t597e8qnkgpsuck5tje3se7ps3ll7kl' |
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,2 @@ | ||
WALLET_SEED = 'rack canyon puzzle grow afford faint heavy kick furnace economy change loop debate tip acquire render rib truth bachelor monster page range wine measure' | ||
CONTRACT_ADDRESS = 'pussy15s8v0pa5g60uhvmjpfj73p6nem6t597e8qnkgpsuck5tje3se7ps3ll7kl' |
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,12 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
FROM python:3.9 as build | ||
|
||
WORKDIR /app | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip3 install -r requirements.txt | ||
|
||
COPY . . | ||
|
||
CMD [ "python3", "-m" , "gunicorn", "--bind", "0.0.0.0:5000", "app:app"] |
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,16 @@ | ||
## run service by python | ||
```bash | ||
pip3 install -r requirements.txt | ||
export FLASK_APP=app.py && python -m flask run --host=0.0.0.0 --port=4000 | ||
``` | ||
|
||
## run service by docker | ||
```bash | ||
docker-compouse up --build | ||
``` | ||
|
||
## check service | ||
```bash | ||
curl --request POST --url http://localhost:4000/proof -H 'accept: application/json' -H 'content-type: application/json' \ | ||
-d '{"proof": {"proof": {"domain": {"lengthBytes": 36, "value": "ton-wasm-cyber-hackathon.netlify.app"}, "signature": "tN5HZQWu0WrtGUQPMW8okSgA28IZJ9oAB6VoDoor4iOoyflA1HRpmXZ5fstdEnWASRwFMjw0zNPScAXy8d03DA==", "payload": "eyJtc2dfdHlwZSI6Im1hcF9uaWNrbmFtZSIsIm1zZ19kYXRhIjoiY3lib3Jnc2hlYWQifQ==", "timestamp": 1716987585, "state_init": " "}, "address": "0:d608e557aa3216e9152ad3e781326d175df0c65e08b08f980fd63266fa6dbe06", "network": "-239"}, "pubkey": "07774b49ea9c97095b718d5d8c691fd8e8900d496f73005442e8243c72753196"}' | ||
``` |
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,77 @@ | ||
import logging | ||
import sys | ||
|
||
from cosmpy.aerial.client import NetworkConfig, Address | ||
from cosmpy.aerial.wallet import LocalWallet | ||
from dotenv import dotenv_values | ||
from flask import Flask, request, jsonify | ||
|
||
from contract_execution import execute_contract | ||
|
||
WALLET_SEED = dotenv_values(".env")['WALLET_SEED'] | ||
CONTRACT_ADDRESS = dotenv_values(".env")['CONTRACT_ADDRESS'] | ||
CHAIN_ID = 'space-pussy' | ||
PREFIX = 'pussy' | ||
NODE_LCD_URL = 'https://lcd.space-pussy.cybernode.ai' | ||
ACCOUNT = 0 | ||
|
||
app = Flask(__name__) | ||
|
||
NETWORK_CONFIG = NetworkConfig( | ||
chain_id=CHAIN_ID, | ||
fee_minimum_gas_price=0, | ||
fee_denomination='pussy', | ||
staking_denomination='pussy', | ||
url="grpc+https://grpc.space-pussy.cybernode.ai:1443" | ||
) | ||
|
||
logging.basicConfig(level=logging.INFO, | ||
format='%(asctime)s - %(levelname)s - %(message)s', | ||
datefmt='%d-%m-%Y %H:%M:%S', | ||
handlers=[ | ||
# logging.FileHandler("tx_service.log"), | ||
logging.StreamHandler(sys.stdout) | ||
]) | ||
|
||
|
||
# Define a route for handling POST requests to "/proof" | ||
@app.route("/proof", methods=["POST"]) | ||
def handle_tx(): | ||
# Parse the JSON request body | ||
req_body = request.get_json() | ||
|
||
# Check the request body | ||
if "proof" not in req_body.keys(): | ||
return jsonify({"status": "fail", "error": "There is no proof value"}) | ||
if "pubkey" not in req_body.keys(): | ||
return jsonify({"status": "fail", "error": "There is no pubkey value"}) | ||
|
||
# Extract the transaction data from the request body | ||
proof = req_body["proof"] | ||
pubkey = req_body["pubkey"] | ||
logging.info(f'proof {proof}, pubkey{pubkey}') | ||
# Build the transaction | ||
message = { | ||
"relay_ton_msg": { | ||
"proof": proof, | ||
"pubkey": pubkey | ||
} | ||
} | ||
wallet = LocalWallet.from_mnemonic(mnemonic=WALLET_SEED, prefix='pussy') | ||
res = execute_contract( | ||
msg=message, | ||
contract_address=Address(CONTRACT_ADDRESS), | ||
signer_wallet=wallet, | ||
network_config=NETWORK_CONFIG | ||
) | ||
print(f'res: {res}') | ||
if res[0] is not True: | ||
logging.error(f'Contract error: {res[1]}') | ||
return jsonify({"status": "fail", "error": res[1]}) | ||
logging.info(f'tx: {res}') | ||
return jsonify({"status": "success", "tx": res[1]}) | ||
|
||
|
||
# Run the Flask app | ||
if __name__ == "__main__": | ||
app.run(debug=True) |
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,48 @@ | ||
from typing import Optional | ||
|
||
from cosmpy.aerial.client import LedgerClient, NetworkConfig | ||
from cosmpy.aerial.contract import LedgerContract | ||
from cosmpy.aerial.wallet import LocalWallet | ||
from cosmpy.crypto.address import Address | ||
|
||
|
||
# from retry import retry | ||
|
||
|
||
# @retry(delay=3, tries=3, backoff=2, max_delay=8) | ||
def execute_contract( | ||
msg: dict, | ||
contract_address: Address, | ||
signer_wallet: LocalWallet, | ||
network_config: NetworkConfig, | ||
wait_for_finalization: bool = True, | ||
gas: Optional[int] = 1_000_000, | ||
funds: Optional[str] = None | ||
) -> [bool, Optional[str]]: | ||
print(f'tx msg {msg}\tsigner_wallet {signer_wallet}') | ||
client = LedgerClient(cfg=network_config) | ||
contract = LedgerContract( | ||
client=client, | ||
address=contract_address, | ||
digest=None, | ||
path=None) | ||
print('start executing contract...') | ||
if not wait_for_finalization: | ||
tx = contract.execute(msg, signer_wallet, gas, funds=funds) | ||
return True, tx | ||
else: | ||
try: | ||
# NOTE will raise exception if tx simulation is not successful, need to catch it | ||
tx = contract.execute(msg, signer_wallet, gas, funds=funds) | ||
except Exception as e: | ||
return False, e.__str__() | ||
|
||
try: | ||
tx.wait_to_complete() | ||
if tx.response.is_successful(): | ||
print(f'Gas used: {tx.response.gas_used:>,}') | ||
return True, tx.response | ||
else: | ||
return False, tx.response.code | ||
except Exception as e: | ||
return False, e.__str__() |
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,10 @@ | ||
version: '3.8' | ||
services: | ||
app: | ||
build: | ||
context: . | ||
target: build | ||
ports: | ||
- '4000:5000' | ||
volumes: | ||
- .:/app |
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,4 @@ | ||
flask | ||
cosmpy | ||
python-dotenv | ||
gunicorn |