35
loading...
This website collects cookies to deliver better user experience
git clone https://github.com/Bamimore-Tomi/fauna-wallet.git
.from tronapi import Tron
tron = Tron()
account = tron.create_account
address = account.address
fernet_key = _generate_fernet_key(os.getenv("MASTER"), os.getenv("SALT"))
encrypted_private_key = _encrypt_private_key(account.private_key, fernet_key)
account= tron.create_account
statement. The account address is stored in the address
variable. To protect our private key, I'm now employing the Fernet symmetric encryption algorithm. More information on the algorithm can be found here. It is critical to encrypt the private key before storing it so that even if another party gains unauthorized access to our database, they will not access our blockchain assets.from tronapi import Tron
tron = Tron()
tron.fromSun(tron.trx.get_balance(address))
def send_trx(sender_private_key: str, reciever_address: str, amount: int):
fernet_key = _generate_fernet_key(os.getenv("MASTER"), os.getenv("SALT"))
private_key = _decrypt_private_key(sender_private_key, fernet_key)
tron = Tron()
tron.private_key = private_key
tron.defa3ult_address = tron.address.from_private_key(tron.private_key)["base58"]
reciever_address = _validate_address(reciever_address)
balance = get_balance(tron.default_address["base58"])
if balance == 0 or amount > balance:
raise errors.InsufficientBalance
transaction = tron.trx.send(reciever_address, amount)
return True
NEW_INDEX
.Terms
indicates which field(s) the index is allowed to browse.Security
then click the NEW KEY
button and complete the required information.SAVE
an API access key is generated.def load_db():
load_dotenv()
client = FaunaClient(secret=os.getenv("FAUNA-KEY"))
return client
def create_wallet(client: FaunaClient, user_id: int, wallet_name: str) -> bool:
tron = Tron()
account = tron.create_account
address = account.address
fernet_key = _generate_fernet_key(os.getenv("MASTER"), os.getenv("SALT"))
encrypted_private_key = _encrypt_private_key(account.private_key, fernet_key)
wallet = client.query(
q.create(
q.collection("wallet"),
{
"data": {
"user_id": user_id,
"wallet_name": wallet_name,
"encrypted_private_key": encrypted_private_key,
"public_key": account.public_key,
"wallet_address": dict(address),
"wallet_account_balance": 0.0,
"transactions": [],
"date_generated": time.time(),
}
},
)
)
save_wallets(client)
return address.base58
def get_wallets(client: FaunaClient, user_id: int, wallet_name: Optional[str] = None):
wallets = client.query(
q.paginate(q.match(q.index("wallet_index"), user_id), size="100_000")
)
if len(wallets["data"]) < 1:
raise errors.WalletNotFound
wallets_data = [
q.get(q.ref(q.collection("wallet"), wallet.id())) for wallet in wallets["data"]
]
wallets_data = client.query(wallets_data)
def record_transaction(
client: FaunaClient, wallet: dict, type_: str, amount: int, address: str, tx_id: str
):
tron = Tron()
bot = telegram.Bot(token=os.getenv("TOKEN"))
wallet = get_wallets(client, wallet["user_id"], wallet_name=wallet["wallet_name"])
prev_transactions = wallet["transactions"]
balance = wallet["wallet_account_balance"]
if type_ == "credit":
balance += amount
if type_ == "debit":
balance -= amount
new = {
"type": type_,
"address": tron.address.from_hex(address).decode(),
"amount": amount,
"tx_id": tx_id,
"time": time.time(),
}
prev_transactions.append(new)
client.query(
q.update(
q.ref(q.collection("wallet"), wallet["ref"]),
{
"data": {
"transactions": prev_transactions,
"wallet_account_balance": balance,
}
},
)
)
def delete_wallet(client: FaunaClient, user_id: int, wallet_ref: str):
try:
client.query(q.delete(q.ref(q.collection("wallet"), wallet_ref)))
except NotFound:
raise errors.WalletNotFound