diff --git a/.gitignore b/.gitignore index 4c49bd78f1d08f2bc09fa0bd8191ed38b7dce5e3..b05c0813badd2ef4303cf8c36e5630bb0a622afd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,174 @@ +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments .env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ diff --git a/database.py b/database.py new file mode 100644 index 0000000000000000000000000000000000000000..9f86a6d3773629e2ca5efb797998e00463969465 --- /dev/null +++ b/database.py @@ -0,0 +1,67 @@ +from peewee import MySQLDatabase, Model, CharField, BlobField, AutoField +from peewee import IntegerField, ForeignKeyField, DoubleField +import os + +db = MySQLDatabase(None) + +def connect(create_tables=False) -> None: + """Initializes the database session and connects to the database + :param create_tables: Creates database tables [default: False] + """ + user = os.getenv("DB_USER") + pw = os.getenv("DB_PASSWORD") + host = os.getenv("DB_HOST") + port = os.getenv("DB_PORT") + name = os.getenv("DB_DATABASE") + db.init(name, host=host, port=int(port), user=user, password=pw) + db.connect() + + if create_tables: + db.create_tables([State, User, Wallet, + WalletRequest, UserWallet, PendingTX]) + +class BaseModel(Model): + id = AutoField() + class Meta: + database = db + + +class State(BaseModel): + message = CharField() + menu = CharField() + prev_state = IntegerField() + + +class User(BaseModel): + telegram_id = IntegerField() + state_id = ForeignKeyField(State) + nickname = CharField() + variables = CharField() + + +class Wallet(BaseModel): + max_co_signers = IntegerField() + min_co_signers = IntegerField() + initiator_user_id = ForeignKeyField(User, backref='users') + name = CharField() + + +class WalletRequest(BaseModel): + token = CharField() + wallet_id = ForeignKeyField(User, backref='walletrequests') + + +class UserWallet(BaseModel): + user_id = ForeignKeyField(User, backref='wallets') + wallet_id = ForeignKeyField(Wallet, backref='wallets') + + +class PendingTX(BaseModel): + from_wallet_id = ForeignKeyField(Wallet, backref='pendingtx') + to_address = CharField() + amount = DoubleField() + fee_sat_per_byte = IntegerField() + status = IntegerField() + sign_count = IntegerField() + raw_data = BlobField() + txid = BlobField() diff --git a/requirements.txt b/requirements.txt index b4fae21037162df8b3163b3e30256cb0210279a6..ebd2f51ed931482ae61e36dc755f8acd85b454c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ python-dotenv~=0.19.2 bitcoinlib~=0.6.4 python-telegram-bot~=13.11 -mysqlclient~=2.1.0 \ No newline at end of file +mysqlclient~=2.1.0 +peewee==3.14.10 diff --git a/wallet.py b/wallet.py new file mode 100755 index 0000000000000000000000000000000000000000..372ecf6aaf0b13c77b553465540c2976f76b1d0a --- /dev/null +++ b/wallet.py @@ -0,0 +1,37 @@ +#!/bin/env python3 +from typing import Union +from bitcoinlib.wallets import Wallet, HDKey, WalletKey +import os + +from util import DBManager +db = DBManager() + + +def new_multig_wallet(userId: Union[int, str], + sigs: int, klist: list[HDKey]) -> WalletKey: + """ + Creates a new multisig wallet. + userId: name of the wallet + sigs: number of signatures needed for the wallet + klist: list of (public master) keys inside the wallet + """ + return Wallet.create(name=userId, + sigs_required=sigs, + keys=klist, + network=os.getenv("BTC_NETWORK"), + db_uri=db.db_uri) + + +def new_HD_key() -> HDKey: + """ + Generates a new Hierarchical Deterministic key + """ + return HDKey(network=os.getenv("BTC_NETWORK")) + + +def main(): + pass + + +if __name__ == "__main__": + main()