diff --git a/blockchain_integration/pi_network/pi_network_university/blockchain_integration/models.py b/blockchain_integration/pi_network/pi_network_university/blockchain_integration/models.py new file mode 100644 index 000000000..ba5892c8d --- /dev/null +++ b/blockchain_integration/pi_network/pi_network_university/blockchain_integration/models.py @@ -0,0 +1,42 @@ +# blockchain_integration/models.py +from. import db, bcrypt + +class User(db.Model, UserMixin): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(50), unique=True, nullable=False) + email = db.Column(db.String(120), unique=True, nullable=False) + password = db.Column(db.String(100), nullable=False) + created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) + updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) + + def set_password(self, password): + self.password = bcrypt.generate_password_hash(password).decode("utf-8") + + def check_password(self, password): + return bcrypt.check_password_hash(self.password, password) + +class Wallet(db.Model): + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey("user.id")) + user = db.relationship("User", backref="wallets") + address = db.Column(db.String(42), nullable=False) + private_key = db.Column(db.String(66), nullable=False) + created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) + updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) + +class Transaction(db.Model): + id = db.Column(db.Integer, primary_key=True) + wallet_id = db.Column(db.Integer, db.ForeignKey("wallet.id")) + wallet = db.relationship("Wallet", backref="transactions") + tx_hash = db.Column(db.String(66), nullable=False) + from_address = db.Column(db.String(42), nullable=False) + to_address = db.Column(db.String(42), nullable=False) + value = db.Column(db.Float, nullable=False) + gas = db.Column(db.Float, nullable=False) + gas_price = db.Column(db.Float, nullable=False) + created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) + updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) + +@login_manager.user_loader +def load_user(user_id): + return User.query.get(int(user_id))