def do_deserialize(self, line): try: if len(line) < 50: print("Too short tx") return False tx = Transaction(False, False) tx.set_signed_raw_tx(line) print(tx.deserialize_raw_tx()) except BadTransactionFormat: print("Cannot deserialize transation") return False
def get_transaction(): sql_select_Query = """select * from transactions""" rows = Connection.Mysql.exec_query(sql_select_Query) list_of_unknown = [] for row in rows: list_of_unknown.append(Transaction.transactionToJson(row)) return jsonify(list_of_unknown)
def main(): blockchain = [] genesis_hash = 0 genesis_transactions = [Transaction('don', 'don', 0)] genesis_block = Block( genesis_hash, genesis_transactions ) blockchain.append(genesis_block) block = Block( blockchain[len(blockchain)-1]._id, [Transaction('don', 'don', 1)] ) blockchain.append(block) block = Block( blockchain[len(blockchain)-1]._id, [Transaction('don', 'don', 2), Transaction('don', 'hazel', 3)] ) blockchain.append(block) [print(block._id) for block in blockchain]
def do_send(self, line): try: sender_private = PRIVATE_KEYS[-1] lines = str(line).split(']}') # TODO: better split if len(lines) != 3 or len(lines[2]) > 1 : raise WrongLineArgs tx = Transaction(lines[0] + ']}', lines[1] + ']}') tx.get_presign_raw_format() tx.calculate_hash() sender_pub_key = wallet_utils.getPublickKey(sender_private) sender_compressed_pub_key = wallet_utils.compressPublicKey(sender_pub_key) tx.sign_tx(sender_private) ser_tx = tx.get_signed_raw_format(sender_compressed_pub_key).hex() TRANSACTIONS.append(ser_tx) print("Your serialized transaction:", ser_tx) except WrongLineArgs: self.help_send() return False except BadTransactionFormat: print("Cannot create transaction") return False except IndexError: print("Generate or import private key first") return False
def do_random(self, line): private_key, public_key, address = wallet_utils.newKeyPair() i = 0 if len(line) == 0: line = "200" while i < int(line): amount = randrange(0, 65535) private_key2, public_key2, address2 = wallet_utils.newKeyPair() PRIVATE_KEYS.append(private_key) pitoshi = "{0:0{1}x}".format(amount, 4) tx = Transaction(address, address2, bytes(pitoshi.encode('utf-8')).decode('utf-8')) tx.calculate_hash() signature, verify_key = wallet_utils.signMessage(private_key, tx.get_hash()) tx.set_sign(signature, verify_key) secs = randint(0, 3) time.sleep(secs) ser_tx = Serializer(tx).get_serialized_tx() TRANSACTIONS.append(ser_tx) self.do_broadcast(line) i += 1
company.stock - quantity stockBroker.capital -= transaction companyExists = False for sbq in stockBroker.quotes: if (sbq.company.id == company.id): companyExists = True sbq.quantity += quantity if not companyExists: stockBroker.quotes.append( StockBrokerQuotes(company, quantity)) stockBroker.transactions.append( Transaction( company, stockBroker, "Buy", company.quote, quantity, strftime("%Y-%m-%d %H:%M:%S", gmtime()))) data = "Successful transaction. %s stock exchange(s) purchased from %s." % ( quantity, company.name) else: data = "Transaction canceled. There's not enough capital for the purchase." else: data = "Transaction canceled. There are not enough stock to complete the purchase." connection.sendall(data) elif serverCode == ServerCodes.Sell: fields = data.split(';') companyId = int(fields[1]) quantity = int(fields[2]) stockBrokerId = int(fields[3]) company = companies[companyId]
def updateStockBroker(sock, stockBroker): # try: request = '%s;%s' % (ServerCodes.UpdateStockBroker, stockBroker.id) sock.sendall(request) data = sock.recv(SOCKET_DATA_SIZE) fields = data.split(';') stockBroker.id = fields[0] stockBroker.name = fields[1] stockBroker.capital = fields[2] request = '%s;%s' % (ServerCodes.GetStockBrokerQuotes, stockBroker.id) sock.sendall(request) data = sock.recv(SOCKET_DATA_SIZE) if not data == " ": fields = data.split(';') stockBroker.quotes = [] for sb in fields: sbQuote = sb.split(',') companyId = sbQuote[0] quantity = int(sbQuote[1]) request = '%s;%s' % (ServerCodes.GetCompany, companyId) sock.sendall(request) data = sock.recv(SOCKET_DATA_SIZE) companyFields = data.split(';') stockBroker.quotes.append( StockBrokerQuotes( Company(int(companyFields[0]), companyFields[1], companyFields[2], int(companyFields[3]), int(companyFields[4]), int(companyFields[5])), quantity)) request = '%s;%s' % (ServerCodes.GetStockBrokerTransactions, stockBroker.id) sock.sendall(request) data = sock.recv(SOCKET_DATA_SIZE) if not data == " ": fields = data.split(';') stockBroker.transactions = [] for t in fields: transactionFields = t.split(',') companyId = transactionFields[0] action = transactionFields[1] value = int(transactionFields[2]) quantity = int(transactionFields[3]) date = transactionFields[4] request = '%s;%s' % (ServerCodes.GetCompany, companyId) sock.sendall(request) data = sock.recv(SOCKET_DATA_SIZE) companyFields = data.split(';') stockBroker.transactions.append( Transaction( Company(int(companyFields[0]), companyFields[1], companyFields[2], int(companyFields[3]), int(companyFields[4]), int(companyFields[5])), stockBroker, action, value, quantity, date)) return stockBroker
def get_session(id_num): if Connection.Mysql.isHealth() == True: return Transaction.get_session(id_num) return "Error: DB Connection"