Ejemplo n.º 1
0
    def __init__(self):
        self.chain = None
        self.tx_list = {}  #hash:(Block_id,Tree_id)
        self.block_id = 0
        self.tree_id = 0

        tmp = zsign.random_key()
        self.user_private_key = tmp[0]
        self.user_public_key = tmp[1]
        self.user_utxo = []  #(hash,n)

        tmp = zsign.random_key()
        self.miner_private_key = tmp[0]
        self.miner_public_key = tmp[1]
        self.chain = Chain(16, self.miner_private_key, self.miner_public_key)

        tmp = zsign.random_key()
        self.supervisor_private_key = tmp[0]
        self.supervisor_public_key = tmp[1]

        tmp = Tx()
        tmp.set_input([])
        tmp.set_output([(1000, self.user_public_key)])
        tmp.sign(self.user_private_key, self.user_public_key, [],
                 ['pay-to-script-hash'] * 2)
        #print('t',len(self.get_tx(tmp.get_hash()).outputs))
        self.chain.append(tmp)
        self.tx_list[tmp.get_hash()] = (0, 1)
        self.user_utxo.append((tmp.get_hash(), 0))
Ejemplo n.º 2
0
    def __init__(self, n, input_num):
        self.chain = None  #区块链
        self.tx_list = {}  #交易列表->hash:(Block_id,Tree_id)
        self.n = n
        self.input_num = input_num
        #self.block_id = 0
        #self.tree_id = 0

        #设置用户密钥
        tmp = zsign.random_key()
        self.user_private_key = tmp[0]
        self.user_public_key = tmp[1]
        self.user_utxo = []  #用户所有的utxo->(hash,n)

        #设置监督者密钥
        ##        tmp = zsign.random_key()
        ##        self.regulator_private_key = tmp[0]
        ##        self.regulator_public_key = tmp[1]
        with open('msk.pem') as fin:
            self.regulator_private_key = RSA.importKey(fin.read())
        with open('mpk.pem') as fin:
            self.regulator_public_key = RSA.importKey(fin.read())
        with open('ssk.pem') as fin:
            self.sub_private_key = RSA.importKey(fin.read())
        with open('spk.pem') as fin:
            self.sub_public_key = RSA.importKey(fin.read())

        #设置矿工密钥
        tmp = zsign.random_key()
        self.miner_private_key = tmp[0]
        self.miner_public_key = tmp[1]
        self.chain = Chain(self.n, self.miner_private_key,
                           self.miner_public_key, self.regulator_public_key,
                           self.sub_public_key)

        #给用户初始金额
        for i in range(self.n - 1):
            tmp = Tx()
            tmp.set_input([])
            tmp.set_output([(1000, self.user_public_key)])
            tmp.sign(self.user_private_key, self.user_public_key, [],
                     ['pay-to-public-key-hash'] * 2, self.regulator_public_key,
                     self.sub_public_key)
            #tmp.set_output([(1000,[self.user_public_key,self.miner_public_key])])
            #tmp.sign([self.user_private_key,self.miner_private_key],[self.user_public_key,self.miner_public_key],[],['pay-to-script-hash'] * 2,self.regulator_public_key,self.sub_public_key,2,2)

            #print('t',len(self.get_tx(tmp.get_hash()).outputs))
            self.chain.append(tmp)
            self.tx_list[tmp.get_hash()] = (0, 1)
            self.user_utxo.append((tmp.get_hash(), 0))
Ejemplo n.º 3
0
    def generate_tx(self):
        tx = Tx()
        idx = random.randint(0, len(self.user_utxo) - 1)
        pay = random.random() * 10
        h = self.user_utxo[idx][0]
        left = self.get_tx_output_money(self.user_utxo[idx]) - pay

        tx_in = [self.user_utxo[idx]]
        tx_out = [(pay, zsign.random_key()[1]), (left, self.user_public_key)]
        tx.set_input(tx_in)
        tx.set_output(tx_out)
        prev_outs = []
        for i in range(len(tx_in)):
            prev_outs.append(self.get_tx_output(tx_in[i]))
        tx.sign(self.user_private_key, self.user_public_key, prev_outs,
                ['pay-to-script-hash'] * 2)

        self.tx_list[tx.get_hash()] = self.get_pos()
        self.user_utxo.append((tx.get_hash(), 1))
        self.chain.append(tx)
        del self.user_utxo[idx]
Ejemplo n.º 4
0
    def generate_tx(self):  #生成新交易
        start = time.time()
        #新建交易,随机选择utxo、金额,并记录交易hash(id)和余额
        tx = Tx()
        in_money = 0
        in_idx = []
        tx_in = []
        in_idx = random.sample(range(len(self.user_utxo)), self.input_num)
        in_idx.sort()
        for i in range(self.input_num):
            tx_in.append(self.user_utxo[in_idx[i]])
            in_money += self.get_tx_output_money(self.user_utxo[in_idx[i]])
        pay = random.random() * 10
        #h = self.user_utxo[idx][0]
        left = in_money - pay

        #设置输入输出,并签名
        #tx_in = [self.user_utxo[idx]]
        #tx_out = [(pay, zsign.random_key()[1]) , (left, self.user_public_key)]
        tx_out = [(pay, zsign.random_key()[1]),
                  (left, [self.user_public_key, self.miner_public_key])]
        tx.set_input(tx_in)
        tx.set_output(tx_out)
        prev_outs = []
        for i in range(len(tx_in)):
            prev_outs.append(self.get_tx_output(tx_in[i]))
        #tx.sign(self.user_private_key,self.user_public_key,prev_outs,['pay-to-public-key'] * 2)
        tx.sign([self.user_private_key, self.miner_private_key],
                [self.user_public_key, self.miner_public_key], prev_outs,
                ['pay-to-public-key-hash', 'pay-to-script-hash'], 2, 2)

        #更新用户utxo列表、所有交易字典,并在utxo中删除刚刚使用过的输出
        self.tx_list[tx.get_hash()] = self.get_pos()
        self.user_utxo.append((tx.get_hash(), 1))
        self.chain.append(tx)
        for i in range(len(in_idx)):
            del self.user_utxo[in_idx[len(in_idx) - 1 - i]]
        return time.time() - start