0% found this document useful (0 votes)
375 views2 pages

Crypto Wallet Code

This document defines two Python classes - CryptocurrencyWallet and WalletManager. CryptocurrencyWallet represents a cryptocurrency wallet with attributes like name, currency, balance, seed and address. WalletManager manages multiple wallets and allows creating, getting and listing wallets. The code sample at the end demonstrates creating Bitcoin and Ethereum wallets, depositing and withdrawing funds from the wallets.

Uploaded by

kimblake177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views2 pages

Crypto Wallet Code

This document defines two Python classes - CryptocurrencyWallet and WalletManager. CryptocurrencyWallet represents a cryptocurrency wallet with attributes like name, currency, balance, seed and address. WalletManager manages multiple wallets and allows creating, getting and listing wallets. The code sample at the end demonstrates creating Bitcoin and Ethereum wallets, depositing and withdrawing funds from the wallets.

Uploaded by

kimblake177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

from bitcoin import bip39

class CryptocurrencyWallet:
def __init__(self, name, currency):
self.name = name
self.currency = currency
self.balance = 0
self.seed = self.generate_seed()
self.address = self.generate_address()

def generate_seed(self):
entropy = random.getrandbits(256)
seed = bip39.mnemonic_from_entropy(entropy)
return seed

def generate_address(self):
# TODO: Implement the logic for generating the address
# For the sake of this example, let's return a random string of length 34
address =
''.join(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567
89') for _ in range(34))
return address

def get_balance(self):
return self.balance

def deposit(self, amount):


self.balance += amount
return self.balance

def withdraw(self, amount):


if amount <= self.balance:
self.balance -= amount
return amount
else:
raise ValueError("Insufficient funds")

class WalletManager:
def __init__(self):
self.wallets = []

def create_wallet(self, name, currency):


wallet = CryptocurrencyWallet(name, currency)
self.wallets.append(wallet)
return wallet

def get_wallet(self, name):


for wallet in self.wallets:
if wallet.name == name:
return wallet
raise ValueError("Wallet not found")

def get_wallets(self):
return self.wallets

# Usage
manager = WalletManager()
wallet1 = manager.create_wallet("My Bitcoin Wallet", "BTC")
print(wallet1.name)
print(wallet1.currency)
print(wallet1.seed)
print(wallet1.address)

wallet2 = manager.create_wallet("My Ethereum Wallet", "ETH")


print(wallet2.name)
print(wallet2.currency)
print(wallet2.seed)
print(wallet2.address)

wallet1.deposit(1000)
print(wallet1.get_balance())

wallet2.deposit(500)
print(wallet2.get_balance())

wallet1.withdraw(200)
print(wallet1.get_balance())

You might also like