def register_user(cls, new_user: dict[str, str]) -> str: "register a user" if not new_user["user_name"] in cls._users: # generate really complicated unique user_id. # Using the existing user_name as the id for simplicity user_id = new_user["user_name"] cls._users[user_id] = new_user Reports.log_event(f"new user `{user_id}` created") # create a wallet for the new user Wallets().create_wallet(user_id) # give the user a sign up bonus Reports.log_event(f"Give new user `{user_id}` sign up bonus of 10") Wallets().adjust_balance(user_id, Decimal(10)) return user_id return ""
def add_block(self, transactions, fee=0): # change add "fee" 6.19 last_block = self.get_last_block() prev_hash = last_block.get_header_hash() height = last_block.block_header.height + 1 block_header = BlockHeader('', height, prev_hash) # reward to wallets[0] wallets = Wallets() keys = list(wallets.wallets.keys()) w = wallets[keys[0]] coin_base_tx = self.coin_base_tx(w.address, fee) # change 6.19 transactions.insert(0, coin_base_tx) utxo_set = UTXOSet() # txs = transactions # change 6.21 txs = utxo_set.clear_transactions( transactions) # change clear transactions(add sort) block = Block(block_header, txs) block.mine(self) block.set_header_hash() self.db.create(block.block_header.hash, block.serialize()) last_hash = block.block_header.hash self.set_last_hash(last_hash) utxo_set.update(block)
def new_transaction(self, from_addr, to_addr, amount, fee): inputs = [] outputs = [] ifname = 'ens33' # enp2s0 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ip = socket.inet_ntoa( fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname[:15], 'utf-8')))[20:24]) wallets = Wallets() from_wallet = wallets[from_addr] pub_key = from_wallet.public_key acc, valid_outpus = self._find_spendable_outputs(from_addr, amount) # if acc < amount + fee: # change # raise NotEnoughAmountError(u'not enough coin') for fout in valid_outpus: index = fout.index txid = fout.txid input = TXInput(txid, index, pub_key) inputs.append(input) output = TXOutput(amount, to_addr) outputs.append(output) if acc > amount + fee: # change # a change outputs.append(TXOutput(acc - amount - fee, from_addr)) # change tx = Transaction(inputs, outputs, fee, ip) # change tx.set_id() self.sign_transaction(tx, from_wallet.private_key) return tx
def create_wallet(): wallets = Wallets() wallet = Wallet() address = wallet.address wallets.add_wallet(address, wallet) wallets.save_to_file() print("Your new address: {}".format(address))
def create_genesis_block(self): bc = BlockChain() w = Wallet.generate_wallet() ws = Wallets() ws[w.address] = w ws.save() tx = bc.coin_base_tx(w.address) bc.new_genesis_block(tx) return w.address
def __init__(self, user, conn=None): self.conn = user if conn: self.conn = os.path.join(conn, user) if not os.path.isdir(self.conn): msg = 'User {} does not exist'.format(user) raise Exception(msg) self.wallets = Wallets(self.conn) self.accounts = Accounts(self.conn) self.funding_templates = FundingTemplates(self.conn)
def start( ): # wait : thread add_block(txs) txs = [] packing function >1MB or no tx verify if tx are valid couch = couchdb.Server("http://127.0.0.1:5984") try: couch.delete('block_chain') except: pass db = DB("http://127.0.0.1:5984") bc = BlockChain() # only one blockchain called bc utxo_set = UTXOSet() utxo_set.reindex(bc) tcpserver = TCPServer() tcpserver.listen() tcpserver.run() rpc = RPCServer(export_instance=Cli()) rpc.start(False) p2p = P2p() server = PeerServer() server.run(p2p) p2p.run() time.sleep(30) # automatically create a genesis block w1 = Wallet.generate_wallet() ws = Wallets() ws[w1.address] = w1 ws.save() tx = bc.coin_base_tx(w1.address) bc.new_genesis_block(tx) fo = open("address.txt", "w") fo.truncate() fo.write(w1.address) # save the address of the genesis block fo.write("\n") w2 = Wallet.generate_wallet() # create another wallet to send transaction ws[w2.address] = w2 ws.save() fo.write(w2.address) fo.close()
def print_all_wallet(self): ws = Wallets() wallets = [] for k, _ in ws.items(): wallets.append(k) return wallets
def create_wallet(self): w = Wallet.generate_wallet() ws = Wallets() ws[w.address] = w ws.save() return w.address
def listAddresses(): # return a list of wallet addresses wallets = Wallets() return wallets.get_addresses()
def isMineAddress(address): # check if address is in our wallet wallets = Wallets() return address in wallets.get_addresses()
def AddAddress(address, wallet): # Add address to wallet wallets = Wallets() wallets.add_wallet(address, wallet) return wallets.save_to_file()