def premine(self): random.seed(time()) privkey = [get_new_private_key() for i in range(3)] publkey = [get_public_key(privkey[i]) for i in range(3)] addr = [get_public_address(privkey[i]) for i in range(3)] for i in range(len(privkey)): item = privkey[i] with open('premine' + str(i), 'w+') as f: f.write(convert_to_wif(item)) for i in range(len(addr)): bl = self.create_gen_block('premine' + str(i)) if bl: if len(self.blocks) > 0: bl.previous_hash = self.blocks[len(self.blocks) - 1].hash bl.heigth = len(self.blocks) bl.previous_hash = self.last_hash bl.target = self.target self.mine(bl) self.blocks.append(bl) self.last_hash = bl.hash utxo = [] for i in self.blocks: trans = Deserializer.deserialize_raw(i.transactions[0]) utxo_add(utxo, trans) for x in range(3): for ind in range(len(addr)): for jnd in range(len(addr)): i = addr[ind] j = addr[jnd] if i != j: amount = random.randint(6, 15) if not self.muted: print (i, j, amount) inputs = utxo_select_inputs(utxo_get(utxo, i), 5, amount) outputs = utxo_create_outputs(i, j, amount, 5, inputs) if len(inputs) == 0: continue tr = Transaction(i, j, amount, {'inp': inputs, 'oup': outputs, 'locktime':0, 'version': 1}) tr_serial = Serializer.serialize_raw(tr, privkey[ind], publkey[ind]) tr_des = Deserializer.deserialize_raw(tr_serial.hex()) utxo_add(utxo, tr_des) bl = Block(time(), 0, self.last_hash, [tr_serial.hex()]) bl.target = self.target self.mine(bl) bl.heigth = len(self.blocks) self.blocks.append(bl) self.last_hash = bl.hash #deleting premine files '''
def get_block(): h = request.args.get('block_height') if not h: if not request.is_json: return jsonify({'error': 'no height parameter passed'}), 202 else: h = request.get_json()['index'] try: if (int(h) <= 0): raise ValueError if int(h) > len(data['blocks']): return jsonify({'error': 'height exceeds blockchain length'}), 202 except ValueError: return jsonify( {'error': 'height parameter must be a positive integer'}), 202 raw_txes = [] for i in range(len(data['blocks'][int(h) - 1]["transactions"])): x = Deserializer.deserialize_raw(data['blocks'][int(h) - 1]["transactions"][i]) jst = {} jst['version'] = x.version jst['inp'] = x.inputs jst['oup'] = x.outputs jst['locktime'] = x.timelock jst['tr_hash'] = x.tr_hash.hex() raw_txes.append(jst) ti = strftime("%a, %d %b %Y %H:%M:%S", gmtime(data['blocks'][int(h) - 1]['timestamp'])) return render_template('block_index.html', title='Block', data=[data['blocks'][int(h) - 1], raw_txes, ti]), 201
def get_address_info(): a = request.args.get('addr') if (len(a) == 0): return jsonify({'error': 'Empty field'}), 404 result = [] for bl in data['blocks']: for tr in bl['transactions']: x = Deserializer.deserialize_raw(tr) for elem in x.outputs: if elem['address'] == a: jst = {} jst['timer'] = strftime("%a, %d %b %Y %H:%M:%S", gmtime(bl['timestamp'])) jst['version'] = x.version jst['inp'] = [] for i in x.inputs: s = utxo_get_trans_output(data['blocks'], i['tx_prev_hash'], i['tx_prev_index']) jst['inp'].append(s if s else {'address': 'Coinbase'}) jst['oup'] = x.outputs for i in range(len(jst['oup'])): jst['oup'][i]['spent'] = utxo_spent( data['utxo'], x.tr_hash.hex(), i) jst['locktime'] = x.timelock jst['tr_hash'] = x.tr_hash.hex() result.append(jst) if len(result) == 0: return jsonify({'error': 'Address is invalid'}), 404 return render_template('address.html', title='Address Info', data=result), 201
def get_chain(): h = request.args.get('len') try: if (int(h) <= 0) or int(h) > len(data['blocks']): h = len(data['blocks']) except ValueError: h = len(data['blocks']) h = int(h) for bl in data['blocks'][-h:]: raw_txes_bl = [] for i in range(len(bl["transactions"])): x = Deserializer.deserialize_raw(bl["transactions"][i]) jst = {} jst['version'] = x.version jst['inp'] = x.inputs jst['oup'] = x.outputs jst['locktime'] = x.timelock jst['tr_hash'] = x.tr_hash.hex() raw_txes_bl.append(jst) bl['jsoni'] = raw_txes_bl bl['ti'] = strftime("%a, %d %b %Y %H:%M:%S", gmtime(bl['timestamp'])) return render_template('blockchain.html', title='Blockchain', data=data['blocks'][-h:]), 201
def utxo_get_trans_output(utxo, tx_id, tx_index): for bl in utxo: for x in bl['transactions']: elem = Deserializer.deserialize_raw(x) if elem.tr_hash.hex() == tx_id: item = elem.outputs return { 'address': item[tx_index]['address'], 'value': item[tx_index]['value'] }
def utxo_init(data): data['utxo'] = [] blocks = data['blocks'] for block in blocks: print(block['hash']) for trans in block['transactions']: x = Deserializer.deserialize_raw(trans) utxo_add(data['utxo'], x) # x.display_raw() '''
def validate(self, prev_blocks, utxo): if (len(prev_blocks) > 0): ts_avg = 0 for i in prev_blocks: ts_avg += i.timestamp ts_avg = ts_avg / len(prev_blocks) if (self.timestamp < ts_avg): print ('Invalid timestamp') return False for item in self.transactions: x = Deserializer.deserialize_raw(item) if not validate_raw(utxo, x): return False return True
def get_mempool(): a = request.args.get('id') print(a) for bl in data['blocks']: for tr in bl['transactions']: x = Deserializer.deserialize_raw(tr) if (x.tr_hash.hex() == a): jst = {} jst['version'] = x.version jst['inp'] = x.inputs jst['oup'] = x.outputs for i in range(len(jst['oup'])): jst['oup'][i]['spent'] = utxo_spent(data['utxo'], a, i) jst['locktime'] = x.timelock jst['tr_hash'] = x.tr_hash.hex() return render_template('transaction.html', title='Transaction', data=[bl, jst, a]), 201 return jsonify({'error': 'Transaction ID is invalid'}), 404
def get_fees(trans): fees = 0 for i in trans: x = Deserializer.deserialize_raw(i) x.display_raw() return fees