def do_attack(self, line): while True: trs = [] f = open('minerkey', 'r') privkey_WIF = f.readline().rstrip('\n') privkey_hex = w.decode_hex(privkey_WIF) privkey_d = int(privkey_hex, 16) addr = w.addr_from_privkey(privkey_d, 0) blocks = requests.get(URL + '/chain') blocks = json.loads(blocks.text)['blocks'] if len(blocks) != 0: trs = [] amount = 50*10**8/(2**int((len(blocks))/50))*100 #print("amount: ", amount) trs.append(CoinbaseTransaction(addr, int(amount)).raw_tx_string) new_trx = requests.get(URL + '/transaction/pendings') new_trx = json.loads(new_trx.text)['trxs'] for i in new_trx: trs.append(i) target = requests.get(URL + '/getDifficulty') #print(target.text) block_to_mine = Block(blocks[-1]['hash_rez'], trs, len(blocks)-1, int(target.text)) #complexity = json.loads(compl.text)["complexity"] if block_to_mine.mining(int(requests.get(URL + '/chain/length').text)): blocks.append(block_to_mine) url = URL + '/chain' payload = {"blocks": self.to_json(blocks)} headers = {"Content-Type": "application/json"} res = requests.post(url, json=payload, headers=headers) if json.loads(res.text)['success']: print('new block is added') print(block_to_mine.hash_rez) else: print("New block was added before you. Let's try again....")
def do_new(self, line): self.privkey_d = w.random_privkey() self.pubkey = w.pubkey_from_privkey(self.privkey_d, 0) self.addr = w.addr_from_privkey(self.privkey_d, 0) self.privkey_hex = hex(self.privkey_d)[2:] if len(self.privkey_hex) % 2 == 1: self.privkey_hex = "0" + self.privkey_hex self.privkey_WIF = w.decode_base58(self.privkey_hex) print('Private key (hex) is:\n ', self.privkey_hex) print('Private key (decimal) is:\n ', self.privkey_d) print('Private key (WIF) is:\n ', self.privkey_WIF) print('Public key (hex) is:\n ', self.pubkey) print('Pitcoin Address (b58check) is:\n ', self.addr) f = open('address', 'w') f.write(self.addr) f.close() print('If you want to save Private key to file enter \'save\'')
def do_import(self, line): if (len(line) != 0): try: f = open(line, 'r') self.privkey_WIF = f.readline() self.privkey_hex = w.decode_hex(self.privkey_WIF) self.privkey_d = int(self.privkey_hex, 16) self.pubkey = w.pubkey_from_privkey(self.privkey_d, 0) self.addr = w.addr_from_privkey(self.privkey_d, 0) print('Private key (hex) is:\n ', self.privkey_hex) print('Private key (decimal) is:\n ', self.privkey_d) print('Private key (WIF) is:\n ', self.privkey_WIF) print('Public key (hex) is:\n ', self.pubkey) print('Pitcoin Address (b58check) is:\n ', self.addr) f = open('address', 'w') f.write(self.addr) f.close() except IOError: print("Error: can\'t find file or read data") else: print('Usage: import / path / to / file')
def mine_gen_block(): f = open('minerkey', 'r') blocks = [] privkey_WIF = f.readline().rstrip('\n') privkey_hex = w.decode_hex(privkey_WIF) privkey_d = int(privkey_hex, 16) addr = w.addr_from_privkey(privkey_d, 0) blockch = Blockchain() gen_block = blockch.genesis_block(addr) if gen_block.mining(0): print(gen_block.hash_rez) blocks.append(gen_block) blocks = to_json(blocks) utxo = Utxo("") for block in blocks: for transaction in block["transactions"]: utxo.add_dell(transaction) #print(utxo.unspend_outputs) with open('chain/blocks.pk1', 'wb') as output: pickle.dump(blocks, output, pickle.HIGHEST_PROTOCOL) with open('chain/utxo.pk1', 'wb') as output: pickle.dump(utxo, output, pickle.HIGHEST_PROTOCOL)
def do_mine(self, line): trs = [] f = open('minerkey', 'r') privkey_WIF = f.readline().rstrip('\n') privkey_hex = w.decode_hex(privkey_WIF) privkey_d = int(privkey_hex, 16) addr = w.addr_from_privkey(privkey_d, 0) blocks = requests.get(self.URL + '/chain') blocks = json.loads(blocks.text)['blocks'] #mine genesis block if len(blocks) == 0: blockch = Blockchain() gen_block = blockch.genesis_block(addr) if gen_block.mining(16): blocks.append(gen_block) url = self.URL + '/chain' payload = {"blocks": self.to_json(blocks)} headers = {"Content-Type": "application/json"} res = requests.post(url, json=payload, headers=headers) if json.loads(res.text)['success']: print(gen_block.hash_rez) #if blockchain not empty else: trs = [] trs.append(CoinbaseTransaction(addr).ser) new_trx = requests.get(self.URL + '/transaction/pendings') new_trx = json.loads(new_trx.text)['trxs'] for i in new_trx: trs.append(i) block_to_mine = Block(blocks[-1]['hash_rez'], trs) if block_to_mine.mining(16): blocks.append(block_to_mine) url = self.URL + '/chain' payload = {"blocks": self.to_json(blocks)} headers = {"Content-Type": "application/json"} res = requests.post(url, json=payload, headers=headers) if json.loads(res.text)['success']: print(block_to_mine.hash_rez)