Exemple #1
0
 def get_address(self, secret_key: int, G: tuple):
     public = self.Emultiple(G, secret_key)
     sa = self.parse(public)
     tmpa = utils.hash256(sa)
     tmpa = utils.rip1(tmpa)
     tmpa_body = '00' + tmpa
     tmpa = tmpa_body
     tmpa = utils.hash256(tmpa)
     prefix = utils.hash256(tmpa)[:8]
     pro_address = tmpa_body + prefix
     return public, utils.base58(pro_address)
Exemple #2
0
 def sign_verify(self, msg: str, public_key: tuple, rs: tuple, G: tuple) -> bool:
     m = int(utils.hash256(msg), 16)
     w = self.__curve_mod_inv(rs[1], self._r)
     rx1, ry1 = self.Emultiple(G, (m * w) % self._r)
     rx2, ry2 = self.Emultiple(public_key, (rs[0] * w) % self._r)
     x, y = self.__curve_add((rx1, ry1), (rx2, ry2))
     return x == rs[0]
Exemple #3
0
 def sign(self, msg: str, private_key: int, G: tuple) -> tuple:
     m = int(utils.hash256(msg), 16)
     randNum = random.randint(1, _r)
     xRand, yRand = self.Emultiple(G, randNum)
     r = xRand % self._r # 临时私钥
     s = ((m + r * private_key) * self.__curve_mod_inv(randNum, self._r)) % self._r
     return r, s
Exemple #4
0
 def sub_transactions(self, recipient, amount):
     if amount > self.amount + self.hypoamount:
         return False
     else:
         t1 = self.generate_transactions(amount, recipient, str(self.public_key))
         signature = self.curvefn.sign(t1, self.private_key, self.G)
         t2 = {'signature': signature}
         t3 = {'txhash': utils.hash256(t1)}
         tx = [t1, t2, t3]
         self.current_transactions.append(tx)
         return True
Exemple #5
0
 def valid_proof(self, block, difficulty=-1, update=True):
     if update:
         self.update_neighbor()
         self.receive_transaction()
     if difficulty == -1:
         difficulty = self.DIFFICULTY
     guess_hash = utils.hash256(block)
     print(guess_hash)
     print(guess_hash[:difficulty])
     print("0" * difficulty)
     return guess_hash[:difficulty] == "0" * difficulty
Exemple #6
0
    def proof_of_work(self, coop=False):
        """
        挖矿
        :return:
        """
        self.update_neighbor()
        self.resolve_conflicts()
        self.receive_transaction()
        print(self.receive_transactions)
        proof = 0
        t = time.time()
        mk = [self.coin_base[0][1]['txhash']]
        for tx in self.current_transactions:
            mk.append(tx[2]['txhash'])
        block = self.generate_block(len(self.chain),
                                    utils.generate_merkle(mk),
                                    utils.hash256(self.chain[-1]),
                                    proof,
                                    t,
                                    self.coin_base + self.current_transactions + self.receive_transactions)
        data = {'difficulty': self.DIFFICULTY, 'block': block}
        if coop :
                # 主机,分配任务
                for child in self.children:
                    requests.post(f'http://{child}/start_work', data=json.dumps(data))
                # 等待
                while self.coop_status is False:
                    for child in self.children:
                        resp = requests.get(f'http://{child}/coop_status').json()
                        if resp['status'] == '1':
                            block['proof'] = int(resp['proof'])
                            self.coop_status = True

                    time.sleep(1)

                for child in self.children:
                    requests.post(f'http://{child}/stop_work')
        else:   # 单机挖矿
            while self.valid_proof(block) is False:
                proof += 1
                block['proof'] = proof
        self.current_transactions = []
        self.receive_transactions = []
        self.chain.append(block)
        self.utxo_pool(block)
        self.diffuculty_resolve()
        self.hypoamount = 0
        self.current_batch = 0
        self.coop_status = False
        self.coop_proof = 0
        return block
Exemple #7
0
 def valid_chain(self, chain):
     last_block = chain[0]
     current_index = 1
     while current_index < len(chain):
         block = chain[current_index]
         last_block_hash = utils.hash256(last_block)
         if block['previous_hash'] != last_block_hash:
             # self.msg.append('hash wrong')
             return False
         if not self.valid_proof(block):
             # self.msg.append('proof wrong')
             return False
         if not self.valid_block_transaction(block):
             # self.msg.append('t wrong')
             return False
         last_block = block
         current_index += 1
     return True
Exemple #8
0
    def __init__(self, encry_params: tuple, G: tuple, ip, root: set):
        self.ROOT = set() # DNS 节点
        self.ROOT = root
        self.chain = [] # 主链
        self.DIFFICULTY = 0 # < 64
        self.curvefn = elliptic_curve.Curve_Encrypt(encry_params[0], encry_params[1], encry_params[2], encry_params[3])
        self.G = G

        self.private_key = random.randint(1, encry_params[3])
        self.public_key, self.address = self.curvefn.get_address(self.private_key, self.G)

        self.genesis_block()

        self.neighbor = set()  # 邻居节点
        self.children = set()   # 子代
        self.ip = ip
        self.coop_ip = ""
        self.coop_batch = 4096
        self.current_batch = 0
        self.coop_status = False
        self.coop_proof = 0

        self.amount = 0
        self.hypoamount = 0
        self.hyporeceive = []
        self.current_transactions = []  # 自己的交易池
        self.receive_transactions = []  # 别人的交易池

        # 初始化 coin_base 奖励
        self.mine_transaction = self.generate_transactions(50, str(self.public_key), "root")
        self.coin_base = [[self.mine_transaction, {'txhash': utils.hash256(self.mine_transaction)}]]
        # self.msg = []

        self.register() # 向 DNS 汇报自己存在
        self.update_neighbor() # 拉取当前在线节点
        self.receive_transaction() # 拉取交易