Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f95c29f5af |
@@ -1,6 +0,0 @@
|
||||
venv/*
|
||||
mine_data/*
|
||||
mariadb_data/*
|
||||
*.pem
|
||||
*__pycache__*
|
||||
*.swp
|
||||
@@ -1,6 +0,0 @@
|
||||
FROM ericomeehan/httpd-mod-wsgi
|
||||
RUN apt update && apt upgrade -y
|
||||
RUN apt install -y libmariadb3 libmariadb-dev python3-dev python3-pip
|
||||
COPY . /usr/local/apache2/htdocs
|
||||
RUN pip3 install --break-system-packages -r /usr/local/apache2/htdocs/requirements.txt
|
||||
EXPOSE 80
|
||||
@@ -1,10 +0,0 @@
|
||||
# WikiDeck
|
||||
An [NFT](https://en.wikipedia.org/wiki/Non-fungible_token)
|
||||
[blockchain](https://en.wikipedia.org/wiki/Blockchain) that rewards Wikipedia trading cards.
|
||||
|
||||
## Use
|
||||
See `test/Mine/TestMine.py` for a demonstration of the basic client workflow.
|
||||
|
||||
## Status
|
||||
At present, only the files in `wikideck/Mine` are functional - and only partially so. This
|
||||
project is currently a proof-of-concept.
|
||||
@@ -1,295 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import requests
|
||||
from typing import Optional
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "wikideck")));
|
||||
|
||||
from Mine.Block import Block
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
WIKIDECK_URL = os.getenv('WIKIDECK_URL', 'http://localhost:8080/');
|
||||
|
||||
|
||||
rsa_private_key: Optional[rsa.RSAPrivateKey] = None;
|
||||
msg_array: list[str] = [""];
|
||||
private_key_file: str = "privatekey.pem";
|
||||
public_key_file: str = "publickey.pem";
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
while True:
|
||||
|
||||
if (msg_array[0]):
|
||||
print(f"Last Action: {msg_array[0]}");
|
||||
|
||||
print("\n--- Wikideck CLI ---");
|
||||
print("1. Create RSA Keypair");
|
||||
print("2. Save Private Key");
|
||||
print("3. Load RSA Keypair");
|
||||
print("4. Save Public Key")
|
||||
print("5. Mine a Block");
|
||||
print("6. Generate a Transaction");
|
||||
print("7. View Deck");
|
||||
print("0. Exit");
|
||||
print("\n-------------------");
|
||||
|
||||
try:
|
||||
choice: int = int(input("Select an option: ").strip());
|
||||
except ValueError:
|
||||
msg_array[0] = "Invalid Input. Please Choose a Number Between 0-6.";
|
||||
clear_console();
|
||||
continue;
|
||||
|
||||
|
||||
if choice == 0:
|
||||
break;
|
||||
elif choice == 1:
|
||||
opt_generate_keypair(65537, 2048);
|
||||
elif choice == 2:
|
||||
opt_save_private_key(rsa_private_key);
|
||||
elif choice == 3:
|
||||
opt_load_private_key();
|
||||
elif choice == 4:
|
||||
opt_save_public_key(rsa_private_key);
|
||||
elif choice == 5:
|
||||
opt_mine_block();
|
||||
elif choice == 6:
|
||||
opt_generate_transaction();
|
||||
elif choice == 7:
|
||||
opt_view_deck();
|
||||
else:
|
||||
msg_array[0] = "Invalid Input. Please Choose a Number Between 0-6.";
|
||||
|
||||
clear_console();
|
||||
|
||||
def opt_generate_keypair(exponent=65537, size=2048):
|
||||
global rsa_private_key;
|
||||
rsa_private_key = rsa.generate_private_key(
|
||||
public_exponent = exponent,
|
||||
key_size = size,
|
||||
)
|
||||
msg_array[0] = "RSA Keypair Generated";
|
||||
return rsa_private_key;
|
||||
|
||||
def opt_save_private_key(rsa, filename=private_key_file):
|
||||
if rsa is None:
|
||||
msg_array[0] = "No RSA Keypair to extract private key. Please generate one first.";
|
||||
return None;
|
||||
else:
|
||||
with open(filename, "wb") as file:
|
||||
pem = rsa.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
)
|
||||
file.write(pem);
|
||||
msg_array[0] = f"RSA Private Key Saved to {filename}";
|
||||
|
||||
def opt_save_public_key(rsa, filename=public_key_file):
|
||||
if rsa is None:
|
||||
msg_array[0] = "No RSA Keypair to extract public key. Please generate one first.";
|
||||
return None;
|
||||
|
||||
public_key = rsa.public_key();
|
||||
|
||||
public_pem = public_key.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
)
|
||||
|
||||
with open(filename, "wb") as file:
|
||||
file.write(public_pem);
|
||||
msg_array[0] = f"RSA Public Key Saved to {filename}";
|
||||
|
||||
def opt_load_private_key():
|
||||
global rsa_private_key;
|
||||
if not os.path.exists(private_key_file):
|
||||
msg_array[0] = f"No saved key found ({private_key_file} missing)";
|
||||
return None;
|
||||
|
||||
with open(private_key_file, "rb") as file:
|
||||
rsa_private_key = serialization.load_pem_private_key(
|
||||
file.read(),
|
||||
password=None,
|
||||
)
|
||||
msg_array[0] = f"RSA Private Key Loaded From {private_key_file}";
|
||||
|
||||
def get_private_key_from_file(path):
|
||||
if not os.path.exists(path):
|
||||
msg_array[0] = f"No key file found ({path} missing)";
|
||||
return None;
|
||||
|
||||
with open(path, "rb") as file:
|
||||
key = serialization.load_pem_private_key(
|
||||
file.read(),
|
||||
password=None,
|
||||
);
|
||||
msg_array[0] = f"RSA Private Key Gotten From {path}"; #inaccessible
|
||||
|
||||
return key;
|
||||
|
||||
def opt_mine_block(path = None):
|
||||
|
||||
global rsa_private_key;
|
||||
|
||||
if (path):
|
||||
rsa_private_key = get_private_key_from_file(path);
|
||||
if not (rsa_private_key):
|
||||
return None;
|
||||
|
||||
if rsa_private_key is None:
|
||||
msg_array[0] = "You must load or generate an RSA keypair first.";
|
||||
return;
|
||||
|
||||
try:
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks");
|
||||
if response.status_code != 200:
|
||||
msg_array[0] = f"Failed to fetch block to mine: {response.status_code}";
|
||||
return;
|
||||
|
||||
block_to_mine = Block(data = response.json());
|
||||
block_to_mine.mine(rsa_private_key);
|
||||
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=block_to_mine.as_dict());
|
||||
if response.status_code == 200:
|
||||
msg_array[0] = "Block successfully mined and submitted";
|
||||
else:
|
||||
msg_array[0] = f"Failed to submit mined block: {response.status_code}";
|
||||
|
||||
except Exception as e:
|
||||
msg_array[0] = f"Error during mining: {str(e)}";
|
||||
|
||||
def opt_generate_transaction(path = None, recv = None):
|
||||
global rsa_private_key;
|
||||
|
||||
if (path):
|
||||
rsa_private_key = get_private_key_from_file(path);
|
||||
if not (rsa_private_key):
|
||||
return None;
|
||||
|
||||
|
||||
if rsa_private_key is None:
|
||||
msg_array[0] = "You must load or generate an RSA keypair first.";
|
||||
return;
|
||||
|
||||
if not (recv):
|
||||
print("\nPaste the recipient's RSA Public Key (PEM format). End with an Empty Line (Press enter twice): ");
|
||||
lines = [];
|
||||
while True:
|
||||
line = input();
|
||||
if not line.strip():
|
||||
break;
|
||||
lines.append(line);
|
||||
|
||||
receiver_pem = "\n".join(lines);
|
||||
|
||||
try:
|
||||
receiver_key = serialization.load_pem_public_key(receiver_pem.encode("utf-8"));
|
||||
except Exception:
|
||||
msg_array[0] = "Invalid public key format";
|
||||
return;
|
||||
|
||||
else:
|
||||
if not os.path.isfile(recv):
|
||||
msg_array[0] = f"Reciever public key file not found {recv}";
|
||||
return None;
|
||||
with open(recv, "rb") as recv_file:
|
||||
receiver_key = serialization.load_pem_public_key(recv_file.read());
|
||||
|
||||
|
||||
|
||||
|
||||
try:
|
||||
|
||||
cards = fetch_cards_json(rsa_private_key);
|
||||
|
||||
if not cards:
|
||||
msg_array[0] = "You have no cards to send";
|
||||
return;
|
||||
|
||||
print("\nYour cards:");
|
||||
|
||||
for i, card in enumerate(cards):
|
||||
print(f"{i+1}. Card ID: {card['cardId']}, Page ID: {card['pageId']}");
|
||||
|
||||
|
||||
try:
|
||||
index: int = int(input("Select a card number to send: ").strip()) - 1;
|
||||
|
||||
if not (0 <= index < len(cards)):
|
||||
raise ValueError;
|
||||
except ValueError:
|
||||
msg_array[0] = "Invalid card selection";
|
||||
return;
|
||||
|
||||
selected_card = cards[index];
|
||||
|
||||
tx = Transaction(
|
||||
cardId = selected_card['cardId'],
|
||||
sender = rsa_private_key.public_key(),
|
||||
receiver=receiver_key,
|
||||
authorPrivateKey=rsa_private_key
|
||||
);
|
||||
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=tx.as_dict());
|
||||
|
||||
if response.status_code != 200:
|
||||
msg_array[0] = f"Transaction failed: {response.status_code}";
|
||||
else:
|
||||
msg_array[0] = "Transaction submitted successfully";
|
||||
|
||||
except Exception as e:
|
||||
msg_array[0] = f"Error creating transaction: {str(e)}";
|
||||
|
||||
|
||||
def opt_view_deck():
|
||||
|
||||
global rsa_private_key;
|
||||
if rsa_private_key is None:
|
||||
msg_array[0] = "You must load or generate an RSA keypair first.";
|
||||
return;
|
||||
|
||||
try:
|
||||
cards = fetch_cards_json(rsa_private_key);
|
||||
|
||||
|
||||
if len(cards) <= 0:
|
||||
msg_array[0] = "You have no cards to view";
|
||||
return;
|
||||
|
||||
msg = "Successfully listed deck.\n"
|
||||
for card in cards:
|
||||
msg += f"Card ID: {card['cardId']}, Page ID: {card['pageId']}\n"
|
||||
msg_array[0] = msg
|
||||
|
||||
except Exception as e:
|
||||
msg_array[0] = f"Error fetching cards: {str(e)}";
|
||||
|
||||
|
||||
def fetch_cards_json(rsa):
|
||||
pem = rsa.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode("utf-8");
|
||||
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={"publicKey": pem});
|
||||
|
||||
if response.status_code != 200:
|
||||
msg_array[0] = f"Failed to fetch cards: {response.status_code}";
|
||||
return;
|
||||
|
||||
cards = response.json();
|
||||
return cards;
|
||||
|
||||
|
||||
def clear_console():
|
||||
os.system('cls' if os.name == 'nt' else 'clear');
|
||||
|
||||
if __name__ == "__main__":
|
||||
main();
|
||||
@@ -1,3 +0,0 @@
|
||||
cryptography
|
||||
dotenv
|
||||
wikipedia
|
||||
@@ -1,30 +0,0 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
wikideck-mine:
|
||||
image: ericomeehan/wikideck:latest
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
ROLE: mine
|
||||
DATA_PATH: /tmp
|
||||
DIFFICULTY_REQUIREMENT: 10
|
||||
MINE_DB_HOST: mariadb-mine
|
||||
MINE_DB_USER: mine
|
||||
MINE_DB_PASSWORD: 123abc
|
||||
MINE_LOG_FILE: /tmp/mine.log
|
||||
depends_on:
|
||||
- mariadb-mine
|
||||
ports:
|
||||
- "8080:80"
|
||||
|
||||
mariadb-mine:
|
||||
image: mariadb:latest
|
||||
environment:
|
||||
MARIADB_ROOT_PASSWORD: abc123
|
||||
MARIADB_DATABASE: mine
|
||||
MARIADB_USER: mine
|
||||
MARIADB_PASSWORD: 123abc
|
||||
ports:
|
||||
- "3306:3306"
|
||||
@@ -1,5 +0,0 @@
|
||||
cryptography
|
||||
dotenv
|
||||
flask
|
||||
mariadb
|
||||
wikipedia
|
||||
@@ -1,261 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
import sys
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'wikideck')))
|
||||
|
||||
from Mine.Block import Block
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
WIKIDECK_URL = os.getenv('WIKIDECK_URL', 'http://localhost:8080/')
|
||||
|
||||
class TestMine(unittest.TestCase):
|
||||
def test_mine_block_send_card_mine_another_block(self):
|
||||
# Create two users
|
||||
privateKeyA = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
privateKeyB = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
# Mine a block with privateKeyA
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockA = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockA.mine(privateKeyA)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockA.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Send card from newBlockA to privateKeyB
|
||||
newTransaction = Transaction(
|
||||
cardId = newBlockA.card.cardId,
|
||||
sender = privateKeyA.public_key(),
|
||||
receiver = privateKeyB.public_key(),
|
||||
authorPrivateKey = privateKeyA
|
||||
)
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Mine a block with privateKeyB
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockB = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockB.mine(privateKeyB)
|
||||
self.assertEqual(newBlockA.blockHash.hexdigest(), newBlockB.previousHash)
|
||||
self.assertTrue(newBlockA.timestamp < newBlockB.timestamp)
|
||||
self.assertTrue(newBlockB.height == newBlockA.height + 1)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockB.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Validate decks
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyA.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 0)
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyB.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.json()[0]['cardId'], str(newBlockA.card.cardId))
|
||||
self.assertEqual(response.json()[0]['pageId'], newBlockA.card.pageId)
|
||||
self.assertEqual(response.json()[1]['cardId'], str(newBlockB.card.cardId))
|
||||
self.assertEqual(response.json()[1]['pageId'], newBlockB.card.pageId)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 2)
|
||||
|
||||
def test_invalid_block_modified_transaction(self):
|
||||
# Create two users
|
||||
privateKeyA = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
privateKeyB = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
# Mine a block with privateKeyA
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockA = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockA.mine(privateKeyA)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockA.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Send card from newBlockA to privateKeyB
|
||||
newTransaction = Transaction(
|
||||
cardId = newBlockA.card.cardId,
|
||||
sender = privateKeyA.public_key(),
|
||||
receiver = privateKeyB.public_key(),
|
||||
authorPrivateKey = privateKeyA
|
||||
)
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Mine a block with privateKeyB
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockB = Block(
|
||||
data = response.json()
|
||||
)
|
||||
# Modify transactions
|
||||
newBlockB.transactions[0].sender = privateKeyB.public_key()
|
||||
newBlockB.transactions[0].receiver = privateKeyA.public_key()
|
||||
newBlockB.mine(privateKeyB)
|
||||
self.assertEqual(newBlockA.blockHash.hexdigest(), newBlockB.previousHash)
|
||||
self.assertTrue(newBlockA.timestamp < newBlockB.timestamp)
|
||||
self.assertTrue(newBlockB.height == newBlockA.height + 1)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockB.as_dict())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
# Validate decks
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyA.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 1)
|
||||
self.assertEqual(response.json()[0]['cardId'], str(newBlockA.card.cardId))
|
||||
self.assertEqual(response.json()[0]['pageId'], newBlockA.card.pageId)
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyB.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 0)
|
||||
# Mine a block with privateKeyB
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockB = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockB.mine(privateKeyB)
|
||||
self.assertEqual(newBlockA.blockHash.hexdigest(), newBlockB.previousHash)
|
||||
self.assertTrue(newBlockA.timestamp < newBlockB.timestamp)
|
||||
self.assertTrue(newBlockB.height == newBlockA.height + 1)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockB.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Validate decks
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyA.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 0)
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyB.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.json()[0]['cardId'], str(newBlockA.card.cardId))
|
||||
self.assertEqual(response.json()[0]['pageId'], newBlockA.card.pageId)
|
||||
self.assertEqual(response.json()[1]['cardId'], str(newBlockB.card.cardId))
|
||||
self.assertEqual(response.json()[1]['pageId'], newBlockB.card.pageId)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 2)
|
||||
|
||||
def test_invalid_transaction(self):
|
||||
# Create three users
|
||||
privateKeyA = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
privateKeyB = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
privateKeyC = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
# Mine a block with privateKeyA
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockA = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockA.mine(privateKeyA)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockA.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Send card from newBlockA to privateKeyB
|
||||
newTransaction = Transaction(
|
||||
cardId = newBlockA.card.cardId,
|
||||
sender = privateKeyA.public_key(),
|
||||
receiver = privateKeyB.public_key(),
|
||||
authorPrivateKey = privateKeyA
|
||||
)
|
||||
# Invalidate transaction
|
||||
newTransaction.cardId = uuid.uuid4()
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
newTransaction.cardId = newBlockA.card.cardId
|
||||
newTransaction.sender = privateKeyC.public_key()
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
newTransaction.sender = privateKeyA.public_key()
|
||||
newTransaction.receiver = privateKeyC.public_key()
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
newTransaction.receiver = privateKeyB.public_key()
|
||||
newTransaction.sign(privateKeyC)
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 400)
|
||||
newTransaction.sign(privateKeyA)
|
||||
response = requests.post(f"{WIKIDECK_URL}/transactions", json=newTransaction.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Mine a block with privateKeyB
|
||||
response = requests.get(f"{WIKIDECK_URL}/blocks")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
newBlockB = Block(
|
||||
data = response.json()
|
||||
)
|
||||
newBlockB.mine(privateKeyB)
|
||||
self.assertEqual(newBlockA.blockHash.hexdigest(), newBlockB.previousHash)
|
||||
self.assertTrue(newBlockA.timestamp < newBlockB.timestamp)
|
||||
self.assertTrue(newBlockB.height == newBlockA.height + 1)
|
||||
response = requests.post(f"{WIKIDECK_URL}/blocks", json=newBlockB.as_dict())
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# Validate decks
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyA.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 0)
|
||||
response = requests.get(f"{WIKIDECK_URL}/cards", params={
|
||||
'publicKey': privateKeyB.public_key().public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
})
|
||||
self.assertEqual(response.json()[0]['cardId'], str(newBlockA.card.cardId))
|
||||
self.assertEqual(response.json()[0]['pageId'], newBlockA.card.pageId)
|
||||
self.assertEqual(response.json()[1]['cardId'], str(newBlockB.card.cardId))
|
||||
self.assertEqual(response.json()[1]['pageId'], newBlockB.card.pageId)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.json()), 2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,10 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
||||
|
||||
from Mine.TestMine import TestMine
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 144 KiB |
|
After Width: | Height: | Size: 143 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 138 KiB |
|
After Width: | Height: | Size: 393 KiB |
|
After Width: | Height: | Size: 403 KiB |
@@ -1,19 +0,0 @@
|
||||
import json
|
||||
|
||||
from Market.Order import Order
|
||||
|
||||
class BuyOrder(Order):
|
||||
def __init__(self, cardId=None, price=0, volume=0, fee=0):
|
||||
super().__init__(
|
||||
orderType = "buy",
|
||||
cardId = cardId,
|
||||
fee = fee
|
||||
)
|
||||
self.price = price
|
||||
self.volume = volume
|
||||
|
||||
def add_transaction(self, transaction):
|
||||
if len(self.transactions) < self.volume:
|
||||
# TODO: update database
|
||||
else:
|
||||
raise self.Fulfilled("Order volume has already been fulfilled.")
|
||||
@@ -1,12 +0,0 @@
|
||||
class Database():
|
||||
def __init__(self, host, port, user, password):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.conn = mariadb.connect(
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
user = self.user,
|
||||
password = self.password
|
||||
)
|
||||
@@ -1,42 +0,0 @@
|
||||
import flask
|
||||
|
||||
from Market.Database import Database
|
||||
|
||||
market = flask.Blueprint("market", __name__)
|
||||
db = Database()
|
||||
|
||||
# TODO: users and logins
|
||||
|
||||
@app.get('/')
|
||||
def market():
|
||||
# TODO: return a page to view market stats
|
||||
return 200
|
||||
|
||||
###
|
||||
# Submit an order to the market.
|
||||
# This method calls the Order().validate() method to validate the order schema.
|
||||
# This method performs additional validations against user table.
|
||||
# User balances are reduced immediately upon receiving a buy order from their account.
|
||||
###
|
||||
@app.post('/orders')
|
||||
def order_post():
|
||||
try:
|
||||
new_order = Order(data=request.get_json())
|
||||
new_order.validate()
|
||||
# TODO: validate order against the Mine
|
||||
# TODO: write order to db
|
||||
if newOrder.orderType == 'buy':
|
||||
# TODO: reduce user balance by ((price + fee) * volume)
|
||||
continue
|
||||
return 200
|
||||
# TODO: exceptions
|
||||
except mariadb.Error as e:
|
||||
return e, 500
|
||||
|
||||
###
|
||||
# Retrieve one or more orders.
|
||||
###
|
||||
@app.get('/orders')
|
||||
def orders_get():
|
||||
# TODO: order queries
|
||||
return 200
|
||||
@@ -1,28 +0,0 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
class Order(ABC):
|
||||
def __init__(self, orderId=uuid.uuid4(),
|
||||
timestamp=datetime.datetime.now(datetime.timezone.utc)
|
||||
userId=None, orderType=None, cardId=None, fee=0, transactions=[], data={}):
|
||||
self.orderId = orderId
|
||||
self.timestamp = timestamp
|
||||
self.userId = userId
|
||||
self.orderType = orderType
|
||||
self.cardId = cardId
|
||||
self.fee = fee
|
||||
self.transactions = transactions
|
||||
|
||||
@abstractmethod
|
||||
def add_transaction(self, transaction):
|
||||
continue
|
||||
|
||||
@abstractmethod
|
||||
def validate(self):
|
||||
continue
|
||||
|
||||
@abstractmethod
|
||||
def load_from_data(self):
|
||||
continue
|
||||
@@ -1,21 +0,0 @@
|
||||
import json
|
||||
|
||||
class SellOrder():
|
||||
def __init__(self, cardId=None, items=[], price=0, fee=0):
|
||||
super().__init__(
|
||||
orderType = "sell",
|
||||
cardId = cardId,
|
||||
fee = fee
|
||||
)
|
||||
self.items = items
|
||||
self.price = price
|
||||
|
||||
def add_transaction(self, transaction):
|
||||
if transaction.cardId in self.items:
|
||||
# TODO: update database
|
||||
self.items.pop(transaction.cardId)
|
||||
self.transactions.append(transaction)
|
||||
|
||||
def validate(self):
|
||||
# TODO: no duplicate items
|
||||
return
|
||||
@@ -1,156 +0,0 @@
|
||||
import datetime
|
||||
import hashlib
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
||||
from Mine.Card import Card
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
class Block():
|
||||
def __init__(self, blockId=None, previousHash="0", timestamp=None, height=0, nonce=0,
|
||||
difficulty=0, card=None, transactions=[], data=None):
|
||||
if data:
|
||||
self.load_from_data(data)
|
||||
else:
|
||||
delay = 2
|
||||
self.blockId = blockId if blockId else uuid.uuid4()
|
||||
self.previousHash = previousHash
|
||||
self.timestamp = timestamp if timestamp else datetime.datetime.now(
|
||||
datetime.timezone.utc
|
||||
)
|
||||
self.height = height
|
||||
self.difficulty = difficulty
|
||||
self.nonce = nonce
|
||||
while True:
|
||||
try:
|
||||
self.card = card if card else Card()
|
||||
except ReadTimeout as e:
|
||||
time.sleep(delay := delay**2)
|
||||
continue
|
||||
break
|
||||
self.transactions = transactions
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
self.blockHash = hashlib.sha256(str(self).encode('utf-8'))
|
||||
|
||||
def mine(self, authorPrivateKey):
|
||||
self.transactions.append(
|
||||
Transaction(
|
||||
cardId = self.card.cardId,
|
||||
sender = authorPrivateKey.public_key(),
|
||||
receiver = authorPrivateKey.public_key(),
|
||||
authorPrivateKey = authorPrivateKey
|
||||
)
|
||||
)
|
||||
while int(self.blockHash.hexdigest(), 16) > 2**(256-self.difficulty):
|
||||
self.nonce += 1
|
||||
self.update()
|
||||
|
||||
###
|
||||
# Validate the internal block structure.
|
||||
# This method confirms that the correct data types have been used and that
|
||||
# values are basically valid.
|
||||
# Further validations against the existing chain must be done elsewhere.
|
||||
# This method calls the Card.validate() and Transaction.validate() methods to validate the
|
||||
# respective schemas.
|
||||
###
|
||||
def validate(self):
|
||||
if not isinstance(self.blockId, uuid.UUID):
|
||||
raise TypeError(f"Block ID should be a UUID not {type(self.blockId).__name__}.")
|
||||
if not self.blockId.version == 4:
|
||||
raise self.Invalid(f"Block ID version should be 4 not {self.blockId.version}.")
|
||||
if not isinstance(self.previousHash, str):
|
||||
raise TypeError(
|
||||
f"Previous hash should be string not {type(self.previousHash).__name__}."
|
||||
)
|
||||
if not int(self.blockHash.hexdigest(), 16) <= 2**(256-self.difficulty):
|
||||
raise self.Invalid("Hash does not meet difficulty requirement.")
|
||||
if not isinstance(self.timestamp, datetime.datetime):
|
||||
raise TypeError(
|
||||
f"Timestamp should be a datetime not {type(self.timestamp).__name__}"
|
||||
)
|
||||
if not self.timestamp.tzinfo == datetime.timezone.utc:
|
||||
raise self.Invalid(
|
||||
f"Timestamp timezone should be in UTC not {self.timestamp.tzinfo}."
|
||||
)
|
||||
if not self.timestamp < datetime.datetime.now(datetime.timezone.utc):
|
||||
raise self.Invalid(f"Timestamp {self.timestamp} in the future.")
|
||||
if not isinstance(self.height, int):
|
||||
raise TypeError(f"Height should be integer not {type(self.height).__name__}.")
|
||||
if not self.height >= 0:
|
||||
raise self.Invalid(f"Height {self.height} less than 0.")
|
||||
if not isinstance(self.difficulty, int):
|
||||
raise TypeError(
|
||||
f"Difficulty should be an integer not {type(self.difficulty).__name__}."
|
||||
)
|
||||
if not self.difficulty >= 0:
|
||||
raise self.Invalid(f"Difficulty {self.difficulty} less than 0.")
|
||||
if not isinstance(self.nonce, int):
|
||||
raise TypeError(f"Nonce should be an integer not {type(self.nonce).__name__}.")
|
||||
if not self.nonce >= 0:
|
||||
raise self.Invalid(f"Nonce {self.nonce} less than 0.")
|
||||
self.card.validate()
|
||||
seenTransactions = []
|
||||
for transaction in self.transactions:
|
||||
if transaction in seenTransactions:
|
||||
raise self.Invalid(
|
||||
f"Contains duplicate transaction {transaction.transactionId}."
|
||||
)
|
||||
transaction.validate()
|
||||
seenTransactions.append(transaction)
|
||||
# TODO validate that one transaction gives the card to the author
|
||||
|
||||
def load_from_data(self, data):
|
||||
self.blockId = uuid.UUID(data['blockId'])
|
||||
self.previousHash = data['previousHash']
|
||||
self.timestamp = datetime.datetime.strptime(
|
||||
data['timestamp'],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
)
|
||||
self.height = data['height']
|
||||
self.difficulty = data['difficulty']
|
||||
self.nonce = data['nonce']
|
||||
self.card = Card(
|
||||
cardId = uuid.UUID(data['card']['cardId']),
|
||||
pageId = data['card']['pageId']
|
||||
)
|
||||
self.transactions = [
|
||||
Transaction(
|
||||
transactionId = uuid.UUID(each['transactionId']),
|
||||
timestamp = datetime.datetime.strptime(
|
||||
each['timestamp'],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
cardId = uuid.UUID(each['cardId']),
|
||||
sender = serialization.load_pem_public_key(each['sender'].encode('utf-8')),
|
||||
receiver = serialization.load_pem_public_key(each['receiver'].encode('utf-8')),
|
||||
signature = bytes.fromhex(each['signature'])
|
||||
) for each in data['transactions']
|
||||
]
|
||||
self.update()
|
||||
|
||||
def as_dict(self):
|
||||
return {
|
||||
"blockId": str(self.blockId),
|
||||
"previousHash": self.previousHash,
|
||||
"timestamp": str(self.timestamp),
|
||||
"height": self.height,
|
||||
"difficulty": self.difficulty,
|
||||
"nonce": self.nonce,
|
||||
"card": self.card.as_dict(),
|
||||
"transactions": [each.as_dict() for each in self.transactions]
|
||||
}
|
||||
|
||||
|
||||
def __str__(self):
|
||||
# The hash of the block is the SHA256 hash of what this method returns.
|
||||
return json.dumps(self.as_dict(), sort_keys=True)
|
||||
|
||||
class Invalid(Exception):
|
||||
def __init__(self, message, statusCode=406):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
@@ -1,62 +0,0 @@
|
||||
import json
|
||||
import requests
|
||||
import time
|
||||
import uuid
|
||||
import wikipedia
|
||||
|
||||
class Card():
|
||||
def __init__(self, cardId=None, pageId=None, data=None):
|
||||
self.cardId = cardId if cardId else uuid.uuid4()
|
||||
delay = 2
|
||||
while True:
|
||||
try:
|
||||
self.pageId = pageId if pageId else int(
|
||||
wikipedia.page(
|
||||
wikipedia.random()
|
||||
).pageid
|
||||
)
|
||||
except wikipedia.exceptions.PageError as e:
|
||||
# TODO: why is the random function returning lower-case pages?
|
||||
continue
|
||||
except wikipedia.exceptions.DisambiguationError as e:
|
||||
# TODO pick random disambiuation option
|
||||
continue
|
||||
except wikipedia.exceptions.WikipediaException as e:
|
||||
time.sleep(delay)
|
||||
delay = delay**2
|
||||
continue
|
||||
break
|
||||
if data:
|
||||
self.load_from_data(data)
|
||||
|
||||
def validate(self):
|
||||
if not isinstance(self.cardId, uuid.UUID):
|
||||
raise TypeError(f"Card ID should be a UUID not {type(self.cardId).__name__}.")
|
||||
if not self.cardId.version == 4:
|
||||
raise self.Invalid(f"Card ID version should be 4 not {self.cardId.version}.")
|
||||
if not isinstance(self.pageId, int):
|
||||
raise TypeError(f"Page ID should be an integer not {type(self.pageId).__name__}.")
|
||||
try:
|
||||
wikipedia.page(pageid=self.pageId)
|
||||
# TODO: may need more precision here.
|
||||
except Exception as e:
|
||||
raise self.Invalid(f"Page ID {self.pageId} does not match a Wikipedia page.")
|
||||
|
||||
def load_from_data(self, data):
|
||||
self.cardId = uuid.UUID(data['cardId'])
|
||||
self.pageId = data['pageId']
|
||||
|
||||
def as_dict(self):
|
||||
return {
|
||||
"cardId": str(self.cardId),
|
||||
"pageId": self.pageId
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
return json.dumps(self.as_dict(), sort_keys=True)
|
||||
|
||||
class Invalid(Exception):
|
||||
def __init__(self, message, statusCode=406):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
@@ -1,455 +0,0 @@
|
||||
import base64
|
||||
import datetime
|
||||
import logging
|
||||
import mariadb
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
|
||||
from Mine.Block import Block
|
||||
from Mine.Card import Card
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
class Database():
|
||||
SQL_CREATE_TABLES = [
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS mine.blocks(
|
||||
sqlId INT PRIMARY KEY AUTO_INCREMENT,
|
||||
blockId UUID UNIQUE NOT NULL,
|
||||
blockHash VARCHAR(64) UNIQUE NOT NULL,
|
||||
previousHash VARCHAR(64) UNIQUE NOT NULL,
|
||||
timestamp VARCHAR(32) NOT NULL,
|
||||
height INT UNIQUE NOT NULL,
|
||||
difficulty INT NOT NULL,
|
||||
nonce INT NOT NULL
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS mine.cards(
|
||||
sqlId INT PRIMARY KEY AUTO_INCREMENT,
|
||||
blockId UUID UNIQUE NOT NULL,
|
||||
cardId UUID UNIQUE NOT NULL,
|
||||
pageId INT NOT NULL,
|
||||
FOREIGN KEY (blockId) REFERENCES blocks(blockId)
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS mine.pending_transactions(
|
||||
sqlId INT PRIMARY KEY AUTO_INCREMENT,
|
||||
transactionId UUID UNIQUE NOT NULL,
|
||||
cardId UUID NOT NULL,
|
||||
timestamp VARCHAR(32) NOT NULL,
|
||||
sender TEXT NOT NULL,
|
||||
receiver TEXT NOT NULL,
|
||||
signature TEXT NOT NULL,
|
||||
FOREIGN KEY (cardId) REFERENCES cards(cardId)
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS mine.transactions(
|
||||
sqlId INT PRIMARY KEY AUTO_INCREMENT,
|
||||
blockId UUID NOT NULL,
|
||||
transactionId UUID UNIQUE NOT NULL,
|
||||
cardId UUID NOT NULL,
|
||||
timestamp VARCHAR(32) NOT NULL,
|
||||
sender TEXT NOT NULL,
|
||||
receiver TEXT NOT NULL,
|
||||
signature TEXT NOT NULL,
|
||||
FOREIGN KEY (blockId) REFERENCES blocks(blockId),
|
||||
FOREIGN KEY (cardId) REFERENCES cards(cardId)
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS mine.peers(
|
||||
sqlId INT PRIMARY KEY AUTO_INCREMENT,
|
||||
peerId UUID UNIQUE NOT NULL,
|
||||
baseUrl VARCHAR(128) UNIQUE NOT NULL,
|
||||
isUp BOOLEAN NOT NULL,
|
||||
downCount INT DEFAULT 0,
|
||||
lastTry TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
);
|
||||
"""
|
||||
]
|
||||
|
||||
SQL_GET_BLOCK_BY_ID = """
|
||||
SELECT *
|
||||
FROM blocks
|
||||
WHERE blockId = ?;
|
||||
"""
|
||||
|
||||
SQL_GET_CARD_BY_ID = """
|
||||
SELECT cardId, pageId
|
||||
FROM cards
|
||||
WHERE cardId = ?;
|
||||
"""
|
||||
|
||||
SQL_GET_CARD_BY_BLOCK_ID = """
|
||||
SELECT cardId, pageId
|
||||
FROM cards
|
||||
WHERE blockId = ?;
|
||||
"""
|
||||
|
||||
SQL_GET_CARD_OWNER = """
|
||||
SELECT receiver
|
||||
FROM transactions
|
||||
WHERE cardId = ?
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1;
|
||||
"""
|
||||
|
||||
SQL_INSERT_BLOCK = """
|
||||
INSERT INTO mine.blocks (
|
||||
blockId, blockHash, previousHash, timestamp, height, difficulty, nonce
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
"""
|
||||
|
||||
SQL_GET_BLOCKS = """
|
||||
SELECT blockId, previousHash, timestamp, height, difficulty, nonce
|
||||
FROM mine.blocks
|
||||
ORDER BY height ASC;
|
||||
"""
|
||||
|
||||
SQL_GET_LAST_BLOCK = """
|
||||
SELECT blockId, previousHash, timestamp, height, difficulty, nonce
|
||||
FROM mine.blocks
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1;
|
||||
"""
|
||||
|
||||
SQL_INSERT_CARD = """
|
||||
INSERT INTO mine.cards (blockId, cardId, pageId) VALUES (?, ?, ?);
|
||||
"""
|
||||
|
||||
SQL_INSERT_PENDING_TRANSACTION = """
|
||||
INSERT INTO mine.pending_transactions (
|
||||
transactionId, timestamp, cardId, sender, receiver, signature
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?);
|
||||
"""
|
||||
|
||||
SQL_GET_PENDING_TRANSACTIONS = """
|
||||
SELECT transactionId, timestamp, cardId, sender, receiver, signature
|
||||
FROM mine.pending_transactions
|
||||
ORDER BY timestamp ASC
|
||||
LIMIT ?;
|
||||
"""
|
||||
|
||||
SQL_DELETE_PENDING_TRANSACTION = """
|
||||
DELETE FROM mine.pending_transactions WHERE transactionId = ?;
|
||||
"""
|
||||
|
||||
SQL_INSERT_TRANSACTION = """
|
||||
INSERT INTO mine.transactions (
|
||||
blockId, transactionId, timestamp, cardId, sender, receiver, signature
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
"""
|
||||
|
||||
SQL_GET_PENDING_TRANSACTION_BY_ID = """
|
||||
SELECT transactionId, timestamp, cardId, sender, receiver, signature
|
||||
FROM pending_transactions
|
||||
WHERE transactionId = ?;
|
||||
"""
|
||||
|
||||
SQL_GET_TRANSACTION_BY_ID = """
|
||||
SELECT transactionId, timestamp, cardId, sender, receiver, signature
|
||||
FROM transactions
|
||||
WHERE transactionId = ?;
|
||||
"""
|
||||
|
||||
SQL_GET_TRANSACTIONS_BY_BLOCK_ID = """
|
||||
SELECT transactionId, timestamp, cardId, sender, receiver, signature
|
||||
FROM mine.transactions
|
||||
WHERE blockId = ?
|
||||
ORDER BY timestamp ASC;
|
||||
"""
|
||||
|
||||
SQL_GET_DECK = """
|
||||
SELECT cards.cardId, cards.pageId
|
||||
FROM (
|
||||
SELECT transactionId, cardId, timestamp, receiver, ROW_NUMBER() OVER (
|
||||
PARTITION BY transactions.cardId
|
||||
ORDER BY transactions.timestamp DESC
|
||||
) AS rownumber
|
||||
FROM transactions
|
||||
) AS subquery
|
||||
JOIN cards ON cards.cardId = subquery.cardId
|
||||
WHERE subquery.rownumber = 1
|
||||
AND receiver = ?;
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
logging.basicConfig(filename=os.getenv('MINE_LOG_FILE', '/var/log/mine.log'), level=logging.INFO)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
delay = 2
|
||||
while True:
|
||||
try:
|
||||
self.conn = mariadb.connect(
|
||||
host = os.getenv('MINE_DB_HOST', "mariadb"),
|
||||
port = os.getenv('MINE_DB_PORT', 3306),
|
||||
user = os.getenv('MINE_DB_USER', None),
|
||||
password = os.getenv('MINE_DB_PASSWORD', None),
|
||||
database = 'mine',
|
||||
autocommit = True
|
||||
)
|
||||
for each in self.SQL_CREATE_TABLES:
|
||||
self.conn.cursor().execute(each)
|
||||
except mariadb.Error as e:
|
||||
self.logger.error(e)
|
||||
time.sleep(delay := delay**2)
|
||||
continue
|
||||
break
|
||||
|
||||
def get_block_by_id(self, blockId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_BLOCK_BY_ID, [blockId])
|
||||
block = cur.fetchone()
|
||||
if block:
|
||||
blockCard = self.get_card_by_block_id(lastBlock[1])
|
||||
blockTransactions = self.get_transactions_by_block_id(lastBlock[1])
|
||||
return Block(
|
||||
blockId = uuid.UUID(block[1]),
|
||||
previousHash = block[3],
|
||||
timestamp = block[4],
|
||||
height = block[5],
|
||||
nonce = block[6],
|
||||
difficulty = block[7],
|
||||
card = blockCard,
|
||||
transactions = blockTransactions
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_card_by_id(self, cardId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_CARD_BY_ID, [str(cardId)])
|
||||
card = cur.fetchone()
|
||||
return Card(
|
||||
cardId = uuid.UUID(card[0]),
|
||||
pageId = card[1]
|
||||
) if card else None
|
||||
|
||||
def get_card_by_block_id(self, blockId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_CARD_BY_BLOCK_ID, [str(blockId)])
|
||||
card = cur.fetchone()
|
||||
return Card(
|
||||
cardId = uuid.UUID(card[0]),
|
||||
pageId = card[1]
|
||||
) if card else None
|
||||
|
||||
def get_blocks(self):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_BLOCKS)
|
||||
blocks = cur.fetchall()
|
||||
return [
|
||||
Block(
|
||||
blockId = uuid.UUID(block[0]),
|
||||
previousHash = block[1],
|
||||
timestamp = datetime.datetime.strptime(
|
||||
block[2],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
height = block[3],
|
||||
difficulty = block[4],
|
||||
nonce = block[5],
|
||||
card = self.get_card_by_block_id(block[0]),
|
||||
transactions = self.get_transactions_by_block_id(block[0])
|
||||
) for block in blocks
|
||||
] if blocks else []
|
||||
|
||||
def get_last_block(self):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_LAST_BLOCK)
|
||||
lastBlock = cur.fetchone()
|
||||
if lastBlock:
|
||||
lastBlockCard = self.get_card_by_block_id(lastBlock[0])
|
||||
lastBlockTransactions = self.get_transactions_by_block_id(lastBlock[0])
|
||||
return Block(
|
||||
blockId = uuid.UUID(lastBlock[0]),
|
||||
previousHash = lastBlock[1],
|
||||
timestamp = datetime.datetime.strptime(
|
||||
lastBlock[2],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
height = lastBlock[3],
|
||||
difficulty = lastBlock[4],
|
||||
nonce = lastBlock[5],
|
||||
card = lastBlockCard,
|
||||
transactions = lastBlockTransactions
|
||||
)
|
||||
|
||||
def get_card_owner(self, cardId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_CARD_OWNER, [str(cardId)])
|
||||
owner = cur.fetchone()
|
||||
return serialization.load_pem_public_key(
|
||||
owner[0].encode('utf-8')
|
||||
) if owner else None
|
||||
|
||||
def get_pending_transactions(self, limit=32768):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(
|
||||
self.SQL_GET_PENDING_TRANSACTIONS,
|
||||
[limit]
|
||||
)
|
||||
pendingTransactions = cur.fetchall()
|
||||
return [
|
||||
Transaction(
|
||||
transactionId = uuid.UUID(pendingTransaction[0]),
|
||||
timestamp = datetime.datetime.strptime(
|
||||
pendingTransaction[1],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
cardId = uuid.UUID(pendingTransaction[2]),
|
||||
# TODO: load rsa keys
|
||||
sender = serialization.load_pem_public_key(
|
||||
pendingTransaction[3].encode('utf-8')
|
||||
),
|
||||
receiver = serialization.load_pem_public_key(
|
||||
pendingTransaction[4].encode('utf-8')
|
||||
),
|
||||
signature = bytes.fromhex(pendingTransaction[5])
|
||||
) for pendingTransaction in pendingTransactions
|
||||
] if pendingTransactions else []
|
||||
|
||||
def get_pending_transaction_by_id(self, transactionId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_PENDING_TRANSACTION_BY_ID, [str(transactionId)])
|
||||
transaction = cur.fetchone()
|
||||
if transaction:
|
||||
return Transaction(
|
||||
transactionId = transaction[0],
|
||||
timestamp = datetime.datetime.strptime(
|
||||
transaction[1],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
cardId = uuid.UUID(transaction[2]),
|
||||
sender = serialization.load_pem_public_key(
|
||||
transaction[3].encode('utf-8')
|
||||
),
|
||||
receiver = serialization.load_pem_public_key(
|
||||
transaction[4].encode('utf-8')
|
||||
),
|
||||
signature = bytes.fromhex(transaction[5])
|
||||
)
|
||||
|
||||
def get_transaction_by_id(self, transactionId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_TRANSACTION_BY_ID, [str(transactionId)])
|
||||
transaction = cur.fetchone()
|
||||
if transaction:
|
||||
return Transaction(
|
||||
transactionId = transaction[0],
|
||||
timestamp = datetime.datetime.strptime(
|
||||
transaction[1],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
cardId = uuid.UUID(transaction[2]),
|
||||
sender = serialization.load_pem_public_key(
|
||||
transaction[3].encode('utf-8')
|
||||
),
|
||||
receiver = serialization.load_pem_public_key(
|
||||
transaction[4].encode('utf-8')
|
||||
),
|
||||
signature = bytes.fromhex(transaction[5])
|
||||
)
|
||||
|
||||
def get_transactions_by_block_id(self, blockId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_GET_TRANSACTIONS_BY_BLOCK_ID, [blockId])
|
||||
transactions = cur.fetchall()
|
||||
return [
|
||||
Transaction(
|
||||
transactionId = transaction[0],
|
||||
timestamp = datetime.datetime.strptime(
|
||||
transaction[1],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
),
|
||||
cardId = transaction[2],
|
||||
sender = serialization.load_pem_public_key(
|
||||
transaction[3].encode('utf-8')
|
||||
),
|
||||
receiver = serialization.load_pem_public_key(
|
||||
transaction[4].encode('utf-8')
|
||||
),
|
||||
signature = bytes.fromhex(transaction[5])
|
||||
) for transaction in transactions
|
||||
]
|
||||
|
||||
def insert_block(self, block):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_INSERT_BLOCK, (
|
||||
str(block.blockId),
|
||||
block.blockHash.hexdigest(),
|
||||
block.previousHash,
|
||||
str(block.timestamp),
|
||||
block.height,
|
||||
block.difficulty,
|
||||
block.nonce
|
||||
))
|
||||
|
||||
def insert_card(self, blockId, card):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_INSERT_CARD, (
|
||||
str(blockId),
|
||||
str(card.cardId),
|
||||
card.pageId
|
||||
))
|
||||
|
||||
def insert_pending_transaction(self, transaction):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_INSERT_PENDING_TRANSACTION, (
|
||||
str(transaction.transactionId),
|
||||
str(transaction.timestamp),
|
||||
str(transaction.cardId),
|
||||
transaction.sender.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
transaction.receiver.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
transaction.signature.hex()
|
||||
))
|
||||
|
||||
def delete_pending_transaction(self, transactionId):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_DELETE_PENDING_TRANSACTION, [str(transactionId)])
|
||||
|
||||
def insert_transaction(self, blockId, transaction):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(self.SQL_INSERT_TRANSACTION, (
|
||||
str(blockId),
|
||||
str(transaction.transactionId),
|
||||
str(transaction.timestamp),
|
||||
str(transaction.cardId),
|
||||
transaction.sender.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
transaction.receiver.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
transaction.signature.hex()
|
||||
))
|
||||
|
||||
def get_deck(self, publicKey):
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(
|
||||
self.SQL_GET_DECK,
|
||||
[publicKey.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')]
|
||||
)
|
||||
deck = cur.fetchall()
|
||||
return [Card(
|
||||
cardId = card[0],
|
||||
pageId = card[1]
|
||||
) for card in deck] if deck else []
|
||||
@@ -1,242 +0,0 @@
|
||||
import datetime
|
||||
import flask
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
|
||||
from Mine.Block import Block
|
||||
from Mine.Card import Card
|
||||
from Mine.Database import Database
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
BLOCK_TRANSACTION_LIMIT = int(os.getenv('BLOCK_TRANSACTION_LIMIT', 1024))
|
||||
DATA_PATH = os.getenv('DATA_PATH', '/var/lib/wikideck/blocks')
|
||||
DIFFICULTY_REQUIREMENT = int(os.getenv('DIFFICULTY_REQUIREMENT', 0))
|
||||
|
||||
mine = flask.Blueprint("mine", __name__)
|
||||
db = Database()
|
||||
privateKey = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
|
||||
def save_block(block):
|
||||
# TODO: error handling (don't reveal mariadb errors)
|
||||
# TODO: disable autocommit
|
||||
db.insert_block(block)
|
||||
db.insert_card(block.blockId, block.card)
|
||||
for transaction in block.transactions:
|
||||
db.delete_pending_transaction(transaction.transactionId)
|
||||
db.insert_transaction(block.blockId, transaction)
|
||||
# Blocks are json strings. This is the true block.
|
||||
with open(f"{DATA_PATH}/{block.blockId}.json", 'w') as f:
|
||||
f.write(str(block))
|
||||
# TODO update peers
|
||||
|
||||
def generate_origin_block():
|
||||
originBlock = Block(
|
||||
difficulty = DIFFICULTY_REQUIREMENT,
|
||||
card = Card(pageId=18618509) # You can target specific cards
|
||||
)
|
||||
originBlock.mine(privateKey)
|
||||
originBlock.validate()
|
||||
save_block(originBlock)
|
||||
return originBlock
|
||||
|
||||
@mine.get('/')
|
||||
def index_get():
|
||||
try:
|
||||
blocks = db.get_blocks()
|
||||
return flask.jsonify([block.as_dict() for block in blocks]) if blocks else flask.jsonify({})
|
||||
except Exception as e:
|
||||
return flask.jsonify(
|
||||
{'Error': str(e)}
|
||||
), e.statusCode if hasattr(
|
||||
e, 'statusCode'
|
||||
) else 500
|
||||
|
||||
###
|
||||
# Retrieve blocks and block data.
|
||||
# Creates an origin block if none exists
|
||||
# Returns a skeleton block to be mined by default.
|
||||
# Queries for a specific block when given parameters.
|
||||
###
|
||||
@mine.get('/blocks')
|
||||
def blocks_get():
|
||||
# TODO: block queries
|
||||
blockId = flask.request.args.get('blockId', None)
|
||||
if not (lastBlock := db.get_last_block()):
|
||||
# TODO: load blocks from files
|
||||
# TODO: try to get blocks from peers
|
||||
lastBlock = generate_origin_block()
|
||||
return flask.jsonify(
|
||||
Block(
|
||||
previousHash = lastBlock.blockHash.hexdigest(),
|
||||
height = lastBlock.height + 1,
|
||||
difficulty = DIFFICULTY_REQUIREMENT,
|
||||
transactions = db.get_pending_transactions()
|
||||
).as_dict()
|
||||
)
|
||||
|
||||
###
|
||||
# Submit a block to the chain.
|
||||
# This method calls the Block().validate() method to validate the block schema.
|
||||
# This method performs additional validations against the rest of the chain.
|
||||
###
|
||||
@mine.post('/blocks')
|
||||
def blocks_post():
|
||||
try:
|
||||
newBlock = Block(data=flask.request.get_json())
|
||||
newBlock.validate()
|
||||
previousBlock = db.get_last_block()
|
||||
if newBlock.previousHash != previousBlock.blockHash.hexdigest():
|
||||
raise Block.Invalid(
|
||||
f"Previous hash should be {previousBlock.blockHash.hexdigest()}."
|
||||
)
|
||||
if newBlock.timestamp <= previousBlock.timestamp:
|
||||
raise Block.Invalid(
|
||||
f"Timestamp {newBlock.timestamp} is before {previousBlock.timestamp}."
|
||||
)
|
||||
if newBlock.height != previousBlock.height + 1:
|
||||
raise Block.Invalid(
|
||||
f"Height should be {previousBlock.height + 1} not {newBlock.height}."
|
||||
)
|
||||
if newBlock.difficulty < DIFFICULTY_REQUIREMENT:
|
||||
raise Block.Invalid(
|
||||
f"Difficulty should be {DIFFICULTY_REQUIREMENT} not {newBlock.difficulty}."
|
||||
)
|
||||
if len(newBlock.transactions) == 0:
|
||||
raise Block.Invalid(
|
||||
"Block contains no transactions."
|
||||
)
|
||||
for transaction in newBlock.transactions:
|
||||
if (transaction.sender == transaction.receiver
|
||||
and transaction.cardId != newBlock.card.cardId):
|
||||
raise Transaction.Invalid(
|
||||
"Recursive transactions are only allowed to collect mining reward."
|
||||
)
|
||||
pendingTransaction = db.get_pending_transaction_by_id(transaction.transactionId)
|
||||
if not pendingTransaction:
|
||||
if transaction.cardId != newBlock.card.cardId:
|
||||
raise Transaction.Invalid(
|
||||
f"No matching pending transaction for {transaction.transactionId}."
|
||||
)
|
||||
else:
|
||||
if transaction.timestamp != pendingTransaction.timestamp:
|
||||
raise Transaction.Invalid(
|
||||
f"Incorrect timestamp on {transaction.transactionId}."
|
||||
)
|
||||
if transaction.cardId != pendingTransaction.cardId:
|
||||
raise Transaction.Invalid(
|
||||
f"Incorrect cardId on {transaction.transactionId}."
|
||||
)
|
||||
if transaction.sender != pendingTransaction.sender:
|
||||
raise Transaction.Invalid(
|
||||
f"Incorrect sender on {transaction.transactionId}."
|
||||
)
|
||||
if transaction.receiver != pendingTransaction.receiver:
|
||||
raise Transaction.Invalid(
|
||||
f"Incorrect receiver on {transaction.transactionId}."
|
||||
)
|
||||
if transaction.signature != pendingTransaction.signature:
|
||||
raise Transaction.Invalid(
|
||||
f"Incorrect signature on {transaction.transactionId}."
|
||||
)
|
||||
save_block(newBlock)
|
||||
return flask.jsonify(newBlock.as_dict())
|
||||
except Exception as e:
|
||||
return flask.jsonify(
|
||||
{'Error': str(e)}
|
||||
), e.statusCode if hasattr(
|
||||
e, 'statusCode'
|
||||
) else 500
|
||||
|
||||
###
|
||||
# Retrieve card data
|
||||
###
|
||||
@mine.get('/cards')
|
||||
def cards_get():
|
||||
try:
|
||||
# TODO: render cards in html
|
||||
# TODO: query cards
|
||||
publicKeyString = flask.request.args.get('publicKey', None)
|
||||
if publicKeyString:
|
||||
deck = db.get_deck(
|
||||
serialization.load_pem_public_key(
|
||||
publicKeyString.encode('utf-8'),
|
||||
backend=default_backend()
|
||||
)
|
||||
)
|
||||
return flask.jsonify([card.as_dict() for card in deck] if deck else [])
|
||||
else:
|
||||
return flask.jsonify({'Error': "No public key."}), 400
|
||||
except Exception as e:
|
||||
return flask.jsonify(
|
||||
{'Error': str(e)}
|
||||
), e.statusCode if hasattr(
|
||||
e, 'statusCode'
|
||||
) else 500
|
||||
|
||||
|
||||
###
|
||||
# Submit a transaction to be mined in a block.
|
||||
# This method performs a number of validations on the submitted transaction and returns
|
||||
# a status code result.
|
||||
###
|
||||
@mine.post('/transactions')
|
||||
def transactions_post():
|
||||
try:
|
||||
newTransaction = Transaction(data=flask.request.get_json())
|
||||
newTransaction.validate()
|
||||
if not db.get_card_by_id(newTransaction.cardId):
|
||||
raise Transaction.Invalid(
|
||||
f"Card {newTransaction.cardId} does not exist.",
|
||||
404
|
||||
)
|
||||
if newTransaction.sender != db.get_card_owner(newTransaction.cardId):
|
||||
raise Transaction.Unauthorized(
|
||||
f"{newTransaction.sender} does not own {newTransaction.cardId}."
|
||||
)
|
||||
if newTransaction.sender == newTransaction.receiver:
|
||||
raise Transaction.Invalid(
|
||||
"Recursive transaction are not accepted at this endpoint."
|
||||
)
|
||||
db.insert_pending_transaction(newTransaction)
|
||||
# TODO: update peers?
|
||||
return flask.jsonify(newTransaction.as_dict())
|
||||
except Exception as e:
|
||||
return flask.jsonify(
|
||||
{'Error': str(e)}
|
||||
), e.statusCode if hasattr(
|
||||
e, 'statusCode'
|
||||
) else 500
|
||||
|
||||
###
|
||||
# Retrieve a transaction.
|
||||
###
|
||||
@mine.get('/transactions')
|
||||
def transactions_get():
|
||||
transactionId = request.args.get('transactionId', None)
|
||||
return get_transaction_by_id(transactionId) if transactionId else json.dumps(
|
||||
db.get_pending_transactions()
|
||||
)
|
||||
|
||||
@mine.post('/peers')
|
||||
def peers_post():
|
||||
try:
|
||||
peer = Peer(data=request.get_json())
|
||||
peer.validate()
|
||||
# TODO: add peers to database
|
||||
return 200
|
||||
# TODO: error handling
|
||||
except Exception as e:
|
||||
print('crap!')
|
||||
|
||||
@mine.get('/peers')
|
||||
def peers_get():
|
||||
# TODO: query peers
|
||||
return 200
|
||||
@@ -1,182 +0,0 @@
|
||||
import datetime
|
||||
import io
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import padding, rsa
|
||||
|
||||
class Transaction():
|
||||
def __init__(self, transactionId=None, timestamp=None, cardId=None, sender=None,
|
||||
receiver=None, authorPrivateKey=None, signature=None, data=None):
|
||||
self.transactionId = transactionId if transactionId else uuid.uuid4()
|
||||
self.timestamp = timestamp if timestamp else datetime.datetime.now(
|
||||
datetime.timezone.utc
|
||||
)
|
||||
self.cardId = cardId
|
||||
self.sender = sender
|
||||
self.receiver = receiver
|
||||
self.signature = signature
|
||||
if data:
|
||||
self.load_from_data(data)
|
||||
if authorPrivateKey:
|
||||
self.sign(authorPrivateKey)
|
||||
|
||||
def validate(self):
|
||||
# TODO: better error codes
|
||||
if not isinstance(self.transactionId, uuid.UUID):
|
||||
raise self.Invalid(
|
||||
f"Transaction ID should be a UUID not {type(self.transactionId).__name__}."
|
||||
)
|
||||
if not self.transactionId.version == 4:
|
||||
raise self.Invalid(
|
||||
f"Transaction ID version should be 4 not {self.transactionId.version}."
|
||||
)
|
||||
if not isinstance(self.timestamp, datetime.datetime):
|
||||
raise self.Invalid(
|
||||
f"Timestamp should be a datetime not {type(self.timestamp).__name__}"
|
||||
)
|
||||
if not self.timestamp.tzinfo == datetime.timezone.utc:
|
||||
raise self.Invalid(
|
||||
f"Timestamp timezone should be in UTC not {self.timestamp.tzinfo}."
|
||||
)
|
||||
if not self.timestamp < datetime.datetime.now(datetime.timezone.utc):
|
||||
raise self.Invalid(f"Timestamp {self.timestamp} in the future.")
|
||||
if not isinstance(self.cardId, uuid.UUID):
|
||||
raise self.Invalid(
|
||||
f"Card ID should be a UUID not {type(self.cardId).__name__}."
|
||||
)
|
||||
if not self.cardId.version == 4:
|
||||
raise self.Invalid(
|
||||
f"Card ID version should be 4 not {self.cardId.version}."
|
||||
)
|
||||
if not isinstance(self.sender, rsa.RSAPublicKey):
|
||||
raise self.Invalid(
|
||||
f"Sender should be an RSA public key not {type(self.sender).__name__}."
|
||||
)
|
||||
if not isinstance(self.receiver, rsa.RSAPublicKey):
|
||||
raise self.Invalid(
|
||||
f"Receiver should be an RSA public key not {type(self.receiver).__name__}."
|
||||
)
|
||||
if not isinstance(self.signature, bytes):
|
||||
raise self.Invalid(
|
||||
f"Signature should be bytes not {type(self.signature).__name__}."
|
||||
)
|
||||
try:
|
||||
self.sender.verify(
|
||||
self.signature,
|
||||
bytearray(
|
||||
json.dumps({
|
||||
"transactionId": str(self.transactionId),
|
||||
"timestamp": str(self.timestamp),
|
||||
"cardId": str(self.cardId),
|
||||
"sender": self.sender.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
"receiver": self.receiver.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
}, sort_keys=True).encode('utf-8')
|
||||
),
|
||||
padding.PSS(
|
||||
mgf=padding.MGF1(hashes.SHA256()),
|
||||
salt_length=padding.PSS.MAX_LENGTH
|
||||
),
|
||||
hashes.SHA256()
|
||||
)
|
||||
# TODO: may need more precision.
|
||||
except Exception as e:
|
||||
raise self.InvalidSignature(str(e))
|
||||
|
||||
def sign(self, authorPrivateKey):
|
||||
self.signature = authorPrivateKey.sign(
|
||||
bytearray(
|
||||
json.dumps({
|
||||
"transactionId": str(self.transactionId),
|
||||
"timestamp": str(self.timestamp),
|
||||
"cardId": str(self.cardId),
|
||||
"sender": self.sender.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
"receiver": self.receiver.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8')
|
||||
}, sort_keys=True).encode('utf-8')
|
||||
),
|
||||
padding.PSS(
|
||||
mgf=padding.MGF1(hashes.SHA256()),
|
||||
salt_length=padding.PSS.MAX_LENGTH
|
||||
),
|
||||
hashes.SHA256()
|
||||
)
|
||||
|
||||
def load_from_data(self, data):
|
||||
self.transactionId = uuid.UUID(data['transactionId'])
|
||||
self.timestamp = datetime.datetime.strptime(
|
||||
data['timestamp'],
|
||||
"%Y-%m-%d %H:%M:%S.%f%z"
|
||||
)
|
||||
self.cardId = uuid.UUID(data['cardId'])
|
||||
# TODO: why is this a tuple when coming from POST /transactions?
|
||||
self.sender = serialization.load_pem_public_key(
|
||||
data['sender'].encode('utf-8'),
|
||||
backend=default_backend()
|
||||
),
|
||||
if isinstance(self.sender, tuple):
|
||||
self.sender = self.sender[0]
|
||||
self.receiver = serialization.load_pem_public_key(
|
||||
data['receiver'].encode('utf-8'),
|
||||
backend=default_backend()
|
||||
),
|
||||
if isinstance(self.receiver, tuple):
|
||||
self.receiver = self.receiver[0]
|
||||
self.signature = bytes.fromhex(data['signature'])
|
||||
|
||||
def as_dict(self):
|
||||
return {
|
||||
"transactionId": str(self.transactionId),
|
||||
"timestamp": str(self.timestamp),
|
||||
"cardId": str(self.cardId),
|
||||
"sender": self.sender.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
"receiver": self.receiver.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf-8'),
|
||||
"signature": self.signature.hex()
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
return json.dumps(self.as_dict(), sort_keys=True)
|
||||
|
||||
class Unauthorized(Exception):
|
||||
def __init__(self, message, statusCode=403):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
|
||||
class Invalid(Exception):
|
||||
def __init__(self, message, statusCode=400):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
|
||||
class AlreadyFulfilled(Exception):
|
||||
def __init__(self, message, statusCode=400):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
|
||||
class InvalidSignature(Exception):
|
||||
def __init__(self, message, statusCode=400):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.statusCode = statusCode
|
||||
@@ -1,64 +0,0 @@
|
||||
class Database():
|
||||
SQL_GET_BUY_ORDERS = """
|
||||
SELECT *
|
||||
FROM orders
|
||||
WHERE orderType == 'buy';
|
||||
"""
|
||||
|
||||
SQL_GET_UNFULFILLED_BUY_ORDERS = """
|
||||
SELECT b.buyOrderId, b.price, b.volume - COUNT(t.sqlId) AS remainingVolume
|
||||
FROM buy_orders b
|
||||
JOIN transactions t
|
||||
ON b.buyOrderId = t.buyOrderId
|
||||
WHERE b.expires > CURRENT_TIMESTAMP()
|
||||
GROUP BY b.buyOrderId
|
||||
HAVING COUNT(t.sqlId) < b.volume
|
||||
ORDER BY b.timestamp ASC
|
||||
"""
|
||||
|
||||
SQL_GET_AVAILABLE_SELL_ORDER_ITEMS_FOR_BUY_ORDER = """
|
||||
SELECT o.sellOrderId, o.price, o.fee, o.userId, i.sqlId, i.cardId
|
||||
FROM sell_orders o
|
||||
JOIN sell_order_items i
|
||||
ON o.sellOrderId = i.sellOrderId
|
||||
WHERE i.cardId == {}
|
||||
AND o.price <= {}
|
||||
AND o.expires > CURRENT_TIMESTAMP()
|
||||
AND i.sqlId NOT IN (
|
||||
SELECT sellOrderItemSqlId
|
||||
FROM transactions
|
||||
WHERE isAbandoned == false;
|
||||
)
|
||||
ORDER BY price DESC;
|
||||
"""
|
||||
|
||||
SQL_INSERT_TRANSACTION = """
|
||||
INSERT INTO transactions (transactionId, buyOrderId, sellOrderItemSqlId)
|
||||
VALUES ({}, {}, {});
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.conn = mariadb.connect(
|
||||
# TODO: default values
|
||||
host = os.getenv("ORDER_MATCHER_DB_HOST", None),
|
||||
port = os.getenv("ORDER_MATCHER_DB_PORT", 3342),
|
||||
user = os.getenv("ORDER_MATCHER_DB_USER", None),
|
||||
password = os.getenv("ORDER_MATCHER_DB_PASSWORD", None)
|
||||
)
|
||||
|
||||
def get_available_sell_order_items_for_buy_order(self, buyOrderId, price):
|
||||
return self.conn.cursor().execute(
|
||||
SQL_GET_AVAILABLE_SELL_ORDER_ITEMS_FOR_BUY_ORDER.format(
|
||||
buyOrderId,
|
||||
price
|
||||
)
|
||||
)
|
||||
|
||||
def insert_transaction(self, transactionId, buyOrderId, sellOrderItemSqlId):
|
||||
self.conn.cursor().execute(
|
||||
SQL_INSERT_TRANSACTION.format(
|
||||
transactionId,
|
||||
buyOrderId,
|
||||
sellOrderItemSqlId
|
||||
)
|
||||
)
|
||||
@@ -1,49 +0,0 @@
|
||||
###
|
||||
# The OrderMatcher converts orders to transactions.
|
||||
# When new orders arrive on the market, the order matcher is triggered to search the
|
||||
# database for an existing order to fulfill the new one.
|
||||
# If the order matcher finds a match for the order, it creates a transaction and
|
||||
# sends it to the mine.
|
||||
###
|
||||
|
||||
import requests
|
||||
|
||||
from Mine.Transaction import Transaction
|
||||
from OrderMatcher.Database import Database
|
||||
|
||||
class OrderMatcher():
|
||||
def __init__(self):
|
||||
self.db = Database()
|
||||
|
||||
def match_orders(self):
|
||||
try:
|
||||
for buyOrder in self.db.get_unfulfilled_buy_orders():
|
||||
for item in self.db.get_available_sell_order_items_for_buy_order(
|
||||
buyOrder[0],
|
||||
buyOrder[1]
|
||||
)[:buyOrder[2]]:
|
||||
newTransaction = Transaction(
|
||||
cardId = item[5],
|
||||
sender = item[3],
|
||||
receiver = currentUser, # TODO: get current currentUser
|
||||
)
|
||||
newTransaction.sign(userPrivateKey) # TODO: get userPrivateKey
|
||||
r = requests.post(
|
||||
f"{MINE_URL}/transactions",
|
||||
data=str(newTransaction)
|
||||
)
|
||||
# TODO: only insert on success
|
||||
self.db.insert_transaction(
|
||||
newTransaction.transactionId,
|
||||
buyOrder[0],
|
||||
item[5]
|
||||
)
|
||||
# TODO: error handling
|
||||
except Exception as e:
|
||||
print("crap!")
|
||||
|
||||
if __name__ == '__main__':
|
||||
orderMatcher = OrderMatcher()
|
||||
while True:
|
||||
orderMatcher.match_orders()
|
||||
time.sleep(INTERVAL)
|
||||
@@ -1,36 +0,0 @@
|
||||
###
|
||||
# The PeerHandler sends new blocks and transactions to available peers.
|
||||
###
|
||||
|
||||
import requests
|
||||
|
||||
from Mine.Block import Block
|
||||
from Mine.Transaction import Transaction
|
||||
|
||||
class PeerHandler():
|
||||
def __init__(self):
|
||||
return
|
||||
|
||||
def add_peer(self. peer):
|
||||
try:
|
||||
db.insert_peer(peer.peerId, peer.url)
|
||||
# TODO: error handling
|
||||
except Exception as e:
|
||||
print('crap!')
|
||||
|
||||
def propogate_block(self, block):
|
||||
try:
|
||||
for peer in db.get_available_peers():
|
||||
requests.post(peer.url, data=str(block))
|
||||
db.update_last_try(peer.peerId)
|
||||
# TODO: error handling
|
||||
except Exception as e:
|
||||
print('crap!')
|
||||
|
||||
def propogate_transaction(self, transaction):
|
||||
try:
|
||||
for peer in db.get_available_peers():
|
||||
requests.post(peer.url, data=str(transaction))
|
||||
# TODO: error handling
|
||||
except Exception as e:
|
||||
print('crap!')
|
||||
@@ -1,12 +0,0 @@
|
||||
class Database():
|
||||
def __init__(self, host, port, user, password):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.conn = mariadb.connect(
|
||||
host = self.host,
|
||||
port = self.port,
|
||||
user = self.user,
|
||||
password = self.password
|
||||
)
|
||||
@@ -1,39 +0,0 @@
|
||||
###
|
||||
# The StatusChecker updates the market database based on the mine database.
|
||||
# It will periodically query for pending transactions, and then confirm their status against
|
||||
# the Mine API.
|
||||
# It will also update user balances upon transaction completion.
|
||||
###
|
||||
|
||||
class StatusChecker():
|
||||
def __init__(self):
|
||||
return
|
||||
|
||||
def update_pending_transactions(self):
|
||||
for pendingTransaction in self.db.get_pending_transactions():
|
||||
self.check_transaction_status(pendingTransaction)
|
||||
|
||||
def check_transaction_status(transaction):
|
||||
transaction = Transaction(
|
||||
data = transaction
|
||||
)
|
||||
# TODO: get transaction data from mine
|
||||
if not mine_transaction.is_pending:
|
||||
db.update_transaction_pending_status(
|
||||
transactionId=transactionId,
|
||||
is_pending=False
|
||||
)
|
||||
if not mine_transaction.block_id:
|
||||
raise Transaction.Abandoned(
|
||||
f"Transaction {pendingTransaction.transactionId} was abandoned."
|
||||
)
|
||||
else:
|
||||
# TODO: increase seller balance by (price - fee)
|
||||
# TODO: increase market balance by (fee)
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
statusChecker = StatusChecker()
|
||||
while True:
|
||||
transactionStatusChecker.update_pending_transactions()
|
||||
time.sleep(INTERVAL)
|
||||
@@ -1,18 +0,0 @@
|
||||
import dotenv
|
||||
import flask
|
||||
import os
|
||||
|
||||
ROLE = os.getenv('ROLE', 'mine')
|
||||
|
||||
if ROLE == 'mine':
|
||||
from Mine.Mine import mine
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(mine)
|
||||
#from Market.Market import market
|
||||
#app.register_blueprint(market, url_prefix="/market")
|
||||
else:
|
||||
raise Exception("Environment variable ROLE must be either 'mine' or 'market'.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
dotenv.load_dotenv()
|
||||
app.run()
|
||||
@@ -1,155 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import glob
|
||||
import os
|
||||
from cli import opt_generate_keypair
|
||||
from cli import opt_save_private_key
|
||||
from cli import opt_load_private_key
|
||||
from cli import opt_save_public_key
|
||||
from cli import opt_mine_block
|
||||
from cli import opt_generate_transaction
|
||||
from cli import opt_view_deck
|
||||
from cli import msg_array
|
||||
from cli import get_private_key_from_file
|
||||
from cli import fetch_cards_json
|
||||
|
||||
DEFAULT_FILENAME: str = "privatekey";
|
||||
|
||||
def resolve_path(path: str | None) -> str | None:
|
||||
if not path or path.strip() == "":
|
||||
return None;
|
||||
|
||||
path = path.strip();
|
||||
|
||||
if not path.endswith(".pem"):
|
||||
path += ".pem";
|
||||
|
||||
if os.path.exists(path) and os.path.isfile(path):
|
||||
return path;
|
||||
|
||||
return None;
|
||||
|
||||
|
||||
def resolve_path_for_key_creation(filename:str | None) -> str:
|
||||
default_name = "privatekey.pem";
|
||||
|
||||
if not filename or filename.strip() == "":
|
||||
return default_name;
|
||||
|
||||
filename = filename.strip();
|
||||
|
||||
if os.path.isdir(filename):
|
||||
return os.path.join(filename, default_name);
|
||||
|
||||
if not filename.endswith(".pem"):
|
||||
filename += ".pem";
|
||||
|
||||
dir_path = os.path.dirname(filename);
|
||||
if dir_path and not os.path.exists(dir_path):
|
||||
os.makedirs(dir_path);
|
||||
|
||||
return filename;
|
||||
|
||||
def get_first_pem_file():
|
||||
pem_files = glob.glob("*.pem");
|
||||
|
||||
if pem_files:
|
||||
first_pem = min(pem_files, key=os.path.getmtime);
|
||||
return first_pem;
|
||||
else:
|
||||
return None;
|
||||
|
||||
def handle_createkey(args):
|
||||
rsa_key = opt_generate_keypair();
|
||||
filepath = resolve_path_for_key_creation(args.output);
|
||||
opt_save_private_key(rsa_key, filepath);
|
||||
print(msg_array[0]);
|
||||
public_path = f"pub_{filepath}"
|
||||
opt_save_public_key(rsa_key, public_path);
|
||||
|
||||
def handle_mine(args):
|
||||
file = resolve_path(args.use);
|
||||
if not file:
|
||||
file = get_first_pem_file();
|
||||
if not file:
|
||||
msg_array[0] = "No valid private key found";
|
||||
return;
|
||||
|
||||
opt_mine_block(file);
|
||||
|
||||
|
||||
def handle_tx(args):
|
||||
|
||||
file = resolve_path(args.use);
|
||||
if not file:
|
||||
file = get_first_pem_file();
|
||||
if not file:
|
||||
msg_array[0] = "No valid private key found";
|
||||
return;
|
||||
|
||||
recv = resolve_path(args.receiver);
|
||||
if args.receiver and not recv:
|
||||
msg_array[0] = "No valid public key found (receiver)";
|
||||
return;
|
||||
|
||||
opt_generate_transaction(file, recv);
|
||||
|
||||
|
||||
def handle_deck(args):
|
||||
file = resolve_path(args.use);
|
||||
if not file:
|
||||
file = get_first_pem_file();
|
||||
if not file:
|
||||
msg_array[0] = "No valid private key found";
|
||||
return;
|
||||
|
||||
rsa = get_private_key_from_file(file);
|
||||
if rsa is None:
|
||||
return;
|
||||
|
||||
cards = fetch_cards_json(rsa);
|
||||
|
||||
if len(cards) < 1:
|
||||
msg_array[0] = "You have no cards to view";
|
||||
return;
|
||||
|
||||
for i, card in enumerate(cards):
|
||||
print(f"{i+1}. Card ID: {card['cardId']}, Page ID: {card['pageId']}");
|
||||
|
||||
|
||||
def build_parser():
|
||||
parser = argparse.ArgumentParser(description="Wikideck CLI");
|
||||
subparsers = parser.add_subparsers(dest="command", required=True);
|
||||
|
||||
#createkey
|
||||
p_create = subparsers.add_parser("createkey",aliases=["ck"], help="Create an RSA keypair and save it to the current directory");
|
||||
p_create.add_argument("-O", "--output", help="Optional custom filename (.pem)");
|
||||
p_create.set_defaults(func=handle_createkey);
|
||||
|
||||
#mine
|
||||
p_mine = subparsers.add_parser("mine", help="Mine a block, using the first ./*.pem by default.");
|
||||
p_mine.add_argument("-u", "--use", help="Use specified keypair (path to .pem file)");
|
||||
p_mine.set_defaults(func=handle_mine);
|
||||
|
||||
#transaction
|
||||
p_transaction = subparsers.add_parser("transaction", aliases=["tx"], help="Generate a transaction, using the first ./*.pem by default.");
|
||||
p_transaction.add_argument("-u", "--use", help="Use specified keypair (path to .pem file)");
|
||||
p_transaction.add_argument("-to", "--receiver", help="Send to specified public key (path to .pem file)");
|
||||
p_transaction.set_defaults(func=handle_tx);
|
||||
|
||||
#deck
|
||||
p_deck = subparsers.add_parser("deck", help="Show all cards tied to the private key, using the first ./*.pem by default.");
|
||||
p_deck.add_argument("-u", "--use", help="Use specified keypair (path to .pem file)");
|
||||
p_deck.set_defaults(func=handle_deck);
|
||||
|
||||
return parser;
|
||||
|
||||
def main():
|
||||
parser = build_parser();
|
||||
args = parser.parse_args();
|
||||
args.func(args);
|
||||
print(msg_array[0]);
|
||||
|
||||
if __name__ == "__main__":
|
||||
main();
|
||||
@@ -1,4 +0,0 @@
|
||||
import sys
|
||||
sys.path.insert(0, '/usr/local/apache2/htdocs/wikideck')
|
||||
|
||||
from app import app as application
|
||||