Skip to content

Commit

Permalink
Add helper scripts. Add database schema. Edit API code. Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
freQniK committed Nov 22, 2023
1 parent 2a13a7e commit f71de00
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 5 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# meile-plan-rest
Meile Subscription Plan REST API

## Dependencies
* uWSGI, `sudo apt install uwsgi-plugin-python3`
* Flask,
* FlaskMySQL,
* pexepct,
* PyJWT,
* date-util
* Werkzeug,
* SQLAlchemy,

`pip install -r requirements.txt`

# Documentation
https://petstore.swagger.io/?url=https://raw.githubusercontent.com/MathNodes/meile-plan-rest/main/doc/meile-api.yaml
https://petstore.swagger.io/?url=https://path/to/file.yaml
34 changes: 34 additions & 0 deletions helpers/add-nodes-to-plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

PLANID=$1
FILE=$2

# Edit These
WALLETNAME="BTCPay2"
KEYRINGDIR="/home/sentinel"

if [[ $# -lt 2 ]]; then
echo " "
echo "Add Nodes from file to Plan, v0.1 - freQniK"
echo " "
echo "Usage: $0 <plan_id> <node_file>"
echo " "
exit
fi

mapfile -t plan_nodes < <(cat $FILE)

for n in ${plan_nodes[@]}; do
echo "Adding: $n, to plan: $PLANID"
echo " "
./sentinelhub tx vpn plan add-node \
--from "$WALLETNAME" \
--gas-prices "0.3udvpn" \
--node "https://rpc.mathnodes.com:443" \
--keyring-dir "$KEYRINGDIR" \
--keyring-backend "file" \
--chain-id "sentinelhub-2" \
--yes \
$PLANID $n
sleep 5
done
74 changes: 74 additions & 0 deletions helpers/insert-nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/env python3

import pymysql
import scrtxxs
import uuid
import argparse
import sys


def connDB():
db = pymysql.connect(host=scrtxxs.MySQLHost,
port=scrtxxs.MySQLPort,
user=scrtxxs.MySQLUsername,
passwd=scrtxxs.MySQLPassword,
db=scrtxxs.MySQLDB,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return db




if __name__ == "__main__":

parser = argparse.ArgumentParser(description="Plan Node Inserter v0.1 - freQniK")
parser.add_argument('-f', '--file', help="file to read nodes from", metavar="node_file")

args = parser.parse_args()

if args.file:
NODE_FILE = str(args.file)
else:
print("Please specify a nodes file")
sys.exit(1)

db = connDB()
c = db.cursor()


puuid = input("Enter the Plan UUID: ")
address = "start"

node_addresses = []



with open(NODE_FILE, 'r') as nfile:
data = nfile.readlines()
k = 0
for d in data:
d = d.rstrip()
node_addresses.append(d)
k += 1


print(f"Inserting: {k}, nodes into plan: {puuid}...")
queries = []
for a in node_addresses:
q ='''
INSERT IGNORE INTO plan_nodes (uuid, node_address)
VALUES ("%s", "%s");
''' % (puuid, a)
queries.append(q)
k = 0
for q in queries:
try:
print(q)
c.execute(q)
db.commit()
k += 1
except Exception as e:
print(str(e))

print(f"Inserted: {k}, nodes into plan: {puuid}.")
44 changes: 44 additions & 0 deletions helpers/insert-plans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/env python3

import pymysql
import scrtxxs
import uuid
from datetime import datetime
from dateutil.relativedelta import relativedelta

def connDB():
db = pymysql.connect(host=scrtxxs.MySQLHost,
port=scrtxxs.MySQLPort,
user=scrtxxs.MySQLUsername,
passwd=scrtxxs.MySQLPassword,
db=scrtxxs.MySQLDB,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return db


db = connDB()
c = db.cursor()

puuid = uuid.uuid4()

sub_id = int(input("Enter the Subscription ID: "))
plan_id = int(input("Enter the Plan ID: "))
plan_name = input("Enter the name of the plan: ")
au_plan_price = int(input("Enter the Plan price in a.u.: "))
au_plan_denom = input("Enter the denom: ")

now = datetime.now()
expires = now + relativedelta(months=+1)

q ='''
INSERT IGNORE INTO meile_plans (uuid, subscription_id, plan_id, plan_name, plan_price, plan_denom, expiration_date)
VALUES ("%s", %d, %d, "%s", %d, "%s", "%s");
''' % (puuid, sub_id, plan_id, plan_name, au_plan_price, au_plan_denom, str(expires))
try:
print(q)
c.execute(q)
db.commit()
print("Plan committed to database")
except Exception as e:
print(str(e))
39 changes: 39 additions & 0 deletions helpers/subscribe-to-nodes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

FILE="$1"

# Edit These
DIR="/home/sentinel/go/bin/subscriptions" #Log dir of sub hashes and results
WALLETNAME="BTCPay2"
KEYRINGDIR="/home/sentinel"
HOURS=720

if [[ $# -lt 1 ]]; then
echo " "
echo "Subscribe To Nodes - v0.1 - freQniK"
echo " "
echo "Usage: $0 <node_file"
echo " "
exit
fi


mapfile -t nodes < <(cat $FILE)

for n in ${nodes[@]}; do
./sentinelhub tx vpn node subscribe \
--from "$WALLETNAME" \
--gas-prices "0.3udvpn" \
--node "https://rpc.mathnodes.com:443" \
--keyring-dir "$KEYRINGDIR" \
--keyring-backend "file" \
--chain-id "sentinelhub-2" \
--yes \
$n "udvpn" --hours $HOURS >> $DIR/$n.resp

sleep 7
hash=`cat $DIR/$n.resp | grep "txhash" | sed 's/txhash\: //g'`
./sentinelhub query tx --type=hash $hash --node https://rpc.mathnodes.com:443 | grep "raw_log" | sed 's/raw_log\: //g' | sed 's/^.//;s/.$//' | jq >> $DIR/$n-$hash.json
cat $DIR/$n-$hash.json

done
2 changes: 0 additions & 2 deletions meile_plan_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pwd
import os
import jwt
import time
import stripe
import pexpect

from datetime import datetime
Expand Down
8 changes: 8 additions & 0 deletions run-meile-rest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

IP=$1

FULLCHAIN="/home/sentinel/api/certs/fullchain.pem"
PRIVKEY="/home/sentinel/api/certs/privkey.pem"

uwsgi --plugin python3 --https-socket $IP:5000,$FULLCHAIN,$PRIVKEY --wsgi-file uWSGI.py --callable app --processes 6 --threads 8 --stats $IP:9191
2 changes: 2 additions & 0 deletions scrtxxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
WalletName = "Name"
HotWalletPW = "password"
SQLAlchemyScrtKey = 'sql_scrt_key'
MySQLHost = "127.0.0.1"
MySQLPort = 3306
MySQLUsername = 'username'
MySQLPassword = 'password'
MySQLDB = 'db_name'
Expand Down
7 changes: 5 additions & 2 deletions sql/plan_db.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
--- Create Meile Plan Table

CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, plan_name VARACHAR(256), plan_price UNSIGNED BIGINT, plan_denom VARCHAR(100), PRIMARY KEY(uuid))
CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id BIGINT UNSIGNED, plan_id BIGINT UNSIGNED, plan_name VARCHAR(256), plan_price BIGINT UNSIGNED, plan_denom VARCHAR(100), expiration_date TIMESTAMP PRIMARY KEY(uuid));

--- Create Meile Subscriber Table
CREATE TABLE meile_subscriptions (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), wallet VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, amt_paid DECIMAL(24,12), amt_denom VARCHAR(100), subscribe_date TIMSTAMP, subscription_duration UNSIGNED SMALLINT, expires TIMESTAMP, PRIMARY KEY(id))
CREATE TABLE meile_subscriptions (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), wallet VARCHAR(100), subscription_id BIGINT UNSIGNED, plan_id BIGINT UNSIGNED, amt_paid DECIMAL(24,12), amt_denom VARCHAR(10), subscribe_date TIMESTAMP, subscription_duration SMALLINT UNSIGNED, expires TIMESTAMP, PRIMARY KEY(id));

--- Create Plan Nodes Table
CREATE TABLE plan_nodes (uuid VARCHAR(100), node_address VARCHAR(255), PRIMARY KEY(uuid, node_address));
4 changes: 4 additions & 0 deletions uWSGI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from meile_plan_api import app

if __name__ == "__main__":
app.run()

0 comments on commit f71de00

Please sign in to comment.