def calc_hash(self): #更新整棵树的hash值 self.node = [] self.cstr = [] tmp2 = [] cnt = len(self.data) n = len(self.data) for i in range(n): tmp = str(self.data[i].h) + str(self.data[i].r) + zsign.to_str( self.data[i]) if self.data[i].sigma != None and not (zsign.verify( tmp, self.data[i].sigma, self.data[i].pk)): print('MSIG!') pass if self.data[i].sigma == None: tmp2.append('0' * 200) self.cstr.append('') elif chrs.HashCheck_CHRS(self.data[i].mpk, self.data[i].spk, self.data[i].h, self.data[i].r, self.data[i].arbitrary): tmp2.append(hex(self.data[i].h)[2::]) self.cstr.append(self.data[i].r) else: print(zsign.to_str(self.data[i])) print('MCHRS!') standard_part = zsign.to_str( self.data[i], ['h', 'r', 'sigma', 'spk', 'pk', 'mpk', 'arbitrary']) if self.data[i].sigma != None: ht, rt = Hash.Hash(RSA.importKey(self.data[i].pk), standard_part) ht = hex(ht)[2::] rt = hex(rt) else: ht = '0' * 200 rt = '' tmp2.append(ht) self.cstr.append(rt) cnt = 2 * n self.node.append(tmp2) #print(len(tmp2),cnt) #print(len(self.data)) while cnt > 1: #其余层 tmp = [] for i in range(0, cnt, 2): tmp1 = self.node[-1][i] if i + 1 < len(self.node[-1]): tmp2 = self.node[-1][i + 1] else: tmp2 = '' tmp.append(zsign.sha256(tmp1 + tmp2)) cnt = cnt // 2 self.node.append(tmp) return self.node[-1][0] #返回树根hash值
def sign(self, private_key, public_key, prev_outs, script_types, m_public_key, sub_public_key, M=None, N=None): #对脚本进行签名,需要传入:交易生成者的私钥、公钥、[用到的txio]、[脚本类型] for i in range(len(self.outputs)): #写入输出中的验证脚本 self.outputs[i].script_type = script_types[i] self.outputs[i].script = self.output_script_gen( self.outputs[i].script, script_types[i], M, N) for i in range(len(self.inputs)): #写入输入中的验证脚本 self.inputs[i].scriptSig = self.input_script_gen( private_key, public_key, prev_outs[i].script_type, prev_outs[i].script, i, M, N) self.pk = public_key self.spk = sub_public_key self.mpk = m_public_key if len(self.inputs) == 0: return self.h, self.r = chrs.Hash_CHRS(m_public_key, self.spk, self.arbitrary) tmp = str(self.h) + str(self.r) + zsign.to_str(self) if type(private_key) == type([]): private_key = private_key[0] #print(tmp) self.sigma = zsign.sign(tmp, private_key)
def sign(self, private_key, public_key, prev_outs, script_types, m_public_key, sub_public_key): #签名,其中输入不需要签名 for i in range(len(self.outputs)): self.outputs[i].script_type = script_types[i] self.outputs[i].script = self.output_script_gen( self.outputs[i].script, script_types[i]) for i in range(len(self.inputs)): self.inputs[i].scriptSig = self.input_script_gen( private_key, public_key, 'nothing', '', i) self.pk = public_key self.spk = sub_public_key self.mpk = m_public_key if len(self.inputs) == 0: return self.h, self.r = chrs.Hash_CHRS(m_public_key, self.spk, self.arbitrary) tmp = str(self.h) + str(self.r) + zsign.to_str(self) if type(private_key) == type([]): private_key = private_key[0] self.sigma = zsign.sign(tmp, private_key)
def calc_hash(self): #计算自身的hash值 #print('sha:',zsign.sha(zsign.to_str(self))) #print('to_str:',zsign.to_str(self),'\n') return zsign.hash160(zsign.to_str(self))